Raw
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 else if (is_from_rebase(whence))
524 die(_("cannot do a partial commit during a rebase."));
525 }
526
527 if (list_paths(&partial, !current_head ? NULL : "HEAD", &pathspec))
528 exit(1);
529
530 discard_index(the_repository->index);
531 if (repo_read_index(the_repository) < 0)
532 die(_("cannot read the index"));
533
534 repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);
535 add_remove_files(&partial);
536 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);
537 cache_tree_update(the_repository->index, WRITE_TREE_SILENT);
538 if (write_locked_index(the_repository->index, &index_lock, 0))
539 die(_("unable to write new index file"));
540
541 path = repo_git_path(the_repository, "next-index-%"PRIuMAX,
542 (uintmax_t) getpid());
543 hold_lock_file_for_update(&false_lock, path,
544 LOCK_DIE_ON_ERROR);
545
546 create_base_index(current_head);
547 add_remove_files(&partial);
548 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);
549
550 if (write_locked_index(the_repository->index, &false_lock, 0))
551 die(_("unable to write temporary index file"));
552
553 discard_index(the_repository->index);
554 ret = get_lock_file_path(&false_lock);
555 read_index_from(the_repository->index, ret, repo_get_git_dir(the_repository));
556 out:
557 string_list_clear(&partial, 0);
558 clear_pathspec(&pathspec);
559 free(path);
560 return ret;
561 }
562
563 static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
564 struct wt_status *s)
565 {
566 struct object_id oid;
567
568 if (s->relative_paths)
569 s->prefix = prefix;
570
571 if (amend) {
572 s->amend = 1;
573 s->reference = "HEAD^1";
574 }
575 s->verbose = verbose;
576 s->index_file = index_file;
577 s->fp = fp;
578 s->nowarn = nowarn;
579 s->is_initial = repo_get_oid(the_repository, s->reference, &oid) ? 1 : 0;
580 if (!s->is_initial)
581 oidcpy(&s->oid_commit, &oid);
582 s->status_format = status_format;
583 s->ignore_submodule_arg = ignore_submodule_arg;
584
585 wt_status_collect(s);
586 wt_status_print(s);
587 wt_status_collect_free_buffers(s);
588
589 return s->committable;
590 }
591
592 static int is_a_merge(const struct commit *current_head)
593 {
594 return !!(current_head->parents && current_head->parents->next);
595 }
596
597 static void assert_split_ident(struct ident_split *id, const struct strbuf *buf)
598 {
599 if (split_ident_line(id, buf->buf, buf->len) || !id->date_begin)
600 BUG("unable to parse our own ident: %s", buf->buf);
601 }
602
603 static void export_one(const char *var, const char *s, const char *e, int hack)
604 {
605 struct strbuf buf = STRBUF_INIT;
606 if (hack)
607 strbuf_addch(&buf, hack);
608 strbuf_add(&buf, s, e - s);
609 setenv(var, buf.buf, 1);
610 strbuf_release(&buf);
611 }
612
613 static int parse_force_date(const char *in, struct strbuf *out)
614 {
615 strbuf_addch(out, '@');
616
617 if (parse_date(in, out) < 0) {
618 int errors = 0;
619 unsigned long t = approxidate_careful(in, &errors);
620 if (errors)
621 return -1;
622 strbuf_addf(out, "%lu", t);
623 }
624
625 return 0;
626 }
627
628 static void set_ident_var(char **buf, char *val)
629 {
630 free(*buf);
631 *buf = val;
632 }
633
634 static void determine_author_info(struct strbuf *author_ident)
635 {
636 char *name, *email, *date;
637 struct ident_split author;
638
639 name = xstrdup_or_null(getenv("GIT_AUTHOR_NAME"));
640 email = xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL"));
641 date = xstrdup_or_null(getenv("GIT_AUTHOR_DATE"));
642
643 if (author_message) {
644 struct ident_split ident;
645 size_t len;
646 const char *a;
647
648 a = find_commit_header(author_message_buffer, "author", &len);
649 if (!a)
650 die(_("commit '%s' lacks author header"), author_message);
651 if (split_ident_line(&ident, a, len) < 0)
652 die(_("commit '%s' has malformed author line"), author_message);
653
654 set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
655 set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
656
657 if (ident.date_begin) {
658 struct strbuf date_buf = STRBUF_INIT;
659 strbuf_addch(&date_buf, '@');
660 strbuf_add(&date_buf, ident.date_begin, ident.date_end - ident.date_begin);
661 strbuf_addch(&date_buf, ' ');
662 strbuf_add(&date_buf, ident.tz_begin, ident.tz_end - ident.tz_begin);
663 set_ident_var(&date, strbuf_detach(&date_buf, NULL));
664 }
665 }
666
667 if (force_author) {
668 struct ident_split ident;
669
670 if (split_ident_line(&ident, force_author, strlen(force_author)) < 0)
671 die(_("malformed --author parameter"));
672 set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
673 set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
674 }
675
676 if (force_date) {
677 struct strbuf date_buf = STRBUF_INIT;
678 if (parse_force_date(force_date, &date_buf))
679 die(_("invalid date format: %s"), force_date);
680 set_ident_var(&date, strbuf_detach(&date_buf, NULL));
681 }
682
683 strbuf_addstr(author_ident, fmt_ident(name, email, WANT_AUTHOR_IDENT, date,
684 IDENT_STRICT));
685 assert_split_ident(&author, author_ident);
686 export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
687 export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
688 export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@');
689 free(name);
690 free(email);
691 free(date);
692 }
693
694 static int author_date_is_interesting(void)
695 {
696 return author_message || force_date;
697 }
698
699 #ifndef WITH_BREAKING_CHANGES
700 static void adjust_comment_line_char(const struct strbuf *sb)
701 {
702 char candidates[] = "#;@!$%^&|:";
703 char *candidate;
704 const char *p;
705 size_t cutoff;
706
707 /* Ignore comment chars in trailing comments (e.g., Conflicts:) */
708 cutoff = sb->len - ignored_log_message_bytes(sb->buf, sb->len);
709
710 if (!memchr(sb->buf, candidates[0], sb->len)) {
711 free(comment_line_str_to_free);
712 comment_line_str = comment_line_str_to_free =
713 xstrfmt("%c", candidates[0]);
714 return;
715 }
716
717 p = sb->buf;
718 candidate = strchr(candidates, *p);
719 if (candidate)
720 *candidate = ' ';
721 for (p = sb->buf; p + 1 < sb->buf + cutoff; p++) {
722 if ((p[0] == '\n' || p[0] == '\r') && p[1]) {
723 candidate = strchr(candidates, p[1]);
724 if (candidate)
725 *candidate = ' ';
726 }
727 }
728
729 for (p = candidates; *p == ' '; p++)
730 ;
731 if (!*p)
732 die(_("unable to select a comment character that is not used\n"
733 "in the current commit message"));
734 free(comment_line_str_to_free);
735 comment_line_str = comment_line_str_to_free = xstrfmt("%c", *p);
736 }
737 #endif /* !WITH_BREAKING_CHANGES */
738
739 static void prepare_amend_commit(struct commit *commit, struct strbuf *sb,
740 struct pretty_print_context *ctx)
741 {
742 const char *buffer, *subject, *fmt;
743
744 buffer = repo_get_commit_buffer(the_repository, commit, NULL);
745 find_commit_subject(buffer, &subject);
746 /*
747 * If we amend the 'amend!' commit then we don't want to
748 * duplicate the subject line.
749 */
750 fmt = starts_with(subject, "amend!") ? "%b" : "%B";
751 repo_format_commit_message(the_repository, commit, fmt, sb, ctx);
752 repo_unuse_commit_buffer(the_repository, commit, buffer);
753 }
754
755 static void change_data_free(void *util, const char *str UNUSED)
756 {
757 struct wt_status_change_data *d = util;
758 free(d->rename_source);
759 free(d);
760 }
761
762 static int prepare_to_commit(const char *index_file, const char *prefix,
763 struct commit *current_head,
764 struct wt_status *s,
765 struct strbuf *author_ident)
766 {
767 struct stat statbuf;
768 struct strbuf committer_ident = STRBUF_INIT;
769 int committable;
770 struct strbuf sb = STRBUF_INIT;
771 const char *hook_arg1 = NULL;
772 const char *hook_arg2 = NULL;
773 int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE);
774 int old_display_comment_prefix;
775 int invoked_hook;
776
777 /* This checks and barfs if author is badly specified */
778 determine_author_info(author_ident);
779
780 if (!no_verify && run_commit_hook(use_editor, index_file, &invoked_hook,
781 "pre-commit", NULL))
782 return 0;
783
784 if (squash_message) {
785 /*
786 * Insert the proper subject line before other commit
787 * message options add their content.
788 */
789 if (use_message && !strcmp(use_message, squash_message))
790 strbuf_addstr(&sb, "squash! ");
791 else {
792 struct pretty_print_context ctx = {0};
793 struct commit *c;
794 c = lookup_commit_reference_by_name(squash_message);
795 if (!c)
796 die(_("could not lookup commit '%s'"), squash_message);
797 ctx.output_encoding = get_commit_output_encoding();
798 repo_format_commit_message(the_repository, c,
799 "squash! %s\n\n", &sb,
800 &ctx);
801 }
802 }
803
804 if (have_option_m && !fixup_message) {
805 strbuf_addbuf(&sb, &message);
806 hook_arg1 = "message";
807 } else if (logfile && !fixup_message && !strcmp(logfile, "-")) {
808 if (isatty(0))
809 fprintf(stderr, _("(reading log message from standard input)\n"));
810 if (strbuf_read(&sb, 0, 0) < 0)
811 die_errno(_("could not read log from standard input"));
812 hook_arg1 = "message";
813 } else if (logfile && !fixup_message) {
814 if (strbuf_read_file(&sb, logfile, 0) < 0)
815 die_errno(_("could not read log file '%s'"),
816 logfile);
817 hook_arg1 = "message";
818 } else if (use_message && !fixup_message) {
819 const char *buffer;
820 buffer = strstr(use_message_buffer, "\n\n");
821 if (buffer)
822 strbuf_addstr(&sb, skip_blank_lines(buffer + 2));
823 hook_arg1 = "commit";
824 hook_arg2 = use_message;
825 } else if (fixup_message) {
826 struct pretty_print_context ctx = {0};
827 struct commit *commit;
828 char *fmt;
829 commit = lookup_commit_reference_by_name(fixup_commit);
830 if (!commit)
831 die(_("could not lookup commit '%s'"), fixup_commit);
832 ctx.output_encoding = get_commit_output_encoding();
833 fmt = xstrfmt("%s! %%s\n\n", fixup_prefix);
834 repo_format_commit_message(the_repository, commit, fmt, &sb,
835 &ctx);
836 free(fmt);
837 hook_arg1 = "message";
838
839 /*
840 * `-m`, `-F`, `-C`, and `-c` provide the message body.
841 * If none was given and this is an amend, use the target
842 * commit's body instead.
843 */
844 if (have_option_m) {
845 strbuf_addbuf(&sb, &message);
846 } else if (logfile && !strcmp(logfile, "-")) {
847 if (isatty(0))
848 fprintf(stderr, _("(reading log message from standard input)\n"));
849 if (strbuf_read(&sb, 0, 0) < 0)
850 die_errno(_("could not read log from standard input"));
851 } else if (logfile) {
852 if (strbuf_read_file(&sb, logfile, 0) < 0)
853 die_errno(_("could not read log file '%s'"), logfile);
854 } else if (use_message) {
855 struct commit *c = lookup_commit_reference_by_name(use_message);
856 if (!c)
857 die(_("could not lookup commit '%s'"), use_message);
858 prepare_amend_commit(c, &sb, &ctx);
859 } else if (!strcmp(fixup_prefix, "amend")) {
860 prepare_amend_commit(commit, &sb, &ctx);
861 }
862 } else if (!stat(git_path_merge_msg(the_repository), &statbuf)) {
863 size_t merge_msg_start;
864
865 /*
866 * prepend SQUASH_MSG here if it exists and a
867 * "merge --squash" was originally performed
868 */
869 if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
870 if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
871 die_errno(_("could not read SQUASH_MSG"));
872 hook_arg1 = "squash";
873 } else
874 hook_arg1 = "merge";
875
876 merge_msg_start = sb.len;
877 if (strbuf_read_file(&sb, git_path_merge_msg(the_repository), 0) < 0)
878 die_errno(_("could not read MERGE_MSG"));
879
880 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
881 wt_status_locate_end(sb.buf + merge_msg_start,
882 sb.len - merge_msg_start) <
883 sb.len - merge_msg_start)
884 s->added_cut_line = 1;
885 } else if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
886 if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
887 die_errno(_("could not read SQUASH_MSG"));
888 hook_arg1 = "squash";
889 } else if (template_file) {
890 if (strbuf_read_file(&sb, template_file, 0) < 0)
891 die_errno(_("could not read '%s'"), template_file);
892 hook_arg1 = "template";
893 clean_message_contents = 0;
894 }
895
896 /*
897 * The remaining cases don't modify the template message, but
898 * just set the argument(s) to the prepare-commit-msg hook.
899 */
900 else if (whence == FROM_MERGE)
901 hook_arg1 = "merge";
902 else if (is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) {
903 hook_arg1 = "commit";
904 hook_arg2 = "CHERRY_PICK_HEAD";
905 }
906
907 if (squash_message) {
908 /*
909 * If squash_commit was used for the commit subject,
910 * then we're possibly hijacking other commit log options.
911 * Reset the hook args to tell the real story.
912 */
913 hook_arg1 = "message";
914 hook_arg2 = "";
915 }
916
917 s->fp = fopen_for_writing(git_path_commit_editmsg());
918 if (!s->fp)
919 die_errno(_("could not open '%s'"), git_path_commit_editmsg());
920
921 /* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */
922 old_display_comment_prefix = s->display_comment_prefix;
923 s->display_comment_prefix = 1;
924
925 /*
926 * Most hints are counter-productive when the commit has
927 * already started.
928 */
929 s->hints = 0;
930
931 if (clean_message_contents)
932 strbuf_stripspace(&sb, NULL);
933
934 if (signoff)
935 append_signoff(&sb, ignored_log_message_bytes(sb.buf, sb.len), 0);
936
937 if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
938 die_errno(_("could not write commit template"));
939
940 #ifndef WITH_BREAKING_CHANGES
941 if (auto_comment_line_char)
942 adjust_comment_line_char(&sb);
943 #endif /* !WITH_BREAKING_CHANGES */
944 strbuf_release(&sb);
945
946 /* This checks if committer ident is explicitly given */
947 strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
948 if (use_editor && include_status) {
949 int ident_shown = 0;
950 enum git_colorbool saved_color_setting;
951 struct ident_split ci, ai;
952 const char *hint_cleanup_all = allow_empty_message ?
953 _("Please enter the commit message for your changes."
954 " Lines starting\nwith '%s' will be ignored.\n") :
955 _("Please enter the commit message for your changes."
956 " Lines starting\nwith '%s' will be ignored, and an empty"
957 " message aborts the commit.\n");
958 const char *hint_cleanup_space = allow_empty_message ?
959 _("Please enter the commit message for your changes."
960 " Lines starting\n"
961 "with '%s' will be kept; you may remove them"
962 " yourself if you want to.\n") :
963 _("Please enter the commit message for your changes."
964 " Lines starting\n"
965 "with '%s' will be kept; you may remove them"
966 " yourself if you want to.\n"
967 "An empty message aborts the commit.\n");
968 if (whence != FROM_COMMIT) {
969 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
970 wt_status_add_cut_line(s);
971 status_printf_ln(
972 s, GIT_COLOR_NORMAL,
973 whence == FROM_MERGE ?
974 _("\n"
975 "It looks like you may be committing a merge.\n"
976 "If this is not correct, please run\n"
977 " git update-ref -d MERGE_HEAD\n"
978 "and try again.\n") :
979 _("\n"
980 "It looks like you may be committing a cherry-pick.\n"
981 "If this is not correct, please run\n"
982 " git update-ref -d CHERRY_PICK_HEAD\n"
983 "and try again.\n"));
984 }
985
986 fprintf(s->fp, "\n");
987 if (cleanup_mode == COMMIT_MSG_CLEANUP_ALL)
988 status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_all, comment_line_str);
989 else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
990 if (whence == FROM_COMMIT)
991 wt_status_add_cut_line(s);
992 } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
993 status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_space, comment_line_str);
994
995 /*
996 * These should never fail because they come from our own
997 * fmt_ident. They may fail the sane_ident test, but we know
998 * that the name and mail pointers will at least be valid,
999 * which is enough for our tests and printing here.
1000 */
1001 assert_split_ident(&ai, author_ident);
1002 assert_split_ident(&ci, &committer_ident);
1003
1004 if (ident_cmp(&ai, &ci))
1005 status_printf_ln(s, GIT_COLOR_NORMAL,
1006 _("%s"
1007 "Author: %.*s <%.*s>"),
1008 ident_shown++ ? "" : "\n",
1009 (int)(ai.name_end - ai.name_begin), ai.name_begin,
1010 (int)(ai.mail_end - ai.mail_begin), ai.mail_begin);
1011
1012 if (author_date_is_interesting())
1013 status_printf_ln(s, GIT_COLOR_NORMAL,
1014 _("%s"
1015 "Date: %s"),
1016 ident_shown++ ? "" : "\n",
1017 show_ident_date(&ai, DATE_MODE(NORMAL)));
1018
1019 if (!committer_ident_sufficiently_given())
1020 status_printf_ln(s, GIT_COLOR_NORMAL,
1021 _("%s"
1022 "Committer: %.*s <%.*s>"),
1023 ident_shown++ ? "" : "\n",
1024 (int)(ci.name_end - ci.name_begin), ci.name_begin,
1025 (int)(ci.mail_end - ci.mail_begin), ci.mail_begin);
1026
1027 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); /* Add new line for clarity */
1028
1029 saved_color_setting = s->use_color;
1030 s->use_color = GIT_COLOR_NEVER;
1031 committable = run_status(s->fp, index_file, prefix, 1, s);
1032 s->use_color = saved_color_setting;
1033 string_list_clear_func(&s->change, change_data_free);
1034 } else {
1035 struct object_id oid;
1036 const char *parent = "HEAD";
1037
1038 if (!the_repository->index->initialized && repo_read_index(the_repository) < 0)
1039 die(_("Cannot read index"));
1040
1041 if (amend)
1042 parent = "HEAD^1";
1043
1044 if (repo_get_oid(the_repository, parent, &oid)) {
1045 int i, ita_nr = 0;
1046
1047 /* TODO: audit for interaction with sparse-index. */
1048 ensure_full_index(the_repository->index);
1049 for (i = 0; i < the_repository->index->cache_nr; i++)
1050 if (ce_intent_to_add(the_repository->index->cache[i]))
1051 ita_nr++;
1052 committable = the_repository->index->cache_nr > ita_nr;
1053 } else {
1054 /*
1055 * Unless the user did explicitly request a submodule
1056 * ignore mode by passing a command line option we do
1057 * not ignore any changed submodule SHA-1s when
1058 * comparing index and parent, no matter what is
1059 * configured. Otherwise we won't commit any
1060 * submodules which were manually staged, which would
1061 * be really confusing.
1062 */
1063 struct diff_flags flags = DIFF_FLAGS_INIT;
1064 flags.override_submodule_config = 1;
1065 if (ignore_submodule_arg &&
1066 !strcmp(ignore_submodule_arg, "all"))
1067 flags.ignore_submodules = 1;
1068 committable = index_differs_from(the_repository,
1069 parent, &flags, 1);
1070 }
1071 }
1072 strbuf_release(&committer_ident);
1073
1074 fclose(s->fp);
1075
1076 if (trailer_args.nr) {
1077 if (amend_file_with_trailers(git_path_commit_editmsg(), &trailer_args))
1078 die(_("unable to pass trailers to --trailers"));
1079 strvec_clear(&trailer_args);
1080 }
1081
1082 /*
1083 * Reject an attempt to record a non-merge empty commit without
1084 * explicit --allow-empty. In the cherry-pick case, it may be
1085 * empty due to conflict resolution, which the user should okay.
1086 */
1087 if (!committable && whence != FROM_MERGE && !allow_empty &&
1088 !(amend && is_a_merge(current_head))) {
1089 s->hints = advice_enabled(ADVICE_STATUS_HINTS);
1090 s->display_comment_prefix = old_display_comment_prefix;
1091 run_status(stdout, index_file, prefix, 0, s);
1092 if (amend)
1093 fputs(_(empty_amend_advice), stderr);
1094 else if (is_from_cherry_pick(whence) ||
1095 whence == FROM_REBASE_PICK) {
1096 fputs(_(empty_cherry_pick_advice), stderr);
1097 if (whence == FROM_CHERRY_PICK_SINGLE)
1098 fputs(_(empty_cherry_pick_advice_single), stderr);
1099 else if (whence == FROM_CHERRY_PICK_MULTI)
1100 fputs(_(empty_cherry_pick_advice_multi), stderr);
1101 else
1102 fputs(_(empty_rebase_pick_advice), stderr);
1103 }
1104 return 0;
1105 }
1106
1107 if (!no_verify && invoked_hook) {
1108 /*
1109 * Re-read the index as the pre-commit-commit hook was invoked
1110 * and could have updated it. We must do this before we invoke
1111 * the editor and after we invoke run_status above.
1112 */
1113 discard_index(the_repository->index);
1114 }
1115 read_index_from(the_repository->index, index_file, repo_get_git_dir(the_repository));
1116
1117 if (cache_tree_update(the_repository->index, 0)) {
1118 error(_("Error building trees"));
1119 return 0;
1120 }
1121
1122 if (run_commit_hook(use_editor, index_file, NULL, "prepare-commit-msg",
1123 git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL))
1124 return 0;
1125
1126 if (use_editor) {
1127 struct strvec env = STRVEC_INIT;
1128
1129 strvec_pushf(&env, "GIT_INDEX_FILE=%s", index_file);
1130 if (launch_editor(git_path_commit_editmsg(), NULL, env.v)) {
1131 fprintf(stderr,
1132 _("Please supply the message using either -m or -F option.\n"));
1133 exit(1);
1134 }
1135 strvec_clear(&env);
1136 }
1137
1138 if (!no_verify &&
1139 run_commit_hook(use_editor, index_file, NULL, "commit-msg",
1140 git_path_commit_editmsg(), NULL)) {
1141 return 0;
1142 }
1143
1144 return 1;
1145 }
1146
1147 static const char *find_author_by_nickname(const char *name)
1148 {
1149 struct rev_info revs;
1150 struct commit *commit;
1151 struct strbuf buf = STRBUF_INIT;
1152 const char *av[20];
1153 int ac = 0;
1154
1155 repo_init_revisions(the_repository, &revs, NULL);
1156 strbuf_addf(&buf, "--author=%s", name);
1157 av[++ac] = "--all";
1158 av[++ac] = "-i";
1159 av[++ac] = buf.buf;
1160 av[++ac] = NULL;
1161 setup_revisions(ac, av, &revs, NULL);
1162 revs.mailmap = xmalloc(sizeof(struct string_list));
1163 string_list_init_nodup(revs.mailmap);
1164 read_mailmap(the_repository, revs.mailmap);
1165
1166 if (prepare_revision_walk(&revs))
1167 die(_("revision walk setup failed"));
1168 commit = get_revision(&revs);
1169 if (commit) {
1170 struct pretty_print_context ctx = {0};
1171 ctx.date_mode.type = DATE_NORMAL;
1172 strbuf_release(&buf);
1173 repo_format_commit_message(the_repository, commit,
1174 "%aN <%aE>", &buf, &ctx);
1175 release_revisions(&revs);
1176 return strbuf_detach(&buf, NULL);
1177 }
1178 die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name);
1179 }
1180
1181 static void handle_ignored_arg(struct wt_status *s)
1182 {
1183 if (!ignored_arg)
1184 ; /* default already initialized */
1185 else if (!strcmp(ignored_arg, "traditional"))
1186 s->show_ignored_mode = SHOW_TRADITIONAL_IGNORED;
1187 else if (!strcmp(ignored_arg, "no"))
1188 s->show_ignored_mode = SHOW_NO_IGNORED;
1189 else if (!strcmp(ignored_arg, "matching"))
1190 s->show_ignored_mode = SHOW_MATCHING_IGNORED;
1191 else
1192 die(_("Invalid ignored mode '%s'"), ignored_arg);
1193 }
1194
1195 static enum untracked_status_type parse_untracked_setting_name(const char *u)
1196 {
1197 /*
1198 * Please update $__git_untracked_file_modes in
1199 * git-completion.bash when you add new options
1200 */
1201 switch (git_parse_maybe_bool(u)) {
1202 case 0:
1203 u = "no";
1204 break;
1205 case 1:
1206 u = "normal";
1207 break;
1208 default:
1209 break;
1210 }
1211
1212 if (!strcmp(u, "no"))
1213 return SHOW_NO_UNTRACKED_FILES;
1214 else if (!strcmp(u, "normal"))
1215 return SHOW_NORMAL_UNTRACKED_FILES;
1216 else if (!strcmp(u, "all"))
1217 return SHOW_ALL_UNTRACKED_FILES;
1218 else
1219 return SHOW_UNTRACKED_FILES_ERROR;
1220 }
1221
1222 static void handle_untracked_files_arg(struct wt_status *s)
1223 {
1224 enum untracked_status_type u;
1225
1226 if (!untracked_files_arg)
1227 return; /* default already initialized */
1228
1229 u = parse_untracked_setting_name(untracked_files_arg);
1230 if (u == SHOW_UNTRACKED_FILES_ERROR)
1231 die(_("Invalid untracked files mode '%s'"),
1232 untracked_files_arg);
1233 s->show_untracked_files = u;
1234 }
1235
1236 static const char *read_commit_message(const char *name)
1237 {
1238 const char *out_enc;
1239 struct commit *commit;
1240
1241 commit = lookup_commit_reference_by_name(name);
1242 if (!commit)
1243 die(_("could not lookup commit '%s'"), name);
1244 out_enc = get_commit_output_encoding();
1245 return repo_logmsg_reencode(the_repository, commit, NULL, out_enc);
1246 }
1247
1248 /*
1249 * Enumerate what needs to be propagated when --porcelain
1250 * is not in effect here.
1251 */
1252 static struct status_deferred_config {
1253 enum wt_status_format status_format;
1254 int show_branch;
1255 enum ahead_behind_flags ahead_behind;
1256 } status_deferred_config = {
1257 STATUS_FORMAT_UNSPECIFIED,
1258 -1, /* unspecified */
1259 AHEAD_BEHIND_UNSPECIFIED,
1260 };
1261
1262 static void finalize_deferred_config(struct wt_status *s)
1263 {
1264 int use_deferred_config = (status_format != STATUS_FORMAT_PORCELAIN &&
1265 status_format != STATUS_FORMAT_PORCELAIN_V2 &&
1266 !s->null_termination);
1267
1268 if (s->null_termination) {
1269 if (status_format == STATUS_FORMAT_NONE ||
1270 status_format == STATUS_FORMAT_UNSPECIFIED)
1271 status_format = STATUS_FORMAT_PORCELAIN;
1272 else if (status_format == STATUS_FORMAT_LONG)
1273 die(_("options '%s' and '%s' cannot be used together"), "--long", "-z");
1274 }
1275
1276 if (use_deferred_config && status_format == STATUS_FORMAT_UNSPECIFIED)
1277 status_format = status_deferred_config.status_format;
1278 if (status_format == STATUS_FORMAT_UNSPECIFIED)
1279 status_format = STATUS_FORMAT_NONE;
1280
1281 if (use_deferred_config && s->show_branch < 0)
1282 s->show_branch = status_deferred_config.show_branch;
1283 if (s->show_branch < 0)
1284 s->show_branch = 0;
1285
1286 /*
1287 * If the user did not give a "--[no]-ahead-behind" command
1288 * line argument *AND* we will print in a human-readable format
1289 * (short, long etc.) then we inherit from the status.aheadbehind
1290 * config setting. In all other cases (and porcelain V[12] formats
1291 * in particular), we inherit _FULL for backwards compatibility.
1292 */
1293 if (use_deferred_config &&
1294 s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
1295 s->ahead_behind_flags = status_deferred_config.ahead_behind;
1296
1297 if (s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
1298 s->ahead_behind_flags = AHEAD_BEHIND_FULL;
1299 }
1300
1301 static void check_fixup_reword_options(int argc, const char *argv[]) {
1302 if (whence != FROM_COMMIT) {
1303 if (whence == FROM_MERGE)
1304 die(_("You are in the middle of a merge -- cannot reword."));
1305 else if (is_from_cherry_pick(whence))
1306 die(_("You are in the middle of a cherry-pick -- cannot reword."));
1307 }
1308 if (argc)
1309 die(_("reword option of '%s' and path '%s' cannot be used together"), "--fixup", *argv);
1310 if (patch_interactive || interactive || all || also || only)
1311 die(_("reword option of '%s' and '%s' cannot be used together"),
1312 "--fixup", "--patch/--interactive/--all/--include/--only");
1313 }
1314
1315 static int parse_and_validate_options(int argc, const char *argv[],
1316 const struct option *options,
1317 const char * const usage[],
1318 const char *prefix,
1319 struct commit *current_head,
1320 struct wt_status *s)
1321 {
1322 argc = parse_options(argc, argv, prefix, options, usage, 0);
1323 finalize_deferred_config(s);
1324
1325 if (force_author && !strchr(force_author, '>'))
1326 force_author = find_author_by_nickname(force_author);
1327
1328 if (force_author && renew_authorship)
1329 die(_("options '%s' and '%s' cannot be used together"), "--reset-author", "--author");
1330
1331 if (logfile || have_option_m || use_message)
1332 use_editor = 0;
1333
1334 /* Sanity check options */
1335 if (amend && !current_head)
1336 die(_("You have nothing to amend."));
1337 if (amend && whence != FROM_COMMIT) {
1338 if (whence == FROM_MERGE)
1339 die(_("You are in the middle of a merge -- cannot amend."));
1340 else if (is_from_cherry_pick(whence))
1341 die(_("You are in the middle of a cherry-pick -- cannot amend."));
1342 else if (whence == FROM_REBASE_PICK)
1343 die(_("You are in the middle of a rebase -- cannot amend."));
1344 }
1345 if (fixup_message && squash_message)
1346 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup");
1347 die_for_incompatible_opt3(!!use_message, "-C",
1348 !!edit_message, "-c",
1349 !!logfile, "-F");
1350 die_for_incompatible_opt4(have_option_m, "-m",
1351 !!edit_message, "-c",
1352 !!use_message, "-C",
1353 !!logfile, "-F");
1354 if (use_message || edit_message || logfile ||fixup_message || have_option_m)
1355 FREE_AND_NULL(template_file);
1356 if (edit_message)
1357 use_message = edit_message;
1358 if (amend && !use_message && !fixup_message)
1359 use_message = "HEAD";
1360 if (!use_message && !is_from_cherry_pick(whence) &&
1361 !is_from_rebase(whence) && renew_authorship)
1362 die(_("--reset-author can be used only with -C, -c or --amend."));
1363 if (use_message) {
1364 use_message_buffer = read_commit_message(use_message);
1365 if (!renew_authorship) {
1366 author_message = use_message;
1367 author_message_buffer = use_message_buffer;
1368 }
1369 }
1370 if ((is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) &&
1371 !renew_authorship) {
1372 author_message = "CHERRY_PICK_HEAD";
1373 author_message_buffer = read_commit_message(author_message);
1374 }
1375
1376 if (patch_interactive)
1377 interactive = 1;
1378
1379 die_for_incompatible_opt4(also, "-i/--include",
1380 only, "-o/--only",
1381 all, "-a/--all",
1382 interactive, "--interactive/-p/--patch");
1383 if (fixup_message) {
1384 /*
1385 * We limit --fixup's suboptions to only alpha characters.
1386 * If the first character after a run of alpha is colon,
1387 * then the part before the colon may be a known suboption
1388 * name like `amend` or `reword`, or a misspelt suboption
1389 * name. In either case, we treat it as
1390 * --fixup=<suboption>:<arg>.
1391 *
1392 * Otherwise, we are dealing with --fixup=<commit>.
1393 */
1394 char *p = fixup_message;
1395 while (isalpha(*p))
1396 p++;
1397 if (p > fixup_message && *p == ':') {
1398 *p = '\0';
1399 fixup_commit = p + 1;
1400 if (!strcmp("amend", fixup_message) ||
1401 !strcmp("reword", fixup_message)) {
1402 fixup_prefix = "amend";
1403 allow_empty = 1;
1404 if (*fixup_message == 'r') {
1405 check_fixup_reword_options(argc, argv);
1406 only = 1;
1407 }
1408 } else {
1409 die(_("unknown option: --fixup=%s:%s"), fixup_message, fixup_commit);
1410 }
1411 } else {
1412 fixup_commit = fixup_message;
1413 fixup_prefix = "fixup";
1414 use_editor = 0;
1415 }
1416 }
1417
1418 if (0 <= edit_flag)
1419 use_editor = edit_flag;
1420
1421 handle_untracked_files_arg(s);
1422
1423 if (all && argc > 0)
1424 die(_("paths '%s ...' with -a does not make sense"),
1425 argv[0]);
1426
1427 if (status_format != STATUS_FORMAT_NONE)
1428 dry_run = 1;
1429
1430 return argc;
1431 }
1432
1433 static int dry_run_commit(const char **argv, const char *prefix,
1434 const struct commit *current_head, struct wt_status *s)
1435 {
1436 int committable;
1437 const char *index_file;
1438
1439 index_file = prepare_index(argv, prefix, current_head, 1);
1440 committable = run_status(stdout, index_file, prefix, 0, s);
1441 rollback_index_files();
1442
1443 return committable ? 0 : 1;
1444 }
1445
1446 define_list_config_array_extra(color_status_slots, {"added"});
1447
1448 static int parse_status_slot(const char *slot)
1449 {
1450 if (!strcasecmp(slot, "added"))
1451 return WT_STATUS_UPDATED;
1452
1453 return LOOKUP_CONFIG(color_status_slots, slot);
1454 }
1455
1456 static int git_status_config(const char *k, const char *v,
1457 const struct config_context *ctx, void *cb)
1458 {
1459 struct wt_status *s = cb;
1460 const char *slot_name;
1461
1462 if (starts_with(k, "column."))
1463 return git_column_config(k, v, "status", &s->colopts);
1464 if (!strcmp(k, "status.submodulesummary")) {
1465 int is_bool;
1466 s->submodule_summary = git_config_bool_or_int(k, v, ctx->kvi,
1467 &is_bool);
1468 if (is_bool && s->submodule_summary)
1469 s->submodule_summary = -1;
1470 return 0;
1471 }
1472 if (!strcmp(k, "status.short")) {
1473 if (git_config_bool(k, v))
1474 status_deferred_config.status_format = STATUS_FORMAT_SHORT;
1475 else
1476 status_deferred_config.status_format = STATUS_FORMAT_NONE;
1477 return 0;
1478 }
1479 if (!strcmp(k, "status.branch")) {
1480 status_deferred_config.show_branch = git_config_bool(k, v);
1481 return 0;
1482 }
1483 if (!strcmp(k, "status.aheadbehind")) {
1484 status_deferred_config.ahead_behind = git_config_bool(k, v);
1485 return 0;
1486 }
1487 if (!strcmp(k, "status.showstash")) {
1488 s->show_stash = git_config_bool(k, v);
1489 return 0;
1490 }
1491 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
1492 s->use_color = git_config_colorbool(k, v);
1493 return 0;
1494 }
1495 if (!strcmp(k, "status.displaycommentprefix")) {
1496 s->display_comment_prefix = git_config_bool(k, v);
1497 return 0;
1498 }
1499 if (skip_prefix(k, "status.color.", &slot_name) ||
1500 skip_prefix(k, "color.status.", &slot_name)) {
1501 int slot = parse_status_slot(slot_name);
1502 if (slot < 0)
1503 return 0;
1504 if (!v)
1505 return config_error_nonbool(k);
1506 return color_parse(v, s->color_palette[slot]);
1507 }
1508 if (!strcmp(k, "status.relativepaths")) {
1509 s->relative_paths = git_config_bool(k, v);
1510 return 0;
1511 }
1512 if (!strcmp(k, "status.showuntrackedfiles")) {
1513 enum untracked_status_type u;
1514
1515 u = parse_untracked_setting_name(v);
1516 if (u == SHOW_UNTRACKED_FILES_ERROR)
1517 return error(_("Invalid untracked files mode '%s'"), v);
1518 s->show_untracked_files = u;
1519 return 0;
1520 }
1521 if (!strcmp(k, "diff.renamelimit")) {
1522 if (s->rename_limit == -1)
1523 s->rename_limit = git_config_int(k, v, ctx->kvi);
1524 return 0;
1525 }
1526 if (!strcmp(k, "status.renamelimit")) {
1527 s->rename_limit = git_config_int(k, v, ctx->kvi);
1528 return 0;
1529 }
1530 if (!strcmp(k, "diff.renames")) {
1531 if (s->detect_rename == -1)
1532 s->detect_rename = git_config_rename(k, v);
1533 return 0;
1534 }
1535 if (!strcmp(k, "status.renames")) {
1536 s->detect_rename = git_config_rename(k, v);
1537 return 0;
1538 }
1539 return git_diff_ui_config(k, v, ctx, NULL);
1540 }
1541
1542 int cmd_status(int argc,
1543 const char **argv,
1544 const char *prefix,
1545 struct repository *repo UNUSED)
1546 {
1547 static int no_renames = -1;
1548 static const char *rename_score_arg = (const char *)-1;
1549 static struct wt_status s;
1550 unsigned int progress_flag = 0;
1551 int fd;
1552 struct object_id oid;
1553 static struct option builtin_status_options[] = {
1554 OPT__VERBOSE(&verbose, N_("be verbose")),
1555 OPT_SET_INT('s', "short", &status_format,
1556 N_("show status concisely"), STATUS_FORMAT_SHORT),
1557 OPT_BOOL('b', "branch", &s.show_branch,
1558 N_("show branch information")),
1559 OPT_BOOL(0, "show-stash", &s.show_stash,
1560 N_("show stash information")),
1561 OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1562 N_("compute full ahead/behind values")),
1563 OPT_CALLBACK_F(0, "porcelain", &status_format,
1564 N_("version"), N_("machine-readable output"),
1565 PARSE_OPT_OPTARG, opt_parse_porcelain),
1566 OPT_SET_INT(0, "long", &status_format,
1567 N_("show status in long format (default)"),
1568 STATUS_FORMAT_LONG),
1569 OPT_BOOL('z', "null", &s.null_termination,
1570 N_("terminate entries with NUL")),
1571 {
1572 .type = OPTION_STRING,
1573 .short_name = 'u',
1574 .long_name = "untracked-files",
1575 .value = &untracked_files_arg,
1576 .argh = N_("mode"),
1577 .help = N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
1578 .flags = PARSE_OPT_OPTARG,
1579 .defval = (intptr_t)"all",
1580 },
1581 {
1582 .type = OPTION_STRING,
1583 .long_name = "ignored",
1584 .value = &ignored_arg,
1585 .argh = N_("mode"),
1586 .help = N_("show ignored files, optional modes: traditional, matching, no. (Default: traditional)"),
1587 .flags = PARSE_OPT_OPTARG,
1588 .defval = (intptr_t)"traditional",
1589 },
1590 {
1591 .type = OPTION_STRING,
1592 .long_name = "ignore-submodules",
1593 .value = &ignore_submodule_arg,
1594 .argh = N_("when"),
1595 .help = N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
1596 .flags = PARSE_OPT_OPTARG,
1597 .defval = (intptr_t)"all",
1598 },
1599 OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")),
1600 OPT_BOOL(0, "no-renames", &no_renames, N_("do not detect renames")),
1601 OPT_CALLBACK_F('M', "find-renames", &rename_score_arg,
1602 N_("n"), N_("detect renames, optionally set similarity index"),
1603 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, opt_parse_rename_score),
1604 OPT_END(),
1605 };
1606
1607 show_usage_with_options_if_asked(argc, argv,
1608 builtin_status_usage, builtin_status_options);
1609
1610 prepare_repo_settings(the_repository);
1611 the_repository->settings.command_requires_full_index = 0;
1612
1613 status_init_config(&s, git_status_config);
1614 argc = parse_options(argc, argv, prefix,
1615 builtin_status_options,
1616 builtin_status_usage, 0);
1617 finalize_colopts(&s.colopts, -1);
1618 finalize_deferred_config(&s);
1619
1620 handle_untracked_files_arg(&s);
1621 handle_ignored_arg(&s);
1622
1623 if (s.show_ignored_mode == SHOW_MATCHING_IGNORED &&
1624 s.show_untracked_files == SHOW_NO_UNTRACKED_FILES)
1625 die(_("Unsupported combination of ignored and untracked-files arguments"));
1626
1627 parse_pathspec(&s.pathspec, 0,
1628 PATHSPEC_PREFER_FULL,
1629 prefix, argv);
1630
1631 if (status_format != STATUS_FORMAT_PORCELAIN &&
1632 status_format != STATUS_FORMAT_PORCELAIN_V2)
1633 progress_flag = REFRESH_PROGRESS;
1634 repo_read_index(the_repository);
1635 refresh_index(the_repository->index,
1636 REFRESH_QUIET|REFRESH_UNMERGED|progress_flag,
1637 &s.pathspec, NULL, NULL);
1638
1639 if (use_optional_locks())
1640 fd = repo_hold_locked_index(the_repository, &index_lock, 0);
1641 else
1642 fd = -1;
1643
1644 s.is_initial = repo_get_oid(the_repository, s.reference, &oid) ? 1 : 0;
1645 if (!s.is_initial)
1646 oidcpy(&s.oid_commit, &oid);
1647
1648 s.ignore_submodule_arg = ignore_submodule_arg;
1649 s.status_format = status_format;
1650 s.verbose = verbose;
1651 if (no_renames != -1)
1652 s.detect_rename = !no_renames;
1653 if ((intptr_t)rename_score_arg != -1) {
1654 if (s.detect_rename < DIFF_DETECT_RENAME)
1655 s.detect_rename = DIFF_DETECT_RENAME;
1656 if (rename_score_arg)
1657 s.rename_score = parse_rename_score(&rename_score_arg);
1658 }
1659
1660 wt_status_collect(&s);
1661
1662 if (0 <= fd)
1663 repo_update_index_if_able(the_repository, &index_lock);
1664
1665 if (s.relative_paths)
1666 s.prefix = prefix;
1667
1668 wt_status_print(&s);
1669 wt_status_collect_free_buffers(&s);
1670
1671 return 0;
1672 }
1673
1674 static int git_commit_config(const char *k, const char *v,
1675 const struct config_context *ctx, void *cb)
1676 {
1677 struct wt_status *s = cb;
1678
1679 if (!strcmp(k, "commit.template"))
1680 return git_config_pathname(&template_file, k, v);
1681 if (!strcmp(k, "commit.status")) {
1682 include_status = git_config_bool(k, v);
1683 return 0;
1684 }
1685 if (!strcmp(k, "commit.cleanup")) {
1686 FREE_AND_NULL(cleanup_config);
1687 return git_config_string(&cleanup_config, k, v);
1688 }
1689 if (!strcmp(k, "commit.gpgsign")) {
1690 sign_commit = git_config_bool(k, v) ? "" : NULL;
1691 return 0;
1692 }
1693 if (!strcmp(k, "commit.verbose")) {
1694 int is_bool;
1695 config_commit_verbose = git_config_bool_or_int(k, v, ctx->kvi,
1696 &is_bool);
1697 return 0;
1698 }
1699
1700 return git_status_config(k, v, ctx, s);
1701 }
1702
1703 int cmd_commit(int argc,
1704 const char **argv,
1705 const char *prefix,
1706 struct repository *repo UNUSED)
1707 {
1708 static struct wt_status s;
1709 static const char *cleanup_arg = NULL;
1710 static struct option builtin_commit_options[] = {
1711 OPT__QUIET(&quiet, N_("suppress summary after successful commit")),
1712 OPT__VERBOSE(&verbose, N_("show diff in commit message template")),
1713
1714 OPT_GROUP(N_("Commit message options")),
1715 OPT_FILENAME('F', "file", &logfile, N_("read message from file")),
1716 OPT_STRING(0, "author", &force_author, N_("author"), N_("override author for commit")),
1717 OPT_STRING(0, "date", &force_date, N_("date"), N_("override date for commit")),
1718 OPT_CALLBACK('m', "message", &message, N_("message"), N_("commit message"), opt_parse_m),
1719 OPT_STRING('c', "reedit-message", &edit_message, N_("commit"), N_("reuse and edit message from specified commit")),
1720 OPT_STRING('C', "reuse-message", &use_message, N_("commit"), N_("reuse message from specified commit")),
1721 /*
1722 * TRANSLATORS: Leave "[(amend|reword):]" as-is,
1723 * and only translate <commit>.
1724 */
1725 OPT_STRING(0, "fixup", &fixup_message, N_("[(amend|reword):]commit"), N_("use autosquash formatted message to fixup or amend/reword specified commit")),
1726 OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
1727 OPT_BOOL(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")),
1728 OPT_STRVEC(0, "trailer", &trailer_args, N_("trailer"),
1729 N_("add custom trailer(s)")),
1730 OPT_BOOL('s', "signoff", &signoff, N_("add a Signed-off-by trailer")),
1731 OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
1732 OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),
1733 OPT_CLEANUP(&cleanup_arg),
1734 OPT_BOOL(0, "status", &include_status, N_("include status in commit message template")),
1735 {
1736 .type = OPTION_STRING,
1737 .short_name = 'S',
1738 .long_name = "gpg-sign",
1739 .value = &sign_commit,
1740 .argh = N_("key-id"),
1741 .help = N_("GPG sign commit"),
1742 .flags = PARSE_OPT_OPTARG,
1743 .defval = (intptr_t) "",
1744 },
1745 /* end commit message options */
1746
1747 OPT_GROUP(N_("Commit contents options")),
1748 OPT_BOOL('a', "all", &all, N_("commit all changed files")),
1749 OPT_BOOL('i', "include", &also, N_("add specified files to index for commit")),
1750 OPT_BOOL(0, "interactive", &interactive, N_("interactively add files")),
1751 OPT_BOOL('p', "patch", &patch_interactive, N_("interactively add changes")),
1752 OPT_DIFF_UNIFIED(&interactive_opts.context),
1753 OPT_DIFF_INTERHUNK_CONTEXT(&interactive_opts.interhunkcontext),
1754 OPT_BOOL('o', "only", &only, N_("commit only specified files")),
1755 OPT_BOOL('n', "no-verify", &no_verify, N_("bypass pre-commit and commit-msg hooks")),
1756 OPT_BOOL(0, "dry-run", &dry_run, N_("show what would be committed")),
1757 OPT_SET_INT(0, "short", &status_format, N_("show status concisely"),
1758 STATUS_FORMAT_SHORT),
1759 OPT_BOOL(0, "branch", &s.show_branch, N_("show branch information")),
1760 OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1761 N_("compute full ahead/behind values")),
1762 OPT_SET_INT(0, "porcelain", &status_format,
1763 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN),
1764 OPT_SET_INT(0, "long", &status_format,
1765 N_("show status in long format (default)"),
1766 STATUS_FORMAT_LONG),
1767 OPT_BOOL('z', "null", &s.null_termination,
1768 N_("terminate entries with NUL")),
1769 OPT_BOOL(0, "amend", &amend, N_("amend previous commit")),
1770 OPT_BOOL(0, "no-post-rewrite", &no_post_rewrite, N_("bypass post-rewrite hook")),
1771 {
1772 .type = OPTION_STRING,
1773 .short_name = 'u',
1774 .long_name = "untracked-files",
1775 .value = &untracked_files_arg,
1776 .argh = N_("mode"),
1777 .help = N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
1778 .flags = PARSE_OPT_OPTARG,
1779 .defval = (intptr_t)"all",
1780 },
1781 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
1782 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
1783 /* end commit contents options */
1784
1785 OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty,
1786 N_("ok to record an empty change")),
1787 OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message,
1788 N_("ok to record a change with an empty message")),
1789
1790 OPT_END()
1791 };
1792
1793 struct strbuf sb = STRBUF_INIT;
1794 struct strbuf author_ident = STRBUF_INIT;
1795 const char *index_file, *reflog_msg;
1796 struct object_id oid;
1797 struct commit_list *parents = NULL;
1798 struct stat statbuf;
1799 struct commit *current_head = NULL;
1800 struct commit_extra_header *extra = NULL;
1801 struct strbuf err = STRBUF_INIT;
1802 int ret = 0;
1803
1804 show_usage_with_options_if_asked(argc, argv,
1805 builtin_commit_usage, builtin_commit_options);
1806
1807 #ifndef WITH_BREAKING_CHANGES
1808 warn_on_auto_comment_char = true;
1809 #endif /* !WITH_BREAKING_CHANGES */
1810 prepare_repo_settings(the_repository);
1811 the_repository->settings.command_requires_full_index = 0;
1812
1813 status_init_config(&s, git_commit_config);
1814 s.commit_template = 1;
1815 status_format = STATUS_FORMAT_NONE; /* Ignore status.short */
1816 s.colopts = 0;
1817
1818 if (repo_get_oid(the_repository, "HEAD", &oid))
1819 current_head = NULL;
1820 else {
1821 current_head = lookup_commit_or_die(&oid, "HEAD");
1822 if (repo_parse_commit(the_repository, current_head))
1823 die(_("could not parse HEAD commit"));
1824 }
1825 verbose = -1; /* unspecified */
1826 argc = parse_and_validate_options(argc, argv, builtin_commit_options,
1827 builtin_commit_usage,
1828 prefix, current_head, &s);
1829 if (trailer_args.nr)
1830 trailer_config_init();
1831
1832 if (verbose == -1)
1833 verbose = (config_commit_verbose < 0) ? 0 : config_commit_verbose;
1834
1835 if (cleanup_arg) {
1836 free(cleanup_config);
1837 cleanup_config = xstrdup(cleanup_arg);
1838 }
1839 cleanup_mode = get_cleanup_mode(cleanup_config, use_editor);
1840
1841 if (dry_run)
1842 return dry_run_commit(argv, prefix, current_head, &s);
1843 index_file = prepare_index(argv, prefix, current_head, 0);
1844
1845 /* Set up everything for writing the commit object. This includes
1846 running hooks, writing the trees, and interacting with the user. */
1847 if (!prepare_to_commit(index_file, prefix,
1848 current_head, &s, &author_ident)) {
1849 ret = 1;
1850 rollback_index_files();
1851 goto cleanup;
1852 }
1853
1854 /* Determine parents */
1855 reflog_msg = getenv("GIT_REFLOG_ACTION");
1856 if (!current_head) {
1857 if (!reflog_msg)
1858 reflog_msg = "commit (initial)";
1859 } else if (amend) {
1860 if (!reflog_msg)
1861 reflog_msg = "commit (amend)";
1862 parents = commit_list_copy(current_head->parents);
1863 } else if (whence == FROM_MERGE) {
1864 struct strbuf m = STRBUF_INIT;
1865 FILE *fp;
1866 int allow_fast_forward = 1;
1867 struct commit_list **pptr = &parents;
1868
1869 if (!reflog_msg)
1870 reflog_msg = "commit (merge)";
1871 pptr = commit_list_append(current_head, pptr);
1872 fp = xfopen(git_path_merge_head(the_repository), "r");
1873 while (strbuf_getline_lf(&m, fp) != EOF) {
1874 struct commit *parent;
1875
1876 parent = get_merge_parent(m.buf);
1877 if (!parent)
1878 die(_("Corrupt MERGE_HEAD file (%s)"), m.buf);
1879 pptr = commit_list_append(parent, pptr);
1880 }
1881 fclose(fp);
1882 strbuf_release(&m);
1883 if (!stat(git_path_merge_mode(the_repository), &statbuf)) {
1884 if (strbuf_read_file(&sb, git_path_merge_mode(the_repository), 0) < 0)
1885 die_errno(_("could not read MERGE_MODE"));
1886 if (!strcmp(sb.buf, "no-ff"))
1887 allow_fast_forward = 0;
1888 }
1889 if (allow_fast_forward)
1890 reduce_heads_replace(&parents);
1891 } else {
1892 if (!reflog_msg)
1893 reflog_msg = is_from_cherry_pick(whence)
1894 ? "commit (cherry-pick)"
1895 : is_from_rebase(whence)
1896 ? "commit (rebase)"
1897 : "commit";
1898 commit_list_insert(current_head, &parents);
1899 }
1900
1901 /* Finally, get the commit message */
1902 strbuf_reset(&sb);
1903 if (strbuf_read_file(&sb, git_path_commit_editmsg(), 0) < 0) {
1904 int saved_errno = errno;
1905 rollback_index_files();
1906 die(_("could not read commit message: %s"), strerror(saved_errno));
1907 }
1908
1909 cleanup_message(&sb, cleanup_mode, verbose);
1910
1911 if (message_is_empty(&sb, cleanup_mode) && !allow_empty_message) {
1912 rollback_index_files();
1913 fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
1914 exit(1);
1915 }
1916 if (template_untouched(&sb, template_file, cleanup_mode) && !allow_empty_message) {
1917 rollback_index_files();
1918 fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
1919 exit(1);
1920 }
1921
1922 if (fixup_message && starts_with(sb.buf, "amend! ") &&
1923 !allow_empty_message) {
1924 struct strbuf body = STRBUF_INIT;
1925 size_t len = commit_subject_length(sb.buf);
1926 strbuf_addstr(&body, sb.buf + len);
1927 if (message_is_empty(&body, cleanup_mode)) {
1928 rollback_index_files();
1929 fprintf(stderr, _("Aborting commit due to empty commit message body.\n"));
1930 exit(1);
1931 }
1932 strbuf_release(&body);
1933 }
1934
1935 if (amend) {
1936 const char *exclude_gpgsig[3] = { "gpgsig", "gpgsig-sha256", NULL };
1937 extra = read_commit_extra_headers(current_head, exclude_gpgsig);
1938 } else {
1939 struct commit_extra_header **tail = &extra;
1940 append_merge_tag_headers(parents, &tail);
1941 }
1942
1943 if (commit_tree_extended(sb.buf, sb.len, &the_repository->index->cache_tree->oid,
1944 parents, &oid, author_ident.buf, NULL,
1945 sign_commit, extra)) {
1946 rollback_index_files();
1947 die(_("failed to write commit object"));
1948 }
1949
1950 if (update_head_with_reflog(current_head, &oid, reflog_msg, &sb,
1951 &err)) {
1952 rollback_index_files();
1953 die("%s", err.buf);
1954 }
1955
1956 sequencer_post_commit_cleanup(the_repository, 0);
1957 unlink(git_path_merge_head(the_repository));
1958 unlink(git_path_merge_msg(the_repository));
1959 unlink(git_path_merge_mode(the_repository));
1960 unlink(git_path_squash_msg(the_repository));
1961
1962 if (commit_index_files())
1963 die(_("repository has been updated, but unable to write\n"
1964 "new index file. Check that disk is not full and quota is\n"
1965 "not exceeded, and then \"git restore --staged :/\" to recover."));
1966
1967 git_test_write_commit_graph_or_die(the_repository->objects->sources);
1968
1969 repo_rerere(the_repository, 0);
1970 run_auto_maintenance(the_repository, quiet);
1971 run_commit_hook(use_editor, repo_get_index_file(the_repository),
1972 NULL, "post-commit", NULL);
1973 if (amend && !no_post_rewrite) {
1974 commit_post_rewrite(the_repository, current_head, &oid);
1975 }
1976 if (!quiet) {
1977 unsigned int flags = 0;
1978
1979 if (!current_head)
1980 flags |= SUMMARY_INITIAL_COMMIT;
1981 if (author_date_is_interesting())
1982 flags |= SUMMARY_SHOW_AUTHOR_DATE;
1983 print_commit_summary(the_repository, prefix,
1984 &oid, flags);
1985 }
1986
1987 apply_autostash_ref(the_repository, "MERGE_AUTOSTASH",
1988 NULL, NULL, NULL, NULL, NULL);
1989
1990 cleanup:
1991 free_commit_extra_headers(extra);
1992 commit_list_free(parents);
1993 strbuf_release(&author_ident);
1994 strbuf_release(&err);
1995 strbuf_release(&sb);
1996 free(logfile);
1997 free(template_file);
1998 return ret;
1999 }