git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Ramkumar Ramachandra <artagnon@gmail.com>
To: Git List <git@vger.kernel.org>
Cc: Christian Couder <chriscool@tuxfamily.org>,
	Daniel Barkalow <barkalow@iabervon.org>,
	Jonathan Nieder <jrnieder@gmail.com>
Subject: [PATCH 03/11] revert: Introduce a struct to parse command-line options into
Date: Sun, 10 Apr 2011 20:41:49 +0530	[thread overview]
Message-ID: <1302448317-32387-4-git-send-email-artagnon@gmail.com> (raw)
In-Reply-To: <1302448317-32387-1-git-send-email-artagnon@gmail.com>

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 builtin/revert.c |  132 +++++++++++++++++++++++++++++------------------------
 1 files changed, 72 insertions(+), 60 deletions(-)

diff --git a/builtin/revert.c b/builtin/revert.c
index fdbacad..2b33220 100644
--- a/builtin/revert.c
+++ b/builtin/revert.c
@@ -35,17 +35,27 @@ static const char * const cherry_pick_usage[] = {
 	NULL
 };
 
-static int edit, no_replay, no_commit, mainline, signoff, allow_ff;
-static enum { REVERT, CHERRY_PICK } action;
-static int commit_argc;
-static const char **commit_argv;
-static int allow_rerere_auto;
 
+static struct {
+	enum { REVERT, CHERRY_PICK } action;
 
-/* Merge strategy. */
-static const char *strategy;
-static const char **xopts;
-static size_t xopts_nr, xopts_alloc;
+	/* Boolean options */
+	int edit;
+	int no_replay;
+	int no_commit;
+	int signoff;
+	int allow_ff;
+	int allow_rerere_auto;
+
+	int mainline;
+	int commit_argc;
+	const char **commit_argv;
+
+	/* Merge strategy */
+	const char *strategy;
+	const char **xopts;
+	size_t xopts_nr, xopts_alloc;
+} cmd_opts;
 
 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
 
