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