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