at commit a3837282

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