From: "Karl Hasselström" <kha@treskal.com>
To: Catalin Marinas <catalin.marinas@gmail.com>
Cc: git@vger.kernel.org, "David Kågedal" <davidk@lysator.liu.se>
Subject: [StGit PATCH 4/5] Make generic --message/--file/--save-template flags
Date: Fri, 14 Dec 2007 07:25:21 +0100 [thread overview]
Message-ID: <20071214062521.29148.66456.stgit@yoghurt> (raw)
In-Reply-To: <20071214062251.29148.86191.stgit@yoghurt>
And let "stg edit" use them.
Signed-off-by: Karl Hasselström <kha@treskal.com>
---
stgit/commands/edit.py | 77 +++++++++++++++++++-----------------------------
stgit/utils.py | 45 ++++++++++++++++++++++++++++
2 files changed, 75 insertions(+), 47 deletions(-)
diff --git a/stgit/commands/edit.py b/stgit/commands/edit.py
index 65b54d9..b9699d5 100644
--- a/stgit/commands/edit.py
+++ b/stgit/commands/edit.py
@@ -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)
diff --git a/stgit/utils.py b/stgit/utils.py
index 3a480c0..837c6c2 100644
--- a/stgit/utils.py
+++ b/stgit/utils.py
@@ -256,3 +256,48 @@ def add_sign_line(desc, sign_str, name, email):
if not any(s in desc for s in ['\nSigned-off-by:', '\nAcked-by:']):
desc = desc + '\n'
return '%s\n%s\n' % (desc, sign_str)
+
+def make_message_options():
+ def no_dup(parser):
+ if parser.values.message != None:
+ raise optparse.OptionValueError(
+ 'Cannot give more than one --message or --file')
+ def no_combine(parser):
+ if (parser.values.message != None
+ and parser.values.save_template != None):
+ raise optparse.OptionValueError(
+ 'Cannot give both --message/--file and --save-template')
+ def msg_callback(option, opt_str, value, parser):
+ no_dup(parser)
+ parser.values.message = value
+ no_combine(parser)
+ def file_callback(option, opt_str, value, parser):
+ no_dup(parser)
+ if value == '-':
+ parser.values.message = sys.stdin.read()
+ else:
+ f = file(value)
+ parser.values.message = f.read()
+ f.close()
+ no_combine(parser)
+ def templ_callback(option, opt_str, value, parser):
+ if value == '-':
+ def w(s):
+ sys.stdout.write(s)
+ else:
+ def w(s):
+ f = file(value, 'w+')
+ f.write(s)
+ f.close()
+ parser.values.save_template = w
+ no_combine(parser)
+ m = optparse.make_option
+ return [m('-m', '--message', action = 'callback', callback = msg_callback,
+ dest = 'message', type = 'string',
+ help = 'use MESSAGE instead of invoking the editor'),
+ m('-f', '--file', action = 'callback', callback = file_callback,
+ dest = 'message', type = 'string', metavar = 'FILE',
+ help = 'use FILE instead of invoking the editor'),
+ m('--save-template', action = 'callback', callback = templ_callback,
+ metavar = 'FILE', dest = 'save_template', type = 'string',
+ help = 'save the message template to FILE and exit')]
next prev parent reply other threads:[~2007-12-14 6:25 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-12-14 6:24 [StGit PATCH 0/5] Various odds and ends Karl Hasselström
2007-12-14 6:25 ` [StGit PATCH 1/5] Added --save-template flag to edit command Karl Hasselström
2007-12-14 6:25 ` [StGit PATCH 2/5] Treat filename '-' as stdin/stdout in edit Karl Hasselström
2007-12-14 6:25 ` [StGit PATCH 3/5] Let parse_patch take a string instead of a file parameter Karl Hasselström
2007-12-14 6:25 ` Karl Hasselström [this message]
2007-12-14 6:44 ` [StGit PATCH 4/5] Make generic --message/--file/--save-template flags Karl Hasselström
2007-12-14 6:25 ` [StGit PATCH 5/5] Name the exit codes to improve legibility Karl Hasselström
2007-12-14 6:45 ` Karl Hasselström
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20071214062521.29148.66456.stgit@yoghurt \
--to=kha@treskal.com \
--cc=catalin.marinas@gmail.com \
--cc=davidk@lysator.liu.se \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.