Wednesday, October 30, 2013

Get GIT log using java/Jgit

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.List;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.LogCommand;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.RawTextComparator;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.revwalk.RevSort;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.eclipse.jgit.treewalk.filter.TreeFilter;
/**
 *
 * @author test
 */
public class GitLogNew {
    public static void main(String[] args) {
        try { 
            
            test4();
             //System.out.println("----------------------------------------");
            
        } catch (Exception e) {
            System.out.println(" ERROR " + e.toString());
        }

    }
     public static void test4(){
          try {
          
           File gitWorkDir = new File("/home/test/GITTEST/");
            Git git = null;
            git = Git.open(gitWorkDir);
            Repository repo = git.getRepository();
            
            LogCommand log = git.log();
            log.addPath("lakmal.txt");
            
            ObjectId lastCommitId = repo.resolve(Constants.HEAD);
             RevWalk rw = new RevWalk(repo);
             RevCommit parent = rw.parseCommit(lastCommitId);
            
             rw.sort(RevSort.COMMIT_TIME_DESC);
        rw.markStart(parent);
             
            
            log.setMaxCount(3);
            Iterable<RevCommit> logMsgs = log.call();
            for (RevCommit commit : logMsgs) {
                System.out.println("\n\n\n\n\n\n\n\n\n\n----------------------------------------");
                System.out.println("commit    "  + commit);
                System.out.println("commit.toObjectId()    "  + commit.toObjectId());
                System.out.println(" commit.getAuthorIdent().getName()         "  + commit.getAuthorIdent().getName());
                System.out.println(""  + commit.getAuthorIdent().getWhen());
                System.out.println(" commit.getFullMessage())--- " + commit.getFullMessage());
                System.out.println("---DIF STARTING ------------------------");
                 
                
                //RevTree tree = commit.getTree();DisabledOutputStream.INSTANCE
                 
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                 
                  DiffFormatter df = new DiffFormatter(out);  // DisabledOutputStream.INSTANCE
      df.setRepository(repo);
      df.setDiffComparator(RawTextComparator.DEFAULT);
      df.setDetectRenames(true);
      
      
       
      
      
      //df.format(parent.getTree(), commit.getTree());
      List<DiffEntry> diffs =df.scan(commit.getTree(), commit.getParent(0).getTree()); //df.scan(parent.getTree(), commit.getTree());
      for (DiffEntry diff : diffs) {
         //System.out.println(getCommitMessage());
//df.format(diff);
         System.out.println("changeType=" + diff.getChangeType().name()
                 + " \n newMode=" + diff.getNewMode().getBits()
                 + " \nnewPath=" + diff.getNewPath()         
                 + " \nold path " + diff.getOldPath()
                 + " \nHash code " + diff.hashCode()
                  + " \nString  " + diff.toString()
                  + " \nchange " + diff.getChangeType().toString()
                  );
         
         df.format(diff);
    String diffText = out.toString("UTF-8");
    System.out.println(diffText);
      }
              df.release();out.close();
             parent =   commit;  
                 
                 
                 
            }
          
          }catch (Exception e) {
            System.out.println("no head exception : " + e);
        }
     }
    
    
     
     
     
     
}