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