为什么我在使用JGit库的DiffFormatter时捕获IOException?

huangapple 未分类评论56阅读模式
英文:

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(&quot;user.dir&quot;)));

            DiffFormatter df = new DiffFormatter(new ByteArrayOutputStream());
            df.setRepository(git.getRepository());

            List&lt;DiffEntry&gt; diffs = git.diff()/*.setOutputStream( System.out )*/.call();

            for(DiffEntry diff : diffs) {
                df.format(diff); //IOException here

                System.out.println(diff.getChangeType() + &quot; &quot; + 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 &amp;&amp; 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?

huangapple
  • 本文由 发表于 2020年5月31日 07:08:09
  • 转载请务必保留本文链接:https://java.coder-hub.com/62109703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定