summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit
diff options
context:
space:
mode:
authorNitzan Gur-Furman <nitzan@google.com>2023-11-15 21:04:00 +0100
committerNitzan Gur-Furman <nitzan@google.com>2023-11-16 09:47:19 +0100
commit754a1b49224d5a977d4d155118862f50996a8622 (patch)
tree144fd9db269108c0496f19c86abc185840d865cc /org.eclipse.jgit
parent91c9146224cf2f8c9053cf565fa292c983acaca0 (diff)
downloadjgit-754a1b49224d5a977d4d155118862f50996a8622.tar.gz
jgit-754a1b49224d5a977d4d155118862f50996a8622.zip
PatchApplier: wrap output's TemporaryBuffer with a CountingOutputStream
The documentation for TemporaryBuffer::length says: "The length is only accurate after {@link #close()} has been invoked". However, we need to have the stream open while accessing the length. This prevents patches on large files to be applied correctly, as the result get trimmed. Bug: Google b/309500446 Change-Id: Ic1540f6d0044088f3b46f1fad5f6a28ec254b711
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/patch/PatchApplier.java8
1 files changed, 6 insertions, 2 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/patch/PatchApplier.java b/org.eclipse.jgit/src/org/eclipse/jgit/patch/PatchApplier.java
index 1edd85421a..a327095c81 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/patch/PatchApplier.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/patch/PatchApplier.java
@@ -88,6 +88,7 @@ import org.eclipse.jgit.util.TemporaryBuffer;
import org.eclipse.jgit.util.TemporaryBuffer.LocalFile;
import org.eclipse.jgit.util.io.BinaryDeltaInputStream;
import org.eclipse.jgit.util.io.BinaryHunkInputStream;
+import org.eclipse.jgit.util.io.CountingOutputStream;
import org.eclipse.jgit.util.io.EolStreamTypeUtil;
import org.eclipse.jgit.util.sha1.SHA1;
@@ -1013,7 +1014,10 @@ public class PatchApplier {
// We could check if old == new, but the short-circuiting complicates
// logic for inCore patching, so just write the new thing regardless.
TemporaryBuffer buffer = new TemporaryBuffer.LocalFile(null);
- try (OutputStream out = buffer) {
+ // TemporaryBuffer::length reports incorrect length until the buffer
+ // is closed. To use it as input for ContentStreamLoader below, we
+ // need a wrapper with a reliable in-progress length.
+ try (CountingOutputStream out = new CountingOutputStream(buffer)) {
for (Iterator<ByteBuffer> l = newLines.iterator(); l.hasNext();) {
ByteBuffer line = l.next();
if (line == null) {
@@ -1026,7 +1030,7 @@ public class PatchApplier {
}
}
return new ContentStreamLoader(buffer::openInputStream,
- buffer.length());
+ out.getCount());
}
}