如何使用JGit获取文件中的更改列表?
使用JGit,我想获得提交文件中的更改列表,就像使用.git log --full-history -p -1 <hash-id>
这可能吗?如果是这样,你是怎么做到的?
我知道如何获取每个提交,并查看提交消息:
//Load repo
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repo = builder.setGitDir(new File("/path/to/repo/.git")).setMustExist(true).build();
Git git = new Git(repo);
//get commits
Iterable<RevCommit> log = git.log().call();
for (RevCommit commit : log) {
//Shows the hashid
System.out.println("LogCommit: " + commit);
//Shows only commit message
String logMessage = commit.getFullMessage();
System.out.println("LogMessage: " + logMessage);
}
git.close();
什么允许我获得文件中的更改?
例如,我写道:
git log --full-history -p -1 8309c1262e1b7ffce8fc86efc1ae5777a4a96777
响应是
commit 8309c1262e1b7ffce8fc86efc1ae5777a4a96777
Author: <redacted>
Date: Thu Aug 4 12:15:23 2016 -0400
Fixed typo in commit
diff --git a/Product/Production/Common/CONNECTCoreLib/src/main/java/gov/hhs/fha/nhinc/messaging/server/BaseService.java b/Product/Production/Common/CONNECTCoreLib/src/main/java/gov/hhs/fha/nhinc/messaging/server/BaseService.java
index fa55e7e..4f3c155 100644
--- a/Product/Production/Common/CONNECTCoreLib/src/main/java/gov/hhs/fha/nhinc/messaging/server/BaseService.java
+++ b/Product/Production/Common/CONNECTCoreLib/src/main/java/gov/hhs/fha/nhinc/messaging/server/BaseService.java
@@ -56,6 +57,7 @@ public abstract class BaseService {
protected AssertionType getAssertion(WebServiceContext context, AssertionType assertionIn) {
AssertionType assertion;
- WSAHeaderHelper wsaHlpr = new WSAHeaderHelper();
+ WSAHeaderHelper wsaHelper = new WSAHeaderHelper();
if (assertionIn == null) {
assertion = SAML2AssertionExtractor.getInstance().extractSamlAssertion(context);
我想要像下面这样的东西。更改是一个虚构的类:
//Load repo
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repo = builder.setGitDir(new File("/path/to/repo/.git")).setMustExist(true).build();
Git git = new Git(repo);
//get commits
Iterable<RevCommit> log = git.log().call();
for (RevCommit commit : log) {
//Shows the hashid
System.out.println("LogCommit: " + commit);
//Shows only commit message
String logMessage = commit.getFullMessage();
System.out.println("LogMessage: " + logMessage);
List<Change> changes = commit.getChanges();
for(Change change: changes):
System.out.println("File: " + change.getFile());
System.out.println("Change: " + change.getChange());
System.out.println("ChangeType: " + change.getChangeType());
}
git.close();
输出将如下所示:
LogCommit: 8309c1262e1b7ffce8fc86efc1ae5777a4a96777
LogMessage: Fixed typo in commit
File: Product/Production/Common/CONNECTCoreLib/src/main/java/gov/hhs/fha/nhinc/messaging/server/BaseService.java
Change: WSAHeaderHelper wsaHlpr = new WSAHeaderHelper();
ChangeType: D
File: Product/Production/Common/CONNECTCoreLib/src/main/java/gov/hhs/fha/nhinc/messaging/server/BaseService.java
Change: WSAHeaderHelper wsaHelper = new WSAHeaderHelper();
ChangeType: A