java - 根据 JGit 的提交日期检查特定修订版

标签 java git version-control jgit

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

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

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

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

修订日志

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

 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üdiger Herrmann 答案的解决方案

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

首先,我从文件中检索所有提交,并在树形图中按日期对它们进行排序。提交按降序排序,因此最近的提交位于最后。

    /*
    * 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!");

    }

最佳答案

如果我正确理解你的问题,你想找到给定日期范围内的提交,然后读取该提交的特定文件形式的内容。

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

try (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;
    }
  }
}

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

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

try (TreeWalk treeWalk = TreeWalk.forPath(repository, fileName, commit.getTree())) {
  InputStream inputStream = repository.open(treeWalk.getObjectId(0), Constants.OBJ_BLOB).openStream();
  // use the inputStream
}

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

关于java - 根据 JGit 的提交日期检查特定修订版,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31074116/

相关文章:

java - HTTP 状态 404 - 调用 Servlet 时未找到

git - Jenkins 管道分支名称返回 null

java - Maven-versions-plugin:用于否定不遵循特定标记格式的版本的正则表达式

java - Eclipse 多项目/单一存储库源代码控制选项

java - 如何更改子类中字段的通用类型?

java - 使用 Gradle maxParallelForks 属性执行并行测试

java - Android中为什么无法从SqlLiteDatabase读取数据

git - 使用 Git 取消暂存新文件

Git:推送到时如何进行远程目录更新?

java - 我可以在没有 ssh 的情况下使用 git 或其他版本控制系统在 http 托管帐户上私有(private)存储库吗?