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>

Junio C Hamano committed Aug 24, 2015 at 09:12 UTC 25b763ba7a544d05fc46ad601f3f9c50a4cea8b0
1 file changed +41 -27
builtin/am.c
+41 -27
@@ -193,6 +193,27 @@ static inline const char *am_path(const struct am_state *state, const char *path
193 return mkpath("%s/%s", state->dir, path);
194 }
195
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 +
217 /**
218 * If state->quiet is false, calls fprintf(fp, fmt, ...), and appends a newline
219 * at the end.
@@ -362,7 +383,7 @@ static void write_author_script(const struct am_state *state)
383 sq_quote_buf(&sb, state->author_date);
384 strbuf_addch(&sb, '\n');
385
365 - write_file(am_path(state, "author-script"), 1, "%s", sb.buf);
386 + write_state_text(state, "author-script", sb.buf);
387
388 strbuf_release(&sb);
389 }
@@ -1000,13 +1021,10 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
1021 if (state->rebasing)
1022 state->threeway = 1;
1023
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);
1028
1029 switch (state->keep) {
1030 case KEEP_FALSE:
@@ -1022,9 +1040,8 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
1040 die("BUG: invalid value for state->keep");
1041 }
1042
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);
1045
1046 switch (state->scissors) {
1047 case SCISSORS_UNSET:
@@ -1039,24 +1056,23 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
1056 default:
1057 die("BUG: invalid value for state->scissors");
1058 }
1042 -
1043 - write_file(am_path(state, "scissors"), 1, "%s", str);
1059 + write_state_text(state, "scissors", str);
1060
1061 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);
1063
1064 if (state->rebasing)
1049 - write_file(am_path(state, "rebasing"), 1, "%s", "");
1065 + write_state_text(state, "rebasing", "");
1066 else
1051 - write_file(am_path(state, "applying"), 1, "%s", "");
1067 + write_state_text(state, "applying", "");
1068
1069 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));
1071 if (!state->rebasing)
1072 update_ref("am", "ORIG_HEAD", curr_head, NULL, 0,
1073 UPDATE_REFS_DIE_ON_ERR);
1074 } else {
1059 - write_file(am_path(state, "abort-safety"), 1, "%s", "");
1075 + write_state_text(state, "abort-safety", "");
1076 if (!state->rebasing)
1077 delete_ref("ORIG_HEAD", NULL, 0);
1078 }
@@ -1066,9 +1082,8 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
1082 * session is in progress, they should be written last.
1083 */
1084
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);
1087
1088 strbuf_release(&sb);
1089 }
@@ -1101,12 +1116,12 @@ static void am_next(struct am_state *state)
1116 unlink(am_path(state, "original-commit"));
1117
1118 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));
1120 else
1106 - write_file(am_path(state, "abort-safety"), 1, "%s", "");
1121 + write_state_text(state, "abort-safety", "");
1122
1123 state->cur++;
1109 - write_file(am_path(state, "next"), 1, "%d", state->cur);
1124 + write_state_count(state, "next", state->cur);
1125 }
1126
1127 /**
@@ -1479,8 +1494,7 @@ static int parse_mail_rebase(struct am_state *state, const char *mail)
1494 write_commit_patch(state, commit);
1495
1496 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));
1498
1499 return 0;
1500 }
@@ -1782,7 +1796,7 @@ static void am_run(struct am_state *state, int resume)
1796 refresh_and_write_cache();
1797
1798 if (index_has_changes(&sb)) {
1785 - write_file(am_path(state, "dirtyindex"), 1, "t");
1799 + write_state_bool(state, "dirtyindex", 1);
1800 die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
1801 }
1802