使用JGit根据提交日期签出特定修订

时间:2015-06-26 13:12:00

标签: java git version-control jgit

我想使用Java JGit库来检索以前提交的文件修订版。该文件每天都会更新,我需要从git存储库访问以前的版本。

我知道如何通过提供我需要的修订的提交哈希来检查特定修订,如here所述,使用此命令:

git.checkout().setName( "<id-to-commit>" ).call();

目前我克隆存储库,搜索该文件的最新提交,然后签出修订版。我现在的过程很麻烦,因为我每次都需要重新检查存储库。我宁愿只从特定版本(例如主分支)导出单个文件。

修订日志

检索存储库的修订日志并获取我感兴趣的文件的提交。

 private static HashMap getRevisionsByLog(Repository repository, String filePath) {


    HashMap commitMap = new HashMap<String, DateTime >();


    Git git = new Git(repository);
    LogCommand logCommand = null;
    try {
        logCommand = git.log()
                .add(git.getRepository().resolve(Constants.HEAD))
                .addPath(filePath);
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        for (RevCommit revCommit : logCommand.call()) {
            DateTime commitDate = new DateTime(1000L * revCommit.getCommitTime());
            // Store commit hash and date
            commitMap.put(revCommit.getName(),commitDate);
        }
    } catch (GitAPIException e) {
        e.printStackTrace();
    }

    return commitMap;
}

最新版本

此代码检索提交的最新修订版本,该修订版本在我感兴趣的日期之前:

 private static String getMostRecentCommit(HashMap<String, DateTime > commitMap, DateTime execDate){
    Iterator it = commitMap.entrySet().iterator();
    Map.Entry<String, DateTime> mostRecentCommit = null;

    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
        DateTime currentCommitDate = (DateTime) pair.getValue();

        if(mostRecentCommit==null && ((DateTime) pair.getValue()).isBefore(execDate)){

           mostRecentCommit = pair;
            }else if (currentCommitDate.isBefore(execDate)){
                System.out.println("Current date is before exec");
                if(currentCommitDate.isAfter(mostRecentCommit.getValue())){
                    System.out.println("Current date is before exec and after the most recent one");
                    mostRecentCommit=pair;
                }
            }

    }

    System.out.println("Current most recent:  " + mostRecentCommit.getKey() + " = " + mostRecentCommit.getValue());
    return mostRecentCommit.getKey();

}

简单测试应用程序

在此应用程序中,我想将文件的工作副本恢复到2015-06-26T14:25:00之前的状态。

    public static void main(String[] args) {

    DateTime dt = new DateTime("2015-06-26T14:25:00");
    Date execDate = dt.toDate();

    String remotePath = "/media/Data/Gittest/repo/";
    String localPath="/tmp/localgit";

    // Clone repository
    try {
        Git.cloneRepository().setURI(remotePath)
                .setDirectory(new File(localPath)).call();
    } catch (GitAPIException e) {
        e.printStackTrace();
    }


    Git git = null;
    try {
        git = Git.open(new File(localPath));
        Repository repo = git.getRepository();

        HashMap map = getRevisionsByLog(repo, "versuch.txt");

        String commitID = getMostRecentCommit(map,dt);

        System.out.println("Commit hash" +commitID);
        git.checkout().setName(commitID).call();



    } catch (IOException e) {
       ...
    }


}

问题

这是一个非常天真的实现,我确信JGit提供了更优雅的方式,但我找不到任何提示。我想以编程方式导出单个文件的特定修订版。我需要参考具体日期,我想获取在该日期有效的修订版。它自己经常更新的文件,然而我需要一种机制来访问以前的版本并将它们用作另一个进程的输入。使用Java和JGit实现这一目标的最佳方法是什么?

解决方案基于RüdigerHerrmann的答案

您建议的解决方案仅适用于确切的日期/时间戳。因此,正如您在答案中所说,只有每天只有一次提交时,该解决方案才有效。由于这不能保证,我使用的方法略有不同。

首先,我从文件中检索所有提交,并在Treemap中按日期对它们进行排序。提交按降序排序,最近一次是最后一次。

    /*
    * Get all commits of a file before a given date
    * */
    private TreeMap<DateTime,RevCommit> getAllCommitsBefore(Date execDate, String path){
    RevWalk walk = new RevWalk( this.repo );
    TreeMap<DateTime, RevCommit> commitsByDate = new TreeMap<DateTime, RevCommit>();
    try {
        walk.markStart( walk.parseCommit( this.repo.resolve( Constants.HEAD ) ) );

        walk.sort( RevSort.COMMIT_TIME_DESC);
        walk.setTreeFilter(PathFilter.create(path));

        for( RevCommit commit : walk ) {
            if ( commit.getCommitterIdent().getWhen().before(execDate) ) {
                DateTime commitTime = new DateTime(commit.getCommitterIdent().getWhen());
                commitsByDate.put(commitTime, commit);

            }
        }
        walk.close();
        System.out.println("Number of valid commits: " + commitsByDate.size());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return commitsByDate;
    }

然后,我可以通过检索TreeMap中的最后一次提交来检索最近的提交。

    public RevCommit getMostRecentCommit(Date execDate, String path){
    TreeMap<DateTime,RevCommit> allCommits = this.getAllCommitsBefore(execDate,path);
    RevCommit lastCommit = allCommits.lastEntry().getValue();
    System.out.println("Last entry: " + lastCommit.getName());
    return lastCommit;

    }

然后,我可以检索该修订版的文件并将其导出。

    public void retrieveFileFromCommit(String path, RevCommit commit, String outputPath){
    TreeWalk treeWalk  = null;
    try {
        treeWalk = TreeWalk.forPath(this.repo, path, commit.getTree());
        InputStream inputStream = this.repo.open( treeWalk.getObjectId( 0 ), Constants.OBJ_BLOB ).openStream();

        this.writeFile(inputStream,outputPath);
        treeWalk.close(); // use release() in JGit < 4.0

    } catch (IOException e) {
        e.printStackTrace();
    }

    }

    /*
    * Write the stream to the disk
    * */
    private void writeFile(InputStream inputStream, String outputPath){
    OutputStream outputStream =
            null;
    try {
        outputStream = new FileOutputStream(new File(outputPath));
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


    System.out.println("Done!");

    }

1 个答案:

答案 0 :(得分:1)

如果我正确理解您的问题,您希望在给定的日期范围内找到提交,然后阅读提交的特定文件表单的内容。

假设每个日期只有一个提交,您可以使用RevWalk来获得所需的提交:

RevWalk walk = new RevWalk( repo );
walk.markStart( walk.parseCommit( repo.resolve( Constants.HEAD ) ) );
walk.sort( RevSort.COMMIT_TIME_DESC);
walk.setRevFilter( myFilter );
for( RevCommit commit : walk ) {
  if( commit.getCommitter().getWhen().equals( date ) ) {
    // this is the commit you are looking for
    revWalk.parseCommit( commit );
    break;
  }
}
walk.close();

我不完全确定revWalk.parseCommit( commit );是否有必要 - 这取决于RevWalk的设置方式。尝试在不解析提交的情况下运行代码,如果找到了文件,请将其留在那里。

现在您已获得所需的提交,请使用TreeWalk获取文件的内容:

TreeWalk treeWalk  = TreeWalk.forPath( repository, fileName, commit.getTree() );
InputStream inputStream = repository.open( treeWalk.getObjectId( 0 ), Constants.OBJ_BLOB ).openStream();
// use the inputStream
treeWalk.close(); // use release() in JGit < 4.0

fileName包含相关文件的存储库相对路径。