Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions 62 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 24 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 3 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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.