[PATCH 1 of 2] mq: automatically upgrade to git patch when necessary (issue767)
Patrick Mezard
pmezard at gmail.com
Tue Dec 29 10:40:45 CST 2009
# HG changeset patch
# User Patrick Mezard <pmezard at gmail.com>
# Date 1262104754 -3600
# Node ID 63293717af8ba7ac579da3a4f80f7be6e621c9c3
# Parent c7355a0e1f39968ab39a62e9bf059e5129a61497
mq: automatically upgrade to git patch when necessary (issue767)
diff --git a/hgext/mq.py b/hgext/mq.py
--- a/hgext/mq.py
+++ b/hgext/mq.py
@@ -26,6 +26,15 @@
add known patch to applied stack qpush
remove patch from applied stack qpop
refresh contents of top applied patch qrefresh
+
+By default, mq will automatically use git patches when required to
+avoid data loss when recording changes to file modes, copy records or
+binary files. This behaviour can be disabled by setting the following
+configuration option:
+
+ [mq]
+ upgradediff = False
+
'''
from mercurial.i18n import _
@@ -261,7 +270,9 @@
def diffopts(self):
if self._diffopts is None:
- self._diffopts = patch.diffopts(self.ui)
+ opts = patch.diffopts(self.ui)
+ opts.upgrade = self.ui.configbool('mq', 'upgradediff', True)
+ self._diffopts = opts
return self._diffopts
def join(self, *p):
@@ -1253,7 +1264,7 @@
patchf.write(chunk)
try:
- if self.diffopts().git:
+ if self.diffopts().git or self.diffopts().upgrade:
copies = {}
for dst in a:
src = repo.dirstate.copied(dst)
diff --git a/mercurial/mdiff.py b/mercurial/mdiff.py
--- a/mercurial/mdiff.py
+++ b/mercurial/mdiff.py
@@ -27,7 +27,9 @@
nodates removes dates from diff headers
ignorews ignores all whitespace changes in the diff
ignorewsamount ignores changes in the amount of whitespace
- ignoreblanklines ignores changes whose lines are all blank'''
+ ignoreblanklines ignores changes whose lines are all blank
+ upgrade generates git diffs to avoid data loss
+ '''
defaults = {
'context': 3,
@@ -38,6 +40,7 @@
'ignorews': False,
'ignorewsamount': False,
'ignoreblanklines': False,
+ 'upgrade': False,
}
__slots__ = defaults.keys()
@@ -55,6 +58,11 @@
raise util.Abort(_('diff context lines count must be '
'an integer, not %r') % self.context)
+ def copy(self, **kwargs):
+ opts = dict((k, getattr(self, k)) for k in self.defaults)
+ opts.update(kwargs)
+ return diffopts(**opts)
+
defaultopts = diffopts()
def wsclean(opts, text, blank=True):
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -1246,10 +1246,8 @@
ret.append('\n')
return ''.join(ret)
-def _addmodehdr(header, omode, nmode):
- if omode != nmode:
- header.append('old mode %s\n' % omode)
- header.append('new mode %s\n' % nmode)
+class GitDiffRequired(Exception):
+ pass
def diff(repo, node1=None, node2=None, match=None, changes=None, opts=None):
'''yields diff of changes to files between two nodes, or node and
@@ -1288,24 +1286,47 @@
modified, added, removed = changes[:3]
if not modified and not added and not removed:
- return
+ return []
+
+ revs = None
+ if not repo.ui.quiet:
+ hexfunc = repo.ui.debugflag and hex or short
+ revs = [hexfunc(node) for node in [node1, node2] if node]
+
+ copy = {}
+ if opts.git or opts.upgrade:
+ copy = copies.copies(repo, ctx1, ctx2, repo[nullid])[0]
+ copy = copy.copy()
+ for k, v in copy.items():
+ copy[v] = k
+
+ difffn = lambda opts: trydiff(repo, revs, ctx1, ctx2, modified,
+ added, removed, copy, getfilectx, opts)
+ if opts.upgrade and not opts.git:
+ try:
+ # Buffer the whole output until we are sure it can be generated
+ return list(difffn(opts.copy(git=False)))
+ except GitDiffRequired:
+ return difffn(opts.copy(git=True))
+ else:
+ return difffn(opts)
+
+def _addmodehdr(header, omode, nmode):
+ if omode != nmode:
+ header.append('old mode %s\n' % omode)
+ header.append('new mode %s\n' % nmode)
+
+def trydiff(repo, revs, ctx1, ctx2, modified, added, removed,
+ copy, getfilectx, opts):
date1 = util.datestr(ctx1.date())
man1 = ctx1.manifest()
- revs = None
- if not repo.ui.quiet and not opts.git:
- hexfunc = repo.ui.debugflag and hex or short
- revs = [hexfunc(node) for node in [node1, node2] if node]
+ gone = set()
+ gitmode = {'l': '120000', 'x': '100755', '': '100644'}
if opts.git:
- copy, diverge = copies.copies(repo, ctx1, ctx2, repo[nullid])
- copy = copy.copy()
- for k, v in copy.items():
- copy[v] = k
-
- gone = set()
- gitmode = {'l': '120000', 'x': '100755', '': '100644'}
+ revs = None
for f in sorted(modified + added + removed):
to = None
@@ -1317,10 +1338,12 @@
if f not in removed:
tn = getfilectx(f, ctx2).data()
a, b = f, f
- if opts.git:
+ if opts.git or opts.upgrade:
if f in added:
mode = gitmode[ctx2.flags(f)]
if f in copy:
+ if not opts.git:
+ raise GitDiffRequired()
a = copy[f]
omode = gitmode[man1.flags(a)]
_addmodehdr(header, omode, mode)
@@ -1333,8 +1356,12 @@
header.append('%s to %s\n' % (op, f))
to = getfilectx(a, ctx1).data()
else:
+ if not opts.git and ctx2.flags(f):
+ raise GitDiffRequired()
header.append('new file mode %s\n' % mode)
if util.binary(tn):
+ if not opts.git:
+ raise GitDiffRequired()
dodiff = 'binary'
elif f in removed:
# have we already reported a copy above?
@@ -1347,9 +1374,19 @@
omode = gitmode[man1.flags(f)]
nmode = gitmode[ctx2.flags(f)]
_addmodehdr(header, omode, nmode)
- if util.binary(to) or util.binary(tn):
+ binary = util.binary(to) or util.binary(tn)
+ if binary:
dodiff = 'binary'
+ if not opts.git and (man1.flags(f) or ctx2.flags(f) or binary):
+ raise GitDiffRequired()
header.insert(0, mdiff.diffline(revs, a, b, opts))
+
+ if not opts.git:
+ # It's simpler to cleanup git changes than branching
+ # all the code
+ dodiff = True
+ header = []
+
if dodiff:
if dodiff == 'binary':
text = b85diff(to, tn)
diff --git a/tests/test-mq-upgrade b/tests/test-mq-upgrade
new file mode 100755
--- /dev/null
+++ b/tests/test-mq-upgrade
@@ -0,0 +1,76 @@
+#!/bin/sh
+
+echo "[extensions]" >> $HGRCPATH
+echo "mq=" >> $HGRCPATH
+echo "[diff]" >> $HGRCPATH
+echo "nodates=1" >> $HGRCPATH
+
+echo % test mq.upgrade
+hg init repo-upgrade
+cd repo-upgrade
+
+echo % regular patch creation
+echo a > a
+hg add a
+hg qnew -d '0 0' -f pa
+cat .hg/patches/pa
+echo % git patch after qrefresh and execute bit
+chmod +x a
+hg qrefresh -d '0 0'
+cat .hg/patches/pa
+
+echo % regular patch for file removal
+hg rm a
+hg qnew -d '0 0' -f rma
+cat .hg/patches/rma
+
+echo % git patch creation for execute bit
+echo b > b
+chmod +x b
+hg add b
+hg qnew -d '0 0' -f pb
+cat .hg/patches/pb
+echo % git patch after execute bit removal, preserving gitness
+chmod -x b
+hg qrefresh -d '0 0'
+cat .hg/patches/pb
+
+echo % git patch creation for copy
+hg cp b b2
+hg qnew -d '0 0' -f copyb
+cat .hg/patches/copyb
+
+echo % regular patch creation for text change
+echo b >> b
+hg qnew -d '0 0' -f changeb
+cat .hg/patches/changeb
+
+echo % git patch after qrefresh on binary file
+python -c "file('b', 'wb').write('\0')"
+hg qrefresh -d '0 0'
+cat .hg/patches/changeb
+
+echo % git patch after restoring text file, preserving gitness
+echo bbb > b
+hg qrefresh -d '0 0'
+cat .hg/patches/changeb
+
+echo % regular patch with file filtering
+echo regular > regular
+echo exec > exec
+chmod +x exec
+python -c "file('binary', 'wb').write('\0')"
+hg cp b copy
+hg add regular exec binary
+hg qnew -d '0 0' -f regular regular
+cat .hg/patches/regular
+hg qrefresh -d '0 0'
+
+echo % git patch when using --git
+echo a >> regular
+hg qnew -d '0 0' --git -f git
+cat .hg/patches/git
+echo % git patch without --git for we preserve gitness
+hg qrefresh -d '0 0'
+cat .hg/patches/git
+cd ..
\ No newline at end of file
diff --git a/tests/test-mq-upgrade.out b/tests/test-mq-upgrade.out
new file mode 100644
--- /dev/null
+++ b/tests/test-mq-upgrade.out
@@ -0,0 +1,115 @@
+% test mq.upgrade
+% regular patch creation
+# HG changeset patch
+# Date 0 0
+
+diff -r 000000000000 -r 5d9da5fe342b a
+--- /dev/null
++++ b/a
+@@ -0,0 +1,1 @@
++a
+% git patch after qrefresh and execute bit
+# HG changeset patch
+# Date 0 0
+
+diff --git a/a b/a
+new file mode 100755
+--- /dev/null
++++ b/a
+@@ -0,0 +1,1 @@
++a
+% regular patch for file removal
+# HG changeset patch
+# Date 0 0
+
+diff -r cf31b8738214 -r 94897e9819cb a
+--- a/a
++++ /dev/null
+@@ -1,1 +0,0 @@
+-a
+% git patch creation for execute bit
+# HG changeset patch
+# Date 0 0
+
+diff --git a/b b/b
+new file mode 100755
+--- /dev/null
++++ b/b
+@@ -0,0 +1,1 @@
++b
+% git patch after execute bit removal, preserving gitness
+# HG changeset patch
+# Date 0 0
+
+diff --git a/b b/b
+new file mode 100644
+--- /dev/null
++++ b/b
+@@ -0,0 +1,1 @@
++b
+% git patch creation for copy
+# HG changeset patch
+# Date 0 0
+
+diff --git a/b b/b2
+copy from b
+copy to b2
+% regular patch creation for text change
+# HG changeset patch
+# Date 0 0
+
+diff -r 7334c7fc8cbe -r bd5fafe02efe b
+--- a/b
++++ b/b
+@@ -1,1 +1,2 @@
+ b
++b
+% git patch after qrefresh on binary file
+# HG changeset patch
+# Date 0 0
+
+diff --git a/b b/b
+index 61780798228d17af2d34fce4cfbdf35556832472..f76dd238ade08917e6712764a16a22005a50573d
+GIT binary patch
+literal 1
+Ic${MZ000310RR91
+
+% git patch after restoring text file, preserving gitness
+# HG changeset patch
+# Date 0 0
+
+diff --git a/b b/b
+--- a/b
++++ b/b
+@@ -1,1 +1,1 @@
+-b
++bbb
+% regular patch with file filtering
+# HG changeset patch
+# Date 0 0
+
+diff -r 921b0108acc0 -r d7603636026d regular
+--- /dev/null
++++ b/regular
+@@ -0,0 +1,1 @@
++regular
+% git patch when using --git
+# HG changeset patch
+# Date 0 0
+
+diff --git a/regular b/regular
+--- a/regular
++++ b/regular
+@@ -1,1 +1,2 @@
+ regular
++a
+% git patch without --git for we preserve gitness
+# HG changeset patch
+# Date 0 0
+
+diff --git a/regular b/regular
+--- a/regular
++++ b/regular
+@@ -1,1 +1,2 @@
+ regular
++a
diff --git a/tests/test-mq.out b/tests/test-mq.out
--- a/tests/test-mq.out
+++ b/tests/test-mq.out
@@ -21,6 +21,12 @@
remove patch from applied stack qpop
refresh contents of top applied patch qrefresh
+By default, mq will automatically use git patches when required to avoid data
+loss when recording changes to file modes, copy records or binary files. This
+behaviour can be disabled by setting the following configuration option:
+
+ [mq] upgradediff = False
+
list of commands:
qapplied print the patches already applied
More information about the Mercurial-devel
mailing list