Browse Source

Show submodule difference as a hunk

Current DiffFormat behavior regarding submodules (aka git links) is
incorrect. The "Subproject commit <sha1>" appears as part of the diff
header, rather than as its own hunk.

--> From JGit implementation
 diff --git a/plugins/cookbook-plugin b/plugins/cookbook-plugin
 index b9d3ca8..ec6ed89 160000
 --- a/plugins/cookbook-plugin
 +++ b/plugins/cookbook-plugin
 -Subproject commit b9d3ca8a65030071e28be19296ba867ab424fbbf
 +Subproject commit ec6ed89c47ba7223f82d9cb512926a6c5081343e

--> From C Git 2.5.2
 diff --git a/plugins/cookbook-plugin b/plugins/cookbook-plugin
 index b9d3ca8..ec6ed89 160000
 --- a/plugins/cookbook-plugin
 +++ b/plugins/cookbook-plugin
 @@ -1 +1 @@
 -Subproject commit b9d3ca8a65030071e28be19296ba867ab424fbbf
 +Subproject commit ec6ed89c47ba7223f82d9cb512926a6c5081343e

The current way of processing submodules results in no hunk header and
includes the contents of the hunk as part of the headers. To fix this, we
can't just have our writeGitLinkDiffText output the hunk header. We have
to change the flow so that the raw text gets parsed as a diff. The easiest
way to do this is to fake the RawText in the FormatResult when we have a
GITLINK.

It should be noted that it seems possible for there to be a difference
between a GITLINK and a non-GITLINK, but I don't think this can happen in
practice, so I don't think we need to worry too much about it.

This patch also fixes up the test for GitLink headers, as the test was
for the old behavior. My setup has 3 other failing tests which may or
may not be the result of environmental changes. However, the same tests
fail without this commit, so I do not believe they are related.

Bug: 477759
Change-Id: If13f7b406904fad814416c93ed09ea47ef183337
Signed-off-by: Jacob Keller <jacob.keller@gmail.com>
stable-4.1
Jacob Keller 9 years ago
parent
commit
a28ba3f2c7
  1. 5
      org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffFormatterTest.java
  2. 31
      org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java

5
org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffFormatterTest.java

@ -241,8 +241,7 @@ public class DiffFormatterTest extends RepositoryTestCase {
ObjectId bId = blob("b\n"); ObjectId bId = blob("b\n");
String diffHeader = makeDiffHeaderModeChange(PATH_A, PATH_A, aId, bId, String diffHeader = makeDiffHeaderModeChange(PATH_A, PATH_A, aId, bId,
GITLINK, REGULAR_FILE) GITLINK, REGULAR_FILE);
+ "-Subproject commit " + aId.name() + "\n";
DiffEntry ad = DiffEntry.delete(PATH_A, aId); DiffEntry ad = DiffEntry.delete(PATH_A, aId);
ad.oldMode = FileMode.GITLINK; ad.oldMode = FileMode.GITLINK;
@ -257,7 +256,7 @@ public class DiffFormatterTest extends RepositoryTestCase {
assertEquals(1, fh.getHunks().size()); assertEquals(1, fh.getHunks().size());
HunkHeader hh = fh.getHunks().get(0); HunkHeader hh = fh.getHunks().get(0);
assertEquals(0, hh.toEditList().size()); assertEquals(1, hh.toEditList().size());
} }
@Test @Test

31
org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java

@ -665,16 +665,10 @@ public class DiffFormatter implements AutoCloseable {
format(res.header, res.a, res.b); format(res.header, res.a, res.b);
} }
private static void writeGitLinkDiffText(OutputStream o, DiffEntry ent) private static byte[] writeGitLinkText(AbbreviatedObjectId id)
throws IOException { throws IOException {
if (ent.getOldMode() == GITLINK) { return encodeASCII("Subproject commit " + id.name() //$NON-NLS-1$
o.write(encodeASCII("-Subproject commit " + ent.getOldId().name() //$NON-NLS-1$ + "\n");
+ "\n")); //$NON-NLS-1$
}
if (ent.getNewMode() == GITLINK) {
o.write(encodeASCII("+Subproject commit " + ent.getNewId().name() //$NON-NLS-1$
+ "\n")); //$NON-NLS-1$
}
} }
private String format(AbbreviatedObjectId id) { private String format(AbbreviatedObjectId id) {
@ -938,13 +932,7 @@ public class DiffFormatter implements AutoCloseable {
formatHeader(buf, ent); formatHeader(buf, ent);
if (ent.getOldMode() == GITLINK || ent.getNewMode() == GITLINK) { if (ent.getOldId() == null || ent.getNewId() == null) {
formatOldNewPaths(buf, ent);
writeGitLinkDiffText(buf, ent);
editList = new EditList();
type = PatchType.UNIFIED;
} else if (ent.getOldId() == null || ent.getNewId() == null) {
// Content not changed (e.g. only mode, pure rename) // Content not changed (e.g. only mode, pure rename)
editList = new EditList(); editList = new EditList();
type = PatchType.UNIFIED; type = PatchType.UNIFIED;
@ -952,8 +940,15 @@ public class DiffFormatter implements AutoCloseable {
} else { } else {
assertHaveRepository(); assertHaveRepository();
byte[] aRaw = open(OLD, ent); byte[] aRaw, bRaw;
byte[] bRaw = open(NEW, ent);
if (ent.getOldMode() == GITLINK || ent.getNewMode() == GITLINK) {
aRaw = writeGitLinkText(ent.getOldId());
bRaw = writeGitLinkText(ent.getNewId());
} else {
aRaw = open(OLD, ent);
bRaw = open(NEW, ent);
}
if (aRaw == BINARY || bRaw == BINARY // if (aRaw == BINARY || bRaw == BINARY //
|| RawText.isBinary(aRaw) || RawText.isBinary(bRaw)) { || RawText.isBinary(aRaw) || RawText.isBinary(bRaw)) {

Loading…
Cancel
Save