Various odds and ends
cover Various odds and ends
Also available from
git://repo.or.cz/stgit/kha.git safe
---
David Kågedal (2):
Treat filename '-' as stdin/stdout in edit
Added --save-template flag to edit command
Karl Hasselström (3):
Name the exit codes to improve legibility
Make generic --message/--file/--save-template flags Let parse_patch take a string instead of a file parameter
stgit/commands/common.py | 6 ++-- stgit/commands/edit.py | 73 +++++++++++++++++++++++----------------------- stgit/commands/imprt.py | 2 + stgit/main.py | 25 ++++++++-------- stgit/utils.py | 50 ++++++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+), 53 deletions(-)
Signature · 2 lines
Karl Hasselström, [email protected] www.treskal.com/kalle
PATCH 1/5 Added --save-template flag to edit command
From: David Kågedal <[email protected]>
---
stgit/commands/edit.py | 21 +++++++++++++++------ 1 files changed, 15 insertions(+), 6 deletions(-)
stgit/commands/edit.py +15 -6
@@ -72,6 +72,8 @@ options = [make_option('-d', '--diff', help = 'annotate the patch log entry'), make_option('-m', '--message', help = 'replace the patch description with MESSAGE'), + make_option('--save-template', metavar = 'FILE', + help = 'save the patch to FILE in the format used by -f'), make_option('--author', metavar = '"NAME <EMAIL>"', help = 'replae the author details with "NAME <EMAIL>"'), make_option('--authname',
@@ -120,8 +122,8 @@ def __update_patch(pname, fname, options): else: out.done() -def __edit_update_patch(pname, options): - """Edit the given patch interactively. +def __generate_file(pname, fname, options): + """Generate a file containing the description to edit """ patch = crt_series.get_patch(pname)
@@ -168,15 +170,20 @@ def __edit_update_patch(pname, options): text = tmpl % tmpl_dict + # write the file to be edited + f = open(fname, 'w+') + f.write(text) + f.close() + +def __edit_update_patch(pname, options): + """Edit the given patch interactively. + """ if options.diff: fname = '.stgit-edit.diff' else: fname = '.stgit-edit.txt' - # write the file to be edited - f = open(fname, 'w+') - f.write(text) - f.close() + __generate_file(pname, fname, options) # invoke the editor call_editor(fname)
@@ -233,6 +240,8 @@ def func(parser, options, args): log = 'edit', notes = options.annotate) out.done() + elif options.save_template: + __generate_file(pname, options.save_template, options) elif options.file: __update_patch(pname, options.file, options) else:
PATCH 2/5 Treat filename '-' as stdin/stdout in edit
In reply to Karl HasselströmFrom: David Kågedal <[email protected]>
---
stgit/commands/edit.py | 14 ++++++++++---- 1 files changed, 10 insertions(+), 4 deletions(-)
stgit/commands/edit.py +10 -4
@@ -96,7 +96,10 @@ def __update_patch(pname, fname, options): bottom = patch.get_bottom() top = patch.get_top() - f = open(fname) + if fname == '-': + f = sys.stdin + else: + f = open(fname) message, author_name, author_email, author_date, diff = parse_patch(f) f.close()
@@ -171,9 +174,12 @@ def __generate_file(pname, fname, options): text = tmpl % tmpl_dict # write the file to be edited - f = open(fname, 'w+') - f.write(text) - f.close() + if fname == '-': + sys.stdout.write(text) + else: + f = open(fname, 'w+') + f.write(text) + f.close() def __edit_update_patch(pname, options): """Edit the given patch interactively.
PATCH 3/5 Let parse_patch take a string instead of a file parameter
In reply to Karl HasselströmThis makes it more generally useful, since all future callers may not have the input in a file.
---
stgit/commands/common.py | 6 +++--- stgit/commands/edit.py | 3 ++- stgit/commands/imprt.py | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-)
stgit/commands/common.py +3 -3
@@ -482,11 +482,11 @@ def parse_mail(msg): return (descr, authname, authemail, authdate, diff) -def parse_patch(fobj): - """Parse the input file and return (description, authname, +def parse_patch(text): + """Parse the input text and return (description, authname, authemail, authdate, diff) """ - descr, diff = __split_descr_diff(fobj.read()) + descr, diff = __split_descr_diff(text) descr, authname, authemail, authdate = __parse_description(descr) # we don't yet have an agreed place for the creation date.
stgit/commands/edit.py +2 -1
@@ -100,7 +100,8 @@ def __update_patch(pname, fname, options): f = sys.stdin else: f = open(fname) - message, author_name, author_email, author_date, diff = parse_patch(f) + (message, author_name, author_email, author_date, diff + ) = parse_patch(f.read()) f.close() out.start('Updating patch "%s"' % pname)
stgit/commands/imprt.py +1 -1
@@ -192,7 +192,7 @@ def __import_file(filename, options, patch = None): parse_mail(msg) else: message, author_name, author_email, author_date, diff = \ - parse_patch(f) + parse_patch(f.read()) if filename: f.close()
PATCH 4/5 Make generic --message/--file/--save-template flags
In reply to Karl HasselströmAnd let "stg edit" use them.
---
stgit/commands/edit.py | 77 +++++++++++++++++++----------------------------- stgit/utils.py | 45 ++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 47 deletions(-)
stgit/commands/edit.py +30 -47
@@ -61,8 +61,6 @@ directory = DirectoryGotoToplevel() options = [make_option('-d', '--diff', help = 'edit the patch diff', action = 'store_true'), - make_option('-f', '--file', - help = 'use FILE instead of invoking the editor'), make_option('-O', '--diff-opts', help = 'options to pass to git-diff'), make_option('--undo',
@@ -70,10 +68,6 @@ options = [make_option('-d', '--diff', action = 'store_true'), make_option('-a', '--annotate', metavar = 'NOTE', help = 'annotate the patch log entry'), - make_option('-m', '--message', - help = 'replace the patch description with MESSAGE'), - make_option('--save-template', metavar = 'FILE', - help = 'save the patch to FILE in the format used by -f'), make_option('--author', metavar = '"NAME <EMAIL>"', help = 'replae the author details with "NAME <EMAIL>"'), make_option('--authname',
@@ -86,47 +80,48 @@ options = [make_option('-d', '--diff', help = 'replace the committer name with COMMNAME'), make_option('--commemail', help = 'replace the committer e-mail with COMMEMAIL') - ] + make_sign_options() + ] + make_sign_options() + make_message_options() -def __update_patch(pname, fname, options): - """Update the current patch from the given file. +def __update_patch(pname, text, options): + """Update the current patch from the given text. """ patch = crt_series.get_patch(pname) bottom = patch.get_bottom() top = patch.get_top() - if fname == '-': - f = sys.stdin - else: - f = open(fname) - (message, author_name, author_email, author_date, diff - ) = parse_patch(f.read()) - f.close() + message, author_name, author_email, author_date, diff = parse_patch(text) out.start('Updating patch "%s"' % pname) if options.diff: git.switch(bottom) try: - git.apply_patch(fname) + git.apply_patch(diff = diff) except: # avoid inconsistent repository state git.switch(top) raise + def c(a, b): + if a != None: + return a + return b crt_series.refresh_patch(message = message, - author_name = author_name, - author_email = author_email, - author_date = author_date, - backup = True, log = 'edit') + author_name = c(options.authname, author_name), + author_email = c(options.authemail, author_email), + author_date = c(options.authdate, author_date), + committer_name = options.commname, + committer_email = options.commemail, + backup = True, sign_str = options.sign_str, + log = 'edit', notes = options.annotate) if crt_series.empty_patch(pname): out.done('empty patch') else: out.done() -def __generate_file(pname, fname, options): +def __generate_file(pname, write_fn, options): """Generate a file containing the description to edit """ patch = crt_series.get_patch(pname)
@@ -175,12 +170,7 @@ def __generate_file(pname, fname, options): text = tmpl % tmpl_dict # write the file to be edited - if fname == '-': - sys.stdout.write(text) - else: - f = open(fname, 'w+') - f.write(text) - f.close() + write_fn(text) def __edit_update_patch(pname, options): """Edit the given patch interactively.
@@ -189,13 +179,17 @@ def __edit_update_patch(pname, options): fname = '.stgit-edit.diff' else: fname = '.stgit-edit.txt' + def write_fn(text): + f = file(fname, 'w') + f.write(text) + f.close() - __generate_file(pname, fname, options) + __generate_file(pname, write_fn, options) # invoke the editor call_editor(fname) - __update_patch(pname, fname, options) + __update_patch(pname, file(fname).read(), options) def func(parser, options, args): """Edit the given patch or the current one.
@@ -232,25 +226,14 @@ def func(parser, options, args): out.start('Undoing the editing of "%s"' % pname) crt_series.undo_refresh() out.done() - elif options.message or options.authname or options.authemail \ - or options.authdate or options.commname or options.commemail \ - or options.sign_str: - # just refresh the patch with the given information - out.start('Updating patch "%s"' % pname) - crt_series.refresh_patch(message = options.message, - author_name = options.authname, - author_email = options.authemail, - author_date = options.authdate, - committer_name = options.commname, - committer_email = options.commemail, - backup = True, sign_str = options.sign_str, - log = 'edit', - notes = options.annotate) - out.done() elif options.save_template: __generate_file(pname, options.save_template, options) - elif options.file: - __update_patch(pname, options.file, options) + elif any([options.message, options.authname, options.authemail, + options.authdate, options.commname, options.commemail, + options.sign_str]): + out.start('Updating patch "%s"' % pname) + __update_patch(pname, options.message, options) + out.done() else: __edit_update_patch(pname, options)
stgit/utils.py +45 -0
48 lines not on this page · raw message
Re: [StGit PATCH 4/5] Make generic --message/--file/--save-template flags
Quoted 1 line · Karl Hasselström
And let "stg edit" use them.
Nothing else in the "safe" branch uses this currently. But one of the "experimental" patches I just sent out does.
Signature · 2 lines
Karl Hasselström, [email protected] www.treskal.com/kalle
PATCH 5/5 Name the exit codes to improve legibility
In reply to Karl Hasselström---
stgit/main.py | 25 +++++++++++++------------ stgit/utils.py | 5 +++++ 2 files changed, 18 insertions(+), 12 deletions(-)
stgit/main.py +13 -12
@@ -23,6 +23,7 @@ from optparse import OptionParser import stgit.commands from stgit.out import * +from stgit import utils # # The commands map
@@ -39,11 +40,11 @@ class Commands(dict): if not candidates: out.error('Unknown command: %s' % key, 'Try "%s help" for a list of supported commands' % prog) - sys.exit(1) + sys.exit(utils.STGIT_GENERAL_ERROR) elif len(candidates) > 1: out.error('Ambiguous command: %s' % key, 'Candidates are: %s' % ', '.join(candidates)) - sys.exit(1) + sys.exit(utils.STGIT_GENERAL_ERROR) return candidates[0]
@@ -206,7 +207,7 @@ def main(): print >> sys.stderr, 'usage: %s <command>' % prog print >> sys.stderr, \ ' Try "%s --help" for a list of supported commands' % prog - sys.exit(1) + sys.exit(utils.STGIT_GENERAL_ERROR) cmd = sys.argv[1]
@@ -216,13 +217,13 @@ def main(): sys.argv[2] = '--help' else: print_help() - sys.exit(0) + sys.exit(utils.STGIT_SUCCESS) if cmd == 'help': if len(sys.argv) == 3 and not sys.argv[2] in ['-h', '--help']: cmd = commands.canonical_cmd(sys.argv[2]) if not cmd in commands: out.error('%s help: "%s" command unknown' % (prog, cmd)) - sys.exit(1) + sys.exit(utils.STGIT_GENERAL_ERROR) sys.argv[0] += ' %s' % cmd command = commands[cmd]
@@ -232,16 +233,16 @@ def main(): pager(parser.format_help()) else: print_help() - sys.exit(0) + sys.exit(utils.STGIT_SUCCESS) if cmd in ['-v', '--version', 'version']: from stgit.version import version print 'Stacked GIT %s' % version os.system('git --version') print 'Python version %s' % sys.version - sys.exit(0) + sys.exit(utils.STGIT_SUCCESS) if cmd in ['copyright']: print __copyright__ - sys.exit(0) + sys.exit(utils.STGIT_SUCCESS) # re-build the command line arguments cmd = commands.canonical_cmd(cmd)
@@ -265,7 +266,7 @@ def main(): debug_level = int(os.environ.get('STGIT_DEBUG_LEVEL', 0)) except ValueError: out.error('Invalid STGIT_DEBUG_LEVEL environment variable') - sys.exit(1) + sys.exit(utils.STGIT_GENERAL_ERROR) try: directory.setup()
@@ -284,8 +285,8 @@ def main(): if debug_level > 0: raise else: - sys.exit(2) + sys.exit(utils.STGIT_COMMAND_ERROR) except KeyboardInterrupt: - sys.exit(1) + sys.exit(utils.STGIT_GENERAL_ERROR) - sys.exit(0) + sys.exit(utils.STGIT_SUCCESS)
stgit/utils.py +5 -0
@@ -301,3 +301,8 @@ def make_message_options(): m('--save-template', action = 'callback', callback = templ_callback, metavar = 'FILE', dest = 'save_template', type = 'string', help = 'save the message template to FILE and exit')] + +# Exit codes. +STGIT_SUCCESS = 0 # everything's OK +STGIT_GENERAL_ERROR = 1 # seems to be non-command-specific error +STGIT_COMMAND_ERROR = 2 # seems to be a command that failed
Re: [StGit PATCH 5/5] Name the exit codes to improve legibility
stgit/utils.py def make_message_options(): · line 308
Quoted 4 lines · Karl Hasselström
stgit/utils.py +4
-0+# Exit codes. +STGIT_SUCCESS = 0 # everything's OK +STGIT_GENERAL_ERROR = 1 # seems to be non-command-specific error +STGIT_COMMAND_ERROR = 2 # seems to be a command that failed
If anyone knows for sure, feel free to fix up these descriptions.
Signature · 2 lines
Karl Hasselström, [email protected] www.treskal.com/kalle