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