B4J Code Snippet A simple jgit wrapper

JGit is a git client developed in pure java. I use this to run git init, add and commit commands.

This is the class file.

B4X:
Sub Class_Globals
    Private gitJO As JavaObject
    Private gitJOStatic As JavaObject
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(path As String)
    gitJOStatic.InitializeStatic("org.eclipse.jgit.api.Git")
    If File.Exists(path,".git")=False Then
        Log("init")
        init(path)
    End If
    gitJO=gitJOStatic.RunMethodJO("open",Array(getFile(path)))
End Sub

Sub getFile(path As String) As JavaObject
    Dim fileJO As JavaObject
    fileJO.InitializeNewInstance("java.io.File",Array(path))
    Return fileJO
End Sub

Public Sub init(path As String)
    gitJOStatic.RunMethodJO("init",Null).RunMethodJO("setDirectory",Array(getFile(path))).RunMethodJO("call",Null)
End Sub

Public Sub add(files As String)
    gitJO.RunMethodJO("add",Null).RunMethodJO("addFilepattern",Array(files)).RunMethodJO("call",Null)
End Sub

public Sub commit(message As String,name As String,email As String)
    Dim commitCommand As JavaObject
    commitCommand=gitJO.RunMethodJO("commit",Null)
    If name<>"" Then
        commitCommand.RunMethod("setCommitter",Array As String(name,email))
        commitCommand.RunMethod("setAuthor",Array As String(name,email))
        commitCommand.RunMethod("setAll",Array(True))
    End If
    commitCommand.RunMethodJO("setMessage",Array(message)).RunMethodJO("call",Null)
End Sub

Public Sub diffList As List
    Return  gitJO.RunMethodJO("diff",Null).RunMethod("call",Null)
End Sub

To use:
B4X:
Dim git1 As Git
git1.Initialize("dirname")
Dim diffList As List
diffList=git1.diffList
If diffList<>Null And diffList.Size<>0 Then
    git1.add(".")
    git1.commit("message","username","email")
End If

It depends on these additional jars, I get this through maven:

B4X:
#AdditionalJar: org.eclipse.jgit-5.1.1.201809181055-r.jar
#AdditionalJar: jsch-0.1.54.jar
#AdditionalJar: jzlib-1.1.1.jar
#AdditionalJar: JavaEWAH-1.1.6.jar
#AdditionalJar: slf4j-api-1.7.2.jar
#AdditionalJar: slf4j-simple-1.7.25.jar
 
Top