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 final Git GIT;
    private static RevCommit latestCommit;

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

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

    public static RevCommit getLatestCommit() {
        if (latestCommit == null) {
            RevCommit latestCommit = null;
            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);
                    }
                    latestCommit = revWalk.next();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            GitHandler.latestCommit = latestCommit;
        }
        return latestCommit;
    }


    public static Git getGit() {
        return GIT;
    }

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

@@ -28,16 +28,65 @@
 rebot;%0A%0A
+import java.io.File;%0Aimport java.io.IOException;%0A
 import o
@@ -134,23 +134,102 @@
 se.jgit.
-lib.Ref
+api.errors.GitAPIException;%0Aimport org.eclipse.jgit.errors.RepositoryNotFoundException
 ;%0Aimport
@@ -253,24 +253,16 @@
 .lib.Ref
-Database
 ;%0Aimport
@@ -342,49 +342,8 @@
 it;%0A
-import org.eclipse.jgit.revwalk.RevSort;%0A
 impo
@@ -384,80 +384,8 @@
 k;%0A%0A
-import java.io.File;%0Aimport java.io.IOException;%0Aimport java.util.Map;%0A%0A
 publ
@@ -570,13 +570,375 @@
 le(%22
-.%22));
+FlareBot/%22));%0A        %7D catch (RepositoryNotFoundException e) %7B%0A            try %7B%0A                git =%0A                        Git.cloneRepository().setDirectory(new File(%22FlareBot/%22)).setURI(%22https://github.com/FlareBot/FlareBot.git%22).call();%0A            %7D catch (GitAPIException e1) %7B%0A                e1.printStackTrace();%0A                git = null;%0A            %7D
 %0A   
@@ -1301,51 +1301,8 @@
 ) %7B%0A
-            RevCommit latestCommit = null;%0A
     
@@ -1344,59 +1344,8 @@
 ) %7B%0A
-                Repository repo = getRepository();%0A
     
@@ -1369,20 +1369,17 @@
 RevWalk 
-revW
+w
 alk = ne
@@ -1392,231 +1392,118 @@
 alk(
-repo)) %7B%0A                    revWalk.sort(RevSort.COMMIT_TIME_DESC);%0A                    Map%3CString, Ref%3E allRefs = repo.getRefDatabase().getRefs(RefDatabase.ALL);%0A                    for (Ref ref : allRefs.values()) %7B%0A    
+getRepository())) %7B%0A                    Ref head = getRepository().exactRef(getRepository().getFullBranch());%0A
     
@@ -1533,28 +1533,25 @@
 it commit = 
-revW
+w
 alk.parseCom
@@ -1558,21 +1558,12 @@
 mit(
-ref.getLeaf()
+head
 .get
@@ -1599,81 +1599,19 @@
     
-    revWalk.markStart(commit);%0A                    %7D%0A                    
+GitHandler.
 late
@@ -1625,22 +1625,14 @@
 t = 
-revWalk.next()
+commit
 ;%0A  
@@ -1748,60 +1748,8 @@
   %7D%0A
-            GitHandler.latestCommit = latestCommit;%0A
     

Improve git a bit