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.
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
}
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:
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:
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
}
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
}
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
/**
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
}
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