From a221bdb2dc72389c41684024b898f88905e17825 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Thu, 11 Dec 2008 18:46:15 -0800 Subject: [PATCH] Parse "GIT binary patch" style patch metadata Git can produce binary patches that carry the full content or delta information encoded in a base-85 text payload. We parse the headers out and produce a BinaryHunk wrapper for these so applications can identify that the hunk exists, but we do not attempt to decode the binary information. Signed-off-by: Shawn O. Pearce Signed-off-by: Robin Rosenberg --- .../spearce/jgit/patch/testParse_GitBinary.patch | 135 +++++++++++++++++++++ .../tst/org/spearce/jgit/patch/PatchTest.java | 42 +++++++ .../src/org/spearce/jgit/patch/BinaryHunk.java | 128 +++++++++++++++++++ .../src/org/spearce/jgit/patch/FileHeader.java | 16 +++ .../src/org/spearce/jgit/patch/Patch.java | 32 +++++ 5 files changed, 353 insertions(+) create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/patch/testParse_GitBinary.patch create mode 100644 org.spearce.jgit/src/org/spearce/jgit/patch/BinaryHunk.java diff --git a/org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/patch/testParse_GitBinary.patch b/org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/patch/testParse_GitBinary.patch new file mode 100644 index 00000000..ab7b2359 --- /dev/null +++ b/org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/patch/testParse_GitBinary.patch @@ -0,0 +1,135 @@ +From 8363f12135a7d0ff0b5fea7d5a35d294c0479518 Mon Sep 17 00:00:00 2001 +From: Robin Rosenberg +Date: Tue, 23 Sep 2008 22:19:19 +0200 +Subject: [PATCH] Push and fetch icons for the toolbar + +Signed-off-by: Robin Rosenberg +Signed-off-by: Shawn O. Pearce +--- + org.spearce.egit.ui/icons/toolbar/fetchd.png | Bin 0 -> 359 bytes + org.spearce.egit.ui/icons/toolbar/fetche.png | Bin 0 -> 393 bytes + org.spearce.egit.ui/icons/toolbar/pushd.png | Bin 0 -> 372 bytes + org.spearce.egit.ui/icons/toolbar/pushe.png | Bin 0 -> 404 bytes + org.spearce.egit.ui/plugin.xml | 32 ++++++++++++++----------- + 5 files changed, 18 insertions(+), 14 deletions(-) + create mode 100644 org.spearce.egit.ui/icons/toolbar/fetchd.png + create mode 100644 org.spearce.egit.ui/icons/toolbar/fetche.png + create mode 100644 org.spearce.egit.ui/icons/toolbar/pushd.png + create mode 100644 org.spearce.egit.ui/icons/toolbar/pushe.png + +diff --git a/org.spearce.egit.ui/icons/toolbar/fetchd.png b/org.spearce.egit.ui/icons/toolbar/fetchd.png +new file mode 100644 +index 0000000000000000000000000000000000000000..4433c543f2a52b586a3ed5e31b138244107bc239 +GIT binary patch +literal 359 +zcmV-t0hs=YP)VW2}+n! +zO1rKr0DRxqm&=7b&q>piUayDaIKlvw2}+e_&-3(PFkmzq(Q380-|r+zg6Da9KA(#Q +zPa2QMl^Gz*GK3HmMPby1VQ9_(U^bf>W`H=3+3)uhMZxuY<#al++wJIfyXFi47K?>p +z2GCj~rNp*vI-L&fb{osGh@!|$@ci;S2_arv_(sil1xb<+1Odb0kjLY}`F!ShJaW6; +z>Lr*?r);;|7ihoV2SC*MjhoFzuh;A9KAB9aMXCk(Pk$Loi}X0uxcmSB002ovPDHLk +FV1lxPoI3yj + +literal 0 +HcmV?d00001 + +diff --git a/org.spearce.egit.ui/icons/toolbar/fetche.png b/org.spearce.egit.ui/icons/toolbar/fetche.png +new file mode 100644 +index 0000000000000000000000000000000000000000..0ffeb419e6ab302caa5e58661854b33853dc43dc +GIT binary patch +literal 393 +zcmV;40e1e0P)*L3{^aqJcvRqCsRQ6~SndH7ExK +zk=KcyE?#y6(aupYt$(ujUi|ChXYD1Vl>A3Z=W-tL|B2KE=%pm#uoxNA1!yxqxEMW& +zB>{jQO^yT+ogtn_{8Ep$Aq3h-C?o|y>g-6?-!e46K4}{7I2X6^?w$w$wKqXWo#uE< +zlN$@u$mIiCW0N$hIYc2#Jf_L5pe_`875HfeP>nhW1zLv1R!iSvNdTZ7`q(*62#YbV +zQhB;#V#z_Hl;tD;jPm%3!!_Fv=xqj&EpW_lqPo^m>_wFE9KxQ3t1@8v1#@h(gk?2k +zU%h_@BTD_vVB{6b=^Lij^3Dkk>j +n^=YB|UiI3T3toz$0fY1nZ1068v8@+b00000NkvXXu0mjfWwNMg + +literal 0 +HcmV?d00001 + +diff --git a/org.spearce.egit.ui/icons/toolbar/pushd.png b/org.spearce.egit.ui/icons/toolbar/pushd.png +new file mode 100644 +index 0000000000000000000000000000000000000000..22c3f7bf17b8b5a931c7527d768af13607b03bce +GIT binary patch +literal 372 +zcmV-)0gL{LP)A(?&%>K?5S92zkHxGyeyN +zQSnxRymxZ%OQJ-CZt%;)O$w?l-T1yl~1VO;{ +zdS$=gv)ODopU<8HfZ1#YAcMg`B@Q~B4vWRYJJDL}%|UCO8Yd6XZnu?)$aFeQidwCf +z_mE--u|}hjN&o=H7-fukIg4hqnKXNVchu|kh_lCf`xbzwX88RJ-{>O;Y5D>6^@Sy# +SDlMe|0000w|sG%6%VHSk(UL*L41dvA&3Z?G>L*BB*n0rWDTl8 +zL8Pb?Jzc!)CSJ}#$rF8}#a?Uu`(JCbg_M&2pmu`H$+oP&=V*R^(bPY1%&ibu+ZV$G +zgp`ttNBF4=c=f%6i@vsq5!CR9fSfc-IT0lZ>^0`E2vbS?r{1v +z8l^m+g%^~^H#FDePyq!%wm_SSqPmu`yJKk6QAXzdroz+R(7L#RCJHY0YK_74TR+C&ZX!&h^>3c +zJvdA^W^@l;f6eS*z&I*^D|{frVpE>&7273F76LY=;y1$BWF(Q0qALI}5jqkZAq&fh +y^_oorR)}l`>CE22@+$y+&Cvb}|KU##2Jr)k?t0Dap2#Es0000 + + ++ class="org.spearce.egit.ui.internal.actions.FetchAction" ++ disabledIcon="icons/toolbar/fetchd.png" ++ icon="icons/toolbar/fetche.png" ++ id="org.spearce.egit.ui.actionfetch" ++ label="%FetchAction_label" ++ menubarPath="org.spearce.egit.ui.gitmenu/repo" ++ style="push" ++ toolbarPath="org.spearce.egit.ui" ++ tooltip="%FetchAction_tooltip"> + + ++ class="org.spearce.egit.ui.internal.actions.PushAction" ++ disabledIcon="icons/toolbar/pushd.png" ++ icon="icons/toolbar/pushe.png" ++ id="org.spearce.egit.ui.actionpush" ++ label="%PushAction_label" ++ menubarPath="org.spearce.egit.ui.gitmenu/repo" ++ style="push" ++ toolbarPath="org.spearce.egit.ui" ++ tooltip="%PushAction_tooltip"> + + = 0) { + type = Type.LITERAL_DEFLATED; + length = parseBase10(buf, ptr + LITERAL.length, null); + + } else if (match(buf, ptr, DELTA) >= 0) { + type = Type.DELTA_DEFLATED; + length = parseBase10(buf, ptr + LITERAL.length, null); + + } else { + // Not a valid binary hunk. Signal to the caller that + // we cannot parse any further and that this line should + // be treated otherwise. + // + return -1; + } + ptr = nextLF(buf, ptr); + + // Skip until the first blank line; that is the end of the binary + // encoded information in this hunk. To save time we don't do a + // validation of the binary data at this point. + // + final int sz = buf.length; + while (ptr < sz) { + final boolean empty = buf[ptr] == '\n'; + ptr = nextLF(buf, ptr); + if (empty) + break; + } + + return ptr; + } +} diff --git a/org.spearce.jgit/src/org/spearce/jgit/patch/FileHeader.java b/org.spearce.jgit/src/org/spearce/jgit/patch/FileHeader.java index 67035ab7..d3dc8bcd 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/patch/FileHeader.java +++ b/org.spearce.jgit/src/org/spearce/jgit/patch/FileHeader.java @@ -158,6 +158,12 @@ public class FileHeader { /** The hunks of this file */ private List hunks; + /** If {@link #patchType} is {@link PatchType#GIT_BINARY}, the new image */ + BinaryHunk forwardBinaryHunk; + + /** If {@link #patchType} is {@link PatchType#GIT_BINARY}, the old image */ + BinaryHunk reverseBinaryHunk; + FileHeader(final byte[] b, final int offset) { buf = b; startOffset = offset; @@ -270,6 +276,16 @@ public class FileHeader { hunks.add(h); } + /** @return if a {@link PatchType#GIT_BINARY}, the new-image delta/literal */ + public BinaryHunk getForwardBinaryHunk() { + return forwardBinaryHunk; + } + + /** @return if a {@link PatchType#GIT_BINARY}, the old-image delta/literal */ + public BinaryHunk getReverseBinaryHunk() { + return reverseBinaryHunk; + } + /** * Parse a "diff --git" or "diff --cc" line. * diff --git a/org.spearce.jgit/src/org/spearce/jgit/patch/Patch.java b/org.spearce.jgit/src/org/spearce/jgit/patch/Patch.java index aeb30e95..5d0a0f88 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/patch/Patch.java +++ b/org.spearce.jgit/src/org/spearce/jgit/patch/Patch.java @@ -62,6 +62,8 @@ public class Patch { private static final byte[] BIN_TRAILER = encodeASCII(" differ\n"); + private static final byte[] GIT_BINARY = encodeASCII("GIT binary patch\n"); + static final byte[] SIG_FOOTER = encodeASCII("-- \n"); /** The files, in the order they were parsed out of the input. */ @@ -268,6 +270,11 @@ public class Patch { } final int eol = nextLF(buf, c); + if (fh.getHunks().isEmpty() && match(buf, c, GIT_BINARY) >= 0) { + fh.patchType = FileHeader.PatchType.GIT_BINARY; + return parseGitBinary(fh, eol); + } + if (fh.getHunks().isEmpty() && BIN_TRAILER.length < eol - c && match(buf, eol - BIN_TRAILER.length, BIN_TRAILER) >= 0 && matchAny(buf, c, BIN_HEADERS)) { @@ -295,6 +302,31 @@ public class Patch { return c; } + private int parseGitBinary(final FileHeader fh, int c) { + final BinaryHunk postImage = new BinaryHunk(fh, c); + final int nEnd = postImage.parseHunk(c); + if (nEnd < 0) { + // Not a binary hunk. + // + + // TODO handle invalid binary hunks + return c; + } + c = nEnd; + postImage.endOffset = c; + fh.forwardBinaryHunk = postImage; + + final BinaryHunk preImage = new BinaryHunk(fh, c); + final int oEnd = preImage.parseHunk(c); + if (oEnd >= 0) { + c = oEnd; + preImage.endOffset = c; + fh.reverseBinaryHunk = preImage; + } + + return c; + } + private static boolean matchAny(final byte[] buf, final int c, final byte[][] srcs) { for (final byte[] s : srcs) { -- 2.11.4.GIT