Re: [PATCH 07/17] revert: Introduce struct to keep command-line options
Ramkumar Ramachandra wrote:
> The variable "me" is left as a file-scope static variable because it
> is not an independent option. "me" is simply a string that needs to
> be inferred from the "action" option, and is kept global to save each
> function the trouble of determining it independently.
Why not do something like this[1]?
[1] http://thread.gmane.org/gmane.comp.version-control.git/176647/focus=176730
diff --git i/builtin/revert.c w/builtin/revert.c
index 63baca85..3161e696 100644
--- i/builtin/revert.c
+++ w/builtin/revert.c
@@ -36,7 +36,6 @@ static const char * const cherry_pick_usage[] = {
};
static struct commit *commit;
-static const char *me;
enum replay_action { REVERT, CHERRY_PICK };
struct replay_opts {
@@ -69,6 +68,11 @@ static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts)
return opts->action == REVERT ? revert_usage : cherry_pick_usage;
}
+static const char *action_name(const struct replay_opts *opts)
+{
+ return opts->action == REVERT ? "revert" : "cherry-pick";
+}
+
static int option_parse_x(const struct option *opt,
const char *arg, int unset)
{
@@ -280,20 +284,20 @@ static struct tree *empty_tree(void)
return tree;
}
-static NORETURN void die_dirty_index(const char *me, enum replay_action action)
+static NORETURN void die_dirty_index(const struct replay_opts *opts)
{
if (read_cache_unmerged()) {
- die_resolve_conflict(me);
+ die_resolve_conflict(action_name(opts));
} else {
if (advice_commit_before_merge) {
- if (action == REVERT)
+ if (opts->action == REVERT)
die(_("Your local changes would be overwritten by revert.\n"
"Please, commit your changes or stash them to proceed."));
else
die(_("Your local changes would be overwritten by cherry-pick.\n"
"Please, commit your changes or stash them to proceed."));
} else {
- if (action == REVERT)
+ if (opts->action == REVERT)
die(_("Your local changes would be overwritten by revert.\n"));
else
die(_("Your local changes would be overwritten by cherry-pick.\n"));
@@ -347,7 +351,8 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
(write_cache(index_fd, active_cache, active_nr) ||
commit_locked_index(&index_lock)))
/* TRANSLATORS: %s will be "revert" or "cherry-pick" */
- die(_("%s: Unable to write new index file"), me);
+ die(_("%s: Unable to write new index file"),
+ action_name(opts));
rollback_lock_file(&index_lock);
if (!clean) {
@@ -418,7 +423,7 @@ static int do_pick_commit(struct replay_opts *opts)
if (get_sha1("HEAD", head))
die (_("You do not have a valid HEAD"));
if (index_differs_from("HEAD", 0))
- die_dirty_index(me, opts->action);
+ die_dirty_index(opts);
}
discard_cache();
@@ -455,7 +460,7 @@ static int do_pick_commit(struct replay_opts *opts)
/* TRANSLATORS: The first %s will be "revert" or
"cherry-pick", the second %s a SHA1 */
die(_("%s: cannot parse parent commit %s"),
- me, sha1_to_hex(parent->object.sha1));
+ action_name(opts), sha1_to_hex(parent->object.sha1));
if (get_message(commit->buffer, &msg) != 0)
die(_("Cannot get commit message for %s"),
@@ -557,10 +562,15 @@ static void prepare_revs(struct rev_info *revs, struct replay_opts *opts)
die(_("empty commit set passed"));
}
-static void read_and_refresh_cache(const char *me, struct replay_opts *opts)
+static void read_and_refresh_cache(struct replay_opts *opts)
{
static struct lock_file index_lock;
- int index_fd = hold_locked_index(&index_lock, 0);
+ int index_fd;
+ const char *me;
+
+ me = action_name(opts);
+
+ index_fd = hold_locked_index(&index_lock, 0);
if (read_index_preload(&the_index, NULL) < 0)
die(_("git %s: failed to read the index"), me);
refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL);
@@ -578,8 +588,7 @@ static int revert_or_cherry_pick(int argc, const char **argv,
struct rev_info revs;
git_config(git_default_config, NULL);
- me = opts->action == REVERT ? "revert" : "cherry-pick";
- setenv(GIT_REFLOG_ACTION, me, 0);
+ setenv(GIT_REFLOG_ACTION, action_name(opts), 0);
parse_args(argc, argv, opts);
if (opts->allow_ff) {
@@ -593,7 +602,7 @@ static int revert_or_cherry_pick(int argc, const char **argv,
die(_("cherry-pick --ff cannot be used with --edit"));
}
- read_and_refresh_cache(me, opts);
+ read_and_refresh_cache(opts);
prepare_revs(&revs, opts);