Skip to content

Commit 4acd788

Browse files
authored
Merge pull request #95 from c4dr01d/master
Add Console Module for Git Tool (Use Kotlin)
2 parents 9cb4c1a + 61ca13d commit 4acd788

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import org.eclipse.jgit.api.Git
2+
import org.eclipse.jgit.api.errors.GitAPIException
3+
import org.eclipse.jgit.api.errors.JGitInternalException
4+
import org.eclipse.jgit.internal.storage.file.FileRepository
5+
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
6+
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider
7+
import org.junit.Test
8+
import java.io.File
9+
import java.io.IOException
10+
// Modified UsernamePasswordCredentialsProvider Constructors string to apply git function is work.
11+
class GitTest {
12+
var remotePath: String? = null
13+
var localPath: String? = null
14+
var initPath: String? = null
15+
@Test
16+
@Throws(IOException::class, GitAPIException::class)
17+
fun TestClone() {
18+
val usernamePasswordCredentialsProvider = UsernamePasswordCredentialsProvider("username", "password")
19+
val cloneCommand = Git.cloneRepository()
20+
val git = cloneCommand.setURI(remotePath).setBranch("master").setDirectory(File(localPath!!)).setCredentialsProvider(usernamePasswordCredentialsProvider).call()
21+
print(git.tag())
22+
}
23+
24+
@Test
25+
@Throws(IOException::class)
26+
fun TestCreate() {
27+
val newRepo = FileRepositoryBuilder.create(File(initPath!! + "/.git"))
28+
newRepo.create()
29+
}
30+
31+
@Test
32+
@Throws(IOException::class, GitAPIException::class)
33+
fun TestAdd() {
34+
val myFile = File(localPath!! + "/myfile.txt")
35+
myFile.createNewFile()
36+
val git = Git(FileRepository(localPath!! + "/.git"))
37+
git.add().addFilepattern("myFile").call()
38+
}
39+
40+
@Test
41+
@Throws(IOException::class, GitAPIException::class, JGitInternalException::class)
42+
fun TestCommit() {
43+
val git = Git(FileRepository(localPath!! + "/.git"))
44+
git.commit().setMessage("Test Kotlin version").call()
45+
}
46+
47+
@Test
48+
@Throws(IOException::class, GitAPIException::class)
49+
fun TestPull() {
50+
val usernamePasswordCredentialsProvider = UsernamePasswordCredentialsProvider("username", "password")
51+
val git = Git(FileRepository(localPath!! + "/.git"))
52+
git.pull().setRemoteBranchName("master").setCredentialsProvider(usernamePasswordCredentialsProvider).call()
53+
}
54+
55+
@Test
56+
@Throws(IOException::class, GitAPIException::class, JGitInternalException::class)
57+
fun TestPush() {
58+
val usernamePasswordCredentialsProvider = UsernamePasswordCredentialsProvider("username", "password")
59+
val git = Git(FileRepository(localPath!! + "/.git"))
60+
git.push().setRemote("origin").setCredentialsProvider(usernamePasswordCredentialsProvider).call()
61+
}
62+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.Scanner
2+
object Main {
3+
@JvmStatic
4+
fun main(args: Array<String>) {
5+
val test = GitTest()
6+
val scanner = Scanner(System.`in`)
7+
println("Please input the Git repository url: ")
8+
test.remotePath = scanner.next()
9+
println("Please input the local path: ")
10+
test.localPath = scanner.next()
11+
println("Please input the init path: ")
12+
test.initPath = scanner.next()
13+
try {
14+
test.TestClone()
15+
test.TestCreate()
16+
test.TestAdd()
17+
test.TestCommit()
18+
test.TestPush()
19+
test.TestPull()
20+
} catch (e: Exception) {
21+
e.printStackTrace()
22+
}
23+
}
24+
}

git-console-kotlin/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Git Module
2+
这是用Kotlin语言写的控制台版的Git工具,如果需要应用到QPython工程中,需要将此API测试类进行重写
3+
## Modified By c4dr01d

0 commit comments

Comments
 (0)