Add a common function for reading template files
[stgit] / stgit / commands / export.py
index 4ea0236..4136b7f 100644 (file)
@@ -23,7 +23,7 @@ from optparse import OptionParser, make_option
 
 from stgit.commands.common import *
 from stgit.utils import *
-from stgit import stack, git
+from stgit import stack, git, templates
 
 
 help = 'exports a series of patches to <dir> (or patches)'
@@ -31,8 +31,9 @@ usage = """%prog [options] [<dir>]
 
 Export the applied patches into a given directory (defaults to
 'patches') in a standard unified GNU diff format. A template file
-(defaulting to '.git/patchexport.tmpl or
-/usr/share/stgit/templates/patchexport.tmpl') can be used for the
+(defaulting to '.git/patchexport.tmpl' or
+'~/.stgit/templates/patchexport.tmpl' or
+'/usr/share/stgit/templates/patchexport.tmpl') can be used for the
 patch format. The following variables are supported in the template
 file:
 
@@ -54,6 +55,9 @@ options = [make_option('-n', '--numbered',
            make_option('-d', '--diff',
                        help = 'append .diff to the patch names',
                        action = 'store_true'),
+           make_option('-p', '--patch',
+                       help = 'append .patch to the patch names',
+                       action = 'store_true'),
            make_option('-t', '--template', metavar = 'FILE',
                        help = 'Use FILE as a template'),
            make_option('-r', '--range',
@@ -137,18 +141,11 @@ def func(parser, options, args):
 
     # get the template
     if options.template:
-        patch_tmpl_list = [options.template]
+        tmpl = file(options.template).read()
     else:
-        patch_tmpl_list = []
-
-    patch_tmpl_list += [os.path.join(git.get_base_dir(), 'patchexport.tmpl'),
-                        os.path.join(sys.prefix,
-                                     'share/stgit/templates/patchexport.tmpl')]
-    tmpl = ''
-    for patch_tmpl in patch_tmpl_list:
-        if os.path.isfile(patch_tmpl):
-            tmpl = file(patch_tmpl).read()
-            break
+        tmpl = templates.get_template('patchexport.tmpl')
+        if not tmpl:
+            tmpl = ''
 
     # note the base commit for this series
     if not options.stdout:
@@ -160,6 +157,8 @@ def func(parser, options, args):
         pname = p
         if options.diff:
             pname = '%s.diff' % pname
+        elif options.patch:
+            pname = '%s.patch' % pname
         if options.numbered:
             pname = '%s-%s' % (str(patch_no).zfill(zpadding), pname)
         pfile = os.path.join(dirname, pname)