revert: Eliminate global "commit" variable

Functions which act on commits currently rely on a file-scope static variable to be set before they're called. Consequently, the API and corresponding callsites are ugly and unclear. Remove this variable and change their API to accept the commit to act on as additional argument so that the callsites change from looking like commit = prepare_a_commit(); act_on_commit(); to looking like commit = prepare_a_commit(); act_on_commit(commit); This change is also in line with our long-term goal of exposing some of these functions through a public API. Inspired-by: Christian Couder <chriscool@tuxfamily.org> Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ramkumar Ramachandra committed Aug 4, 2011 at 16:09 UTC 708f9d96d95be7e9912eea8350fdf3f04484695f
1 file changed +11 -11
builtin/revert.c
+11 -11
@@ -37,7 +37,6 @@ static const char * const cherry_pick_usage[] = {
37
38 static int edit, record_origin, no_commit, mainline, signoff, allow_ff;
39 static enum { REVERT, CHERRY_PICK } action;
40 -static struct commit *commit;
40 static int commit_argc;
41 static const char **commit_argv;
42 static int allow_rerere_auto;
@@ -116,25 +115,25 @@ struct commit_message {
115 const char *message;
116 };
117
119 -static int get_message(const char *raw_message, struct commit_message *out)
118 +static int get_message(struct commit *commit, struct commit_message *out)
119 {
120 const char *encoding;
121 const char *abbrev, *subject;
122 int abbrev_len, subject_len;
123 char *q;
124
126 - if (!raw_message)
125 + if (!commit->buffer)
126 return -1;
128 - encoding = get_encoding(raw_message);
127 + encoding = get_encoding(commit->buffer);
128 if (!encoding)
129 encoding = "UTF-8";
130 if (!git_commit_encoding)
131 git_commit_encoding = "UTF-8";
132
133 out->reencoded_message = NULL;
135 - out->message = raw_message;
134 + out->message = commit->buffer;
135 if (strcmp(encoding, git_commit_encoding))
137 - out->reencoded_message = reencode_string(raw_message,
136 + out->reencoded_message = reencode_string(commit->buffer,
137 git_commit_encoding, encoding);
138 if (out->reencoded_message)
139 out->message = out->reencoded_message;
@@ -182,7 +181,7 @@ static char *get_encoding(const char *message)
181 return NULL;
182 }
183
185 -static void write_cherry_pick_head(void)
184 +static void write_cherry_pick_head(struct commit *commit)
185 {
186 int fd;
187 struct strbuf buf = STRBUF_INIT;
@@ -355,7 +354,7 @@ static int run_git_commit(const char *defmsg)
354 return run_command_v_opt(args, RUN_GIT_CMD);
355 }
356
358 -static int do_pick_commit(void)
357 +static int do_pick_commit(struct commit *commit)
358 {
359 unsigned char head[20];
360 struct commit *base, *next, *parent;
@@ -417,7 +416,7 @@ static int do_pick_commit(void)
416 die(_("%s: cannot parse parent commit %s"),
417 me, sha1_to_hex(parent->object.sha1));
418
420 - if (get_message(commit->buffer, &msg) != 0)
419 + if (get_message(commit, &msg) != 0)
420 die(_("Cannot get commit message for %s"),
421 sha1_to_hex(commit->object.sha1));
422
@@ -470,7 +469,7 @@ static int do_pick_commit(void)
469 strbuf_addstr(&msgbuf, ")\n");
470 }
471 if (!no_commit)
473 - write_cherry_pick_head();
472 + write_cherry_pick_head(commit);
473 }
474
475 if (!strategy || !strcmp(strategy, "recursive") || action == REVERT) {
@@ -548,6 +547,7 @@ static void read_and_refresh_cache(const char *me)
547 static int revert_or_cherry_pick(int argc, const char **argv)
548 {
549 struct rev_info revs;
550 + struct commit *commit;
551
552 git_config(git_default_config, NULL);
553 me = action == REVERT ? "revert" : "cherry-pick";
@@ -570,7 +570,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)
570 prepare_revs(&revs);
571
572 while ((commit = get_revision(&revs))) {
573 - int res = do_pick_commit();
573 + int res = do_pick_commit(commit);
574 if (res)
575 return res;
576 }