Add prune-safety to StGIT
[stgit] / stgit / commands / export.py
index 3e318a9..b8ca133 100644 (file)
@@ -37,6 +37,8 @@ patch format. The following variables are supported in the template
 file:
 
   %(description)s - patch description
+  %(shortdescr)s  - the first line of the patch description
+  %(longdescr)s   - the rest of the patch description, after the first line
   %(diffstat)s    - the diff statistics
   %(authname)s    - author's name
   %(authemail)s   - author's e-mail
@@ -78,6 +80,7 @@ def func(parser, options, args):
     series = file(os.path.join(dirname, 'series'), 'w+')
 
     applied = crt_series.get_applied()
+    unapplied = crt_series.get_unapplied()
 
     if options.range:
         boundaries = options.range.split(':')
@@ -99,11 +102,18 @@ def func(parser, options, args):
         if start in applied:
             start_idx = applied.index(start)
         else:
-            raise CmdException, 'Patch "%s" not applied' % start
+            if start in unapplied:
+                raise CmdException, 'Patch "%s" not applied' % start
+            else:
+                raise CmdException, 'Patch "%s" does not exist' % start
+
         if stop in applied:
             stop_idx = applied.index(stop) + 1
         else:
-            raise CmdException, 'Patch "%s" not applied' % stop
+            if stop in unapplied:
+                raise CmdException, 'Patch "%s" not applied' % stop
+            else:
+                raise CmdException, 'Patch "%s" does not exist' % stop
 
         if start_idx >= stop_idx:
             raise CmdException, 'Incorrect patch range order'
@@ -114,6 +124,9 @@ def func(parser, options, args):
     patches = applied[start_idx:stop_idx]
 
     num = len(patches)
+    if num == 0:
+        raise CmdException, 'No patches applied'
+
     zpadding = len(str(num))
     if zpadding < 2:
         zpadding = 2
@@ -124,7 +137,7 @@ def func(parser, options, args):
     else:
         patch_tmpl_list = []
 
-    patch_tmpl_list += [os.path.join(git.base_dir, 'patchexport.tmpl'),
+    patch_tmpl_list += [os.path.join(git.get_base_dir(), 'patchexport.tmpl'),
                         os.path.join(sys.prefix,
                                      'share/stgit/templates/patchexport.tmpl')]
     tmpl = ''
@@ -133,6 +146,10 @@ def func(parser, options, args):
             tmpl = file(patch_tmpl).read()
             break
 
+    # note the base commit for this series
+    base_commit = crt_series.get_patch(patches[0]).get_bottom()
+    print >> series, '# This series applies on GIT commit %s' % base_commit
+
     patch_no = 1;
     for p in patches:
         pname = p
@@ -146,9 +163,18 @@ def func(parser, options, args):
         # get the patch description
         patch = crt_series.get_patch(p)
 
+        descr = patch.get_description().strip()
+        descr_lines = descr.split('\n')
+
+        short_descr = descr_lines[0].rstrip()
+        long_descr = reduce(lambda x, y: x + '\n' + y,
+                            descr_lines[1:], '').strip()
+
         tmpl_dict = {'description': patch.get_description().rstrip(),
-                     'diffstat': git.diffstat(rev1 = git_id('%s/bottom' % p),
-                                              rev2 = git_id('%s/top' % p)),
+                     'shortdescr': short_descr,
+                     'longdescr': long_descr,
+                     'diffstat': git.diffstat(rev1 = patch.get_bottom(),
+                                              rev2 = patch.get_top()),
                      'authname': patch.get_authname(),
                      'authemail': patch.get_authemail(),
                      'authdate': patch.get_authdate(),
@@ -170,8 +196,8 @@ def func(parser, options, args):
         f.write(descr)
 
         # write the diff
-        git.diff(rev1 = git_id('%s/bottom' % p),
-                 rev2 = git_id('%s/top' % p),
+        git.diff(rev1 = patch.get_bottom(),
+                 rev2 = patch.get_top(),
                  out_fd = f)
         f.close()
         patch_no += 1