使用JGit根据提交日期签出特定修订版 [英] Checkout a specific revision based on the commit date with JGit
问题描述
我想使用Java JGit库来检索文件的先前提交的修订.该文件每天都会更新,我需要从git存储库访问以前的版本.
I want to use the Java JGit library in order to retrieve previously committed revisions of a file. The file gets updated on a daily basis and I need to have access to the previous versions from a git repository.
我知道如何通过提供所需的修订的提交哈希值来签出特定修订版,如
I know how to checkout a specific revision by providing the commit hash of the revision I need, as described here with this command:
git.checkout().setName( "<id-to-commit>" ).call();
当前,我克隆存储库,搜索该文件的最新提交,然后签出修订版.我现在的过程很麻烦,因为我每次都需要重新检出存储库.我宁愿只是从特定修订版(例如,master分支)中导出单个文件.
Currently I clone the repository, search for the most recent commit of that file and then checkout the revision. The process I have now is cumbersome as I would need to re-checkout the repository each time. I would rather just export a single file from a specific revision (of the master branch, for instance).
检索存储库的修订日志并获取我感兴趣的文件的提交.
Retrieve the revision log of the repository and get the commits of the file I am interested in.
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;
}
最新修订版
此代码检索提交的最新修订版,该修订版早于我感兴趣的日期:
Most recent Revision
This code retrieves the most recent revision of the commits, which is before the date I am interested in:
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之前的状态.
A Simple Test Application
In this application, I want to revert the working copy of the file to the state which it was before 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实现此目标的最佳方法是什么?
Questions
This is a very naive implementation, I am sure that JGit offers a more elegant way, but I could not find any hints. I would like to export a specific revision of one single file programmatically. I need to refer to a specific date, I want to fetch the revision which was valid during that date. The file it self gets updated quite often, nevertheless I need a mechanism to access the previous versions and use them as input for another process. What would be the best way of achieving this with Java and JGit?
您提出的解决方案仅适用于确切的日期/时间戳.因此,正如您在回答中所说,该解决方案仅在每天只有一次提交的情况下才有效.由于不能保证,因此我使用了稍微不同的方法.
The solution you proposed only works with the exact date/timestamp. Thus as you said in your answer, the solution only works if there is only one commit per day. As this is not guaranteed, I use a slightly different approach.
首先,我从文件中检索所有提交,并在树图中按日期对它们进行排序.提交按降序排列,因此最新提交是最后提交.
First, I retrieve all the commits from the file and sort them by date in a Treemap. The commits are sorted in a descending order, the most recent one is therefore last.
/*
* 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中检索最后的提交即可检索最近的提交.
Then, I can retrieve the most recent commit simply by retrieving the last commit in the 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;
}
然后,我可以检索该修订版的文件并将其导出.
Then, I can retrieve the file of that revision and export it.
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!");
}
推荐答案
如果我正确理解了您的问题,则希望在给定的日期范围内查找提交,然后读取提交的特定文件格式的内容.
If I understand your question correctly, you want to find a commit within a given date range and then read the contents of a specific file form that commit.
假设每个日期只有一次提交,则可以使用RevWalk
来获取所需的提交:
Assuming that there is only one commit per date, you can use a RevWalk
to To get get the desired commit:
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
的设置方式.尝试在不解析提交的情况下运行代码,如果找到了文件,则保留该文件.
I am not entirely sure if the revWalk.parseCommit(commit);
is necessary - it depends on how the RevWalk
is set up. Try to run the code without parsing the commit and if the file is found, leave it at that.
现在您具有所需的提交,请使用TreeWalk
来获取文件的内容:
Now that you have the desired commit, use a TreeWalk
to obtain the content of the file:
try (TreeWalk treeWalk = TreeWalk.forPath(repository, fileName, commit.getTree())) {
InputStream inputStream = repository.open(treeWalk.getObjectId(0), Constants.OBJ_BLOB).openStream();
// use the inputStream
}
fileName
保存所涉及文件的存储库相对路径.
fileName
holds the repository-relative path to the file in question.
这篇关于使用JGit根据提交日期签出特定修订版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!