]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/commit.c
Merge branch 'rs/t-prio-queue-cleanup'
[thirdparty/git.git] / builtin / commit.c
1 /*
2 * Builtin "git commit"
3 *
4 * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
6 */
7
8 #define USE_THE_INDEX_VARIABLE
9 #include "builtin.h"
10 #include "advice.h"
11 #include "config.h"
12 #include "lockfile.h"
13 #include "cache-tree.h"
14 #include "color.h"
15 #include "dir.h"
16 #include "editor.h"
17 #include "environment.h"
18 #include "diff.h"
19 #include "commit.h"
20 #include "gettext.h"
21 #include "revision.h"
22 #include "wt-status.h"
23 #include "run-command.h"
24 #include "strbuf.h"
25 #include "object-name.h"
26 #include "parse-options.h"
27 #include "path.h"
28 #include "preload-index.h"
29 #include "read-cache.h"
30 #include "string-list.h"
31 #include "rerere.h"
32 #include "unpack-trees.h"
33 #include "column.h"
34 #include "sequencer.h"
35 #include "sparse-index.h"
36 #include "mailmap.h"
37 #include "help.h"
38 #include "commit-reach.h"
39 #include "commit-graph.h"
40 #include "pretty.h"
41
42 static const char * const builtin_commit_usage[] = {
43 N_("git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]\n"
44 " [--dry-run] [(-c | -C | --squash) <commit> | --fixup [(amend|reword):]<commit>)]\n"
45 " [-F <file> | -m <msg>] [--reset-author] [--allow-empty]\n"
46 " [--allow-empty-message] [--no-verify] [-e] [--author=<author>]\n"
47 " [--date=<date>] [--cleanup=<mode>] [--[no-]status]\n"
48 " [-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
49 " [(--trailer <token>[(=|:)<value>])...] [-S[<keyid>]]\n"
50 " [--] [<pathspec>...]"),
51 NULL
52 };
53
54 static const char * const builtin_status_usage[] = {
55 N_("git status [<options>] [--] [<pathspec>...]"),
56 NULL
57 };
58
59 static const char empty_amend_advice[] =
60 N_("You asked to amend the most recent commit, but doing so would make\n"
61 "it empty. You can repeat your command with --allow-empty, or you can\n"
62 "remove the commit entirely with \"git reset HEAD^\".\n");
63
64 static const char empty_cherry_pick_advice[] =
65 N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n"
66 "If you wish to commit it anyway, use:\n"
67 "\n"
68 " git commit --allow-empty\n"
69 "\n");
70
71 static const char empty_rebase_pick_advice[] =
72 N_("Otherwise, please use 'git rebase --skip'\n");
73
74 static const char empty_cherry_pick_advice_single[] =
75 N_("Otherwise, please use 'git cherry-pick --skip'\n");
76
77 static const char empty_cherry_pick_advice_multi[] =
78 N_("and then use:\n"
79 "\n"
80 " git cherry-pick --continue\n"
81 "\n"
82 "to resume cherry-picking the remaining commits.\n"
83 "If you wish to skip this commit, use:\n"
84 "\n"
85 " git cherry-pick --skip\n"
86 "\n");
87
88 static const char *color_status_slots[] = {
89 [WT_STATUS_HEADER] = "header",
90 [WT_STATUS_UPDATED] = "updated",
91 [WT_STATUS_CHANGED] = "changed",
92 [WT_STATUS_UNTRACKED] = "untracked",
93 [WT_STATUS_NOBRANCH] = "noBranch",
94 [WT_STATUS_UNMERGED] = "unmerged",
95 [WT_STATUS_LOCAL_BRANCH] = "localBranch",
96 [WT_STATUS_REMOTE_BRANCH] = "remoteBranch",
97 [WT_STATUS_ONBRANCH] = "branch",
98 };
99
100 static const char *use_message_buffer;
101 static struct lock_file index_lock; /* real index */
102 static struct lock_file false_lock; /* used only for partial commits */
103 static enum {
104 COMMIT_AS_IS = 1,
105 COMMIT_NORMAL,
106 COMMIT_PARTIAL
107 } commit_style;
108
109 static const char *logfile, *force_author;
110 static const char *template_file;
111 /*
112 * The _message variables are commit names from which to take
113 * the commit message and/or authorship.
114 */
115 static const char *author_message, *author_message_buffer;
116 static char *edit_message, *use_message;
117 static char *fixup_message, *fixup_commit, *squash_message;
118 static const char *fixup_prefix;
119 static int all, also, interactive, patch_interactive, only, amend, signoff;
120 static int edit_flag = -1; /* unspecified */
121 static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
122 static int config_commit_verbose = -1; /* unspecified */
123 static int no_post_rewrite, allow_empty_message, pathspec_file_nul;
124 static char *untracked_files_arg, *force_date, *ignore_submodule_arg, *ignored_arg;
125 static char *sign_commit, *pathspec_from_file;
126 static struct strvec trailer_args = STRVEC_INIT;
127
128 /*
129 * The default commit message cleanup mode will remove the lines
130 * beginning with # (shell comments) and leading and trailing
131 * whitespaces (empty lines or containing only whitespaces)
132 * if editor is used, and only the whitespaces if the message
133 * is specified explicitly.
134 */
135 static enum commit_msg_cleanup_mode cleanup_mode;
136 static const char *cleanup_arg;
137
138 static enum commit_whence whence;
139 static int use_editor = 1, include_status = 1;
140 static int have_option_m;
141 static struct strbuf message = STRBUF_INIT;
142
143 static enum wt_status_format status_format = STATUS_FORMAT_UNSPECIFIED;
144
145 static int opt_pass_trailer(const struct option *opt, const char *arg, int unset)
146 {
147 BUG_ON_OPT_NEG(unset);
148
149 strvec_pushl(opt->value, "--trailer", arg, NULL);
150 return 0;
151 }
152
153 static int opt_parse_porcelain(const struct option *opt, const char *arg, int unset)
154 {
155 enum wt_status_format *value = (enum wt_status_format *)opt->value;
156 if (unset)
157 *value = STATUS_FORMAT_NONE;
158 else if (!arg)
159 *value = STATUS_FORMAT_PORCELAIN;
160 else if (!strcmp(arg, "v1") || !strcmp(arg, "1"))
161 *value = STATUS_FORMAT_PORCELAIN;
162 else if (!strcmp(arg, "v2") || !strcmp(arg, "2"))
163 *value = STATUS_FORMAT_PORCELAIN_V2;
164 else
165 die("unsupported porcelain version '%s'", arg);
166
167 return 0;
168 }
169
170 static int opt_parse_m(const struct option *opt, const char *arg, int unset)
171 {
172 struct strbuf *buf = opt->value;
173 if (unset) {
174 have_option_m = 0;
175 strbuf_setlen(buf, 0);
176 } else {
177 have_option_m = 1;
178 if (buf->len)
179 strbuf_addch(buf, '\n');
180 strbuf_addstr(buf, arg);
181 strbuf_complete_line(buf);
182 }
183 return 0;
184 }
185
186 static int opt_parse_rename_score(const struct option *opt, const char *arg, int unset)
187 {
188 const char **value = opt->value;
189
190 BUG_ON_OPT_NEG(unset);
191
192 if (arg != NULL && *arg == '=')
193 arg = arg + 1;
194
195 *value = arg;
196 return 0;
197 }
198
199 static void determine_whence(struct wt_status *s)
200 {
201 if (file_exists(git_path_merge_head(the_repository)))
202 whence = FROM_MERGE;
203 else if (!sequencer_determine_whence(the_repository, &whence))
204 whence = FROM_COMMIT;
205 if (s)
206 s->whence = whence;
207 }
208
209 static void status_init_config(struct wt_status *s, config_fn_t fn)
210 {
211 wt_status_prepare(the_repository, s);
212 init_diff_ui_defaults();
213 git_config(fn, s);
214 determine_whence(s);
215 s->hints = advice_enabled(ADVICE_STATUS_HINTS); /* must come after git_config() */
216 }
217
218 static void rollback_index_files(void)
219 {
220 switch (commit_style) {
221 case COMMIT_AS_IS:
222 break; /* nothing to do */
223 case COMMIT_NORMAL:
224 rollback_lock_file(&index_lock);
225 break;
226 case COMMIT_PARTIAL:
227 rollback_lock_file(&index_lock);
228 rollback_lock_file(&false_lock);
229 break;
230 }
231 }
232
233 static int commit_index_files(void)
234 {
235 int err = 0;
236
237 switch (commit_style) {
238 case COMMIT_AS_IS:
239 break; /* nothing to do */
240 case COMMIT_NORMAL:
241 err = commit_lock_file(&index_lock);
242 break;
243 case COMMIT_PARTIAL:
244 err = commit_lock_file(&index_lock);
245 rollback_lock_file(&false_lock);
246 break;
247 }
248
249 return err;
250 }
251
252 /*
253 * Take a union of paths in the index and the named tree (typically, "HEAD"),
254 * and return the paths that match the given pattern in list.
255 */
256 static int list_paths(struct string_list *list, const char *with_tree,
257 const struct pathspec *pattern)
258 {
259 int i, ret;
260 char *m;
261
262 if (!pattern->nr)
263 return 0;
264
265 m = xcalloc(1, pattern->nr);
266
267 if (with_tree) {
268 char *max_prefix = common_prefix(pattern);
269 overlay_tree_on_index(&the_index, with_tree, max_prefix);
270 free(max_prefix);
271 }
272
273 /* TODO: audit for interaction with sparse-index. */
274 ensure_full_index(&the_index);
275 for (i = 0; i < the_index.cache_nr; i++) {
276 const struct cache_entry *ce = the_index.cache[i];
277 struct string_list_item *item;
278
279 if (ce->ce_flags & CE_UPDATE)
280 continue;
281 if (!ce_path_match(&the_index, ce, pattern, m))
282 continue;
283 item = string_list_insert(list, ce->name);
284 if (ce_skip_worktree(ce))
285 item->util = item; /* better a valid pointer than a fake one */
286 }
287
288 ret = report_path_error(m, pattern);
289 free(m);
290 return ret;
291 }
292
293 static void add_remove_files(struct string_list *list)
294 {
295 int i;
296 for (i = 0; i < list->nr; i++) {
297 struct stat st;
298 struct string_list_item *p = &(list->items[i]);
299
300 /* p->util is skip-worktree */
301 if (p->util)
302 continue;
303
304 if (!lstat(p->string, &st)) {
305 if (add_to_index(&the_index, p->string, &st, 0))
306 die(_("updating files failed"));
307 } else
308 remove_file_from_index(&the_index, p->string);
309 }
310 }
311
312 static void create_base_index(const struct commit *current_head)
313 {
314 struct tree *tree;
315 struct unpack_trees_options opts;
316 struct tree_desc t;
317
318 if (!current_head) {
319 discard_index(&the_index);
320 return;
321 }
322
323 memset(&opts, 0, sizeof(opts));
324 opts.head_idx = 1;
325 opts.index_only = 1;
326 opts.merge = 1;
327 opts.src_index = &the_index;
328 opts.dst_index = &the_index;
329
330 opts.fn = oneway_merge;
331 tree = parse_tree_indirect(&current_head->object.oid);
332 if (!tree)
333 die(_("failed to unpack HEAD tree object"));
334 if (parse_tree(tree) < 0)
335 exit(128);
336 init_tree_desc(&t, &tree->object.oid, tree->buffer, tree->size);
337 if (unpack_trees(1, &t, &opts))
338 exit(128); /* We've already reported the error, finish dying */
339 }
340
341 static void refresh_cache_or_die(int refresh_flags)
342 {
343 /*
344 * refresh_flags contains REFRESH_QUIET, so the only errors
345 * are for unmerged entries.
346 */
347 if (refresh_index(&the_index, refresh_flags | REFRESH_IN_PORCELAIN, NULL, NULL, NULL))
348 die_resolve_conflict("commit");
349 }
350
351 static const char *prepare_index(const char **argv, const char *prefix,
352 const struct commit *current_head, int is_status)
353 {
354 struct string_list partial = STRING_LIST_INIT_DUP;
355 struct pathspec pathspec;
356 int refresh_flags = REFRESH_QUIET;
357 const char *ret;
358
359 if (is_status)
360 refresh_flags |= REFRESH_UNMERGED;
361 parse_pathspec(&pathspec, 0,
362 PATHSPEC_PREFER_FULL,
363 prefix, argv);
364
365 if (pathspec_from_file) {
366 if (interactive)
367 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
368
369 if (all)
370 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "-a");
371
372 if (pathspec.nr)
373 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
374
375 parse_pathspec_file(&pathspec, 0,
376 PATHSPEC_PREFER_FULL,
377 prefix, pathspec_from_file, pathspec_file_nul);
378 } else if (pathspec_file_nul) {
379 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
380 }
381
382 if (!pathspec.nr && (also || (only && !allow_empty &&
383 (!amend || (fixup_message && strcmp(fixup_prefix, "amend"))))))
384 die(_("No paths with --include/--only does not make sense."));
385
386 if (repo_read_index_preload(the_repository, &pathspec, 0) < 0)
387 die(_("index file corrupt"));
388
389 if (interactive) {
390 char *old_index_env = NULL, *old_repo_index_file;
391 repo_hold_locked_index(the_repository, &index_lock,
392 LOCK_DIE_ON_ERROR);
393
394 refresh_cache_or_die(refresh_flags);
395
396 if (write_locked_index(&the_index, &index_lock, 0))
397 die(_("unable to create temporary index"));
398
399 old_repo_index_file = the_repository->index_file;
400 the_repository->index_file =
401 (char *)get_lock_file_path(&index_lock);
402 old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
403 setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
404
405 if (interactive_add(argv, prefix, patch_interactive) != 0)
406 die(_("interactive add failed"));
407
408 the_repository->index_file = old_repo_index_file;
409 if (old_index_env && *old_index_env)
410 setenv(INDEX_ENVIRONMENT, old_index_env, 1);
411 else
412 unsetenv(INDEX_ENVIRONMENT);
413 FREE_AND_NULL(old_index_env);
414
415 discard_index(&the_index);
416 read_index_from(&the_index, get_lock_file_path(&index_lock),
417 get_git_dir());
418 if (cache_tree_update(&the_index, WRITE_TREE_SILENT) == 0) {
419 if (reopen_lock_file(&index_lock) < 0)
420 die(_("unable to write index file"));
421 if (write_locked_index(&the_index, &index_lock, 0))
422 die(_("unable to update temporary index"));
423 } else
424 warning(_("Failed to update main cache tree"));
425
426 commit_style = COMMIT_NORMAL;
427 ret = get_lock_file_path(&index_lock);
428 goto out;
429 }
430
431 /*
432 * Non partial, non as-is commit.
433 *
434 * (1) get the real index;
435 * (2) update the_index as necessary;
436 * (3) write the_index out to the real index (still locked);
437 * (4) return the name of the locked index file.
438 *
439 * The caller should run hooks on the locked real index, and
440 * (A) if all goes well, commit the real index;
441 * (B) on failure, rollback the real index.
442 */
443 if (all || (also && pathspec.nr)) {
444 repo_hold_locked_index(the_repository, &index_lock,
445 LOCK_DIE_ON_ERROR);
446 add_files_to_cache(the_repository, also ? prefix : NULL,
447 &pathspec, 0, 0);
448 refresh_cache_or_die(refresh_flags);
449 cache_tree_update(&the_index, WRITE_TREE_SILENT);
450 if (write_locked_index(&the_index, &index_lock, 0))
451 die(_("unable to write new index file"));
452 commit_style = COMMIT_NORMAL;
453 ret = get_lock_file_path(&index_lock);
454 goto out;
455 }
456
457 /*
458 * As-is commit.
459 *
460 * (1) return the name of the real index file.
461 *
462 * The caller should run hooks on the real index,
463 * and create commit from the_index.
464 * We still need to refresh the index here.
465 */
466 if (!only && !pathspec.nr) {
467 repo_hold_locked_index(the_repository, &index_lock,
468 LOCK_DIE_ON_ERROR);
469 refresh_cache_or_die(refresh_flags);
470 if (the_index.cache_changed
471 || !cache_tree_fully_valid(the_index.cache_tree))
472 cache_tree_update(&the_index, WRITE_TREE_SILENT);
473 if (write_locked_index(&the_index, &index_lock,
474 COMMIT_LOCK | SKIP_IF_UNCHANGED))
475 die(_("unable to write new index file"));
476 commit_style = COMMIT_AS_IS;
477 ret = get_index_file();
478 goto out;
479 }
480
481 /*
482 * A partial commit.
483 *
484 * (0) find the set of affected paths;
485 * (1) get lock on the real index file;
486 * (2) update the_index with the given paths;
487 * (3) write the_index out to the real index (still locked);
488 * (4) get lock on the false index file;
489 * (5) reset the_index from HEAD;
490 * (6) update the_index the same way as (2);
491 * (7) write the_index out to the false index file;
492 * (8) return the name of the false index file (still locked);
493 *
494 * The caller should run hooks on the locked false index, and
495 * create commit from it. Then
496 * (A) if all goes well, commit the real index;
497 * (B) on failure, rollback the real index;
498 * In either case, rollback the false index.
499 */
500 commit_style = COMMIT_PARTIAL;
501
502 if (whence != FROM_COMMIT) {
503 if (whence == FROM_MERGE)
504 die(_("cannot do a partial commit during a merge."));
505 else if (is_from_cherry_pick(whence))
506 die(_("cannot do a partial commit during a cherry-pick."));
507 else if (is_from_rebase(whence))
508 die(_("cannot do a partial commit during a rebase."));
509 }
510
511 if (list_paths(&partial, !current_head ? NULL : "HEAD", &pathspec))
512 exit(1);
513
514 discard_index(&the_index);
515 if (repo_read_index(the_repository) < 0)
516 die(_("cannot read the index"));
517
518 repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);
519 add_remove_files(&partial);
520 refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
521 cache_tree_update(&the_index, WRITE_TREE_SILENT);
522 if (write_locked_index(&the_index, &index_lock, 0))
523 die(_("unable to write new index file"));
524
525 hold_lock_file_for_update(&false_lock,
526 git_path("next-index-%"PRIuMAX,
527 (uintmax_t) getpid()),
528 LOCK_DIE_ON_ERROR);
529
530 create_base_index(current_head);
531 add_remove_files(&partial);
532 refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
533
534 if (write_locked_index(&the_index, &false_lock, 0))
535 die(_("unable to write temporary index file"));
536
537 discard_index(&the_index);
538 ret = get_lock_file_path(&false_lock);
539 read_index_from(&the_index, ret, get_git_dir());
540 out:
541 string_list_clear(&partial, 0);
542 clear_pathspec(&pathspec);
543 return ret;
544 }
545
546 static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
547 struct wt_status *s)
548 {
549 struct object_id oid;
550
551 if (s->relative_paths)
552 s->prefix = prefix;
553
554 if (amend) {
555 s->amend = 1;
556 s->reference = "HEAD^1";
557 }
558 s->verbose = verbose;
559 s->index_file = index_file;
560 s->fp = fp;
561 s->nowarn = nowarn;
562 s->is_initial = repo_get_oid(the_repository, s->reference, &oid) ? 1 : 0;
563 if (!s->is_initial)
564 oidcpy(&s->oid_commit, &oid);
565 s->status_format = status_format;
566 s->ignore_submodule_arg = ignore_submodule_arg;
567
568 wt_status_collect(s);
569 wt_status_print(s);
570 wt_status_collect_free_buffers(s);
571
572 return s->committable;
573 }
574
575 static int is_a_merge(const struct commit *current_head)
576 {
577 return !!(current_head->parents && current_head->parents->next);
578 }
579
580 static void assert_split_ident(struct ident_split *id, const struct strbuf *buf)
581 {
582 if (split_ident_line(id, buf->buf, buf->len) || !id->date_begin)
583 BUG("unable to parse our own ident: %s", buf->buf);
584 }
585
586 static void export_one(const char *var, const char *s, const char *e, int hack)
587 {
588 struct strbuf buf = STRBUF_INIT;
589 if (hack)
590 strbuf_addch(&buf, hack);
591 strbuf_add(&buf, s, e - s);
592 setenv(var, buf.buf, 1);
593 strbuf_release(&buf);
594 }
595
596 static int parse_force_date(const char *in, struct strbuf *out)
597 {
598 strbuf_addch(out, '@');
599
600 if (parse_date(in, out) < 0) {
601 int errors = 0;
602 unsigned long t = approxidate_careful(in, &errors);
603 if (errors)
604 return -1;
605 strbuf_addf(out, "%lu", t);
606 }
607
608 return 0;
609 }
610
611 static void set_ident_var(char **buf, char *val)
612 {
613 free(*buf);
614 *buf = val;
615 }
616
617 static void determine_author_info(struct strbuf *author_ident)
618 {
619 char *name, *email, *date;
620 struct ident_split author;
621
622 name = xstrdup_or_null(getenv("GIT_AUTHOR_NAME"));
623 email = xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL"));
624 date = xstrdup_or_null(getenv("GIT_AUTHOR_DATE"));
625
626 if (author_message) {
627 struct ident_split ident;
628 size_t len;
629 const char *a;
630
631 a = find_commit_header(author_message_buffer, "author", &len);
632 if (!a)
633 die(_("commit '%s' lacks author header"), author_message);
634 if (split_ident_line(&ident, a, len) < 0)
635 die(_("commit '%s' has malformed author line"), author_message);
636
637 set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
638 set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
639
640 if (ident.date_begin) {
641 struct strbuf date_buf = STRBUF_INIT;
642 strbuf_addch(&date_buf, '@');
643 strbuf_add(&date_buf, ident.date_begin, ident.date_end - ident.date_begin);
644 strbuf_addch(&date_buf, ' ');
645 strbuf_add(&date_buf, ident.tz_begin, ident.tz_end - ident.tz_begin);
646 set_ident_var(&date, strbuf_detach(&date_buf, NULL));
647 }
648 }
649
650 if (force_author) {
651 struct ident_split ident;
652
653 if (split_ident_line(&ident, force_author, strlen(force_author)) < 0)
654 die(_("malformed --author parameter"));
655 set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
656 set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
657 }
658
659 if (force_date) {
660 struct strbuf date_buf = STRBUF_INIT;
661 if (parse_force_date(force_date, &date_buf))
662 die(_("invalid date format: %s"), force_date);
663 set_ident_var(&date, strbuf_detach(&date_buf, NULL));
664 }
665
666 strbuf_addstr(author_ident, fmt_ident(name, email, WANT_AUTHOR_IDENT, date,
667 IDENT_STRICT));
668 assert_split_ident(&author, author_ident);
669 export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
670 export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
671 export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@');
672 free(name);
673 free(email);
674 free(date);
675 }
676
677 static int author_date_is_interesting(void)
678 {
679 return author_message || force_date;
680 }
681
682 static void adjust_comment_line_char(const struct strbuf *sb)
683 {
684 char candidates[] = "#;@!$%^&|:";
685 char *candidate;
686 const char *p;
687
688 if (!memchr(sb->buf, candidates[0], sb->len)) {
689 comment_line_str = xstrfmt("%c", candidates[0]);
690 return;
691 }
692
693 p = sb->buf;
694 candidate = strchr(candidates, *p);
695 if (candidate)
696 *candidate = ' ';
697 for (p = sb->buf; *p; p++) {
698 if ((p[0] == '\n' || p[0] == '\r') && p[1]) {
699 candidate = strchr(candidates, p[1]);
700 if (candidate)
701 *candidate = ' ';
702 }
703 }
704
705 for (p = candidates; *p == ' '; p++)
706 ;
707 if (!*p)
708 die(_("unable to select a comment character that is not used\n"
709 "in the current commit message"));
710 comment_line_str = xstrfmt("%c", *p);
711 }
712
713 static void prepare_amend_commit(struct commit *commit, struct strbuf *sb,
714 struct pretty_print_context *ctx)
715 {
716 const char *buffer, *subject, *fmt;
717
718 buffer = repo_get_commit_buffer(the_repository, commit, NULL);
719 find_commit_subject(buffer, &subject);
720 /*
721 * If we amend the 'amend!' commit then we don't want to
722 * duplicate the subject line.
723 */
724 fmt = starts_with(subject, "amend!") ? "%b" : "%B";
725 repo_format_commit_message(the_repository, commit, fmt, sb, ctx);
726 repo_unuse_commit_buffer(the_repository, commit, buffer);
727 }
728
729 static int prepare_to_commit(const char *index_file, const char *prefix,
730 struct commit *current_head,
731 struct wt_status *s,
732 struct strbuf *author_ident)
733 {
734 struct stat statbuf;
735 struct strbuf committer_ident = STRBUF_INIT;
736 int committable;
737 struct strbuf sb = STRBUF_INIT;
738 const char *hook_arg1 = NULL;
739 const char *hook_arg2 = NULL;
740 int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE);
741 int old_display_comment_prefix;
742 int invoked_hook;
743
744 /* This checks and barfs if author is badly specified */
745 determine_author_info(author_ident);
746
747 if (!no_verify && run_commit_hook(use_editor, index_file, &invoked_hook,
748 "pre-commit", NULL))
749 return 0;
750
751 if (squash_message) {
752 /*
753 * Insert the proper subject line before other commit
754 * message options add their content.
755 */
756 if (use_message && !strcmp(use_message, squash_message))
757 strbuf_addstr(&sb, "squash! ");
758 else {
759 struct pretty_print_context ctx = {0};
760 struct commit *c;
761 c = lookup_commit_reference_by_name(squash_message);
762 if (!c)
763 die(_("could not lookup commit '%s'"), squash_message);
764 ctx.output_encoding = get_commit_output_encoding();
765 repo_format_commit_message(the_repository, c,
766 "squash! %s\n\n", &sb,
767 &ctx);
768 }
769 }
770
771 if (have_option_m && !fixup_message) {
772 strbuf_addbuf(&sb, &message);
773 hook_arg1 = "message";
774 } else if (logfile && !strcmp(logfile, "-")) {
775 if (isatty(0))
776 fprintf(stderr, _("(reading log message from standard input)\n"));
777 if (strbuf_read(&sb, 0, 0) < 0)
778 die_errno(_("could not read log from standard input"));
779 hook_arg1 = "message";
780 } else if (logfile) {
781 if (strbuf_read_file(&sb, logfile, 0) < 0)
782 die_errno(_("could not read log file '%s'"),
783 logfile);
784 hook_arg1 = "message";
785 } else if (use_message) {
786 char *buffer;
787 buffer = strstr(use_message_buffer, "\n\n");
788 if (buffer)
789 strbuf_addstr(&sb, skip_blank_lines(buffer + 2));
790 hook_arg1 = "commit";
791 hook_arg2 = use_message;
792 } else if (fixup_message) {
793 struct pretty_print_context ctx = {0};
794 struct commit *commit;
795 char *fmt;
796 commit = lookup_commit_reference_by_name(fixup_commit);
797 if (!commit)
798 die(_("could not lookup commit '%s'"), fixup_commit);
799 ctx.output_encoding = get_commit_output_encoding();
800 fmt = xstrfmt("%s! %%s\n\n", fixup_prefix);
801 repo_format_commit_message(the_repository, commit, fmt, &sb,
802 &ctx);
803 free(fmt);
804 hook_arg1 = "message";
805
806 /*
807 * Only `-m` commit message option is checked here, as
808 * it supports `--fixup` to append the commit message.
809 *
810 * The other commit message options `-c`/`-C`/`-F` are
811 * incompatible with all the forms of `--fixup` and
812 * have already errored out while parsing the `git commit`
813 * options.
814 */
815 if (have_option_m && !strcmp(fixup_prefix, "fixup"))
816 strbuf_addbuf(&sb, &message);
817
818 if (!strcmp(fixup_prefix, "amend")) {
819 if (have_option_m)
820 die(_("options '%s' and '%s:%s' cannot be used together"), "-m", "--fixup", fixup_message);
821 prepare_amend_commit(commit, &sb, &ctx);
822 }
823 } else if (!stat(git_path_merge_msg(the_repository), &statbuf)) {
824 size_t merge_msg_start;
825
826 /*
827 * prepend SQUASH_MSG here if it exists and a
828 * "merge --squash" was originally performed
829 */
830 if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
831 if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
832 die_errno(_("could not read SQUASH_MSG"));
833 hook_arg1 = "squash";
834 } else
835 hook_arg1 = "merge";
836
837 merge_msg_start = sb.len;
838 if (strbuf_read_file(&sb, git_path_merge_msg(the_repository), 0) < 0)
839 die_errno(_("could not read MERGE_MSG"));
840
841 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
842 wt_status_locate_end(sb.buf + merge_msg_start,
843 sb.len - merge_msg_start) <
844 sb.len - merge_msg_start)
845 s->added_cut_line = 1;
846 } else if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
847 if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
848 die_errno(_("could not read SQUASH_MSG"));
849 hook_arg1 = "squash";
850 } else if (template_file) {
851 if (strbuf_read_file(&sb, template_file, 0) < 0)
852 die_errno(_("could not read '%s'"), template_file);
853 hook_arg1 = "template";
854 clean_message_contents = 0;
855 }
856
857 /*
858 * The remaining cases don't modify the template message, but
859 * just set the argument(s) to the prepare-commit-msg hook.
860 */
861 else if (whence == FROM_MERGE)
862 hook_arg1 = "merge";
863 else if (is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) {
864 hook_arg1 = "commit";
865 hook_arg2 = "CHERRY_PICK_HEAD";
866 }
867
868 if (squash_message) {
869 /*
870 * If squash_commit was used for the commit subject,
871 * then we're possibly hijacking other commit log options.
872 * Reset the hook args to tell the real story.
873 */
874 hook_arg1 = "message";
875 hook_arg2 = "";
876 }
877
878 s->fp = fopen_for_writing(git_path_commit_editmsg());
879 if (!s->fp)
880 die_errno(_("could not open '%s'"), git_path_commit_editmsg());
881
882 /* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */
883 old_display_comment_prefix = s->display_comment_prefix;
884 s->display_comment_prefix = 1;
885
886 /*
887 * Most hints are counter-productive when the commit has
888 * already started.
889 */
890 s->hints = 0;
891
892 if (clean_message_contents)
893 strbuf_stripspace(&sb, NULL);
894
895 if (signoff)
896 append_signoff(&sb, ignored_log_message_bytes(sb.buf, sb.len), 0);
897
898 if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
899 die_errno(_("could not write commit template"));
900
901 if (auto_comment_line_char)
902 adjust_comment_line_char(&sb);
903 strbuf_release(&sb);
904
905 /* This checks if committer ident is explicitly given */
906 strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
907 if (use_editor && include_status) {
908 int ident_shown = 0;
909 int saved_color_setting;
910 struct ident_split ci, ai;
911 const char *hint_cleanup_all = allow_empty_message ?
912 _("Please enter the commit message for your changes."
913 " Lines starting\nwith '%s' will be ignored.\n") :
914 _("Please enter the commit message for your changes."
915 " Lines starting\nwith '%s' will be ignored, and an empty"
916 " message aborts the commit.\n");
917 const char *hint_cleanup_space = allow_empty_message ?
918 _("Please enter the commit message for your changes."
919 " Lines starting\n"
920 "with '%s' will be kept; you may remove them"
921 " yourself if you want to.\n") :
922 _("Please enter the commit message for your changes."
923 " Lines starting\n"
924 "with '%s' will be kept; you may remove them"
925 " yourself if you want to.\n"
926 "An empty message aborts the commit.\n");
927 if (whence != FROM_COMMIT) {
928 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
929 wt_status_add_cut_line(s);
930 status_printf_ln(
931 s, GIT_COLOR_NORMAL,
932 whence == FROM_MERGE ?
933 _("\n"
934 "It looks like you may be committing a merge.\n"
935 "If this is not correct, please run\n"
936 " git update-ref -d MERGE_HEAD\n"
937 "and try again.\n") :
938 _("\n"
939 "It looks like you may be committing a cherry-pick.\n"
940 "If this is not correct, please run\n"
941 " git update-ref -d CHERRY_PICK_HEAD\n"
942 "and try again.\n"));
943 }
944
945 fprintf(s->fp, "\n");
946 if (cleanup_mode == COMMIT_MSG_CLEANUP_ALL)
947 status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_all, comment_line_str);
948 else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
949 if (whence == FROM_COMMIT)
950 wt_status_add_cut_line(s);
951 } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
952 status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_space, comment_line_str);
953
954 /*
955 * These should never fail because they come from our own
956 * fmt_ident. They may fail the sane_ident test, but we know
957 * that the name and mail pointers will at least be valid,
958 * which is enough for our tests and printing here.
959 */
960 assert_split_ident(&ai, author_ident);
961 assert_split_ident(&ci, &committer_ident);
962
963 if (ident_cmp(&ai, &ci))
964 status_printf_ln(s, GIT_COLOR_NORMAL,
965 _("%s"
966 "Author: %.*s <%.*s>"),
967 ident_shown++ ? "" : "\n",
968 (int)(ai.name_end - ai.name_begin), ai.name_begin,
969 (int)(ai.mail_end - ai.mail_begin), ai.mail_begin);
970
971 if (author_date_is_interesting())
972 status_printf_ln(s, GIT_COLOR_NORMAL,
973 _("%s"
974 "Date: %s"),
975 ident_shown++ ? "" : "\n",
976 show_ident_date(&ai, DATE_MODE(NORMAL)));
977
978 if (!committer_ident_sufficiently_given())
979 status_printf_ln(s, GIT_COLOR_NORMAL,
980 _("%s"
981 "Committer: %.*s <%.*s>"),
982 ident_shown++ ? "" : "\n",
983 (int)(ci.name_end - ci.name_begin), ci.name_begin,
984 (int)(ci.mail_end - ci.mail_begin), ci.mail_begin);
985
986 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); /* Add new line for clarity */
987
988 saved_color_setting = s->use_color;
989 s->use_color = 0;
990 committable = run_status(s->fp, index_file, prefix, 1, s);
991 s->use_color = saved_color_setting;
992 string_list_clear(&s->change, 1);
993 } else {
994 struct object_id oid;
995 const char *parent = "HEAD";
996
997 if (!the_index.initialized && repo_read_index(the_repository) < 0)
998 die(_("Cannot read index"));
999
1000 if (amend)
1001 parent = "HEAD^1";
1002
1003 if (repo_get_oid(the_repository, parent, &oid)) {
1004 int i, ita_nr = 0;
1005
1006 /* TODO: audit for interaction with sparse-index. */
1007 ensure_full_index(&the_index);
1008 for (i = 0; i < the_index.cache_nr; i++)
1009 if (ce_intent_to_add(the_index.cache[i]))
1010 ita_nr++;
1011 committable = the_index.cache_nr - ita_nr > 0;
1012 } else {
1013 /*
1014 * Unless the user did explicitly request a submodule
1015 * ignore mode by passing a command line option we do
1016 * not ignore any changed submodule SHA-1s when
1017 * comparing index and parent, no matter what is
1018 * configured. Otherwise we won't commit any
1019 * submodules which were manually staged, which would
1020 * be really confusing.
1021 */
1022 struct diff_flags flags = DIFF_FLAGS_INIT;
1023 flags.override_submodule_config = 1;
1024 if (ignore_submodule_arg &&
1025 !strcmp(ignore_submodule_arg, "all"))
1026 flags.ignore_submodules = 1;
1027 committable = index_differs_from(the_repository,
1028 parent, &flags, 1);
1029 }
1030 }
1031 strbuf_release(&committer_ident);
1032
1033 fclose(s->fp);
1034
1035 if (trailer_args.nr) {
1036 struct child_process run_trailer = CHILD_PROCESS_INIT;
1037
1038 strvec_pushl(&run_trailer.args, "interpret-trailers",
1039 "--in-place", "--no-divider",
1040 git_path_commit_editmsg(), NULL);
1041 strvec_pushv(&run_trailer.args, trailer_args.v);
1042 run_trailer.git_cmd = 1;
1043 if (run_command(&run_trailer))
1044 die(_("unable to pass trailers to --trailers"));
1045 strvec_clear(&trailer_args);
1046 }
1047
1048 /*
1049 * Reject an attempt to record a non-merge empty commit without
1050 * explicit --allow-empty. In the cherry-pick case, it may be
1051 * empty due to conflict resolution, which the user should okay.
1052 */
1053 if (!committable && whence != FROM_MERGE && !allow_empty &&
1054 !(amend && is_a_merge(current_head))) {
1055 s->hints = advice_enabled(ADVICE_STATUS_HINTS);
1056 s->display_comment_prefix = old_display_comment_prefix;
1057 run_status(stdout, index_file, prefix, 0, s);
1058 if (amend)
1059 fputs(_(empty_amend_advice), stderr);
1060 else if (is_from_cherry_pick(whence) ||
1061 whence == FROM_REBASE_PICK) {
1062 fputs(_(empty_cherry_pick_advice), stderr);
1063 if (whence == FROM_CHERRY_PICK_SINGLE)
1064 fputs(_(empty_cherry_pick_advice_single), stderr);
1065 else if (whence == FROM_CHERRY_PICK_MULTI)
1066 fputs(_(empty_cherry_pick_advice_multi), stderr);
1067 else
1068 fputs(_(empty_rebase_pick_advice), stderr);
1069 }
1070 return 0;
1071 }
1072
1073 if (!no_verify && invoked_hook) {
1074 /*
1075 * Re-read the index as the pre-commit-commit hook was invoked
1076 * and could have updated it. We must do this before we invoke
1077 * the editor and after we invoke run_status above.
1078 */
1079 discard_index(&the_index);
1080 }
1081 read_index_from(&the_index, index_file, get_git_dir());
1082
1083 if (cache_tree_update(&the_index, 0)) {
1084 error(_("Error building trees"));
1085 return 0;
1086 }
1087
1088 if (run_commit_hook(use_editor, index_file, NULL, "prepare-commit-msg",
1089 git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL))
1090 return 0;
1091
1092 if (use_editor) {
1093 struct strvec env = STRVEC_INIT;
1094
1095 strvec_pushf(&env, "GIT_INDEX_FILE=%s", index_file);
1096 if (launch_editor(git_path_commit_editmsg(), NULL, env.v)) {
1097 fprintf(stderr,
1098 _("Please supply the message using either -m or -F option.\n"));
1099 exit(1);
1100 }
1101 strvec_clear(&env);
1102 }
1103
1104 if (!no_verify &&
1105 run_commit_hook(use_editor, index_file, NULL, "commit-msg",
1106 git_path_commit_editmsg(), NULL)) {
1107 return 0;
1108 }
1109
1110 return 1;
1111 }
1112
1113 static const char *find_author_by_nickname(const char *name)
1114 {
1115 struct rev_info revs;
1116 struct commit *commit;
1117 struct strbuf buf = STRBUF_INIT;
1118 const char *av[20];
1119 int ac = 0;
1120
1121 repo_init_revisions(the_repository, &revs, NULL);
1122 strbuf_addf(&buf, "--author=%s", name);
1123 av[++ac] = "--all";
1124 av[++ac] = "-i";
1125 av[++ac] = buf.buf;
1126 av[++ac] = NULL;
1127 setup_revisions(ac, av, &revs, NULL);
1128 revs.mailmap = xmalloc(sizeof(struct string_list));
1129 string_list_init_nodup(revs.mailmap);
1130 read_mailmap(revs.mailmap);
1131
1132 if (prepare_revision_walk(&revs))
1133 die(_("revision walk setup failed"));
1134 commit = get_revision(&revs);
1135 if (commit) {
1136 struct pretty_print_context ctx = {0};
1137 ctx.date_mode.type = DATE_NORMAL;
1138 strbuf_release(&buf);
1139 repo_format_commit_message(the_repository, commit,
1140 "%aN <%aE>", &buf, &ctx);
1141 release_revisions(&revs);
1142 return strbuf_detach(&buf, NULL);
1143 }
1144 die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name);
1145 }
1146
1147 static void handle_ignored_arg(struct wt_status *s)
1148 {
1149 if (!ignored_arg)
1150 ; /* default already initialized */
1151 else if (!strcmp(ignored_arg, "traditional"))
1152 s->show_ignored_mode = SHOW_TRADITIONAL_IGNORED;
1153 else if (!strcmp(ignored_arg, "no"))
1154 s->show_ignored_mode = SHOW_NO_IGNORED;
1155 else if (!strcmp(ignored_arg, "matching"))
1156 s->show_ignored_mode = SHOW_MATCHING_IGNORED;
1157 else
1158 die(_("Invalid ignored mode '%s'"), ignored_arg);
1159 }
1160
1161 static enum untracked_status_type parse_untracked_setting_name(const char *u)
1162 {
1163 /*
1164 * Please update $__git_untracked_file_modes in
1165 * git-completion.bash when you add new options
1166 */
1167 switch (git_parse_maybe_bool(u)) {
1168 case 0:
1169 u = "no";
1170 break;
1171 case 1:
1172 u = "normal";
1173 break;
1174 default:
1175 break;
1176 }
1177
1178 if (!strcmp(u, "no"))
1179 return SHOW_NO_UNTRACKED_FILES;
1180 else if (!strcmp(u, "normal"))
1181 return SHOW_NORMAL_UNTRACKED_FILES;
1182 else if (!strcmp(u, "all"))
1183 return SHOW_ALL_UNTRACKED_FILES;
1184 else
1185 return SHOW_UNTRACKED_FILES_ERROR;
1186 }
1187
1188 static void handle_untracked_files_arg(struct wt_status *s)
1189 {
1190 enum untracked_status_type u;
1191
1192 if (!untracked_files_arg)
1193 return; /* default already initialized */
1194
1195 u = parse_untracked_setting_name(untracked_files_arg);
1196 if (u == SHOW_UNTRACKED_FILES_ERROR)
1197 die(_("Invalid untracked files mode '%s'"),
1198 untracked_files_arg);
1199 s->show_untracked_files = u;
1200 }
1201
1202 static const char *read_commit_message(const char *name)
1203 {
1204 const char *out_enc;
1205 struct commit *commit;
1206
1207 commit = lookup_commit_reference_by_name(name);
1208 if (!commit)
1209 die(_("could not lookup commit '%s'"), name);
1210 out_enc = get_commit_output_encoding();
1211 return repo_logmsg_reencode(the_repository, commit, NULL, out_enc);
1212 }
1213
1214 /*
1215 * Enumerate what needs to be propagated when --porcelain
1216 * is not in effect here.
1217 */
1218 static struct status_deferred_config {
1219 enum wt_status_format status_format;
1220 int show_branch;
1221 enum ahead_behind_flags ahead_behind;
1222 } status_deferred_config = {
1223 STATUS_FORMAT_UNSPECIFIED,
1224 -1, /* unspecified */
1225 AHEAD_BEHIND_UNSPECIFIED,
1226 };
1227
1228 static void finalize_deferred_config(struct wt_status *s)
1229 {
1230 int use_deferred_config = (status_format != STATUS_FORMAT_PORCELAIN &&
1231 status_format != STATUS_FORMAT_PORCELAIN_V2 &&
1232 !s->null_termination);
1233
1234 if (s->null_termination) {
1235 if (status_format == STATUS_FORMAT_NONE ||
1236 status_format == STATUS_FORMAT_UNSPECIFIED)
1237 status_format = STATUS_FORMAT_PORCELAIN;
1238 else if (status_format == STATUS_FORMAT_LONG)
1239 die(_("options '%s' and '%s' cannot be used together"), "--long", "-z");
1240 }
1241
1242 if (use_deferred_config && status_format == STATUS_FORMAT_UNSPECIFIED)
1243 status_format = status_deferred_config.status_format;
1244 if (status_format == STATUS_FORMAT_UNSPECIFIED)
1245 status_format = STATUS_FORMAT_NONE;
1246
1247 if (use_deferred_config && s->show_branch < 0)
1248 s->show_branch = status_deferred_config.show_branch;
1249 if (s->show_branch < 0)
1250 s->show_branch = 0;
1251
1252 /*
1253 * If the user did not give a "--[no]-ahead-behind" command
1254 * line argument *AND* we will print in a human-readable format
1255 * (short, long etc.) then we inherit from the status.aheadbehind
1256 * config setting. In all other cases (and porcelain V[12] formats
1257 * in particular), we inherit _FULL for backwards compatibility.
1258 */
1259 if (use_deferred_config &&
1260 s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
1261 s->ahead_behind_flags = status_deferred_config.ahead_behind;
1262
1263 if (s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
1264 s->ahead_behind_flags = AHEAD_BEHIND_FULL;
1265 }
1266
1267 static void check_fixup_reword_options(int argc, const char *argv[]) {
1268 if (whence != FROM_COMMIT) {
1269 if (whence == FROM_MERGE)
1270 die(_("You are in the middle of a merge -- cannot reword."));
1271 else if (is_from_cherry_pick(whence))
1272 die(_("You are in the middle of a cherry-pick -- cannot reword."));
1273 }
1274 if (argc)
1275 die(_("reword option of '%s' and path '%s' cannot be used together"), "--fixup", *argv);
1276 if (patch_interactive || interactive || all || also || only)
1277 die(_("reword option of '%s' and '%s' cannot be used together"),
1278 "--fixup", "--patch/--interactive/--all/--include/--only");
1279 }
1280
1281 static int parse_and_validate_options(int argc, const char *argv[],
1282 const struct option *options,
1283 const char * const usage[],
1284 const char *prefix,
1285 struct commit *current_head,
1286 struct wt_status *s)
1287 {
1288 argc = parse_options(argc, argv, prefix, options, usage, 0);
1289 finalize_deferred_config(s);
1290
1291 if (force_author && !strchr(force_author, '>'))
1292 force_author = find_author_by_nickname(force_author);
1293
1294 if (force_author && renew_authorship)
1295 die(_("options '%s' and '%s' cannot be used together"), "--reset-author", "--author");
1296
1297 if (logfile || have_option_m || use_message)
1298 use_editor = 0;
1299
1300 /* Sanity check options */
1301 if (amend && !current_head)
1302 die(_("You have nothing to amend."));
1303 if (amend && whence != FROM_COMMIT) {
1304 if (whence == FROM_MERGE)
1305 die(_("You are in the middle of a merge -- cannot amend."));
1306 else if (is_from_cherry_pick(whence))
1307 die(_("You are in the middle of a cherry-pick -- cannot amend."));
1308 else if (whence == FROM_REBASE_PICK)
1309 die(_("You are in the middle of a rebase -- cannot amend."));
1310 }
1311 if (fixup_message && squash_message)
1312 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup");
1313 die_for_incompatible_opt4(!!use_message, "-C",
1314 !!edit_message, "-c",
1315 !!logfile, "-F",
1316 !!fixup_message, "--fixup");
1317 die_for_incompatible_opt4(have_option_m, "-m",
1318 !!edit_message, "-c",
1319 !!use_message, "-C",
1320 !!logfile, "-F");
1321 if (use_message || edit_message || logfile ||fixup_message || have_option_m)
1322 template_file = NULL;
1323 if (edit_message)
1324 use_message = edit_message;
1325 if (amend && !use_message && !fixup_message)
1326 use_message = "HEAD";
1327 if (!use_message && !is_from_cherry_pick(whence) &&
1328 !is_from_rebase(whence) && renew_authorship)
1329 die(_("--reset-author can be used only with -C, -c or --amend."));
1330 if (use_message) {
1331 use_message_buffer = read_commit_message(use_message);
1332 if (!renew_authorship) {
1333 author_message = use_message;
1334 author_message_buffer = use_message_buffer;
1335 }
1336 }
1337 if ((is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) &&
1338 !renew_authorship) {
1339 author_message = "CHERRY_PICK_HEAD";
1340 author_message_buffer = read_commit_message(author_message);
1341 }
1342
1343 if (patch_interactive)
1344 interactive = 1;
1345
1346 die_for_incompatible_opt4(also, "-i/--include",
1347 only, "-o/--only",
1348 all, "-a/--all",
1349 interactive, "--interactive/-p/--patch");
1350 if (fixup_message) {
1351 /*
1352 * We limit --fixup's suboptions to only alpha characters.
1353 * If the first character after a run of alpha is colon,
1354 * then the part before the colon may be a known suboption
1355 * name like `amend` or `reword`, or a misspelt suboption
1356 * name. In either case, we treat it as
1357 * --fixup=<suboption>:<arg>.
1358 *
1359 * Otherwise, we are dealing with --fixup=<commit>.
1360 */
1361 char *p = fixup_message;
1362 while (isalpha(*p))
1363 p++;
1364 if (p > fixup_message && *p == ':') {
1365 *p = '\0';
1366 fixup_commit = p + 1;
1367 if (!strcmp("amend", fixup_message) ||
1368 !strcmp("reword", fixup_message)) {
1369 fixup_prefix = "amend";
1370 allow_empty = 1;
1371 if (*fixup_message == 'r') {
1372 check_fixup_reword_options(argc, argv);
1373 only = 1;
1374 }
1375 } else {
1376 die(_("unknown option: --fixup=%s:%s"), fixup_message, fixup_commit);
1377 }
1378 } else {
1379 fixup_commit = fixup_message;
1380 fixup_prefix = "fixup";
1381 use_editor = 0;
1382 }
1383 }
1384
1385 if (0 <= edit_flag)
1386 use_editor = edit_flag;
1387
1388 cleanup_mode = get_cleanup_mode(cleanup_arg, use_editor);
1389
1390 handle_untracked_files_arg(s);
1391
1392 if (all && argc > 0)
1393 die(_("paths '%s ...' with -a does not make sense"),
1394 argv[0]);
1395
1396 if (status_format != STATUS_FORMAT_NONE)
1397 dry_run = 1;
1398
1399 return argc;
1400 }
1401
1402 static int dry_run_commit(const char **argv, const char *prefix,
1403 const struct commit *current_head, struct wt_status *s)
1404 {
1405 int committable;
1406 const char *index_file;
1407
1408 index_file = prepare_index(argv, prefix, current_head, 1);
1409 committable = run_status(stdout, index_file, prefix, 0, s);
1410 rollback_index_files();
1411
1412 return committable ? 0 : 1;
1413 }
1414
1415 define_list_config_array_extra(color_status_slots, {"added"});
1416
1417 static int parse_status_slot(const char *slot)
1418 {
1419 if (!strcasecmp(slot, "added"))
1420 return WT_STATUS_UPDATED;
1421
1422 return LOOKUP_CONFIG(color_status_slots, slot);
1423 }
1424
1425 static int git_status_config(const char *k, const char *v,
1426 const struct config_context *ctx, void *cb)
1427 {
1428 struct wt_status *s = cb;
1429 const char *slot_name;
1430
1431 if (starts_with(k, "column."))
1432 return git_column_config(k, v, "status", &s->colopts);
1433 if (!strcmp(k, "status.submodulesummary")) {
1434 int is_bool;
1435 s->submodule_summary = git_config_bool_or_int(k, v, ctx->kvi,
1436 &is_bool);
1437 if (is_bool && s->submodule_summary)
1438 s->submodule_summary = -1;
1439 return 0;
1440 }
1441 if (!strcmp(k, "status.short")) {
1442 if (git_config_bool(k, v))
1443 status_deferred_config.status_format = STATUS_FORMAT_SHORT;
1444 else
1445 status_deferred_config.status_format = STATUS_FORMAT_NONE;
1446 return 0;
1447 }
1448 if (!strcmp(k, "status.branch")) {
1449 status_deferred_config.show_branch = git_config_bool(k, v);
1450 return 0;
1451 }
1452 if (!strcmp(k, "status.aheadbehind")) {
1453 status_deferred_config.ahead_behind = git_config_bool(k, v);
1454 return 0;
1455 }
1456 if (!strcmp(k, "status.showstash")) {
1457 s->show_stash = git_config_bool(k, v);
1458 return 0;
1459 }
1460 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
1461 s->use_color = git_config_colorbool(k, v);
1462 return 0;
1463 }
1464 if (!strcmp(k, "status.displaycommentprefix")) {
1465 s->display_comment_prefix = git_config_bool(k, v);
1466 return 0;
1467 }
1468 if (skip_prefix(k, "status.color.", &slot_name) ||
1469 skip_prefix(k, "color.status.", &slot_name)) {
1470 int slot = parse_status_slot(slot_name);
1471 if (slot < 0)
1472 return 0;
1473 if (!v)
1474 return config_error_nonbool(k);
1475 return color_parse(v, s->color_palette[slot]);
1476 }
1477 if (!strcmp(k, "status.relativepaths")) {
1478 s->relative_paths = git_config_bool(k, v);
1479 return 0;
1480 }
1481 if (!strcmp(k, "status.showuntrackedfiles")) {
1482 enum untracked_status_type u;
1483
1484 u = parse_untracked_setting_name(v);
1485 if (u == SHOW_UNTRACKED_FILES_ERROR)
1486 return error(_("Invalid untracked files mode '%s'"), v);
1487 s->show_untracked_files = u;
1488 return 0;
1489 }
1490 if (!strcmp(k, "diff.renamelimit")) {
1491 if (s->rename_limit == -1)
1492 s->rename_limit = git_config_int(k, v, ctx->kvi);
1493 return 0;
1494 }
1495 if (!strcmp(k, "status.renamelimit")) {
1496 s->rename_limit = git_config_int(k, v, ctx->kvi);
1497 return 0;
1498 }
1499 if (!strcmp(k, "diff.renames")) {
1500 if (s->detect_rename == -1)
1501 s->detect_rename = git_config_rename(k, v);
1502 return 0;
1503 }
1504 if (!strcmp(k, "status.renames")) {
1505 s->detect_rename = git_config_rename(k, v);
1506 return 0;
1507 }
1508 return git_diff_ui_config(k, v, ctx, NULL);
1509 }
1510
1511 int cmd_status(int argc, const char **argv, const char *prefix)
1512 {
1513 static int no_renames = -1;
1514 static const char *rename_score_arg = (const char *)-1;
1515 static struct wt_status s;
1516 unsigned int progress_flag = 0;
1517 int fd;
1518 struct object_id oid;
1519 static struct option builtin_status_options[] = {
1520 OPT__VERBOSE(&verbose, N_("be verbose")),
1521 OPT_SET_INT('s', "short", &status_format,
1522 N_("show status concisely"), STATUS_FORMAT_SHORT),
1523 OPT_BOOL('b', "branch", &s.show_branch,
1524 N_("show branch information")),
1525 OPT_BOOL(0, "show-stash", &s.show_stash,
1526 N_("show stash information")),
1527 OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1528 N_("compute full ahead/behind values")),
1529 OPT_CALLBACK_F(0, "porcelain", &status_format,
1530 N_("version"), N_("machine-readable output"),
1531 PARSE_OPT_OPTARG, opt_parse_porcelain),
1532 OPT_SET_INT(0, "long", &status_format,
1533 N_("show status in long format (default)"),
1534 STATUS_FORMAT_LONG),
1535 OPT_BOOL('z', "null", &s.null_termination,
1536 N_("terminate entries with NUL")),
1537 { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
1538 N_("mode"),
1539 N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
1540 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
1541 { OPTION_STRING, 0, "ignored", &ignored_arg,
1542 N_("mode"),
1543 N_("show ignored files, optional modes: traditional, matching, no. (Default: traditional)"),
1544 PARSE_OPT_OPTARG, NULL, (intptr_t)"traditional" },
1545 { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, N_("when"),
1546 N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
1547 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
1548 OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")),
1549 OPT_BOOL(0, "no-renames", &no_renames, N_("do not detect renames")),
1550 OPT_CALLBACK_F('M', "find-renames", &rename_score_arg,
1551 N_("n"), N_("detect renames, optionally set similarity index"),
1552 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, opt_parse_rename_score),
1553 OPT_END(),
1554 };
1555
1556 if (argc == 2 && !strcmp(argv[1], "-h"))
1557 usage_with_options(builtin_status_usage, builtin_status_options);
1558
1559 prepare_repo_settings(the_repository);
1560 the_repository->settings.command_requires_full_index = 0;
1561
1562 status_init_config(&s, git_status_config);
1563 argc = parse_options(argc, argv, prefix,
1564 builtin_status_options,
1565 builtin_status_usage, 0);
1566 finalize_colopts(&s.colopts, -1);
1567 finalize_deferred_config(&s);
1568
1569 handle_untracked_files_arg(&s);
1570 handle_ignored_arg(&s);
1571
1572 if (s.show_ignored_mode == SHOW_MATCHING_IGNORED &&
1573 s.show_untracked_files == SHOW_NO_UNTRACKED_FILES)
1574 die(_("Unsupported combination of ignored and untracked-files arguments"));
1575
1576 parse_pathspec(&s.pathspec, 0,
1577 PATHSPEC_PREFER_FULL,
1578 prefix, argv);
1579
1580 if (status_format != STATUS_FORMAT_PORCELAIN &&
1581 status_format != STATUS_FORMAT_PORCELAIN_V2)
1582 progress_flag = REFRESH_PROGRESS;
1583 repo_read_index(the_repository);
1584 refresh_index(&the_index,
1585 REFRESH_QUIET|REFRESH_UNMERGED|progress_flag,
1586 &s.pathspec, NULL, NULL);
1587
1588 if (use_optional_locks())
1589 fd = repo_hold_locked_index(the_repository, &index_lock, 0);
1590 else
1591 fd = -1;
1592
1593 s.is_initial = repo_get_oid(the_repository, s.reference, &oid) ? 1 : 0;
1594 if (!s.is_initial)
1595 oidcpy(&s.oid_commit, &oid);
1596
1597 s.ignore_submodule_arg = ignore_submodule_arg;
1598 s.status_format = status_format;
1599 s.verbose = verbose;
1600 if (no_renames != -1)
1601 s.detect_rename = !no_renames;
1602 if ((intptr_t)rename_score_arg != -1) {
1603 if (s.detect_rename < DIFF_DETECT_RENAME)
1604 s.detect_rename = DIFF_DETECT_RENAME;
1605 if (rename_score_arg)
1606 s.rename_score = parse_rename_score(&rename_score_arg);
1607 }
1608
1609 wt_status_collect(&s);
1610
1611 if (0 <= fd)
1612 repo_update_index_if_able(the_repository, &index_lock);
1613
1614 if (s.relative_paths)
1615 s.prefix = prefix;
1616
1617 wt_status_print(&s);
1618 wt_status_collect_free_buffers(&s);
1619
1620 return 0;
1621 }
1622
1623 static int git_commit_config(const char *k, const char *v,
1624 const struct config_context *ctx, void *cb)
1625 {
1626 struct wt_status *s = cb;
1627
1628 if (!strcmp(k, "commit.template"))
1629 return git_config_pathname(&template_file, k, v);
1630 if (!strcmp(k, "commit.status")) {
1631 include_status = git_config_bool(k, v);
1632 return 0;
1633 }
1634 if (!strcmp(k, "commit.cleanup"))
1635 return git_config_string(&cleanup_arg, k, v);
1636 if (!strcmp(k, "commit.gpgsign")) {
1637 sign_commit = git_config_bool(k, v) ? "" : NULL;
1638 return 0;
1639 }
1640 if (!strcmp(k, "commit.verbose")) {
1641 int is_bool;
1642 config_commit_verbose = git_config_bool_or_int(k, v, ctx->kvi,
1643 &is_bool);
1644 return 0;
1645 }
1646
1647 return git_status_config(k, v, ctx, s);
1648 }
1649
1650 int cmd_commit(int argc, const char **argv, const char *prefix)
1651 {
1652 static struct wt_status s;
1653 static struct option builtin_commit_options[] = {
1654 OPT__QUIET(&quiet, N_("suppress summary after successful commit")),
1655 OPT__VERBOSE(&verbose, N_("show diff in commit message template")),
1656
1657 OPT_GROUP(N_("Commit message options")),
1658 OPT_FILENAME('F', "file", &logfile, N_("read message from file")),
1659 OPT_STRING(0, "author", &force_author, N_("author"), N_("override author for commit")),
1660 OPT_STRING(0, "date", &force_date, N_("date"), N_("override date for commit")),
1661 OPT_CALLBACK('m', "message", &message, N_("message"), N_("commit message"), opt_parse_m),
1662 OPT_STRING('c', "reedit-message", &edit_message, N_("commit"), N_("reuse and edit message from specified commit")),
1663 OPT_STRING('C', "reuse-message", &use_message, N_("commit"), N_("reuse message from specified commit")),
1664 /*
1665 * TRANSLATORS: Leave "[(amend|reword):]" as-is,
1666 * and only translate <commit>.
1667 */
1668 OPT_STRING(0, "fixup", &fixup_message, N_("[(amend|reword):]commit"), N_("use autosquash formatted message to fixup or amend/reword specified commit")),
1669 OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
1670 OPT_BOOL(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")),
1671 OPT_CALLBACK_F(0, "trailer", &trailer_args, N_("trailer"), N_("add custom trailer(s)"), PARSE_OPT_NONEG, opt_pass_trailer),
1672 OPT_BOOL('s', "signoff", &signoff, N_("add a Signed-off-by trailer")),
1673 OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
1674 OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),
1675 OPT_CLEANUP(&cleanup_arg),
1676 OPT_BOOL(0, "status", &include_status, N_("include status in commit message template")),
1677 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
1678 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
1679 /* end commit message options */
1680
1681 OPT_GROUP(N_("Commit contents options")),
1682 OPT_BOOL('a', "all", &all, N_("commit all changed files")),
1683 OPT_BOOL('i', "include", &also, N_("add specified files to index for commit")),
1684 OPT_BOOL(0, "interactive", &interactive, N_("interactively add files")),
1685 OPT_BOOL('p', "patch", &patch_interactive, N_("interactively add changes")),
1686 OPT_BOOL('o', "only", &only, N_("commit only specified files")),
1687 OPT_BOOL('n', "no-verify", &no_verify, N_("bypass pre-commit and commit-msg hooks")),
1688 OPT_BOOL(0, "dry-run", &dry_run, N_("show what would be committed")),
1689 OPT_SET_INT(0, "short", &status_format, N_("show status concisely"),
1690 STATUS_FORMAT_SHORT),
1691 OPT_BOOL(0, "branch", &s.show_branch, N_("show branch information")),
1692 OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1693 N_("compute full ahead/behind values")),
1694 OPT_SET_INT(0, "porcelain", &status_format,
1695 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN),
1696 OPT_SET_INT(0, "long", &status_format,
1697 N_("show status in long format (default)"),
1698 STATUS_FORMAT_LONG),
1699 OPT_BOOL('z', "null", &s.null_termination,
1700 N_("terminate entries with NUL")),
1701 OPT_BOOL(0, "amend", &amend, N_("amend previous commit")),
1702 OPT_BOOL(0, "no-post-rewrite", &no_post_rewrite, N_("bypass post-rewrite hook")),
1703 { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, N_("mode"), N_("show untracked files, optional modes: all, normal, no. (Default: all)"), PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
1704 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
1705 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
1706 /* end commit contents options */
1707
1708 OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty,
1709 N_("ok to record an empty change")),
1710 OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message,
1711 N_("ok to record a change with an empty message")),
1712
1713 OPT_END()
1714 };
1715
1716 struct strbuf sb = STRBUF_INIT;
1717 struct strbuf author_ident = STRBUF_INIT;
1718 const char *index_file, *reflog_msg;
1719 struct object_id oid;
1720 struct commit_list *parents = NULL;
1721 struct stat statbuf;
1722 struct commit *current_head = NULL;
1723 struct commit_extra_header *extra = NULL;
1724 struct strbuf err = STRBUF_INIT;
1725 int ret = 0;
1726
1727 if (argc == 2 && !strcmp(argv[1], "-h"))
1728 usage_with_options(builtin_commit_usage, builtin_commit_options);
1729
1730 prepare_repo_settings(the_repository);
1731 the_repository->settings.command_requires_full_index = 0;
1732
1733 status_init_config(&s, git_commit_config);
1734 s.commit_template = 1;
1735 status_format = STATUS_FORMAT_NONE; /* Ignore status.short */
1736 s.colopts = 0;
1737
1738 if (repo_get_oid(the_repository, "HEAD", &oid))
1739 current_head = NULL;
1740 else {
1741 current_head = lookup_commit_or_die(&oid, "HEAD");
1742 if (repo_parse_commit(the_repository, current_head))
1743 die(_("could not parse HEAD commit"));
1744 }
1745 verbose = -1; /* unspecified */
1746 argc = parse_and_validate_options(argc, argv, builtin_commit_options,
1747 builtin_commit_usage,
1748 prefix, current_head, &s);
1749 if (verbose == -1)
1750 verbose = (config_commit_verbose < 0) ? 0 : config_commit_verbose;
1751
1752 if (dry_run)
1753 return dry_run_commit(argv, prefix, current_head, &s);
1754 index_file = prepare_index(argv, prefix, current_head, 0);
1755
1756 /* Set up everything for writing the commit object. This includes
1757 running hooks, writing the trees, and interacting with the user. */
1758 if (!prepare_to_commit(index_file, prefix,
1759 current_head, &s, &author_ident)) {
1760 ret = 1;
1761 rollback_index_files();
1762 goto cleanup;
1763 }
1764
1765 /* Determine parents */
1766 reflog_msg = getenv("GIT_REFLOG_ACTION");
1767 if (!current_head) {
1768 if (!reflog_msg)
1769 reflog_msg = "commit (initial)";
1770 } else if (amend) {
1771 if (!reflog_msg)
1772 reflog_msg = "commit (amend)";
1773 parents = copy_commit_list(current_head->parents);
1774 } else if (whence == FROM_MERGE) {
1775 struct strbuf m = STRBUF_INIT;
1776 FILE *fp;
1777 int allow_fast_forward = 1;
1778 struct commit_list **pptr = &parents;
1779
1780 if (!reflog_msg)
1781 reflog_msg = "commit (merge)";
1782 pptr = commit_list_append(current_head, pptr);
1783 fp = xfopen(git_path_merge_head(the_repository), "r");
1784 while (strbuf_getline_lf(&m, fp) != EOF) {
1785 struct commit *parent;
1786
1787 parent = get_merge_parent(m.buf);
1788 if (!parent)
1789 die(_("Corrupt MERGE_HEAD file (%s)"), m.buf);
1790 pptr = commit_list_append(parent, pptr);
1791 }
1792 fclose(fp);
1793 strbuf_release(&m);
1794 if (!stat(git_path_merge_mode(the_repository), &statbuf)) {
1795 if (strbuf_read_file(&sb, git_path_merge_mode(the_repository), 0) < 0)
1796 die_errno(_("could not read MERGE_MODE"));
1797 if (!strcmp(sb.buf, "no-ff"))
1798 allow_fast_forward = 0;
1799 }
1800 if (allow_fast_forward)
1801 reduce_heads_replace(&parents);
1802 } else {
1803 if (!reflog_msg)
1804 reflog_msg = is_from_cherry_pick(whence)
1805 ? "commit (cherry-pick)"
1806 : is_from_rebase(whence)
1807 ? "commit (rebase)"
1808 : "commit";
1809 commit_list_insert(current_head, &parents);
1810 }
1811
1812 /* Finally, get the commit message */
1813 strbuf_reset(&sb);
1814 if (strbuf_read_file(&sb, git_path_commit_editmsg(), 0) < 0) {
1815 int saved_errno = errno;
1816 rollback_index_files();
1817 die(_("could not read commit message: %s"), strerror(saved_errno));
1818 }
1819
1820 cleanup_message(&sb, cleanup_mode, verbose);
1821
1822 if (message_is_empty(&sb, cleanup_mode) && !allow_empty_message) {
1823 rollback_index_files();
1824 fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
1825 exit(1);
1826 }
1827 if (template_untouched(&sb, template_file, cleanup_mode) && !allow_empty_message) {
1828 rollback_index_files();
1829 fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
1830 exit(1);
1831 }
1832
1833 if (fixup_message && starts_with(sb.buf, "amend! ") &&
1834 !allow_empty_message) {
1835 struct strbuf body = STRBUF_INIT;
1836 size_t len = commit_subject_length(sb.buf);
1837 strbuf_addstr(&body, sb.buf + len);
1838 if (message_is_empty(&body, cleanup_mode)) {
1839 rollback_index_files();
1840 fprintf(stderr, _("Aborting commit due to empty commit message body.\n"));
1841 exit(1);
1842 }
1843 strbuf_release(&body);
1844 }
1845
1846 if (amend) {
1847 const char *exclude_gpgsig[3] = { "gpgsig", "gpgsig-sha256", NULL };
1848 extra = read_commit_extra_headers(current_head, exclude_gpgsig);
1849 } else {
1850 struct commit_extra_header **tail = &extra;
1851 append_merge_tag_headers(parents, &tail);
1852 }
1853
1854 if (commit_tree_extended(sb.buf, sb.len, &the_index.cache_tree->oid,
1855 parents, &oid, author_ident.buf, NULL,
1856 sign_commit, extra)) {
1857 rollback_index_files();
1858 die(_("failed to write commit object"));
1859 }
1860 free_commit_extra_headers(extra);
1861
1862 if (update_head_with_reflog(current_head, &oid, reflog_msg, &sb,
1863 &err)) {
1864 rollback_index_files();
1865 die("%s", err.buf);
1866 }
1867
1868 sequencer_post_commit_cleanup(the_repository, 0);
1869 unlink(git_path_merge_head(the_repository));
1870 unlink(git_path_merge_msg(the_repository));
1871 unlink(git_path_merge_mode(the_repository));
1872 unlink(git_path_squash_msg(the_repository));
1873
1874 if (commit_index_files())
1875 die(_("repository has been updated, but unable to write\n"
1876 "new index file. Check that disk is not full and quota is\n"
1877 "not exceeded, and then \"git restore --staged :/\" to recover."));
1878
1879 git_test_write_commit_graph_or_die();
1880
1881 repo_rerere(the_repository, 0);
1882 run_auto_maintenance(quiet);
1883 run_commit_hook(use_editor, get_index_file(), NULL, "post-commit",
1884 NULL);
1885 if (amend && !no_post_rewrite) {
1886 commit_post_rewrite(the_repository, current_head, &oid);
1887 }
1888 if (!quiet) {
1889 unsigned int flags = 0;
1890
1891 if (!current_head)
1892 flags |= SUMMARY_INITIAL_COMMIT;
1893 if (author_date_is_interesting())
1894 flags |= SUMMARY_SHOW_AUTHOR_DATE;
1895 print_commit_summary(the_repository, prefix,
1896 &oid, flags);
1897 }
1898
1899 apply_autostash_ref(the_repository, "MERGE_AUTOSTASH");
1900
1901 cleanup:
1902 strbuf_release(&author_ident);
1903 strbuf_release(&err);
1904 strbuf_release(&sb);
1905 return ret;
1906 }