replace: "libify" create_graft() and callees

File this away as yet another patch in the "libification" category. As with all useful functions, in the next commit we want to use create_graft() from a higher-level function where it would be inconvenient if the called function simply die()s: if there is a problem, we want to let the user know how to proceed, and the callee simply has no way of knowing what to say. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Apr 29, 2018 at 00:44 UTC e24e871920ac04473a470645e4c4dc53b422dd75
1 file changed +112 -57
builtin/replace.c
+112 -57
@@ -79,9 +79,9 @@ static int list_replace_refs(const char *pattern, const char *format)
79 else if (!strcmp(format, "long"))
80 data.format = REPLACE_FORMAT_LONG;
81 else
82 - die("invalid replace format '%s'\n"
83 - "valid formats are 'short', 'medium' and 'long'\n",
84 - format);
82 + return error("invalid replace format '%s'\n"
83 + "valid formats are 'short', 'medium' and 'long'\n",
84 + format);
85
86 for_each_replace_ref(show_reference, (void *)&data);
87
@@ -134,7 +134,7 @@ static int delete_replace_ref(const char *name, const char *ref,
134 return 0;
135 }
136
137 -static void check_ref_valid(struct object_id *object,
137 +static int check_ref_valid(struct object_id *object,
138 struct object_id *prev,
139 struct strbuf *ref,
140 int force)
@@ -142,12 +142,13 @@ static void check_ref_valid(struct object_id *object,
142 strbuf_reset(ref);
143 strbuf_addf(ref, "%s%s", git_replace_ref_base, oid_to_hex(object));
144 if (check_refname_format(ref->buf, 0))
145 - die("'%s' is not a valid ref name.", ref->buf);
145 + return error("'%s' is not a valid ref name.", ref->buf);
146
147 if (read_ref(ref->buf, prev))
148 oidclr(prev);
149 else if (!force)
150 - die("replace ref '%s' already exists", ref->buf);
150 + return error("replace ref '%s' already exists", ref->buf);
151 + return 0;
152 }
153
154 static int replace_object_oid(const char *object_ref,
@@ -161,28 +162,33 @@ static int replace_object_oid(const char *object_ref,
162 struct strbuf ref = STRBUF_INIT;
163 struct ref_transaction *transaction;
164 struct strbuf err = STRBUF_INIT;
165 + int res = 0;
166
167 obj_type = oid_object_info(object, NULL);
168 repl_type = oid_object_info(repl, NULL);
169 if (!force && obj_type != repl_type)
168 - die("Objects must be of the same type.\n"
169 - "'%s' points to a replaced object of type '%s'\n"
170 - "while '%s' points to a replacement object of type '%s'.",
171 - object_ref, type_name(obj_type),
172 - replace_ref, type_name(repl_type));
173 -
174 - check_ref_valid(object, &prev, &ref, force);
170 + return error("Objects must be of the same type.\n"
171 + "'%s' points to a replaced object of type '%s'\n"
172 + "while '%s' points to a replacement object of "
173 + "type '%s'.",
174 + object_ref, type_name(obj_type),
175 + replace_ref, type_name(repl_type));
176 +
177 + if (check_ref_valid(object, &prev, &ref, force)) {
178 + strbuf_release(&ref);
179 + return -1;
180 + }
181
182 transaction = ref_transaction_begin(&err);
183 if (!transaction ||
184 ref_transaction_update(transaction, ref.buf, repl, &prev,
185 0, NULL, &err) ||
186 ref_transaction_commit(transaction, &err))
181 - die("%s", err.buf);
187 + res = error("%s", err.buf);
188
189 ref_transaction_free(transaction);
190 strbuf_release(&ref);
185 - return 0;
191 + return res;
192 }
193
194 static int replace_object(const char *object_ref, const char *replace_ref, int force)
@@ -190,9 +196,11 @@ static int replace_object(const char *object_ref, const char *replace_ref, int f
196 struct object_id object, repl;
197
198 if (get_oid(object_ref, &object))
193 - die("Failed to resolve '%s' as a valid ref.", object_ref);
199 + return error("Failed to resolve '%s' as a valid ref.",
200 + object_ref);
201 if (get_oid(replace_ref, &repl))
195 - die("Failed to resolve '%s' as a valid ref.", replace_ref);
202 + return error("Failed to resolve '%s' as a valid ref.",
203 + replace_ref);
204
205 return replace_object_oid(object_ref, &object, replace_ref, &repl, force);
206 }
@@ -202,7 +210,7 @@ static int replace_object(const char *object_ref, const char *replace_ref, int f
210 * If "raw" is true, then the object's raw contents are printed according to
211 * "type". Otherwise, we pretty-print the contents for human editing.
212 */
205 -static void export_object(const struct object_id *oid, enum object_type type,
213 +static int export_object(const struct object_id *oid, enum object_type type,
214 int raw, const char *filename)
215 {
216 struct child_process cmd = CHILD_PROCESS_INIT;
@@ -210,7 +218,7 @@ static void export_object(const struct object_id *oid, enum object_type type,
218
219 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
220 if (fd < 0)
213 - die_errno("unable to open %s for writing", filename);
221 + return error_errno("unable to open %s for writing", filename);
222
223 argv_array_push(&cmd.args, "--no-replace-objects");
224 argv_array_push(&cmd.args, "cat-file");
@@ -223,7 +231,8 @@ static void export_object(const struct object_id *oid, enum object_type type,
231 cmd.out = fd;
232
233 if (run_command(&cmd))
226 - die("cat-file reported failure");
234 + return error("cat-file reported failure");
235 + return 0;
236 }
237
238 /*
@@ -231,14 +240,14 @@ static void export_object(const struct object_id *oid, enum object_type type,
240 * interpreting it as "type", and writing the result to the object database.
241 * The sha1 of the written object is returned via sha1.
242 */
234 -static void import_object(struct object_id *oid, enum object_type type,
243 +static int import_object(struct object_id *oid, enum object_type type,
244 int raw, const char *filename)
245 {
246 int fd;
247
248 fd = open(filename, O_RDONLY);
249 if (fd < 0)
241 - die_errno("unable to open %s for reading", filename);
250 + return error_errno("unable to open %s for reading", filename);
251
252 if (!raw && type == OBJ_TREE) {
253 const char *argv[] = { "mktree", NULL };
@@ -250,27 +259,40 @@ static void import_object(struct object_id *oid, enum object_type type,
259 cmd.in = fd;
260 cmd.out = -1;
261
253 - if (start_command(&cmd))
254 - die("unable to spawn mktree");
262 + if (start_command(&cmd)) {
263 + close(fd);
264 + return error("unable to spawn mktree");
265 + }
266
256 - if (strbuf_read(&result, cmd.out, 41) < 0)
257 - die_errno("unable to read from mktree");
267 + if (strbuf_read(&result, cmd.out, 41) < 0) {
268 + error_errno("unable to read from mktree");
269 + close(fd);
270 + close(cmd.out);
271 + return -1;
272 + }
273 close(cmd.out);
274
260 - if (finish_command(&cmd))
261 - die("mktree reported failure");
262 - if (get_oid_hex(result.buf, oid) < 0)
263 - die("mktree did not return an object name");
275 + if (finish_command(&cmd)) {
276 + strbuf_release(&result);
277 + return error("mktree reported failure");
278 + }
279 + if (get_oid_hex(result.buf, oid) < 0) {
280 + strbuf_release(&result);
281 + return error("mktree did not return an object name");
282 + }
283
284 strbuf_release(&result);
285 } else {
286 struct stat st;
287 int flags = HASH_FORMAT_CHECK | HASH_WRITE_OBJECT;
288
270 - if (fstat(fd, &st) < 0)
271 - die_errno("unable to fstat %s", filename);
289 + if (fstat(fd, &st) < 0) {
290 + error_errno("unable to fstat %s", filename);
291 + close(fd);
292 + return -1;
293 + }
294 if (index_fd(oid, fd, &st, type, NULL, flags) < 0)
273 - die("unable to write object to database");
295 + return error("unable to write object to database");
296 /* index_fd close()s fd for us */
297 }
298
@@ -278,30 +300,43 @@ static void import_object(struct object_id *oid, enum object_type type,
300 * No need to close(fd) here; both run-command and index-fd
301 * will have done it for us.
302 */
303 + return 0;
304 }
305
306 static int edit_and_replace(const char *object_ref, int force, int raw)
307 {
285 - char *tmpfile = git_pathdup("REPLACE_EDITOBJ");
308 + char *tmpfile;
309 enum object_type type;
310 struct object_id old_oid, new_oid, prev;
311 struct strbuf ref = STRBUF_INIT;
312
313 if (get_oid(object_ref, &old_oid) < 0)
291 - die("Not a valid object name: '%s'", object_ref);
314 + return error("Not a valid object name: '%s'", object_ref);
315
316 type = oid_object_info(&old_oid, NULL);
317 if (type < 0)
295 - die("unable to get object type for %s", oid_to_hex(&old_oid));
318 + return error("unable to get object type for %s",
319 + oid_to_hex(&old_oid));
320
297 - check_ref_valid(&old_oid, &prev, &ref, force);
321 + if (check_ref_valid(&old_oid, &prev, &ref, force)) {
322 + strbuf_release(&ref);
323 + return -1;
324 + }
325 strbuf_release(&ref);
326
300 - export_object(&old_oid, type, raw, tmpfile);
301 - if (launch_editor(tmpfile, NULL, NULL) < 0)
302 - die("editing object file failed");
303 - import_object(&new_oid, type, raw, tmpfile);
304 -
327 + tmpfile = git_pathdup("REPLACE_EDITOBJ");
328 + if (export_object(&old_oid, type, raw, tmpfile)) {
329 + free(tmpfile);
330 + return -1;
331 + }
332 + if (launch_editor(tmpfile, NULL, NULL) < 0) {
333 + free(tmpfile);
334 + return error("editing object file failed");
335 + }
336 + if (import_object(&new_oid, type, raw, tmpfile)) {
337 + free(tmpfile);
338 + return -1;
339 + }
340 free(tmpfile);
341
342 if (!oidcmp(&old_oid, &new_oid))
@@ -310,7 +345,7 @@ static int edit_and_replace(const char *object_ref, int force, int raw)
345 return replace_object_oid(object_ref, &old_oid, "replacement", &new_oid, force);
346 }
347
313 -static void replace_parents(struct strbuf *buf, int argc, const char **argv)
348 +static int replace_parents(struct strbuf *buf, int argc, const char **argv)
349 {
350 struct strbuf new_parents = STRBUF_INIT;
351 const char *parent_start, *parent_end;
@@ -327,9 +362,15 @@ static void replace_parents(struct strbuf *buf, int argc, const char **argv)
362 /* prepare new parents */
363 for (i = 0; i < argc; i++) {
364 struct object_id oid;
330 - if (get_oid(argv[i], &oid) < 0)
331 - die(_("Not a valid object name: '%s'"), argv[i]);
332 - lookup_commit_or_die(&oid, argv[i]);
365 + if (get_oid(argv[i], &oid) < 0) {
366 + strbuf_release(&new_parents);
367 + return error(_("Not a valid object name: '%s'"),
368 + argv[i]);
369 + }
370 + if (!lookup_commit_reference(&oid)) {
371 + strbuf_release(&new_parents);
372 + return error(_("could not parse %s"), argv[i]);
373 + }
374 strbuf_addf(&new_parents, "parent %s\n", oid_to_hex(&oid));
375 }
376
@@ -338,6 +379,7 @@ static void replace_parents(struct strbuf *buf, int argc, const char **argv)
379 new_parents.buf, new_parents.len);
380
381 strbuf_release(&new_parents);
382 + return 0;
383 }
384
385 struct check_mergetag_data {
@@ -358,21 +400,23 @@ static int check_one_mergetag(struct commit *commit,
400 hash_object_file(extra->value, extra->len, type_name(OBJ_TAG), &tag_oid);
401 tag = lookup_tag(&tag_oid);
402 if (!tag)
361 - die(_("bad mergetag in commit '%s'"), ref);
403 + return error(_("bad mergetag in commit '%s'"), ref);
404 if (parse_tag_buffer(tag, extra->value, extra->len))
363 - die(_("malformed mergetag in commit '%s'"), ref);
405 + return error(_("malformed mergetag in commit '%s'"), ref);
406
407 /* iterate over new parents */
408 for (i = 1; i < mergetag_data->argc; i++) {
409 struct object_id oid;
410 if (get_oid(mergetag_data->argv[i], &oid) < 0)
369 - die(_("Not a valid object name: '%s'"), mergetag_data->argv[i]);
411 + return error(_("Not a valid object name: '%s'"),
412 + mergetag_data->argv[i]);
413 if (!oidcmp(&tag->tagged->oid, &oid))
414 return 0; /* found */
415 }
416
374 - die(_("original commit '%s' contains mergetag '%s' that is discarded; "
375 - "use --edit instead of --graft"), ref, oid_to_hex(&tag_oid));
417 + return error(_("original commit '%s' contains mergetag '%s' that is "
418 + "discarded; use --edit instead of --graft"), ref,
419 + oid_to_hex(&tag_oid));
420 }
421
422 static int check_mergetags(struct commit *commit, int argc, const char **argv)
@@ -394,24 +438,35 @@ static int create_graft(int argc, const char **argv, int force)
438 unsigned long size;
439
440 if (get_oid(old_ref, &old_oid) < 0)
397 - die(_("Not a valid object name: '%s'"), old_ref);
398 - commit = lookup_commit_or_die(&old_oid, old_ref);
441 + return error(_("Not a valid object name: '%s'"), old_ref);
442 + commit = lookup_commit_reference(&old_oid);
443 + if (!commit)
444 + return error(_("could not parse %s"), old_ref);
445
446 buffer = get_commit_buffer(commit, &size);
447 strbuf_add(&buf, buffer, size);
448 unuse_commit_buffer(commit, buffer);
449
404 - replace_parents(&buf, argc - 1, &argv[1]);
450 + if (replace_parents(&buf, argc - 1, &argv[1]) < 0) {
451 + strbuf_release(&buf);
452 + return -1;
453 + }
454
455 if (remove_signature(&buf)) {
456 warning(_("the original commit '%s' has a gpg signature."), old_ref);
457 warning(_("the signature will be removed in the replacement commit!"));
458 }
459
411 - check_mergetags(commit, argc, argv);
460 + if (check_mergetags(commit, argc, argv)) {
461 + strbuf_release(&buf);
462 + return -1;
463 + }
464
413 - if (write_object_file(buf.buf, buf.len, commit_type, &new_oid))
414 - die(_("could not write replacement commit for: '%s'"), old_ref);
465 + if (write_object_file(buf.buf, buf.len, commit_type, &new_oid)) {
466 + strbuf_release(&buf);
467 + return error(_("could not write replacement commit for: '%s'"),
468 + old_ref);
469 + }
470
471 strbuf_release(&buf);
472