@@ -53,7 +63,7 @@ static char *get_encoding(struct commit *commit, const char *message);
 
 static const char * const *revert_or_cherry_pick_usage(void)
 {
-	return action == REVERT ? revert_usage : cherry_pick_usage;
+	return cmd_opts.action == REVERT ? revert_usage : cherry_pick_usage;
 }
 
 static int option_parse_x(const struct option *opt,
@@ -62,8 +72,8 @@ static int option_parse_x(const struct option *opt,
 	if (unset)
 		return 0;
 
-	ALLOC_GROW(xopts, xopts_nr + 1, xopts_alloc);
-	xopts[xopts_nr++] = xstrdup(arg);
+	ALLOC_GROW(cmd_opts.xopts, cmd_opts.xopts_nr + 1, cmd_opts.xopts_alloc);
+	cmd_opts.xopts[cmd_opts.xopts_nr++] = xstrdup(arg);
 	return 0;
 }
 
@@ -72,37 +82,37 @@ static void parse_args(int argc, const char **argv)
 	const char * const * usage_str = revert_or_cherry_pick_usage();
 	int noop;
 	struct option options[] = {
-		OPT_BOOLEAN('n', "no-commit", &no_commit, "don't automatically commit"),
-		OPT_BOOLEAN('e', "edit", &edit, "edit the commit message"),
+		OPT_BOOLEAN('n', "no-commit", &cmd_opts.no_commit, "don't automatically commit"),
+		OPT_BOOLEAN('e', "edit", &cmd_opts.edit, "edit the commit message"),
 		OPT_BOOLEAN('r', NULL, &noop, "no-op (backward compatibility)"),
-		OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"),
-		OPT_INTEGER('m', "mainline", &mainline, "parent number"),
-		OPT_RERERE_AUTOUPDATE(&allow_rerere_auto),
-		OPT_STRING(0, "strategy", &strategy, "strategy", "merge strategy"),
-		OPT_CALLBACK('X', "strategy-option", &xopts, "option",
+		OPT_BOOLEAN('s', "signoff", &cmd_opts.signoff, "add Signed-off-by:"),
+		OPT_INTEGER('m', "mainline", &cmd_opts.mainline, "parent number"),
+		OPT_RERERE_AUTOUPDATE(&cmd_opts.allow_rerere_auto),
+		OPT_STRING(0, "strategy", &cmd_opts.strategy, "strategy", "merge strategy"),
+		OPT_CALLBACK('X', "strategy-option", &cmd_opts.xopts, "option",
 			"option for merge strategy", option_parse_x),
 		OPT_END(),
 		OPT_END(),
 		OPT_END(),
 	};
 
-	if (action == CHERRY_PICK) {
+	if (cmd_opts.action == CHERRY_PICK) {
 		struct option cp_extra[] = {
-			OPT_BOOLEAN('x', NULL, &no_replay, "append commit name"),
-			OPT_BOOLEAN(0, "ff", &allow_ff, "allow fast-forward"),
+			OPT_BOOLEAN('x', NULL, &cmd_opts.no_replay, "append commit name"),
+			OPT_BOOLEAN(0, "ff", &cmd_opts.allow_ff, "allow fast-forward"),
 			OPT_END(),
 		};
 		if (parse_options_concat(options, ARRAY_SIZE(options), cp_extra))
 			die(_("program error"));
 	}
 
-	commit_argc = parse_options(argc, argv, NULL, options, usage_str,
+	cmd_opts.commit_argc = parse_options(argc, argv, NULL, options, usage_str,
 				    PARSE_OPT_KEEP_ARGV0 |
 				    PARSE_OPT_KEEP_UNKNOWN);
-	if (commit_argc < 2)
+	if (cmd_opts.commit_argc < 2)
 		usage_with_options(usage_str, options);
 
-	commit_argv = argv;
+	cmd_opts.commit_argv = argv;
 }
 
 struct commit_message {
@@ -268,17 +278,17 @@ static struct tree *empty_tree(void)
 static int error_dirty_index()
 {
 	if (read_cache_unmerged()) {
-		die_resolve_conflict(action == REVERT ? "revert" : "cherry-pick");
+		die_resolve_conflict(cmd_opts.action == REVERT ? "revert" : "cherry-pick");
 	} else {
 		if (advice_commit_before_merge) {
-			if (action == REVERT)
+			if (cmd_opts.action == REVERT)
 				return error(_("Your local changes would be overwritten by revert.\n"
 					  "Please, commit your changes or stash them to proceed."));
 			else
 				return error(_("Your local changes would be overwritten by cherry-pick.\n"
 					  "Please, commit your changes or stash them to proceed."));
 		} else {
-			if (action == REVERT)
+			if (cmd_opts.action == REVERT)
 				return error(_("Your local changes would be overwritten by revert.\n"));
 			else
 				return error(_("Your local changes would be overwritten by cherry-pick.\n"));
@@ -320,7 +330,7 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
 	next_tree = next ? next->tree : empty_tree();
 	base_tree = base ? base->tree : empty_tree();
 
-	for (xopt = xopts; xopt != xopts + xopts_nr; xopt++)
+	for (xopt = cmd_opts.xopts; xopt != cmd_opts.xopts + cmd_opts.xopts_nr; xopt++)
 		parse_merge_opt(&o, *xopt);
 
 	clean = merge_trees(&o,
@@ -331,7 +341,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" */
-		return error(_("%s: Unable to write new index file"), me);
+		return error(_("%s: Unable to write new index file"),
+			cmd_opts.action == REVERT ? "revert" : "cherry-pick");
 	rollback_lock_file(&index_lock);
 
 	if (!clean) {
@@ -368,9 +379,9 @@ static int run_git_commit(const char *defmsg)
 
 	args[i++] = "commit";
 	args[i++] = "-n";
-	if (signoff)
+	if (cmd_opts.signoff)
 		args[i++] = "-s";
-	if (!edit) {
+	if (!cmd_opts.edit) {
 		args[i++] = "-F";
 		args[i++] = defmsg;
 	}
@@ -389,7 +400,7 @@ static int do_pick_commit(struct commit *commit)
 	struct strbuf msgbuf = STRBUF_INIT;
 	int res;
 
-	if (no_commit) {
+	if (cmd_opts.no_commit) {
 		/*
 		 * We do not intend to commit immediately.  We just want to
 		 * merge the differences in, so let's compute the tree
@@ -407,7 +418,7 @@ static int do_pick_commit(struct commit *commit)
 	discard_cache();
 
 	if (!commit->parents) {
-		if (action == REVERT)
+		if (cmd_opts.action == REVERT)
 			return error(_("Cannot revert a root commit"));
 		parent = NULL;
 	}
@@ -416,32 +427,32 @@ static int do_pick_commit(struct commit *commit)
 		int cnt;
 		struct commit_list *p;
 
-		if (!mainline)
+		if (!cmd_opts.mainline)
 			return error(_("Commit %s is a merge but no -m option was given."),
 				sha1_to_hex(commit->object.sha1));
 
 		for (cnt = 1, p = commit->parents;
-		     cnt != mainline && p;
+		     cnt != cmd_opts.mainline && p;
 		     cnt++)
 			p = p->next;
-		if (cnt != mainline || !p)
+		if (cnt != cmd_opts.mainline || !p)
 			return error(_("Commit %s does not have parent %d"),
-			    sha1_to_hex(commit->object.sha1), mainline);
+			    sha1_to_hex(commit->object.sha1), cmd_opts.mainline);
 		parent = p->item;
-	} else if (mainline > 0)
+	} else if (cmd_opts.mainline > 0)
 		return error(_("Mainline was specified but commit %s is not a merge."),
 		    sha1_to_hex(commit->object.sha1));
 	else
 		parent = commit->parents->item;
 
-	if (allow_ff && parent && !hashcmp(parent->object.sha1, head))
+	if (cmd_opts.allow_ff && parent && !hashcmp(parent->object.sha1, head))
 		return fast_forward_to(commit->object.sha1, head);
 
 	if (parent && parse_commit(parent) < 0)
 		/* TRANSLATORS: The first %s will be "revert" or
 		   "cherry-pick", the second %s a SHA1 */
 		return error(_("%s: cannot parse parent commit %s"),
-			action == REVERT ? "revert" : "cherry-pick",
+			cmd_opts.action == REVERT ? "revert" : "cherry-pick",
 			sha1_to_hex(parent->object.sha1));
 
 	if (get_message(commit, commit->buffer, &msg) != 0)
@@ -457,7 +468,7 @@ static int do_pick_commit(struct commit *commit)
 
 	defmsg = git_pathdup("MERGE_MSG");
 
-	if (action == REVERT) {
+	if (cmd_opts.action == REVERT) {
 		base = commit;
 		base_label = msg.label;
 		next = parent;
@@ -478,16 +489,16 @@ static int do_pick_commit(struct commit *commit)
 		next = commit;
 		next_label = msg.label;
 		add_message_to_msg(commit, &msgbuf, msg.message);
-		if (no_replay) {
+		if (cmd_opts.no_replay) {
 			strbuf_addstr(&msgbuf, "(cherry picked from commit ");
 			strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
 			strbuf_addstr(&msgbuf, ")\n");
 		}
-		if (!no_commit)
+		if (!cmd_opts.no_commit)
 			write_cherry_pick_head(commit);
 	}
 
-	if (!strategy || !strcmp(strategy, "recursive") || action == REVERT) {
+	if (!cmd_opts.strategy || !strcmp(cmd_opts.strategy, "recursive") || cmd_opts.action == REVERT) {
 		res = do_recursive_merge(base, next, base_label, next_label,
 					 head, &msgbuf);
 		write_message(&msgbuf, defmsg);
@@ -499,22 +510,23 @@ static int do_pick_commit(struct commit *commit)
 
 		commit_list_insert(base, &common);
 		commit_list_insert(next, &remotes);
-		res = try_merge_command(strategy, xopts_nr, xopts, common,
+		res = try_merge_command(cmd_opts.strategy, cmd_opts.xopts_nr,
+					cmd_opts.xopts, common,
 					sha1_to_hex(head), remotes);
 		free_commit_list(common);
 		free_commit_list(remotes);
 	}
 
 	if (res) {
-		error(action == REVERT
+		error(cmd_opts.action == REVERT
 		      ? _("could not revert %s... %s")
 		      : _("could not apply %s... %s"),
 		      find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV),
 		      msg.subject);
 		print_advice();
-		rerere(allow_rerere_auto);
+		rerere(cmd_opts.allow_rerere_auto);
 	} else {
-		if (!no_commit)
+		if (!cmd_opts.no_commit)
 			res = run_git_commit(defmsg);
 	}
 
@@ -530,10 +542,10 @@ static int prepare_revs(struct rev_info *revs)
 
 	init_revisions(revs, NULL);
 	revs->no_walk = 1;
-	if (action != REVERT)
+	if (cmd_opts.action != REVERT)
 		revs->reverse = 1;
 
-	argc = setup_revisions(commit_argc, commit_argv, revs, NULL);
+	argc = setup_revisions(cmd_opts.commit_argc, cmd_opts.commit_argv, revs, NULL);
 	if (argc > 1) {
 		fprintf(stderr, "usage: %s", _(*revert_or_cherry_pick_usage()));
 		return 129;
@@ -574,18 +586,18 @@ static int revert_or_cherry_pick(int argc, const char **argv)
 	int res;
 
 	git_config(git_default_config, NULL);
-	me = (action == REVERT ? "revert" : "cherry-pick");
+	me = (cmd_opts.action == REVERT ? "revert" : "cherry-pick");
 	setenv(GIT_REFLOG_ACTION, me, 0);
 	parse_args(argc, argv);
 
-	if (allow_ff) {
-		if (signoff)
+	if (cmd_opts.allow_ff) {
+		if (cmd_opts.signoff)
 			die(_("cherry-pick --ff cannot be used with --signoff"));
-		if (no_commit)
+		if (cmd_opts.no_commit)
 			die(_("cherry-pick --ff cannot be used with --no-commit"));
-		if (no_replay)
+		if (cmd_opts.no_replay)
 			die(_("cherry-pick --ff cannot be used with -x"));
-		if (edit)
+		if (cmd_opts.edit)
 			die(_("cherry-pick --ff cannot be used with --edit"));
 	}
 
@@ -603,13 +615,13 @@ static int revert_or_cherry_pick(int argc, const char **argv)
 int cmd_revert(int argc, const char **argv, const char *prefix)
 {
 	if (isatty(0))
-		edit = 1;
-	action = REVERT;
+		cmd_opts.edit = 1;
+	cmd_opts.action = REVERT;
 	return revert_or_cherry_pick(argc, argv);
 }
 
 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
 {
-	action = CHERRY_PICK;
+	cmd_opts.action = CHERRY_PICK;
 	return revert_or_cherry_pick(argc, argv);
 }
-- 
1.7.4.rc1.7.g2cf08.dirty

  parent reply	other threads:[~2011-04-10 15:13 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-10 15:11 [RFC PATCH 00/11] Sequencer Foundations Ramkumar Ramachandra
2011-04-10 15:11 ` [PATCH 01/11] revert: Avoid calling die; return error instead Ramkumar Ramachandra
2011-04-10 19:14   ` Jonathan Nieder
2011-05-08 12:04     ` Ramkumar Ramachandra
2011-04-11 20:26   ` Junio C Hamano
2011-04-10 15:11 ` [PATCH 02/11] revert: Lose global variables "commit" and "me" Ramkumar Ramachandra
2011-04-11  3:24   ` Christian Couder
2011-04-11  8:57     ` Ramkumar Ramachandra
2011-04-10 15:11 ` Ramkumar Ramachandra [this message]
2011-04-10 19:21   ` [PATCH 03/11] revert: Introduce a struct to parse command-line options into Jonathan Nieder
2011-05-08 12:18     ` Ramkumar Ramachandra
2011-04-11 21:41   ` Junio C Hamano
2011-05-08 12:09     ` Ramkumar Ramachandra
2011-04-10 15:11 ` [PATCH 04/11] revert: Separate cmdline argument handling from the functional code Ramkumar Ramachandra
2011-04-10 15:11 ` [PATCH 05/11] revert: Catch incompatible command-line options early Ramkumar Ramachandra
2011-04-11 21:44   ` Junio C Hamano
2011-05-08 11:47     ` Ramkumar Ramachandra
2011-04-10 15:11 ` [PATCH 06/11] revert: Implement parsing --continue, --abort and --skip Ramkumar Ramachandra
2011-04-10 15:11 ` [PATCH 07/11] revert: Handle conflict resolutions more elegantly Ramkumar Ramachandra
2011-04-10 15:11 ` [PATCH 08/11] usage: Introduce error_errno correspoding to die_errno Ramkumar Ramachandra
2011-04-10 15:11 ` [PATCH 09/11] revert: Write head, todo, done files Ramkumar Ramachandra
2011-04-10 15:11 ` [PATCH 10/11] revert: Give noop a default value while argument parsing Ramkumar Ramachandra
2011-04-10 15:11 ` [PATCH 11/11] revert: Implement --abort processing Ramkumar Ramachandra
2011-04-10 19:33 ` [RFC PATCH 00/11] Sequencer Foundations Daniel Barkalow
2011-04-11  8:55   ` Ramkumar Ramachandra
2011-04-10 19:47 ` Jonathan Nieder
2011-04-11  1:16   ` Daniel Barkalow
2011-04-11  6:42     ` Jonathan Nieder
2011-04-11  9:07   ` Ramkumar Ramachandra
2011-04-11  3:18 ` Christian Couder
2011-04-11  4:49   ` Ramkumar Ramachandra
2011-04-11  6:20     ` Christian Couder
2011-04-11 10:48       ` Ramkumar Ramachandra
2011-04-11  5:30   ` Daniel Barkalow
2011-04-11  5:38     ` Jonathan Nieder
2011-04-11  6:34       ` Daniel Barkalow

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

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1302448317-32387-4-git-send-email-artagnon@gmail.com \
    --to=artagnon@gmail.com \
    --cc=barkalow@iabervon.org \
    --cc=chriscool@tuxfamily.org \
    --cc=git@vger.kernel.org \
    --cc=jrnieder@gmail.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).