Skip to content

Commit 708f9d9

Browse files
artagnongitster
authored andcommitted
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 <[email protected]> Helped-by: Junio C Hamano <[email protected]> Signed-off-by: Ramkumar Ramachandra <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 54decbd commit 708f9d9

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

builtin/revert.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ static const char * const cherry_pick_usage[] = {
3737

3838
static int edit, record_origin, no_commit, mainline, signoff, allow_ff;
3939
static enum { REVERT, CHERRY_PICK } action;
40-
static struct commit *commit;
4140
static int commit_argc;
4241
static const char **commit_argv;
4342
static int allow_rerere_auto;
@@ -116,25 +115,25 @@ struct commit_message {
116115
const char *message;
117116
};
118117

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)
120119
{
121120
const char *encoding;
122121
const char *abbrev, *subject;
123122
int abbrev_len, subject_len;
124123
char *q;
125124

126-
if (!raw_message)
125+
if (!commit->buffer)
127126
return -1;
128-
encoding = get_encoding(raw_message);
127+
encoding = get_encoding(commit->buffer);
129128
if (!encoding)
130129
encoding = "UTF-8";
131130
if (!git_commit_encoding)
132131
git_commit_encoding = "UTF-8";
133132

134133
out->reencoded_message = NULL;
135-
out->message = raw_message;
134+
out->message = commit->buffer;
136135
if (strcmp(encoding, git_commit_encoding))
137-
out->reencoded_message = reencode_string(raw_message,
136+
out->reencoded_message = reencode_string(commit->buffer,
138137
git_commit_encoding, encoding);
139138
if (out->reencoded_message)
140139
out->message = out->reencoded_message;
@@ -182,7 +181,7 @@ static char *get_encoding(const char *message)
182181
return NULL;
183182
}
184183

185-
static void write_cherry_pick_head(void)
184+
static void write_cherry_pick_head(struct commit *commit)
186185
{
187186
int fd;
188187
struct strbuf buf = STRBUF_INIT;
@@ -355,7 +354,7 @@ static int run_git_commit(const char *defmsg)
355354
return run_command_v_opt(args, RUN_GIT_CMD);
356355
}
357356

358-
static int do_pick_commit(void)
357+
static int do_pick_commit(struct commit *commit)
359358
{
360359
unsigned char head[20];
361360
struct commit *base, *next, *parent;
@@ -417,7 +416,7 @@ static int do_pick_commit(void)
417416
die(_("%s: cannot parse parent commit %s"),
418417
me, sha1_to_hex(parent->object.sha1));
419418

420-
if (get_message(commit->buffer, &msg) != 0)
419+
if (get_message(commit, &msg) != 0)
421420
die(_("Cannot get commit message for %s"),
422421
sha1_to_hex(commit->object.sha1));
423422

@@ -470,7 +469,7 @@ static int do_pick_commit(void)
470469
strbuf_addstr(&msgbuf, ")\n");
471470
}
472471
if (!no_commit)
473-
write_cherry_pick_head();
472+
write_cherry_pick_head(commit);
474473
}
475474

476475
if (!strategy || !strcmp(strategy, "recursive") || action == REVERT) {
@@ -548,6 +547,7 @@ static void read_and_refresh_cache(const char *me)
548547
static int revert_or_cherry_pick(int argc, const char **argv)
549548
{
550549
struct rev_info revs;
550+
struct commit *commit;
551551

552552
git_config(git_default_config, NULL);
553553
me = action == REVERT ? "revert" : "cherry-pick";
@@ -570,7 +570,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)
570570
prepare_revs(&revs);
571571

572572
while ((commit = get_revision(&revs))) {
573-
int res = do_pick_commit();
573+
int res = do_pick_commit(commit);
574574
if (res)
575575
return res;
576576
}

0 commit comments

Comments
 (0)