X-Git-Url: https://git.distorted.org.uk/~mdw/stgit/blobdiff_plain/b054c8bd5293f4b905460321a5c0e620b2232296..bae29dddde41058567a5ef9b44c160dd76b1c025:/stgit/commands/export.py diff --git a/stgit/commands/export.py b/stgit/commands/export.py index 425d84a..b33c9ea 100644 --- a/stgit/commands/export.py +++ b/stgit/commands/export.py @@ -27,10 +27,27 @@ from stgit import stack, git help = 'exports a series of patches to (or patches)' -usage = """%prog [options] []""" +usage = """%prog [options] [] + +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 +patch format. The following variables are supported in the template +file: + + %(description)s - patch description + %(diffstat)s - the diff statistics + %(authname)s - author's name + %(authemail)s - author's e-mail + %(authdate)s - patch creation date + %(commname)s - committer's name + %(commemail)s - committer's e-mail + +'export' can also generate a diff for a range of patches.""" options = [make_option('-n', '--numbered', - help = 'number the patch names', + help = 'prefix the patch names with order numbers', action = 'store_true'), make_option('-d', '--diff', help = 'append .diff to the patch names', @@ -39,18 +56,20 @@ options = [make_option('-n', '--numbered', help = 'Use FILE as a template'), make_option('-r', '--range', metavar = '[PATCH1][:[PATCH2]]', - help = 'export patches between PATCH1 and PATCH2')] + help = 'export patches between PATCH1 and PATCH2'), + make_option('-b', '--branch', + help = 'use BRANCH instead of the default one')] def func(parser, options, args): if len(args) == 0: - dirname = 'patches' + dirname = 'patches-%s' % crt_series.get_branch() elif len(args) == 1: dirname = args[0] else: parser.error('incorrect number of arguments') - if git.local_changes(): + if not options.branch and git.local_changes(): print 'Warning: local changes in the tree. ' \ 'You might want to commit them first' @@ -59,13 +78,14 @@ 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(':') if len(boundaries) == 1: start = boundaries[0] stop = boundaries[0] - if len(boundaries) == 2: + elif len(boundaries) == 2: if boundaries[0] == '': start = applied[0] else: @@ -75,19 +95,26 @@ def func(parser, options, args): else: stop = boundaries[1] else: - raise MainException, 'incorrect parameters to "--range"' + raise CmdException, 'incorrect parameters to "--range"' if start in applied: start_idx = applied.index(start) else: - raise MainException, '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 MainException, '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 MainException, 'Incorrect patch range order' + raise CmdException, 'Incorrect patch range order' else: start_idx = 0 stop_idx = len(applied) @@ -99,6 +126,21 @@ def func(parser, options, args): if zpadding < 2: zpadding = 2 + # get the template + if options.template: + patch_tmpl_list = [options.template] + 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 + patch_no = 1; for p in patches: pname = p @@ -109,22 +151,12 @@ def func(parser, options, args): pfile = os.path.join(dirname, pname) print >> series, pname - # get the template - if options.template: - patch_tmpl = options.template - else: - patch_tmpl = os.path.join(git.base_dir, 'patchexport.tmpl') - if os.path.isfile(patch_tmpl): - tmpl = file(patch_tmpl).read() - else: - tmpl = '' - # get the patch description patch = crt_series.get_patch(p) tmpl_dict = {'description': patch.get_description().rstrip(), - 'diffstat': git.diffstat(rev1 = git_id('%s/bottom' % p), - rev2 = git_id('%s/top' % p)), + 'diffstat': git.diffstat(rev1 = patch.get_bottom(), + rev2 = patch.get_top()), 'authname': patch.get_authname(), 'authemail': patch.get_authemail(), 'authdate': patch.get_authdate(), @@ -137,19 +169,19 @@ def func(parser, options, args): try: descr = tmpl % tmpl_dict except KeyError, err: - raise MainException, 'Unknown patch template variable: %s' \ + raise CmdException, 'Unknown patch template variable: %s' \ % err except TypeError: - raise MainException, 'Only "%(name)s" variables are ' \ + raise CmdException, 'Only "%(name)s" variables are ' \ 'supported in the patch template' f = open(pfile, 'w+') f.write(descr) - f.close() # write the diff - git.diff(rev1 = git_id('%s/bottom' % p), - rev2 = git_id('%s/top' % p), - output = pfile, append = True) + git.diff(rev1 = patch.get_bottom(), + rev2 = patch.get_top(), + out_fd = f) + f.close() patch_no += 1 series.close()