diff --git a/git-console-kotlin/GitTestProject/src/GitTest.kt b/git-console-kotlin/GitTestProject/src/GitTest.kt new file mode 100644 index 00000000..defda7ff --- /dev/null +++ b/git-console-kotlin/GitTestProject/src/GitTest.kt @@ -0,0 +1,62 @@ +import org.eclipse.jgit.api.Git +import org.eclipse.jgit.api.errors.GitAPIException +import org.eclipse.jgit.api.errors.JGitInternalException +import org.eclipse.jgit.internal.storage.file.FileRepository +import org.eclipse.jgit.storage.file.FileRepositoryBuilder +import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider +import org.junit.Test +import java.io.File +import java.io.IOException +// Modified UsernamePasswordCredentialsProvider Constructors string to apply git function is work. +class GitTest { + var remotePath: String? = null + var localPath: String? = null + var initPath: String? = null + @Test + @Throws(IOException::class, GitAPIException::class) + fun TestClone() { + val usernamePasswordCredentialsProvider = UsernamePasswordCredentialsProvider("username", "password") + val cloneCommand = Git.cloneRepository() + val git = cloneCommand.setURI(remotePath).setBranch("master").setDirectory(File(localPath!!)).setCredentialsProvider(usernamePasswordCredentialsProvider).call() + print(git.tag()) + } + + @Test + @Throws(IOException::class) + fun TestCreate() { + val newRepo = FileRepositoryBuilder.create(File(initPath!! + "/.git")) + newRepo.create() + } + + @Test + @Throws(IOException::class, GitAPIException::class) + fun TestAdd() { + val myFile = File(localPath!! + "/myfile.txt") + myFile.createNewFile() + val git = Git(FileRepository(localPath!! + "/.git")) + git.add().addFilepattern("myFile").call() + } + + @Test + @Throws(IOException::class, GitAPIException::class, JGitInternalException::class) + fun TestCommit() { + val git = Git(FileRepository(localPath!! + "/.git")) + git.commit().setMessage("Test Kotlin version").call() + } + + @Test + @Throws(IOException::class, GitAPIException::class) + fun TestPull() { + val usernamePasswordCredentialsProvider = UsernamePasswordCredentialsProvider("username", "password") + val git = Git(FileRepository(localPath!! + "/.git")) + git.pull().setRemoteBranchName("master").setCredentialsProvider(usernamePasswordCredentialsProvider).call() + } + + @Test + @Throws(IOException::class, GitAPIException::class, JGitInternalException::class) + fun TestPush() { + val usernamePasswordCredentialsProvider = UsernamePasswordCredentialsProvider("username", "password") + val git = Git(FileRepository(localPath!! + "/.git")) + git.push().setRemote("origin").setCredentialsProvider(usernamePasswordCredentialsProvider).call() + } +} diff --git a/git-console-kotlin/GitTestProject/src/Main.kt b/git-console-kotlin/GitTestProject/src/Main.kt new file mode 100644 index 00000000..8927091f --- /dev/null +++ b/git-console-kotlin/GitTestProject/src/Main.kt @@ -0,0 +1,24 @@ +import java.util.Scanner +object Main { + @JvmStatic + fun main(args: Array) { + val test = GitTest() + val scanner = Scanner(System.`in`) + println("Please input the Git repository url: ") + test.remotePath = scanner.next() + println("Please input the local path: ") + test.localPath = scanner.next() + println("Please input the init path: ") + test.initPath = scanner.next() + try { + test.TestClone() + test.TestCreate() + test.TestAdd() + test.TestCommit() + test.TestPush() + test.TestPull() + } catch (e: Exception) { + e.printStackTrace() + } + } +} diff --git a/git-console-kotlin/README.md b/git-console-kotlin/README.md new file mode 100644 index 00000000..50153956 --- /dev/null +++ b/git-console-kotlin/README.md @@ -0,0 +1,3 @@ +# Git Module +这是用Kotlin语言写的控制台版的Git工具,如果需要应用到QPython工程中,需要将此API测试类进行重写 +## Modified By c4dr01d \ No newline at end of file