简体   繁体   English

JGit中的文件提交日期

[英]File commit date in JGit

Is it possible to resolve the date and time when a certain file was committed for the first time using JGit? 是否可以使用JGit解析首次提交某个文件的日期和时间?

Git equivalent would be listing the first commit, like: 相当于Git的将列出第一次提交,例如:

git log --format=%aD <FILE> | tail -1

A RevWalk can be used as follows to obtain the first commit that contains 'file.txt' RevWalk可以按如下方式使用,以获取包含“ file.txt”的第一个提交

RevWalk revWalk = new RevWalk( repository );
revWalk.markStart( revWalk.parseCommit( repository.resolve( Constants.HEAD ) ) );
revWalk.setTreeFilter( PathFilter.create( "path/to/file.txt" ) );
revWalk.sort( RevSort.COMMIT_TIME_DESC );
revWalk.sort( RevSort.REVERSE, true );
RevCommit commit = revWalk.next();
...
revWalk.dispose();

In the example the history starts at HEAD . 在示例中,历史记录始于HEAD Adjust the markStart() call the start from somewhere else or call markStart() multiple times to include several start points. 调整markStart()从其他位置调用起点,或多次调用markStart()以包括多个起点。

The PathFilter excludes commits that do not contain the given repository-relative path name. PathFilter排除不包含给定存储库相对路径名的提交。 And finally the two sort() calls take care that commits are ordered by their time stamp (newest first) in reverse order. 最后,这两个sort()调用要注意,提交按其时间戳记(最新的顺序)以相反的顺序进行排序。 Hence the oldest commit that contains the given file is returned by next() . 因此,包含给定文件的最早提交将由next()返回。

Be aware that the commit passed to markStart() must be from the same revision walker, ie it must be obtained with a call to parseCommit() from the same revWalk instance. 请注意,传递给markStart()的提交必须来自同一修订版修订程序,即必须通过从同一revWalk实例调用parseCommit()来获得。 See also this thread for more details. 另请参见此线程以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM