From: "Karl Hasselström" <kha@treskal.com>
To: Jon Smirl <jonsmirl@gmail.com>
Cc: git@vger.kernel.org, Catalin Marinas <catalin.marinas@gmail.com>,
Yann Dirson <ydirson@altern.org>
Subject: [StGit PATCH 2/4] Refactor --diff-opts handling
Date: Thu, 24 Jan 2008 09:07:33 +0100 [thread overview]
Message-ID: <20080124080726.25525.485.stgit@yoghurt> (raw)
In-Reply-To: <20080124075935.25525.24416.stgit@yoghurt>
Lots of commands take a -O/--diff-opts flag, and they all handle it
identically. So break that out into a library function.
Signed-off-by: Karl Hasselström <kha@treskal.com>
---
stgit/commands/diff.py | 14 ++++----------
stgit/commands/edit.py | 14 +++-----------
stgit/commands/export.py | 12 +++---------
stgit/commands/files.py | 13 ++++---------
stgit/commands/mail.py | 12 +++---------
stgit/commands/status.py | 12 +++---------
stgit/utils.py | 12 ++++++++++++
7 files changed, 32 insertions(+), 57 deletions(-)
diff --git a/stgit/commands/diff.py b/stgit/commands/diff.py
index 1425518..7c213d1 100644
--- a/stgit/commands/diff.py
+++ b/stgit/commands/diff.py
@@ -46,12 +46,10 @@ directory = DirectoryHasRepository()
options = [make_option('-r', '--range',
metavar = 'rev1[..[rev2]]', dest = 'revs',
help = 'show the diff between revisions'),
- make_option('-O', '--diff-opts',
- help = 'options to pass to git-diff'),
make_option('-s', '--stat',
help = 'show the stat instead of the diff',
- action = 'store_true')]
-
+ action = 'store_true')
+ ] + make_diff_opts_option()
def func(parser, options, args):
"""Show the tree diff
@@ -83,16 +81,12 @@ def func(parser, options, args):
rev1 = 'HEAD'
rev2 = None
- if options.diff_opts:
- diff_flags = options.diff_opts.split()
- else:
- diff_flags = []
-
if options.stat:
out.stdout_raw(git.diffstat(args, git_id(crt_series, rev1),
git_id(crt_series, rev2)) + '\n')
else:
diff_str = git.diff(args, git_id(crt_series, rev1),
- git_id(crt_series, rev2), diff_flags = diff_flags )
+ git_id(crt_series, rev2),
+ diff_flags = options.diff_flags)
if diff_str:
pager(diff_str)
diff --git a/stgit/commands/edit.py b/stgit/commands/edit.py
index 2e8ae37..da67275 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('-O', '--diff-opts',
- help = 'options to pass to git-diff'),
make_option('--undo',
help = 'revert the commit generated by the last edit',
action = 'store_true'),
@@ -80,7 +78,8 @@ 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_message_options()
+ ] + (make_sign_options() + make_message_options()
+ + make_diff_opts_option())
def __update_patch(pname, text, options):
"""Update the current patch from the given text.
@@ -130,13 +129,6 @@ def __generate_file(pname, write_fn, options):
"""
patch = crt_series.get_patch(pname)
- if options.diff_opts:
- if not options.diff:
- raise CmdException, '--diff-opts only available with --diff'
- diff_flags = options.diff_opts.split()
- else:
- diff_flags = []
-
# generate the file to be edited
descr = patch.get_description().strip()
authdate = patch.get_authdate()
@@ -164,7 +156,7 @@ def __generate_file(pname, write_fn, options):
tmpl_dict['diffstat'] = git.diffstat(rev1 = bottom, rev2 = top)
tmpl_dict['diff'] = git.diff(rev1 = bottom, rev2 = top,
- diff_flags = diff_flags)
+ diff_flags = options.diff_flags)
for key in tmpl_dict:
# make empty strings if key is not available
diff --git a/stgit/commands/export.py b/stgit/commands/export.py
index 9131729..16c64ba 100644
--- a/stgit/commands/export.py
+++ b/stgit/commands/export.py
@@ -64,11 +64,10 @@ options = [make_option('-d', '--dir',
help = 'Use FILE as a template'),
make_option('-b', '--branch',
help = 'use BRANCH instead of the default one'),
- make_option('-O', '--diff-opts',
- help = 'options to pass to git-diff'),
make_option('-s', '--stdout',
help = 'dump the patches to the standard output',
- action = 'store_true')]
+ action = 'store_true')
+ ] + make_diff_opts_option()
def func(parser, options, args):
@@ -89,11 +88,6 @@ def func(parser, options, args):
os.makedirs(dirname)
series = file(os.path.join(dirname, 'series'), 'w+')
- if options.diff_opts:
- diff_flags = options.diff_opts.split()
- else:
- diff_flags = []
-
applied = crt_series.get_applied()
if len(args) != 0:
patches = parse_patches(args, applied)
@@ -180,7 +174,7 @@ def func(parser, options, args):
f.write(descr)
f.write(git.diff(rev1 = patch.get_bottom(),
rev2 = patch.get_top(),
- diff_flags = diff_flags))
+ diff_flags = options.diff_flags))
if not options.stdout:
f.close()
patch_no += 1
diff --git a/stgit/commands/files.py b/stgit/commands/files.py
index 4550251..ab1f6a3 100644
--- a/stgit/commands/files.py
+++ b/stgit/commands/files.py
@@ -40,11 +40,10 @@ options = [make_option('-s', '--stat',
action = 'store_true'),
make_option('-b', '--branch',
help = 'use BRANCH instead of the default one'),
- make_option('-O', '--diff-opts',
- help = 'options to pass to git-diff'),
make_option('--bare',
help = 'bare file names (useful for scripting)',
- action = 'store_true')]
+ action = 'store_true')
+ ] + make_diff_opts_option()
def func(parser, options, args):
@@ -65,9 +64,5 @@ def func(parser, options, args):
elif options.bare:
out.stdout_raw(git.barefiles(rev1, rev2) + '\n')
else:
- if options.diff_opts:
- diff_flags = options.diff_opts.split()
- else:
- diff_flags = []
-
- out.stdout_raw(git.files(rev1, rev2, diff_flags = diff_flags) + '\n')
+ out.stdout_raw(git.files(rev1, rev2, diff_flags = options.diff_flags)
+ + '\n')
diff --git a/stgit/commands/mail.py b/stgit/commands/mail.py
index eea84f2..4aa16fb 100644
--- a/stgit/commands/mail.py
+++ b/stgit/commands/mail.py
@@ -144,11 +144,10 @@ options = [make_option('-a', '--all',
action = 'store_true'),
make_option('-b', '--branch',
help = 'use BRANCH instead of the default one'),
- make_option('-O', '--diff-opts',
- help = 'options to pass to git-diff'),
make_option('-m', '--mbox',
help = 'generate an mbox file instead of sending',
- action = 'store_true')]
+ action = 'store_true')
+ ] + make_diff_opts_option()
def __get_sender():
@@ -426,11 +425,6 @@ def __build_message(tmpl, patch, patch_nr, total_nr, msg_id, ref_id, options):
prefix_str = confprefix + ' '
else:
prefix_str = ''
-
- if options.diff_opts:
- diff_flags = options.diff_opts.split()
- else:
- diff_flags = []
total_nr_str = str(total_nr)
patch_nr_str = str(patch_nr).zfill(len(total_nr_str))
@@ -450,7 +444,7 @@ def __build_message(tmpl, patch, patch_nr, total_nr, msg_id, ref_id, options):
'diff': git.diff(
rev1 = git_id(crt_series, '%s//bottom' % patch),
rev2 = git_id(crt_series, '%s//top' % patch),
- diff_flags = diff_flags),
+ diff_flags = options.diff_flags),
'diffstat': git.diffstat(
rev1 = git_id(crt_series, '%s//bottom'%patch),
rev2 = git_id(crt_series, '%s//top' % patch)),
diff --git a/stgit/commands/status.py b/stgit/commands/status.py
index 02a5832..6da4516 100644
--- a/stgit/commands/status.py
+++ b/stgit/commands/status.py
@@ -59,11 +59,10 @@ options = [make_option('-m', '--modified',
make_option('-x', '--noexclude',
help = 'do not exclude any files from listing',
action = 'store_true'),
- make_option('-O', '--diff-opts',
- help = 'options to pass to git-diff'),
make_option('--reset',
help = 'reset the current tree changes',
- action = 'store_true')]
+ action = 'store_true')
+ ] + make_diff_opts_option()
def status(files, modified, new, deleted, conflict, unknown, noexclude,
@@ -115,11 +114,6 @@ def func(parser, options, args):
resolved_all()
git.reset()
else:
- if options.diff_opts:
- diff_flags = options.diff_opts.split()
- else:
- diff_flags = []
-
status(args, options.modified, options.new, options.deleted,
options.conflict, options.unknown, options.noexclude,
- diff_flags = diff_flags)
+ options.diff_flags)
diff --git a/stgit/utils.py b/stgit/utils.py
index 2ff1d74..00776b0 100644
--- a/stgit/utils.py
+++ b/stgit/utils.py
@@ -313,6 +313,18 @@ def make_message_options():
metavar = 'FILE', dest = 'save_template', type = 'string',
help = 'save the message template to FILE and exit')]
+def make_diff_opts_option():
+ def diff_opts_callback(option, opt_str, value, parser):
+ if value:
+ parser.values.diff_flags.extend(value.split())
+ else:
+ parser.values.diff_flags = []
+ return [optparse.make_option(
+ '-O', '--diff-opts', dest = 'diff_flags', default = [],
+ action = 'callback', callback = diff_opts_callback,
+ type = 'string', metavar = 'OPTIONS',
+ help = 'extra options to pass to "git diff"')]
+
# Exit codes.
STGIT_SUCCESS = 0 # everything's OK
STGIT_GENERAL_ERROR = 1 # seems to be non-command-specific error
next prev parent reply other threads:[~2008-01-24 8:08 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-01-23 3:04 stgit: config option for diff-opts Jon Smirl
2008-01-23 3:18 ` Karl Hasselström
2008-01-24 7:48 ` Yann Dirson
2008-01-24 8:06 ` [StGit PATCH 0/4] --diff-opts in config file Karl Hasselström
2008-01-24 8:07 ` [StGit PATCH 1/4] Remove unused default values Karl Hasselström
2008-01-24 8:07 ` Karl Hasselström [this message]
2008-01-24 8:08 ` [StGit PATCH 3/4] Let "stg show" use the unified --diff-opts handling Karl Hasselström
2008-01-24 8:08 ` [StGit PATCH 4/4] Read default diff options from the user's config Karl Hasselström
2008-01-24 8:18 ` [StGit PATCH 0/4] --diff-opts in config file Karl Hasselström
2008-01-24 20:20 ` Jon Smirl
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=20080124080726.25525.485.stgit@yoghurt \
--to=kha@treskal.com \
--cc=catalin.marinas@gmail.com \
--cc=git@vger.kernel.org \
--cc=jonsmirl@gmail.com \
--cc=ydirson@altern.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).