X-Git-Url: https://git.distorted.org.uk/~mdw/stgit/blobdiff_plain/2f7c8b0ba410854dc7dd0c2282323cc04dfcce8e..8560c67bce5458f876284afbee43be9ca862529a:/stgit/commands/export.py diff --git a/stgit/commands/export.py b/stgit/commands/export.py index 5f43da5..35a25ba 100644 --- a/stgit/commands/export.py +++ b/stgit/commands/export.py @@ -23,132 +23,122 @@ 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 (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 +Export a range of applied patches to a given directory (defaults to +'patches-') in a standard unified GNU diff format. A template +file (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: %(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 %(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 = 'prefix the patch names with order numbers', +options = [make_option('-d', '--dir', + help = 'export patches to DIR instead of the default'), + make_option('-p', '--patch', + help = 'append .patch to the patch names', action = 'store_true'), - make_option('-d', '--diff', - help = 'append .diff to the patch names', + make_option('-e', '--extension', + help = 'append .EXTENSION to the patch names'), + make_option('-n', '--numbered', + help = 'prefix the patch names with order numbers', action = 'store_true'), make_option('-t', '--template', metavar = 'FILE', help = 'Use FILE as a template'), - make_option('-r', '--range', - metavar = '[PATCH1][:[PATCH2]]', - help = 'export patches between PATCH1 and PATCH2'), make_option('-b', '--branch', - help = 'use BRANCH instead of the default one')] + help = 'use BRANCH instead of the default one'), + make_option('-s', '--stdout', + help = 'dump the patches to the standard output', + action = 'store_true')] def func(parser, options, args): - if len(args) == 0: - dirname = 'patches' - elif len(args) == 1: - dirname = args[0] + """Export a range of patches. + """ + if options.dir: + dirname = options.dir else: - parser.error('incorrect number of arguments') + dirname = 'patches-%s' % crt_series.get_branch() if not options.branch and git.local_changes(): print 'Warning: local changes in the tree. ' \ 'You might want to commit them first' - if not os.path.isdir(dirname): - os.makedirs(dirname) - series = file(os.path.join(dirname, 'series'), 'w+') + if not options.stdout: + if not os.path.isdir(dirname): + os.makedirs(dirname) + series = file(os.path.join(dirname, 'series'), 'w+') applied = crt_series.get_applied() - - if options.range: - boundaries = options.range.split(':') - if len(boundaries) == 1: - start = boundaries[0] - stop = boundaries[0] - elif len(boundaries) == 2: - if boundaries[0] == '': - start = applied[0] - else: - start = boundaries[0] - if boundaries[1] == '': - stop = applied[-1] - else: - stop = boundaries[1] - else: - raise CmdException, 'incorrect parameters to "--range"' - - if start in applied: - start_idx = applied.index(start) - else: - raise CmdException, 'Patch "%s" not applied' % start - if stop in applied: - stop_idx = applied.index(stop) + 1 - else: - raise CmdException, 'Patch "%s" not applied' % stop - - if start_idx >= stop_idx: - raise CmdException, 'Incorrect patch range order' + if len(args) != 0: + patches = parse_patches(args, applied) else: - start_idx = 0 - stop_idx = len(applied) - - patches = applied[start_idx:stop_idx] + patches = applied num = len(patches) + if num == 0: + raise CmdException, 'No patches applied' + zpadding = len(str(num)) if zpadding < 2: zpadding = 2 # get the template if options.template: - patch_tmpl_list = [options.template] + tmpl = file(options.template).read() else: - patch_tmpl_list = [] + tmpl = templates.get_template('patchexport.tmpl') + if not tmpl: + tmpl = '' - patch_tmpl_list += [os.path.join(git.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 + # note the base commit for this series + if not options.stdout: + 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 - if options.diff: - pname = '%s.diff' % pname + if options.patch: + pname = '%s.patch' % pname + elif options.extension: + pname = '%s.%s' % (pname, options.extension) if options.numbered: pname = '%s-%s' % (str(patch_no).zfill(zpadding), pname) pfile = os.path.join(dirname, pname) - print >> series, pname + if not options.stdout: + print >> series, pname # 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(), @@ -166,14 +156,26 @@ def func(parser, options, args): except TypeError: raise CmdException, 'Only "%(name)s" variables are ' \ 'supported in the patch template' - f = open(pfile, 'w+') - f.write(descr) + if options.stdout: + f = sys.stdout + else: + f = open(pfile, 'w+') + + if options.stdout and num > 1: + print '-------------------------------------------------------------------------------' + print patch.get_name() + print '-------------------------------------------------------------------------------' + + # write description + 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() + if not options.stdout: + f.close() patch_no += 1 - series.close() + if not options.stdout: + series.close()