Skip to content

Commit

Permalink
Merge pull request #95 from c4dr01d/master
Browse files Browse the repository at this point in the history
Add Console Module for Git Tool (Use Kotlin)
  • Loading branch information
riverfor committed Jan 16, 2018
2 parents 9cb4c1a + 61ca13d commit 4acd788
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
62 changes: 62 additions & 0 deletions git-console-kotlin/GitTestProject/src/GitTest.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
24 changes: 24 additions & 0 deletions git-console-kotlin/GitTestProject/src/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.Scanner
object Main {
@JvmStatic
fun main(args: Array<String>) {
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()
}
}
}
3 changes: 3 additions & 0 deletions git-console-kotlin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Git Module
这是用Kotlin语言写的控制台版的Git工具,如果需要应用到QPython工程中,需要将此API测试类进行重写
## Modified By c4dr01d

0 comments on commit 4acd788

Please sign in to comment.