Mercurial > hg-stable
changeset 20137:9f1d4323c749
patch: add support for git delta hunks
When creating patches modifying binary files using "git format-patch",
git creates 'literal' and 'delta' hunks. Mercurial currently supports
'literal' hunks only, which makes it impossible to import patches with
'delta' hunks.
This changeset adds support for 'delta' hunks. It is a reimplementation
of patch-delta.c from git :
http://git.kernel.org/cgit/git/git.git/tree/patch-delta.c
author | Nicolas Vigier <boklm@mars-attacks.org> |
---|---|
date | Wed, 27 Nov 2013 18:39:00 +0100 |
parents | 1df77035c814 |
children | db0f8738d3d4 |
files | mercurial/patch.py tests/test-import-git.t |
diffstat | 2 files changed, 183 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/patch.py Mon Nov 25 12:12:42 2013 -0500 +++ b/mercurial/patch.py Wed Nov 27 18:39:00 2013 +0100 @@ -721,8 +721,9 @@ if self.remove: self.backend.unlink(self.fname) else: - self.lines[:] = h.new() - self.offset += len(h.new()) + l = h.new(self.lines) + self.lines[:] = l + self.offset += len(l) self.dirty = True return 0 @@ -1016,9 +1017,10 @@ return old, oldstart, new, newstart class binhunk(object): - 'A binary patch file. Only understands literals so far.' + 'A binary patch file.' def __init__(self, lr, fname): self.text = None + self.delta = False self.hunk = ['GIT binary patch\n'] self._fname = fname self._read(lr) @@ -1026,7 +1028,9 @@ def complete(self): return self.text is not None - def new(self): + def new(self, lines): + if self.delta: + return [applybindelta(self.text, ''.join(lines))] return [self.text] def _read(self, lr): @@ -1035,14 +1039,19 @@ hunk.append(l) return l.rstrip('\r\n') + size = 0 while True: line = getline(lr, self.hunk) if not line: raise PatchError(_('could not extract "%s" binary data') % self._fname) if line.startswith('literal '): + size = int(line[8:].rstrip()) break - size = int(line[8:].rstrip()) + if line.startswith('delta '): + size = int(line[6:].rstrip()) + self.delta = True + break dec = [] line = getline(lr, self.hunk) while len(line) > 1: @@ -1265,6 +1274,62 @@ gp = gitpatches.pop() yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp.copy()) +def applybindelta(binchunk, data): + """Apply a binary delta hunk + The algorithm used is the algorithm from git's patch-delta.c + """ + def deltahead(binchunk): + i = 0 + for c in binchunk: + i += 1 + if not (ord(c) & 0x80): + return i + return i + out = "" + s = deltahead(binchunk) + binchunk = binchunk[s:] + s = deltahead(binchunk) + binchunk = binchunk[s:] + i = 0 + while i < len(binchunk): + cmd = ord(binchunk[i]) + i += 1 + if (cmd & 0x80): + offset = 0 + size = 0 + if (cmd & 0x01): + offset = ord(binchunk[i]) + i += 1 + if (cmd & 0x02): + offset |= ord(binchunk[i]) << 8 + i += 1 + if (cmd & 0x04): + offset |= ord(binchunk[i]) << 16 + i += 1 + if (cmd & 0x08): + offset |= ord(binchunk[i]) << 24 + i += 1 + if (cmd & 0x10): + size = ord(binchunk[i]) + i += 1 + if (cmd & 0x20): + size |= ord(binchunk[i]) << 8 + i += 1 + if (cmd & 0x40): + size |= ord(binchunk[i]) << 16 + i += 1 + if size == 0: + size = 0x10000 + offset_end = offset + size + out += data[offset:offset_end] + elif cmd != 0: + offset_end = i + cmd + out += binchunk[i:offset_end] + i += cmd + else: + raise PatchError(_('unexpected delta opcode 0')) + return out + def applydiff(ui, fp, backend, store, strip=1, eolmode='strict'): """Reads a patch from fp and tries to apply it.
--- a/tests/test-import-git.t Mon Nov 25 12:12:42 2013 -0500 +++ b/tests/test-import-git.t Wed Nov 27 18:39:00 2013 +0100 @@ -320,6 +320,115 @@ 045c85ba38952325e126c70962cc0f9d9077bc67 644 mbinary1 a874b471193996e7cb034bb301cac7bdaf3e3f46 644 mbinary2 +Binary file and delta hunk: + + $ hg import -d "1000000 0" -m delta - <<'EOF' + > diff --git a/delta b/delta + > new file mode 100644 + > index 0000000000000000000000000000000000000000..8c9b7831b231c2600843e303e66b521353a200b3 + > GIT binary patch + > literal 3749 + > zcmV;W4qEYvP)<h;3K|Lk000e1NJLTq006iE002D*0ssI2kt{U(0000PbVXQnQ*UN; + > zcVTj606}DLVr3vnZDD6+Qe|Oed2z{QJOBU=M@d9MRCwC#oC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`vljgo!(wHf=)F#m2_MIuj;L(2ja2YsQRX+rswV{d<H`Ar;(@%aNa9VPU8Z + > z;tq*`y}dm#NDJHKlV}uTIm!_vAq5E7!X-p{P=Z=Sh668>PuVS1*6e}OwOiMc;u3OQ + > z@Bs)w3=lzfKoufH$SFuPG@uZ4NOnM#+=8LnQ2Q4zUd+nM+OT26;lqbN{P07dhH{jH + > zManE8^dLms-Q2;1kB<*Q1a3f8kZr;xX=!Qro@`~@xN*Qj>gx;i;0Z24!~i2uLb`}v + > zA?R$|wvC+m^Ups=*(4lDh*=UN8{5h(A?p#D^2N$8u4Z55!q?ZAh(iEEng9_Zi>IgO + > z#~**JC8hE4@n{hO&8btT5F*?nC_%LhA3i)PDhh-pB_&1wGrDIl^*=8x3n&;akBf^- + > zJd&86kq$%%907v^tgWoQdwI`|oNK%VvU~S#C<o^F?6c48?Cjj#-4P<>HFD%&|Ni~t + > zKJ(|#H`$<5W+6ZkBb213rXonKZLB+X>^L}J@W6osP3piLD_5?R!`S}*{xLBzFiL4@ + > zX+}l{`A%?f@T5tT%ztu60p;)be`fWC`tP@WpO=?cpf8Xuf1OSj6d3f@Ki(ovDYq%0 + > z{4ZSe`kOay5@=lAT!}vFzxyemC{sXDrhuYM0Y#ZI1r%ipD9W11{w=@&xgJ}t2x;ep + > P00000NkvXXu0mjfZ5|Er + > + > literal 0 + > HcmV?d00001 + > + > EOF + applying patch from stdin + + $ hg manifest --debug | grep delta + 9600f98bb60ce732634d126aaa4ac1ec959c573e 644 delta + + $ hg import -d "1000000 0" -m delta - <<'EOF' + > diff --git a/delta b/delta + > index 8c9b7831b231c2600843e303e66b521353a200b3..0021dd95bc0dba53c39ce81377126d43731d68df 100644 + > GIT binary patch + > delta 49 + > zcmZ1~yHs|=21Z8J$r~9bFdA-lVv=EEw4WT$qRf2QSa5SIOAHI6(&k4T8H|kLo4vWB + > FSO9ZT4bA`n + > + > delta 49 + > zcmV-10M7rV9i<(xumJ(}ld%Di0Xefm0vrMXpOaq%BLm9I%d>?9Tm%6Vv*HM70RcC& + > HOA1;9yU-AD + > + > EOF + applying patch from stdin + + $ hg manifest --debug | grep delta + 56094bbea136dcf8dbd4088f6af469bde1a98b75 644 delta + Filenames with spaces: $ sed 's,EOL$,,g' <<EOF | hg import -d "1000000 0" -m spaces - @@ -334,7 +443,7 @@ applying patch from stdin $ hg tip -q - 12:47500ce1614e + 14:4b79479c9a6d $ cat "foo bar" foo @@ -357,7 +466,7 @@ applying patch from stdin $ hg tip -q - 13:6757efb07ea9 + 15:9cbe44af4ae9 $ cat foo3 foo @@ -392,8 +501,8 @@ Invalid base85 content $ hg rollback - repository tip rolled back to revision 14 (undo import) - working directory now based on revision 14 + repository tip rolled back to revision 16 (undo import) + working directory now based on revision 16 $ hg revert -aq $ hg import -d "1000000 0" -m invalid-binary - <<"EOF" > diff --git a/text2 b/binary2