diff --git a/hgext/mq.py b/hgext/mq.py --- a/hgext/mq.py +++ b/hgext/mq.py @@ -26,6 +26,18 @@ Common tasks (use "hg help command" for 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 losing file mode changes, copy records, binary files or empty +files creations or deletions. This behaviour can be configured with:: + + [mq] + git = auto/keep/yes/no + +If set to 'keep', mq will obey the [diff] section configuration while +preserving existing git patches upon qrefresh. If set to 'yes' or +'no', mq will override the [diff] section and always generate git or +regular patches, possibly losing data in the second case. ''' from mercurial.i18n import _ @@ -225,6 +237,14 @@ class queue(object): self.guards_path = "guards" self.active_guards = None self.guards_dirty = False + # Handle mq.git as a bool with extended values + try: + gitmode = ui.configbool('mq', 'git', None) + if gitmode is None: + raise error.ConfigError() + self.gitmode = gitmode and 'yes' or 'no' + except error.ConfigError: + self.gitmode = ui.config('mq', 'git', 'auto').lower() @util.propertycache def applied(self): @@ -260,23 +280,33 @@ class queue(object): def diffopts(self, opts={}, patchfn=None): diffopts = patch.diffopts(self.ui, opts) + if self.gitmode == 'auto': + diffopts.upgrade = True + elif self.gitmode == 'keep': + pass + elif self.gitmode in ('yes', 'no'): + diffopts.git = self.gitmode == 'yes' + else: + raise util.Abort(_('mq.git option can be auto/keep/yes/no' + ' got %s') % self.gitmode) if patchfn: diffopts = self.patchopts(diffopts, patchfn) return diffopts def patchopts(self, diffopts, *patches): """Return a copy of input diff options with git set to true if - referenced patch is a git patch. + referenced patch is a git patch and should be preserved as such. """ diffopts = diffopts.copy() - for patchfn in patches: - patchf = self.opener(patchfn, 'r') - # if the patch was a git patch, refresh it as a git patch - for line in patchf: - if line.startswith('diff --git'): - diffopts.git = True - break - patchf.close() + if not diffopts.git and self.gitmode == 'keep': + for patchfn in patches: + patchf = self.opener(patchfn, 'r') + # if the patch was a git patch, refresh it as a git patch + for line in patchf: + if line.startswith('diff --git'): + diffopts.git = True + break + patchf.close() return diffopts def join(self, *p): @@ -1260,7 +1290,7 @@ class queue(object): patchf.write(chunk) try: - if diffopts.git: + if diffopts.git or diffopts.upgrade: copies = {} for dst in a: src = repo.dirstate.copied(dst) diff --git a/tests/test-mq-eol b/tests/test-mq-eol --- a/tests/test-mq-eol +++ b/tests/test-mq-eol @@ -4,6 +4,8 @@ echo "[extensions]" >> $HGRCPATH echo "mq=" >> $HGRCPATH +echo "[diff]" >> $HGRCPATH +echo "nodates=1" >> $HGRCPATH cat > makepatch.py < -diff --git a/a b/a +diff -r 0d0bf99a8b7a a --- a/a +++ b/a @@ -1,5 +1,5 @@ diff --git a/tests/test-mq-git b/tests/test-mq-git new file mode 100755 --- /dev/null +++ b/tests/test-mq-git @@ -0,0 +1,79 @@ +#!/bin/sh + +# Test the plumbing of mq.git option +# Automatic upgrade itself is tested elsewhere. + +echo "[extensions]" >> $HGRCPATH +echo "mq=" >> $HGRCPATH +echo "[diff]" >> $HGRCPATH +echo "nodates=1" >> $HGRCPATH + +hg init repo-auto +cd repo-auto +echo '% git=auto: regular patch creation' +echo a > a +hg add a +hg qnew -d '0 0' -f adda +cat .hg/patches/adda +echo '% git=auto: git patch creation with copy' +hg cp a b +hg qnew -d '0 0' -f copy +cat .hg/patches/copy +echo '% git=auto: git patch when using --git' +echo regular > regular +hg add regular +hg qnew -d '0 0' --git -f git +cat .hg/patches/git +echo '% git=auto: regular patch after qrefresh without --git' +hg qrefresh -d '0 0' +cat .hg/patches/git +cd .. + +hg init repo-keep +cd repo-keep +echo '[mq]' > .hg/hgrc +echo 'git = KEEP' >> .hg/hgrc +echo '% git=keep: git patch with --git' +echo a > a +hg add a +hg qnew -d '0 0' -f --git git +cat .hg/patches/git +echo '% git=keep: git patch after qrefresh without --git' +echo a >> a +hg qrefresh -d '0 0' +cat .hg/patches/git +cd .. + +hg init repo-yes +cd repo-yes +echo '[mq]' > .hg/hgrc +echo 'git = yes' >> .hg/hgrc +echo '% git=yes: git patch' +echo a > a +hg add a +hg qnew -d '0 0' -f git +cat .hg/patches/git +echo '% git=yes: git patch after qrefresh' +echo a >> a +hg qrefresh -d '0 0' +cat .hg/patches/git +cd .. + +hg init repo-no +cd repo-no +echo '[diff]' > .hg/hgrc +echo 'git = True' >> .hg/hgrc +echo '[mq]' > .hg/hgrc +echo 'git = False' >> .hg/hgrc +echo '% git=no: regular patch with copy' +echo a > a +hg add a +hg qnew -d '0 0' -f adda +hg cp a b +hg qnew -d '0 0' -f regular +cat .hg/patches/regular +echo '% git=no: regular patch after qrefresh with copy' +hg cp a c +hg qrefresh -d '0 0' +cat .hg/patches/regular +cd .. \ No newline at end of file diff --git a/tests/test-mq-git.out b/tests/test-mq-git.out new file mode 100644 --- /dev/null +++ b/tests/test-mq-git.out @@ -0,0 +1,100 @@ +% git=auto: regular patch creation +# HG changeset patch +# Date 0 0 + +diff -r 000000000000 -r ef8dafc9fa4c a +--- /dev/null ++++ b/a +@@ -0,0 +1,1 @@ ++a +% git=auto: git patch creation with copy +# HG changeset patch +# Date 0 0 + +diff --git a/a b/b +copy from a +copy to b +% git=auto: git patch when using --git +# HG changeset patch +# Date 0 0 + +diff --git a/regular b/regular +new file mode 100644 +--- /dev/null ++++ b/regular +@@ -0,0 +1,1 @@ ++regular +% git=auto: regular patch after qrefresh without --git +# HG changeset patch +# Date 0 0 + +diff -r 2962f232b49d regular +--- /dev/null ++++ b/regular +@@ -0,0 +1,1 @@ ++regular +% git=keep: git patch with --git +# HG changeset patch +# Date 0 0 + +diff --git a/a b/a +new file mode 100644 +--- /dev/null ++++ b/a +@@ -0,0 +1,1 @@ ++a +% git=keep: git patch after qrefresh without --git +# HG changeset patch +# Date 0 0 + +diff --git a/a b/a +new file mode 100644 +--- /dev/null ++++ b/a +@@ -0,0 +1,2 @@ ++a ++a +% git=yes: git patch +# HG changeset patch +# Date 0 0 + +diff --git a/a b/a +new file mode 100644 +--- /dev/null ++++ b/a +@@ -0,0 +1,1 @@ ++a +% git=yes: git patch after qrefresh +# HG changeset patch +# Date 0 0 + +diff --git a/a b/a +new file mode 100644 +--- /dev/null ++++ b/a +@@ -0,0 +1,2 @@ ++a ++a +% git=no: regular patch with copy +# HG changeset patch +# Date 0 0 + +diff -r ef8dafc9fa4c -r 110cde11d262 b +--- /dev/null ++++ b/b +@@ -0,0 +1,1 @@ ++a +% git=no: regular patch after qrefresh with copy +# HG changeset patch +# Date 0 0 + +diff -r ef8dafc9fa4c b +--- /dev/null ++++ b/b +@@ -0,0 +1,1 @@ ++a +diff -r ef8dafc9fa4c c +--- /dev/null ++++ b/c +@@ -0,0 +1,1 @@ ++a diff --git a/tests/test-mq-merge b/tests/test-mq-merge --- a/tests/test-mq-merge +++ b/tests/test-mq-merge @@ -16,6 +16,8 @@ checkundo() echo "[extensions]" >> $HGRCPATH echo "mq =" >> $HGRCPATH +echo "[mq]" >> $HGRCPATH +echo "git = keep" >> $HGRCPATH # Commit two dummy files in "init" changeset hg init t diff --git a/tests/test-mq-qfold b/tests/test-mq-qfold --- a/tests/test-mq-qfold +++ b/tests/test-mq-qfold @@ -2,6 +2,8 @@ echo "[extensions]" >> $HGRCPATH echo "mq=" >> $HGRCPATH +echo "[mq]" >> $HGRCPATH +echo "git=keep" >> $HGRCPATH filterdiff() { 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,18 @@ Common tasks (use "hg help command" for 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 +losing file mode changes, copy records, binary files or empty files creations +or deletions. This behaviour can be configured with: + + [mq] + git = auto/keep/yes/no + +If set to 'keep', mq will obey the [diff] section configuration while +preserving existing git patches upon qrefresh. If set to 'yes' or 'no', mq +will override the [diff] section and always generate git or regular patches, +possibly losing data in the second case. + list of commands: qapplied print the patches already applied