Browse Source

Apply hunks when renaming or copying from patch files

When applying a patch that contains renames or copies using ApplyCommand,
also apply all hunks that apply to the renamed or copied file.

Change-Id: I9f3fa4370458bd7c14beeb2e2b49e846d70203cb
Signed-off-by: Jack Wickham <jwickham@palantir.com>
stable-5.8
Jack Wickham 5 years ago committed by Matthias Sohn
parent
commit
b6d70a66ae
  1. 13
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/CopyWithHunks.patch
  2. 4
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/CopyWithHunks_PostImage
  3. 4
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/CopyWithHunks_PreImage
  4. 13
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/RenameWithHunks.patch
  5. 4
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/RenameWithHunks_PostImage
  6. 4
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/RenameWithHunks_PreImage
  7. 20
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java
  8. 7
      org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java

13
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/CopyWithHunks.patch

@ -0,0 +1,13 @@
diff --git a/CopyWithHunks b/CopyResult
similarity index 75%
copy from CopyWithHunks
copy to CopyResult
index 0000000..de98044
--- a/CopyWithHunks
+++ b/CopyResult
@@ -1,4 +1,4 @@
line1
-line2
+lineB
line3
line4

4
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/CopyWithHunks_PostImage

@ -0,0 +1,4 @@
line1
lineB
line3
line4

4
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/CopyWithHunks_PreImage

@ -0,0 +1,4 @@
line1
line2
line3
line4

13
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/RenameWithHunks.patch

@ -0,0 +1,13 @@
diff --git a/RenameWithHunks b/nested/subdir/Renamed
similarity index 75%
rename from RenameWithHunks
rename to nested/subdir/Renamed
index 0000000..de98044
--- a/RenameWithHunks
+++ b/nested/subdir/Renamed
@@ -1,4 +1,4 @@
line1
-line2
+lineB
line3
line4

4
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/RenameWithHunks_PostImage

@ -0,0 +1,4 @@
line1
lineB
line3
line4

4
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/RenameWithHunks_PreImage

@ -0,0 +1,4 @@
line1
line2
line3
line4

20
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java

@ -260,6 +260,26 @@ public class ApplyCommandTest extends RepositoryTestCase {
b.getString(0, b.size(), false)); b.getString(0, b.size(), false));
} }
@Test
public void testRenameWithHunks() throws Exception {
ApplyResult result = init("RenameWithHunks", true, true);
assertEquals(1, result.getUpdatedFiles().size());
assertEquals(new File(db.getWorkTree(), "RenameWithHunks"), result.getUpdatedFiles()
.get(0));
checkFile(new File(db.getWorkTree(), "nested/subdir/Renamed"),
b.getString(0, b.size(), false));
}
@Test
public void testCopyWithHunks() throws Exception {
ApplyResult result = init("CopyWithHunks", true, true);
assertEquals(1, result.getUpdatedFiles().size());
assertEquals(new File(db.getWorkTree(), "CopyWithHunks"), result.getUpdatedFiles()
.get(0));
checkFile(new File(db.getWorkTree(), "CopyResult"),
b.getString(0, b.size(), false));
}
private static byte[] readFile(String patchFile) throws IOException { private static byte[] readFile(String patchFile) throws IOException {
final InputStream in = getTestResource(patchFile); final InputStream in = getTestResource(patchFile);
if (in == null) { if (in == null) {

7
org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java

@ -121,18 +121,19 @@ public class ApplyCommand extends GitCommand<ApplyResult> {
throw new PatchApplyException(MessageFormat.format( throw new PatchApplyException(MessageFormat.format(
JGitText.get().renameFileFailed, f, dest), e); JGitText.get().renameFileFailed, f, dest), e);
} }
apply(dest, fh);
break; break;
case COPY: case COPY:
f = getFile(fh.getOldPath(), false); f = getFile(fh.getOldPath(), false);
byte[] bs = IO.readFully(f); byte[] bs = IO.readFully(f);
FileOutputStream fos = new FileOutputStream(getFile( File target = getFile(fh.getNewPath(), true);
fh.getNewPath(), FileOutputStream fos = new FileOutputStream(target);
true));
try { try {
fos.write(bs); fos.write(bs);
} finally { } finally {
fos.close(); fos.close();
} }
apply(target, fh);
} }
r.addUpdatedFile(f); r.addUpdatedFile(f);
} }

Loading…
Cancel
Save