package stream.flarebot.flarebot;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefDatabase;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevSort;
import org.eclipse.jgit.revwalk.RevWalk;

import java.io.File;
import java.io.IOException;
import java.util.Map;

public class GitHandler {

    private static Git GIT;

    static {
        try {
            GIT = Git.open(new File("."));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Repository getRepository() {
        if (GIT != null) {
            return GIT.getRepository();
        }
        return null;
    }

    public static RevCommit getLatestCommit() {
        if (getRepository() != null) {
            Repository repo = getRepository();
            try (RevWalk revWalk = new RevWalk(repo)) {
                revWalk.sort(RevSort.COMMIT_TIME_DESC);
                Map<String, Ref> allRefs = repo.getRefDatabase().getRefs(RefDatabase.ALL);
                for (Ref ref : allRefs.values()) {
                    RevCommit commit = revWalk.parseCommit(ref.getLeaf().getObjectId());
                    revWalk.markStart(commit);
                }
                return revWalk.next();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    public static Git getGit() {
        return GIT;
    }

    public static String getLatestCommitId() {
        if (getLatestCommit() != null) {
            return getLatestCommit().abbreviate(8).name();
        }
        return null;
    }
}

@@ -415,24 +415,30 @@
 vate static 
+final 
 Git GIT;%0A%0A  
@@ -438,21 +438,89 @@
 IT;%0A
-%0A    static %7B
+    private static final RevCommit LATEST_COMMIT;%0A%0A    static %7B%0A%0A        Git git;
 %0A   
@@ -542,19 +542,19 @@
         
-GIT
+git
  = Git.o
@@ -652,187 +652,63 @@
     
-%7D%0A
     
-%7D%0A%0A    public static Repository getRepository() %7B%0A        if (GIT !
+git 
 = null
-) %7B%0A
+;%0A  
      
+ %7D%0A
        
-return GIT.getRepository();%0A        %7D%0A        return null;
+ GIT = git;%0A%0A
 %0A    
-%7D%0A%0A
     
-public static 
 RevC
@@ -713,20 +713,17 @@
 vCommit 
-getL
+l
 atestCom
@@ -725,20 +725,24 @@
 stCommit
-() %7B
+ = null;
 %0A       
@@ -1244,22 +1244,30 @@
         
-return
+latestCommit =
  revWalk
@@ -1378,35 +1378,289 @@
   %7D%0A        
-return null
+LATEST_COMMIT = latestCommit;%0A    %7D%0A%0A    public static Repository getRepository() %7B%0A        if (GIT != null) %7B%0A            return GIT.getRepository();%0A        %7D%0A        return null;%0A    %7D%0A%0A    public static RevCommit getLatestCommit() %7B%0A        return LATEST_COMMIT
 ;%0A    %7D%0A%0A%0A  

Make stuff more... final