Add the --replace option to import
[stgit] / stgit / commands / imprt.py
index ad61b88..cfbf0de 100644 (file)
@@ -41,12 +41,18 @@ options = [make_option('-m', '--mail',
                        action = 'store_true'),
            make_option('-n', '--name',
                        help = 'use NAME as the patch name'),
+           make_option('-t', '--strip',
+                       help = 'strip numbering and extension from patch name',
+                       action = 'store_true'),
            make_option('-s', '--series',
                        help = 'import a series of patches',
                        action = 'store_true'),
            make_option('-i', '--ignore',
                        help = 'ignore the applied patches in the series',
                        action = 'store_true'),
+           make_option('--replace',
+                       help = 'replace the unapplied patches in the series',
+                       action = 'store_true'),
            make_option('-b', '--base',
                        help = 'use BASE instead of HEAD for file importing'),
            make_option('-e', '--edit',
@@ -73,6 +79,12 @@ def __end_descr(line):
     return re.match('---\s*$', line) or re.match('diff -', line) or \
             re.match('Index: ', line)
 
+def __strip_patch_name(name):
+    stripped = re.sub('^[0-9]+-(.*)$', '\g<1>', name)
+    stripped = re.sub('^(.*)\.(diff|patch)$', '\g<1>', stripped)
+
+    return stripped
+
 def __parse_description(descr):
     """Parse the patch description and return the new description and
     author information (if any).
@@ -239,6 +251,9 @@ def __import_patch(patch, filename, options):
     if options.commemail:
         committer_email = options.commemail
 
+    if options.replace and patch in crt_series.get_unapplied():
+        crt_series.delete_patch(patch)
+
     crt_series.new_patch(patch, message = message, can_edit = False,
                          author_name = author_name,
                          author_email = author_email,
@@ -275,11 +290,14 @@ def __import_series(filename, options):
         patch = re.sub('#.*$', '', line).strip()
         if not patch:
             continue
+        patchfile = os.path.join(patchdir, patch)
+
+        if options.strip:
+            patch = __strip_patch_name(patch)
         if options.ignore and patch in applied:
             print 'Ignoring already applied patch "%s"' % patch
             continue
 
-        patchfile = os.path.join(patchdir, patch)
         __import_patch(patch, patchfile, options)
 
 def func(parser, options, args):
@@ -306,6 +324,8 @@ def func(parser, options, args):
             patch = os.path.basename(filename)
         else:
             raise CmdException, 'Unknown patch name'
+        if options.strip:
+            patch = __strip_patch_name(patch)
 
         __import_patch(patch, filename, options)