Skip to content

Commit 25b763b

Browse files
committed
builtin/am: introduce write_state_*() helper functions
There are many calls to write_file() that repeat the same pattern in the implementation of the builtin version of "am". They all share the same traits, i.e they - produce a text file with a single string in it; - have enough information to produce the entire contents of that file; - generate the pathname of the file by making a call to am_path(); and - they ask write_file() to die() upon failure. The slight differences among the call sites throw them into roughly three categories: - many write either "t" or "f" based on a boolean value to a file; - some write the integer value in decimal text; - some others write more general string, e.g. an object name in hex, an empty string (i.e. the presense of the file itself serves as a flag), etc. Introduce three helpers, write_state_bool(), write_state_count() and write_state_text(), to reduce direct calls to write_file(). This is a preparatory step for the next step to ensure that no "state" file this command leaves in $GIT_DIR is with an incomplete line at the end. Suggested-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent b5e8235 commit 25b763b

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

builtin/am.c

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,27 @@ static inline const char *am_path(const struct am_state *state, const char *path
193193
return mkpath("%s/%s", state->dir, path);
194194
}
195195

196+
/**
197+
* For convenience to call write_file()
198+
*/
199+
static int write_state_text(const struct am_state *state,
200+
const char *name, const char *string)
201+
{
202+
return write_file(am_path(state, name), 1, "%s", string);
203+
}
204+
205+
static int write_state_count(const struct am_state *state,
206+
const char *name, int value)
207+
{
208+
return write_file(am_path(state, name), 1, "%d", value);
209+
}
210+
211+
static int write_state_bool(const struct am_state *state,
212+
const char *name, int value)
213+
{
214+
return write_state_text(state, name, value ? "t" : "f");
215+
}
216+
196217
/**
197218
* If state->quiet is false, calls fprintf(fp, fmt, ...), and appends a newline
198219
* at the end.
@@ -362,7 +383,7 @@ static void write_author_script(const struct am_state *state)
362383
sq_quote_buf(&sb, state->author_date);
363384
strbuf_addch(&sb, '\n');
364385

365-
write_file(am_path(state, "author-script"), 1, "%s", sb.buf);
386+
write_state_text(state, "author-script", sb.buf);
366387

367388
strbuf_release(&sb);
368389
}
@@ -1000,13 +1021,10 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
10001021
if (state->rebasing)
10011022
state->threeway = 1;
10021023

1003-
write_file(am_path(state, "threeway"), 1, state->threeway ? "t" : "f");
1004-
1005-
write_file(am_path(state, "quiet"), 1, state->quiet ? "t" : "f");
1006-
1007-
write_file(am_path(state, "sign"), 1, state->signoff ? "t" : "f");
1008-
1009-
write_file(am_path(state, "utf8"), 1, state->utf8 ? "t" : "f");
1024+
write_state_bool(state, "threeway", state->threeway);
1025+
write_state_bool(state, "quiet", state->quiet);
1026+
write_state_bool(state, "sign", state->signoff);
1027+
write_state_bool(state, "utf8", state->utf8);
10101028

10111029
switch (state->keep) {
10121030
case KEEP_FALSE:
@@ -1022,9 +1040,8 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
10221040
die("BUG: invalid value for state->keep");
10231041
}
10241042

1025-
write_file(am_path(state, "keep"), 1, "%s", str);
1026-
1027-
write_file(am_path(state, "messageid"), 1, state->message_id ? "t" : "f");
1043+
write_state_text(state, "keep", str);
1044+
write_state_bool(state, "messageid", state->message_id);
10281045

10291046
switch (state->scissors) {
10301047
case SCISSORS_UNSET:
@@ -1039,24 +1056,23 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
10391056
default:
10401057
die("BUG: invalid value for state->scissors");
10411058
}
1042-
1043-
write_file(am_path(state, "scissors"), 1, "%s", str);
1059+
write_state_text(state, "scissors", str);
10441060

10451061
sq_quote_argv(&sb, state->git_apply_opts.argv, 0);
1046-
write_file(am_path(state, "apply-opt"), 1, "%s", sb.buf);
1062+
write_state_text(state, "apply-opt", sb.buf);
10471063

10481064
if (state->rebasing)
1049-
write_file(am_path(state, "rebasing"), 1, "%s", "");
1065+
write_state_text(state, "rebasing", "");
10501066
else
1051-
write_file(am_path(state, "applying"), 1, "%s", "");
1067+
write_state_text(state, "applying", "");
10521068

10531069
if (!get_sha1("HEAD", curr_head)) {
1054-
write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(curr_head));
1070+
write_state_text(state, "abort-safety", sha1_to_hex(curr_head));
10551071
if (!state->rebasing)
10561072
update_ref("am", "ORIG_HEAD", curr_head, NULL, 0,
10571073
UPDATE_REFS_DIE_ON_ERR);
10581074
} else {
1059-
write_file(am_path(state, "abort-safety"), 1, "%s", "");
1075+
write_state_text(state, "abort-safety", "");
10601076
if (!state->rebasing)
10611077
delete_ref("ORIG_HEAD", NULL, 0);
10621078
}
@@ -1066,9 +1082,8 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
10661082
* session is in progress, they should be written last.
10671083
*/
10681084

1069-
write_file(am_path(state, "next"), 1, "%d", state->cur);
1070-
1071-
write_file(am_path(state, "last"), 1, "%d", state->last);
1085+
write_state_count(state, "next", state->cur);
1086+
write_state_count(state, "last", state->last);
10721087

10731088
strbuf_release(&sb);
10741089
}
@@ -1101,12 +1116,12 @@ static void am_next(struct am_state *state)
11011116
unlink(am_path(state, "original-commit"));
11021117

11031118
if (!get_sha1("HEAD", head))
1104-
write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(head));
1119+
write_state_text(state, "abort-safety", sha1_to_hex(head));
11051120
else
1106-
write_file(am_path(state, "abort-safety"), 1, "%s", "");
1121+
write_state_text(state, "abort-safety", "");
11071122

11081123
state->cur++;
1109-
write_file(am_path(state, "next"), 1, "%d", state->cur);
1124+
write_state_count(state, "next", state->cur);
11101125
}
11111126

11121127
/**
@@ -1479,8 +1494,7 @@ static int parse_mail_rebase(struct am_state *state, const char *mail)
14791494
write_commit_patch(state, commit);
14801495

14811496
hashcpy(state->orig_commit, commit_sha1);
1482-
write_file(am_path(state, "original-commit"), 1, "%s",
1483-
sha1_to_hex(commit_sha1));
1497+
write_state_text(state, "original-commit", sha1_to_hex(commit_sha1));
14841498

14851499
return 0;
14861500
}
@@ -1782,7 +1796,7 @@ static void am_run(struct am_state *state, int resume)
17821796
refresh_and_write_cache();
17831797

17841798
if (index_has_changes(&sb)) {
1785-
write_file(am_path(state, "dirtyindex"), 1, "t");
1799+
write_state_bool(state, "dirtyindex", 1);
17861800
die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
17871801
}
17881802

0 commit comments

Comments
 (0)