英文:
Why do I catch IOException, when I use JGit library DiffFormatter?
问题
我想要检查使用DiffEntries确切更改了哪些文件,但是捕获了IOException:
try {
Git git = Git.open(new File(System.getProperty("user.dir")));
DiffFormatter df = new DiffFormatter(new ByteArrayOutputStream());
df.setRepository(git.getRepository());
List<DiffEntry> diffs = git.diff()/*.setOutputStream(System.out)*/.call();
for (DiffEntry diff : diffs) {
df.format(diff); // 这里会抛出IOException
System.out.println(diff.getChangeType() + " " + diff.getNewPath());
}
} catch (IOException e) {
e.printStackTrace();
}
以下是堆栈跟踪:
org.eclipse.jgit.errors.MissingObjectException: Missing blob 74f1fb04c13074b809262b7ab7af1766cfe4bf95
at org.eclipse.jgit.internal.storage.file.WindowCursor.open(WindowCursor.java:170)
at org.eclipse.jgit.diff.ContentSource$ObjectReaderSource.open(ContentSource.java:145)
at org.eclipse.jgit.diff.ContentSource$Pair.open(ContentSource.java:282)
at org.eclipse.jgit.diff.DiffFormatter.open(DiffFormatter.java:1073)
at org.eclipse.jgit.diff.DiffFormatter.createFormatResult(DiffFormatter.java:1001)
at org.eclipse.jgit.diff.DiffFormatter.format(DiffFormatter.java:706)
at com.plugin.engine.Main.main(Main.java:39)
总体而言,问题出现在JGit库的WindowsCursor.java
中:
/** {@inheritDoc} */
@Override
public ObjectLoader open(AnyObjectId objectId, int typeHint)
throws MissingObjectException, IncorrectObjectTypeException,
IOException {
final ObjectLoader ldr = db.openObject(this, objectId);
if (ldr == null) {
if (typeHint == OBJ_ANY)
throw new MissingObjectException(objectId.copy(),
JGitText.get().unknownObjectType2);
throw new MissingObjectException(objectId.copy(), typeHint); // 异常在此处抛出
}
if (typeHint != OBJ_ANY && ldr.getType() != typeHint)
throw new IncorrectObjectTypeException(objectId.copy(), typeHint);
return ldr;
}
我如何使用JGit打印文件更改,并且为什么会捕获IOException?
英文:
I want to check what files are exactly changed, using DiffEntries, but I catch IOException:
try {
Git git = Git.open(new File(System.getProperty("user.dir")));
DiffFormatter df = new DiffFormatter(new ByteArrayOutputStream());
df.setRepository(git.getRepository());
List<DiffEntry> diffs = git.diff()/*.setOutputStream( System.out )*/.call();
for(DiffEntry diff : diffs) {
df.format(diff); //IOException here
System.out.println(diff.getChangeType() + " " + diff.getNewPath());
}
}
catch (IOException e) {
e.printStackTrace();
}
Here is the stack trace:
org.eclipse.jgit.errors.MissingObjectException: Missing blob 74f1fb04c13074b809262b7ab7af1766cfe4bf95
at org.eclipse.jgit.internal.storage.file.WindowCursor.open(WindowCursor.java:170)
at org.eclipse.jgit.diff.ContentSource$ObjectReaderSource.open(ContentSource.java:145)
at org.eclipse.jgit.diff.ContentSource$Pair.open(ContentSource.java:282)
at org.eclipse.jgit.diff.DiffFormatter.open(DiffFormatter.java:1073)
at org.eclipse.jgit.diff.DiffFormatter.createFormatResult(DiffFormatter.java:1001)
at org.eclipse.jgit.diff.DiffFormatter.format(DiffFormatter.java:706)
at com.plugin.engine.Main.main(Main.java:39)
Overall it's came to WindowsCurcor.java in JGit library:
/** {@inheritDoc} */
@Override
public ObjectLoader open(AnyObjectId objectId, int typeHint)
throws MissingObjectException, IncorrectObjectTypeException,
IOException {
final ObjectLoader ldr = db.openObject(this, objectId);
if (ldr == null) {
if (typeHint == OBJ_ANY)
throw new MissingObjectException(objectId.copy(),
JGitText.get().unknownObjectType2);
throw new MissingObjectException(objectId.copy(), typeHint); //Exception thrown here
}
if (typeHint != OBJ_ANY && ldr.getType() != typeHint)
throw new IncorrectObjectTypeException(objectId.copy(), typeHint);
return ldr;
}
How can I print file changes using JGit and why do I catch IOException?
专注分享java语言的经验与见解,让所有开发者获益!
评论