[PATCH 1 of 1 RFC] import: allow to strip content after a --- mark (issue2148)

Nicolas Dumazet nicdumz at gmail.com
Thu Apr 22 04:26:28 CDT 2010


# HG changeset patch
# User Nicolas Dumazet <nicdumz.commits at gmail.com>
# Date 1271928118 -32400
# Node ID 74a4aac7fbee70ddc8a77d8d35291389ce9d50d4
# Parent  8c541f98428016c19a32fcee4eefed6d1b96218b
import: allow to strip content after a --- mark (issue2148)

Git users sometimes produce patches formatted like:
"""
commit message
---
some comments or stats that should not be included
diff --git ....
  [diff content]
"""

Add a --gitcompat option to import those patches correctly.

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1872,6 +1872,12 @@
     With -s/--similarity, hg will attempt to discover renames and
     copies in the patch in the same way as 'addremove'.
 
+    A line containing only three hyphens (---) is by default a legal
+    commit message line for Mercurial. With --gitcompat however,
+    import will treat such line as a delimiter marking the end of
+    the commit message: text between such mark and the first diff
+    will be ignored.
+
     To read a patch from standard input, use "-" as the patch name. If
     a URL is specified, the patch will be downloaded from it.
     See 'hg help dates' for a list of formats valid for -d/--date.
@@ -1896,6 +1902,11 @@
     strip = opts["strip"]
     wlock = lock = None
 
+    if opts["gitcompat"]:
+        commentre = re.compile('^---$', re.MULTILINE)
+    else:
+        commentre = None
+
     def tryone(ui, hunk):
         tmpname, message, user, date, branch, nodeid, p1, p2 = \
             patch.extract(ui, hunk)
@@ -1911,7 +1922,12 @@
                 message = cmdline_message
             elif message:
                 # pickup the patch msg
+                if commentre:
+                    m = commentre.search(message)
+                    if m:
+                        message = message[:m.start()]
                 message = message.strip()
+
             else:
                 # launch the editor
                 message = None
@@ -3699,8 +3715,10 @@
           ('', 'exact', None,
            _('apply patch to the nodes from which it was generated')),
           ('', 'import-branch', None,
-           _('use any branch information in patch (implied by --exact)'))] +
-         commitopts + commitopts2 + similarityopts,
+           _('use any branch information in patch (implied by --exact)')),
+          ('', 'gitcompat', False,
+            _("do not include content after '---' in commit message")),
+         ] + commitopts + commitopts2 + similarityopts,
          _('[OPTION]... PATCH...')),
     "incoming|in":
         (incoming,
diff --git a/tests/test-import b/tests/test-import
--- a/tests/test-import
+++ b/tests/test-import
@@ -144,6 +144,25 @@
 hg --cwd b tip --template '{desc}\n'
 rm -r b
 
+# removal of [PATCH x/y]
+# Check that with --gitcompat, --- is understood as an
+# end of commit log marker.
+cat > mkmsg3.py <<EOF
+import email.Message, sys
+msg = email.Message.Message()
+msg.set_payload('email patch\n\nnext line\n---\nafter\n' + open('tip.patch').read())
+msg['Subject'] = '[PATCH 1/4] email patch'
+msg['From'] = 'email patcher'
+sys.stdout.write(msg.as_string())
+EOF
+
+echo '% plain diff in email, [PATCH x/y] subject; gitcompat'
+hg clone -r0 a b
+hg --cwd a diff -r0:1 > tip.patch
+python mkmsg3.py | hg --cwd b import --gitcompat -
+hg --cwd b tip --template '{desc}\n'
+rm -r b
+
 # We weren't backing up the correct dirstate file when importing many patches
 # (issue963)
 echo '% import patch1 patch2; rollback'
diff --git a/tests/test-import.out b/tests/test-import.out
--- a/tests/test-import.out
+++ b/tests/test-import.out
@@ -177,6 +177,18 @@
 
 next line
 ---
+% plain diff in email, [PATCH x/y] subject; gitcompat
+requesting all changes
+adding changesets
+adding manifests
+adding file changes
+added 1 changesets with 2 changes to 2 files
+updating to branch default
+2 files updated, 0 files merged, 0 files removed, 0 files unresolved
+applying patch from stdin
+email patch
+
+next line
 % import patch1 patch2; rollback
 parent: 0
 applying ../patch1


More information about the Mercurial-devel mailing list