有没有办法从JGit获取已更改文件,已删除文件,新添加文件的时间戳?我有下面的代码遍历树并获取这些文件,但我无法弄清楚如何获取这些文件的时间戳。
public static Map<String, Object> diffFormatter(Git git, ObjectId lastCommitId) {
Map<String, Object> m = new HashMap<String, Object>();
ByteArrayOutputStream out = new ByteArrayOutputStream();
DiffFormatter formatter = new DiffFormatter(out);
formatter.setRepository(git.getRepository());
AbstractTreeIterator commitTreeIterator = prepareTreeParser(git.getRepository(), lastCommitId);
FileTreeIterator workTreeIterator = new FileTreeIterator(git.getRepository());
List<DiffEntry> diffEntries = formatter.scan(commitTreeIterator, workTreeIterator);
Set<String> changedFiles = new HashSet<String>();
Set<String> newlyAddedFiles = new HashSet<String>();
Set<String> deletedFiles = new HashSet<String>();
if (diffEntries.size() < 1) {
return m;
}
for (DiffEntry entry : diffEntries) {
if (entry.getChangeType().name().equals(ChangeType.ADD.name())) {
newlyAddedFiles.add(entry.getNewPath());
// newlyAddedFiles.add(entry.getNewPath() + ":" + "file_timestamp");
} else if (entry.getChangeType().name().equals(ChangeType.DELETE.name())) {
deletedFiles.add(entry.getOldPath());
// deletedFiles.add(entry.getOldPath() + ":" + "file_timestamp");
} else {
formatter.format(entry);
changedFiles.add(entry.getNewPath());
// changedFiles.add(entry.getNewPath() + ":" + "file_timestamp");
}
}
m.put(Constants.CHANGED_FILE_STR, changedFiles);
m.put(Constants.NEWLY_ADDED_FILE_STR, newlyAddedFiles);
m.put(Constants.DELETED_FILE_STR, deletedFiles);
return m;
}
答案 0 :(得分:1)
Git不存储文件修改时间戳。但是,存储的是创建提交的时间。
可以使用RevCommit::getCommitTime()