天天看點

JGit初試牛刀

http://wendal.net/299.html

package com.aliyun.qa;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.NoHeadException;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.revwalk.RevCommit;
public class JgitDemo {
    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        File rootFile = new File("/home/wuhe.jyh/V7FHDmaster/yunos/packages/apps/Contacts/.git");
          
        FileRepository repo = new FileRepository(rootFile);
          
        Git git = new Git(repo);
          
        try {
            for(RevCommit rc : git.log().call()) {
                System.out.println("id: " + rc.getId().toString());
                System.out.println("author name: " + rc.getAuthorIdent().getName());
                  
                // 注意:如果這裡寫成int,就錯掉啦!!!
                long commitTime = rc.getCommitTime();
                System.out.println("commitTime :" + commitTime);
                  
                  
                long time = commitTime * 1000;
                       
                Date date = new Date();
                date.setTime(time);
                System.out.println("date: " + date);
            }
        } catch (NoHeadException e) {
            e.printStackTrace();
        } catch (GitAPIException e) {
            e.printStackTrace();
        }
    }
}