[prev in list] [next in list] [prev in thread] [next in thread] 

List:       mercurial
Subject:    [PATCH 2 of 2] mq: add --strip option to qimport
From:       Brendan Cully <brendan () kublai ! com>
Date:       2008-11-23 2:11:27
Message-ID: e13768c5d09a65df59b9.1227406287 () zanzibar ! lan
[Download RAW message or body]

# HG changeset patch
# User Brendan Cully <brendan@kublai.com>
# Date 1227405831 28800
# Node ID e13768c5d09a65df59b967cfa16dc869c978749b
# Parent  bb9a410a054a704d17564075760cad7c8be3fed8
mq: add --strip option to qimport

diff --git a/hgext/mq.py b/hgext/mq.py
--- a/hgext/mq.py
+++ b/hgext/mq.py
@@ -57,12 +57,13 @@
         return self.rev + ':' + self.name
 
 class patchheader(object):
-    def __init__(self, message, comments, user, date, haspatch):
+    def __init__(self, message, comments, user, date, haspatch, strip=1):
         self.message = message
         self.comments = comments
         self.user = user
         self.date = date
         self.haspatch = haspatch
+        self.strip = strip
 
     def setuser(self, user):
         if not self.setheader(['From: ', '# User '], user):
@@ -83,6 +84,11 @@
         self.message = [message]
         self.comments += self.message
 
+    def setstrip(self, strip=1):
+        self.clearheader('MQStrip: ')
+        if strip != 1:
+            self.comments.insert(0, 'MQStrip: %d' % strip)
+
     def setheader(self, prefixes, new):
         '''Update all references to a field in the patch header.
         If none found, add it email style.'''
@@ -95,6 +101,12 @@
                     break
         return res
 
+    def clearheader(self, header):
+        for i in xrange(len(self.comments)):
+            if self.comments[i].startswith(header):
+                del self.comments[i]
+                return
+
     def __str__(self):
         if not self.comments:
             return ''
@@ -319,6 +331,7 @@
         format = None
         subject = None
         diffstart = 0
+        strip = 1
 
         for line in file(pf):
             line = line.rstrip()
@@ -351,6 +364,12 @@
                                            line.startswith("from: "))):
                 user = line[6:]
                 format = "tag"
+            elif format != 'tagdone' and line.startswith('MQStrip: '):
+                try:
+                    strip = int(line[9:])
+                except ValueError:
+                    self.ui.warn(_('bad strip header: %s') % line[9:])
+                format = 'tag'
             elif format == "tag" and line == "":
                 # when looking for tags (subject: from: etc) they
                 # end once you find a blank line in the source
@@ -368,7 +387,7 @@
         if format and format.startswith("tag") and subject:
             message.insert(0, "")
             message.insert(0, subject)
-        return patchheader(message, comments, user, date, diffstart > 1)
+        return patchheader(message, comments, user, date, diffstart > 1, strip)
 
     def removeundo(self, repo):
         undo = repo.sjoin('undo')
@@ -483,12 +502,12 @@
         self.save_dirty()
         return (0, head)
 
-    def patch(self, repo, patchfile):
+    def patch(self, repo, patchfile, strip=1):
         '''Apply patchfile  to the working directory.
         patchfile: file name of patch'''
         files = {}
         try:
-            fuzz = patch.patch(patchfile, self.ui, strip=1, cwd=repo.root,
+            fuzz = patch.patch(patchfile, self.ui, strip=strip, cwd=repo.root,
                                files=files)
         except Exception, inst:
             self.ui.note(str(inst) + '\n')
@@ -552,7 +571,7 @@
                     message.append(_("\nimported patch %s") % patchname)
                 message = '\n'.join(message)
 
-            (patcherr, files, fuzz) = self.patch(repo, pf)
+            (patcherr, files, fuzz) = self.patch(repo, pf, strip=ph.strip)
             all_files.update(files)
             patcherr = not patcherr
 
@@ -1078,6 +1097,8 @@
             cparents = repo.changelog.parents(top)
             patchparent = self.qparents(repo, top)
             ph = self.readheaders(patchfn)
+            # Refresh uses default strip value
+            ph.setstrip()
 
             patchf = self.opener(patchfn, 'r+')
 
@@ -1457,7 +1478,7 @@
         return p
 
     def qimport(self, repo, files, patchname=None, rev=None, existing=None,
-                force=None, git=False):
+                force=None, git=False, strip=1):
         def checkseries(patchname):
             if patchname in self.series:
                 raise util.Abort(_('patch %s is already in the series file')
@@ -1538,6 +1559,8 @@
             if existing:
                 if filename == '-':
                     raise util.Abort(_('-e is incompatible with import from -'))
+                if strip != 1:
+                    raise util.Abort(_('--strip is not supported with --existing'))
                 if not patchname:
                     patchname = normname(filename)
                 self.check_reserved_name(patchname)
@@ -1558,6 +1581,8 @@
                 self.check_reserved_name(patchname)
                 checkfile(patchname)
                 patchf = self.opener(patchname, "w")
+                if strip != 1:
+                    patchf.write('MQStrip: %d\n' % strip)
                 patchf.write(text)
             if not force:
                 checkseries(patchname)
@@ -1636,7 +1661,7 @@
     q = repo.mq
     q.qimport(repo, filename, patchname=opts['name'],
               existing=opts['existing'], force=opts['force'], rev=opts['rev'],
-              git=opts['git'])
+              git=opts['git'], strip=opts.get('strip'))
     q.save_dirty()
     return 0
 
@@ -2460,7 +2485,9 @@
           ('n', 'name', '', _('patch file name')),
           ('f', 'force', None, _('overwrite existing files')),
           ('r', 'rev', [], _('place existing revisions under mq control')),
-          ('g', 'git', None, _('use git extended diff format'))],
+          ('g', 'git', None, _('use git extended diff format')),
+          ('p', 'strip', 1,
+           _('directory strip option for patch'))],
          _('hg qimport [-e] [-n NAME] [-f] [-g] [-r REV]... FILE...')),
     "^qinit":
         (init,
diff --git a/tests/test-mq-qimport b/tests/test-mq-qimport
--- a/tests/test-mq-qimport
+++ b/tests/test-mq-qimport
@@ -59,6 +59,20 @@
 cat foo
 hg qpop
 
+echo % qimport -p 0
+cat > ../p0.diff <<EOF
+diff --git foo foo
+new file mode 100644
+--- /dev/null
++++ foo
+@@ -0,0 +1,1 @@
++foo2
+EOF
+hg qimport -p 0 ../p0.diff
+hg qpush
+cat foo
+hg qpop
+
 echo % build diff with CRLF
 python ../writelines.py b 5 'a\n' 5 'a\r\n'
 hg ci -Am addb
@@ -68,5 +82,3 @@
 echo % qimport CRLF diff
 hg qimport b.diff
 hg qpush
-
-
diff --git a/tests/test-mq-qimport.out b/tests/test-mq-qimport.out
--- a/tests/test-mq-qimport.out
+++ b/tests/test-mq-qimport.out
@@ -15,6 +15,12 @@
 Now at: url.diff
 foo2
 Patch queue now empty
+% qimport -p 0
+adding p0.diff to series file
+applying p0.diff
+Now at: p0.diff
+foo2
+Patch queue now empty
 % build diff with CRLF
 adding b
 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
_______________________________________________
Mercurial mailing list
Mercurial@selenic.com
http://selenic.com/mailman/listinfo/mercurial
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic