[PATCH] commands: add a --plain option to 'hg export'

Steve Losh steve at stevelosh.com
Sat Feb 6 16:13:21 UTC 2010


# HG changeset patch
# User Steve Losh <steve at stevelosh.com>
# Date 1265472761 18000
# Node ID fb40449c2116c9f28f2ae0cca4c9b09bdfce5246
# Parent  f9108768b89285fc262dd1d637e6eda5399df24c
commands: add a --plain option to 'hg export'

This allows users to export patches without the "# HG changeset patch" header.
Instead a simpler header with the author and date is added. The result looks
like this:

From: Test User <test at example.com>
Date: 1265470036 18000
... full commit message ...

... all diffs ...

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1166,6 +1166,10 @@
     Use the -g/--git option to generate diffs in the git extended diff
     format. See 'hg help diffs' for more information.
 
+    Use the -p/--plain option to generate plain headers instead of Mercurial's
+    standard changeset header. The information shown in plain headers is
+    author and date.
+
     With the --switch-parent option, the diff will be against the
     second parent. It can be useful to review a merge.
     """
@@ -1177,9 +1181,10 @@
         ui.note(_('exporting patches:\n'))
     else:
         ui.note(_('exporting patch:\n'))
+
     patch.export(repo, revs, template=opts.get('output'),
                  switch_parent=opts.get('switch_parent'),
-                 opts=patch.diffopts(ui, opts))
+                 plain=opts.get('plain'), opts=patch.diffopts(ui, opts))
 
 def forget(ui, repo, *pats, **opts):
     """forget the specified files on the next commit
@@ -3465,6 +3470,7 @@
     "^export":
         (export,
          [('o', 'output', '', _('print output to file with formatted name')),
+          ('p', 'plain', None, _('print plain headers')),
           ('', 'switch-parent', None, _('diff against the second parent')),
           ('r', 'rev', [], _('revisions to export')),
           ] + diffopts,
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -1425,7 +1425,7 @@
                 yield text
 
 def export(repo, revs, template='hg-%h.patch', fp=None, switch_parent=False,
-           opts=None):
+           plain=None, opts=None):
     '''export changesets as hg patches.'''
 
     total = len(revs)
@@ -1447,15 +1447,19 @@
         if fp != sys.stdout and hasattr(fp, 'name'):
             repo.ui.note("%s\n" % fp.name)
 
-        fp.write("# HG changeset patch\n")
-        fp.write("# User %s\n" % ctx.user())
-        fp.write("# Date %d %d\n" % ctx.date())
-        if branch and (branch != 'default'):
-            fp.write("# Branch %s\n" % branch)
-        fp.write("# Node ID %s\n" % hex(node))
-        fp.write("# Parent  %s\n" % hex(prev))
-        if len(parents) > 1:
-            fp.write("# Parent  %s\n" % hex(parents[1]))
+        if plain:
+            fp.write("From: %s\n" % ctx.user())
+            fp.write("Date: %d %d\n" % ctx.date())
+        else:
+            fp.write("# HG changeset patch\n")
+            fp.write("# User %s\n" % ctx.user())
+            fp.write("# Date %d %d\n" % ctx.date())
+            if branch and (branch != 'default'):
+                fp.write("# Branch %s\n" % branch)
+            fp.write("# Node ID %s\n" % hex(node))
+            fp.write("# Parent  %s\n" % hex(prev))
+            if len(parents) > 1:
+                fp.write("# Parent  %s\n" % hex(parents[1]))
         fp.write(ctx.description().rstrip())
         fp.write("\n\n")
 
diff --git a/tests/test-export b/tests/test-export
--- a/tests/test-export
+++ b/tests/test-export
@@ -21,3 +21,10 @@
 hg export 1 2 3 4 | grep HG | wc -l | sed -e 's/^ *//'
 echo "# exporting revision -2 to a file"
 hg export -- -2
+
+echo "# exporting 4 changesets with plain headers to a file"
+hg export -p -o export_internal_a 1 2 3 4
+grep HG export_internal_a | wc -l | sed -e 's/^ *//'
+grep "# User" export_internal_a | wc -l | sed -e 's/^ *//'
+grep "From:" export_internal_a | wc -l | sed -e 's/^ *//'
+grep "Date:" export_internal_a | wc -l | sed -e 's/^ *//'
diff --git a/tests/test-export.out b/tests/test-export.out
--- a/tests/test-export.out
+++ b/tests/test-export.out
@@ -78,3 +78,8 @@
  foo-8
  foo-9
 +foo-10
+# exporting 4 changesets with plain headers to a file
+0
+0
+4
+4



More information about the Mercurial-devel mailing list