Skip to content
Permalink
Browse files
vcs: proper source paths for merge commits
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Jun 22, 2020
1 parent 9f5bf30 commit 7f84ac80545403c9677bd0505197659cf9bd4eb0
@@ -147,7 +147,9 @@ private List<List<Hunk>> parseSingleFileMultiParentDiff(UnixStreamReader reader,

private List<PatchHeader> parseCombinedRawLine(String line) {
var headers = new ArrayList<PatchHeader>(numParents);
var words = line.substring(2).split("\\s");
var parts = line.substring(numParents).split("\\t");
var metadata = parts[0];
var words = metadata.split(" ");

int index = 0;
int end = index + numParents;
@@ -175,16 +177,21 @@ private List<PatchHeader> parseCombinedRawLine(String line) {
statuses.add(Status.from(statusWord.charAt(i)));
}

index++;
var dstPath = Path.of(words[index]);
if (words.length != (index + 1)) {
throw new IllegalStateException("Unexpected characters at end of raw line: " + line);

var srcPaths = new ArrayList<Path>(numParents);
index = 1;
end = index + numParents;
while (index < end) {
srcPaths.add(Path.of(parts[index]));
index++;
}

var dstPath = Path.of(parts[index]);

for (int i = 0; i < numParents; i++) {
var status = statuses.get(i);
var srcType = srcTypes.get(i);
var srcPath = status.isModified() ? dstPath : null;
var srcPath = status.isAdded() ? null : srcPaths.get(i);
var srcHash = srcHashes.get(i);
headers.add(new PatchHeader(srcPath, srcType, srcHash, dstPath, dstType, dstHash, status));
}
@@ -66,6 +66,7 @@ public Iterator<Commit> iterator() {
"--topo-order",
"--binary",
"-c",
"--combined-all-paths",
"--raw",
"--no-abbrev",
"--unified=0",
@@ -2350,4 +2350,50 @@ void testLightweightTags() throws IOException, InterruptedException {
assertEquals(Optional.empty(), repo.annotate(tag));
}
}

@ParameterizedTest
@EnumSource(VCS.class)
void testMergeCommitWithRenamedP0AndModifiedP1(VCS vcs) throws IOException {
try (var dir = new TemporaryDirectory(false)) {
var r = Repository.init(dir.path(), vcs);

var readme = dir.path().resolve("README.old");
Files.write(readme, List.of("Hello, world!"));
r.add(readme);
var first = r.commit("Added README.old", "duke", "duke@openjdk.java.net");

Files.write(readme, List.of("One more line"), WRITE, APPEND);
r.add(readme);
var second = r.commit("Modified README.old", "duke", "duke@openjdk.java.net");

r.checkout(first, false);
r.move(Path.of("README.old"), Path.of("README.new"));
var third = r.commit("Renamed README.old to README.new", "duke", "duke@openjdk.java.net");

r.merge(second);
var hash = r.commit("Merge", "duke", "duke@openjdk.java.net");
var merge = r.lookup(hash).orElseThrow();

var diffs = merge.parentDiffs();
assertEquals(2, diffs.size());

assertEquals(1, diffs.get(0).patches().size());
var p0 = diffs.get(0).patches().get(0);
assertTrue(p0.status().isModified());
assertEquals(Path.of("README.new"), p0.source().path().get());
assertEquals(Path.of("README.new"), p0.target().path().get());

assertEquals(1, diffs.get(1).patches().size());
var p1 = diffs.get(1).patches().get(0);
if (vcs == VCS.GIT) {
assertTrue(p1.status().isRenamed());
} else if (vcs == VCS.HG) {
assertTrue(p1.status().isCopied());
} else {
fail("Unknown VCS");
}
assertEquals(Path.of("README.old"), p1.source().path().get());
assertEquals(Path.of("README.new"), p1.target().path().get());
}
}
}

1 comment on commit 7f84ac8

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented on 7f84ac8 Jun 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.