assume parse_commit checks for NULL commit
[git/git.git] / builtin / commit.c
CommitLineData
f5bbc322
KH
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#include "cache.h"
9#include "cache-tree.h"
6b2f2d98 10#include "color.h"
2888605c 11#include "dir.h"
f5bbc322
KH
12#include "builtin.h"
13#include "diff.h"
14#include "diffcore.h"
15#include "commit.h"
16#include "revision.h"
17#include "wt-status.h"
18#include "run-command.h"
19#include "refs.h"
20#include "log-tree.h"
21#include "strbuf.h"
22#include "utf8.h"
23#include "parse-options.h"
c455c87c 24#include "string-list.h"
5b2fd956 25#include "rerere.h"
fa9dcf80 26#include "unpack-trees.h"
76e2f7ce 27#include "quote.h"
302ad7a9 28#include "submodule.h"
ba3c69a9 29#include "gpg-interface.h"
323d0530 30#include "column.h"
5ed75e2a 31#include "sequencer.h"
49c24704 32#include "notes-utils.h"
f5bbc322
KH
33
34static const char * const builtin_commit_usage[] = {
d32805dc 35 N_("git commit [options] [--] <pathspec>..."),
f5bbc322
KH
36 NULL
37};
38
2f02b25f 39static const char * const builtin_status_usage[] = {
d32805dc 40 N_("git status [options] [--] <pathspec>..."),
2f02b25f
SB
41 NULL
42};
43
49ff9a7a 44static const char implicit_ident_advice[] =
fc88e316 45N_("Your name and email address were configured automatically based\n"
49ff9a7a
JK
46"on your username and hostname. Please check that they are accurate.\n"
47"You can suppress this message by setting them explicitly:\n"
48"\n"
8bb45b25 49" git config --global user.name \"Your Name\"\n"
49ff9a7a
JK
50" git config --global user.email you@example.com\n"
51"\n"
3f142468 52"After doing this, you may fix the identity used for this commit with:\n"
49ff9a7a 53"\n"
fc88e316 54" git commit --amend --reset-author\n");
49ff9a7a 55
f197ed2f 56static const char empty_amend_advice[] =
fc88e316 57N_("You asked to amend the most recent commit, but doing so would make\n"
f197ed2f 58"it empty. You can repeat your command with --allow-empty, or you can\n"
fc88e316 59"remove the commit entirely with \"git reset HEAD^\".\n");
f197ed2f 60
37f7a857 61static const char empty_cherry_pick_advice[] =
6c80cd29 62N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n"
37f7a857
JS
63"If you wish to commit it anyway, use:\n"
64"\n"
65" git commit --allow-empty\n"
c17592a7
JK
66"\n");
67
68static const char empty_cherry_pick_advice_single[] =
69N_("Otherwise, please use 'git reset'\n");
70
71static const char empty_cherry_pick_advice_multi[] =
72N_("If you wish to skip this commit, use:\n"
37f7a857 73"\n"
c17592a7
JK
74" git reset\n"
75"\n"
76"Then \"git cherry-pick --continue\" will resume cherry-picking\n"
77"the remaining commits.\n");
37f7a857 78
37f7a857 79static const char *use_message_buffer;
f5bbc322 80static const char commit_editmsg[] = "COMMIT_EDITMSG";
2888605c
JH
81static struct lock_file index_lock; /* real index */
82static struct lock_file false_lock; /* used only for partial commits */
83static enum {
84 COMMIT_AS_IS = 1,
85 COMMIT_NORMAL,
4b05548f 86 COMMIT_PARTIAL
2888605c 87} commit_style;
f5bbc322 88
dbd0f5c7 89static const char *logfile, *force_author;
984c6e7e 90static const char *template_file;
37f7a857
JS
91/*
92 * The _message variables are commit names from which to take
93 * the commit message and/or authorship.
94 */
95static const char *author_message, *author_message_buffer;
f5bbc322 96static char *edit_message, *use_message;
89ac1223 97static char *fixup_message, *squash_message;
ca1ba201
JH
98static int all, also, interactive, patch_interactive, only, amend, signoff;
99static int edit_flag = -1; /* unspecified */
c51f6cee 100static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
c9b5fde7 101static int no_post_rewrite, allow_empty_message;
46a958b3 102static char *untracked_files_arg, *force_date, *ignore_submodule_arg;
ba3c69a9
JH
103static char *sign_commit;
104
5f065737
AR
105/*
106 * The default commit message cleanup mode will remove the lines
107 * beginning with # (shell comments) and leading and trailing
108 * whitespaces (empty lines or containing only whitespaces)
109 * if editor is used, and only the whitespaces if the message
110 * is specified explicitly.
111 */
112static enum {
113 CLEANUP_SPACE,
114 CLEANUP_NONE,
4b05548f 115 CLEANUP_ALL
5f065737 116} cleanup_mode;
51fb3a3d 117static const char *cleanup_arg;
f5bbc322 118
37f7a857 119static enum commit_whence whence;
c17592a7 120static int sequencer_in_use;
06bb643b 121static int use_editor = 1, include_status = 1;
25206778 122static int show_ignored_in_status, have_option_m;
3b6aeb3c 123static const char *only_include_assumed;
2c47789d 124static struct strbuf message = STRBUF_INIT;
f9568530 125
84b4202d 126static enum status_format {
f3f47a1e 127 STATUS_FORMAT_NONE = 0,
7c9f7038
JK
128 STATUS_FORMAT_LONG,
129 STATUS_FORMAT_SHORT,
84b4202d
JH
130 STATUS_FORMAT_PORCELAIN,
131
132 STATUS_FORMAT_UNSPECIFIED
133} status_format = STATUS_FORMAT_UNSPECIFIED;
7c9f7038 134
f9568530
JS
135static int opt_parse_m(const struct option *opt, const char *arg, int unset)
136{
137 struct strbuf *buf = opt->value;
25206778
RS
138 if (unset) {
139 have_option_m = 0;
f9568530 140 strbuf_setlen(buf, 0);
25206778
RS
141 } else {
142 have_option_m = 1;
a24a41ea
BC
143 if (buf->len)
144 strbuf_addch(buf, '\n');
f9568530 145 strbuf_addstr(buf, arg);
a24a41ea 146 strbuf_complete_line(buf);
f9568530
JS
147 }
148 return 0;
149}
f5bbc322 150
37f7a857
JS
151static void determine_whence(struct wt_status *s)
152{
153 if (file_exists(git_path("MERGE_HEAD")))
154 whence = FROM_MERGE;
c17592a7 155 else if (file_exists(git_path("CHERRY_PICK_HEAD"))) {
37f7a857 156 whence = FROM_CHERRY_PICK;
c17592a7
JK
157 if (file_exists(git_path("sequencer")))
158 sequencer_in_use = 1;
159 }
37f7a857
JS
160 else
161 whence = FROM_COMMIT;
162 if (s)
163 s->whence = whence;
164}
165
2888605c
JH
166static void rollback_index_files(void)
167{
168 switch (commit_style) {
169 case COMMIT_AS_IS:
170 break; /* nothing to do */
171 case COMMIT_NORMAL:
172 rollback_lock_file(&index_lock);
173 break;
174 case COMMIT_PARTIAL:
175 rollback_lock_file(&index_lock);
176 rollback_lock_file(&false_lock);
177 break;
178 }
179}
180
5a9dd399 181static int commit_index_files(void)
2888605c 182{
5a9dd399
BC
183 int err = 0;
184
2888605c
JH
185 switch (commit_style) {
186 case COMMIT_AS_IS:
187 break; /* nothing to do */
188 case COMMIT_NORMAL:
5a9dd399 189 err = commit_lock_file(&index_lock);
2888605c
JH
190 break;
191 case COMMIT_PARTIAL:
5a9dd399 192 err = commit_lock_file(&index_lock);
2888605c
JH
193 rollback_lock_file(&false_lock);
194 break;
195 }
5a9dd399
BC
196
197 return err;
2888605c
JH
198}
199
200/*
201 * Take a union of paths in the index and the named tree (typically, "HEAD"),
202 * and return the paths that match the given pattern in list.
203 */
c455c87c 204static int list_paths(struct string_list *list, const char *with_tree,
2888605c
JH
205 const char *prefix, const char **pattern)
206{
207 int i;
208 char *m;
209
ea2d4ed3
JK
210 if (!pattern)
211 return 0;
212
2888605c
JH
213 for (i = 0; pattern[i]; i++)
214 ;
215 m = xcalloc(1, i);
216
8894d535 217 if (with_tree) {
f950eb95 218 char *max_prefix = common_prefix(pattern);
5879f568
CB
219 overlay_tree_on_cache(with_tree, max_prefix ? max_prefix : prefix);
220 free(max_prefix);
8894d535 221 }
2888605c
JH
222
223 for (i = 0; i < active_nr; i++) {
9c5e6c80 224 const struct cache_entry *ce = active_cache[i];
7fce6e3c
NTND
225 struct string_list_item *item;
226
7a51ed66 227 if (ce->ce_flags & CE_UPDATE)
e87e22d0 228 continue;
0b50922a 229 if (!match_pathspec(pattern, ce->name, ce_namelen(ce), 0, m))
2888605c 230 continue;
78a395d3 231 item = string_list_insert(list, ce->name);
7fce6e3c
NTND
232 if (ce_skip_worktree(ce))
233 item->util = item; /* better a valid pointer than a fake one */
2888605c
JH
234 }
235
0f64bfa9 236 return report_path_error(m, pattern, prefix);
2888605c
JH
237}
238
c455c87c 239static void add_remove_files(struct string_list *list)
2888605c
JH
240{
241 int i;
242 for (i = 0; i < list->nr; i++) {
d177cab0 243 struct stat st;
c455c87c 244 struct string_list_item *p = &(list->items[i]);
d177cab0 245
7fce6e3c
NTND
246 /* p->util is skip-worktree */
247 if (p->util)
b4d1690d 248 continue;
d177cab0 249
c455c87c
JS
250 if (!lstat(p->string, &st)) {
251 if (add_to_cache(p->string, &st, 0))
8a6179bc 252 die(_("updating files failed"));
960b8ad1 253 } else
c455c87c 254 remove_file_from_cache(p->string);
2888605c
JH
255 }
256}
257
06bb643b 258static void create_base_index(const struct commit *current_head)
fa9dcf80
LT
259{
260 struct tree *tree;
261 struct unpack_trees_options opts;
262 struct tree_desc t;
263
06bb643b 264 if (!current_head) {
fa9dcf80
LT
265 discard_cache();
266 return;
267 }
268
269 memset(&opts, 0, sizeof(opts));
270 opts.head_idx = 1;
271 opts.index_only = 1;
272 opts.merge = 1;
34110cd4
LT
273 opts.src_index = &the_index;
274 opts.dst_index = &the_index;
fa9dcf80
LT
275
276 opts.fn = oneway_merge;
06bb643b 277 tree = parse_tree_indirect(current_head->object.sha1);
fa9dcf80 278 if (!tree)
8a6179bc 279 die(_("failed to unpack HEAD tree object"));
fa9dcf80
LT
280 parse_tree(tree);
281 init_tree_desc(&t, tree->buffer, tree->size);
203a2fe1
DB
282 if (unpack_trees(1, &t, &opts))
283 exit(128); /* We've already reported the error, finish dying */
fa9dcf80
LT
284}
285
d38a30df
MM
286static void refresh_cache_or_die(int refresh_flags)
287{
288 /*
289 * refresh_flags contains REFRESH_QUIET, so the only errors
290 * are for unmerged entries.
291 */
292 if (refresh_cache(refresh_flags | REFRESH_IN_PORCELAIN))
293 die_resolve_conflict("commit");
294}
295
06bb643b
JH
296static char *prepare_index(int argc, const char **argv, const char *prefix,
297 const struct commit *current_head, int is_status)
f5bbc322
KH
298{
299 int fd;
c455c87c 300 struct string_list partial;
2888605c 301 const char **pathspec = NULL;
1020d087 302 char *old_index_env = NULL;
50b7e70f 303 int refresh_flags = REFRESH_QUIET;
f5bbc322 304
50b7e70f
JH
305 if (is_status)
306 refresh_flags |= REFRESH_UNMERGED;
f5bbc322 307
f64fe7b4
JH
308 if (*argv)
309 pathspec = get_pathspec(prefix, argv);
2888605c 310
671c9b7e 311 if (read_cache_preload(pathspec) < 0)
8a6179bc 312 die(_("index file corrupt"));
671c9b7e 313
1020d087
CI
314 if (interactive) {
315 fd = hold_locked_index(&index_lock, 1);
316
317 refresh_cache_or_die(refresh_flags);
318
319 if (write_cache(fd, active_cache, active_nr) ||
320 close_lock_file(&index_lock))
321 die(_("unable to create temporary index"));
322
323 old_index_env = getenv(INDEX_ENVIRONMENT);
324 setenv(INDEX_ENVIRONMENT, index_lock.filename, 1);
325
b4bd4668 326 if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
1020d087
CI
327 die(_("interactive add failed"));
328
329 if (old_index_env && *old_index_env)
330 setenv(INDEX_ENVIRONMENT, old_index_env, 1);
331 else
332 unsetenv(INDEX_ENVIRONMENT);
333
334 discard_cache();
335 read_cache_from(index_lock.filename);
336
337 commit_style = COMMIT_NORMAL;
338 return index_lock.filename;
339 }
340
2888605c
JH
341 /*
342 * Non partial, non as-is commit.
343 *
344 * (1) get the real index;
345 * (2) update the_index as necessary;
346 * (3) write the_index out to the real index (still locked);
347 * (4) return the name of the locked index file.
348 *
349 * The caller should run hooks on the locked real index, and
350 * (A) if all goes well, commit the real index;
351 * (B) on failure, rollback the real index.
352 */
353 if (all || (also && pathspec && *pathspec)) {
1f2362a9 354 fd = hold_locked_index(&index_lock, 1);
7ae02a30 355 add_files_to_cache(also ? prefix : NULL, pathspec, 0);
d38a30df 356 refresh_cache_or_die(refresh_flags);
e859c69b 357 update_main_cache_tree(WRITE_TREE_SILENT);
4ed7cd3a
BC
358 if (write_cache(fd, active_cache, active_nr) ||
359 close_lock_file(&index_lock))
8a6179bc 360 die(_("unable to write new_index file"));
2888605c
JH
361 commit_style = COMMIT_NORMAL;
362 return index_lock.filename;
f5bbc322
KH
363 }
364
2888605c
JH
365 /*
366 * As-is commit.
367 *
368 * (1) return the name of the real index file.
369 *
73276235
MH
370 * The caller should run hooks on the real index,
371 * and create commit from the_index.
2888605c
JH
372 * We still need to refresh the index here.
373 */
ea2d4ed3 374 if (!only && (!pathspec || !*pathspec)) {
2888605c 375 fd = hold_locked_index(&index_lock, 1);
d38a30df 376 refresh_cache_or_die(refresh_flags);
d5f5d0a9 377 if (active_cache_changed) {
e859c69b 378 update_main_cache_tree(WRITE_TREE_SILENT);
d5f5d0a9
JH
379 if (write_cache(fd, active_cache, active_nr) ||
380 commit_locked_index(&index_lock))
8a6179bc 381 die(_("unable to write new_index file"));
d5f5d0a9
JH
382 } else {
383 rollback_lock_file(&index_lock);
384 }
2888605c 385 commit_style = COMMIT_AS_IS;
f5bbc322
KH
386 return get_index_file();
387 }
388
2888605c
JH
389 /*
390 * A partial commit.
391 *
392 * (0) find the set of affected paths;
393 * (1) get lock on the real index file;
394 * (2) update the_index with the given paths;
395 * (3) write the_index out to the real index (still locked);
396 * (4) get lock on the false index file;
397 * (5) reset the_index from HEAD;
398 * (6) update the_index the same way as (2);
399 * (7) write the_index out to the false index file;
400 * (8) return the name of the false index file (still locked);
401 *
402 * The caller should run hooks on the locked false index, and
403 * create commit from it. Then
404 * (A) if all goes well, commit the real index;
405 * (B) on failure, rollback the real index;
406 * In either case, rollback the false index.
407 */
408 commit_style = COMMIT_PARTIAL;
409
b0cea47e
ÆAB
410 if (whence != FROM_COMMIT) {
411 if (whence == FROM_MERGE)
412 die(_("cannot do a partial commit during a merge."));
413 else if (whence == FROM_CHERRY_PICK)
414 die(_("cannot do a partial commit during a cherry-pick."));
415 }
2888605c
JH
416
417 memset(&partial, 0, sizeof(partial));
c455c87c 418 partial.strdup_strings = 1;
06bb643b 419 if (list_paths(&partial, !current_head ? NULL : "HEAD", prefix, pathspec))
2888605c
JH
420 exit(1);
421
422 discard_cache();
423 if (read_cache() < 0)
8a6179bc 424 die(_("cannot read the index"));
2888605c
JH
425
426 fd = hold_locked_index(&index_lock, 1);
427 add_remove_files(&partial);
ef12b50d 428 refresh_cache(REFRESH_QUIET);
4ed7cd3a
BC
429 if (write_cache(fd, active_cache, active_nr) ||
430 close_lock_file(&index_lock))
8a6179bc 431 die(_("unable to write new_index file"));
f5bbc322 432
2888605c 433 fd = hold_lock_file_for_update(&false_lock,
a157400c
JH
434 git_path("next-index-%"PRIuMAX,
435 (uintmax_t) getpid()),
acd3b9ec 436 LOCK_DIE_ON_ERROR);
fa9dcf80 437
06bb643b 438 create_base_index(current_head);
2888605c 439 add_remove_files(&partial);
d37d3203 440 refresh_cache(REFRESH_QUIET);
f5bbc322 441
4ed7cd3a
BC
442 if (write_cache(fd, active_cache, active_nr) ||
443 close_lock_file(&false_lock))
8a6179bc 444 die(_("unable to write temporary index file"));
959ba670
JK
445
446 discard_cache();
447 read_cache_from(false_lock.filename);
448
2888605c 449 return false_lock.filename;
f5bbc322
KH
450}
451
d249b098
JH
452static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
453 struct wt_status *s)
f5bbc322 454{
76e2f7ce
JH
455 unsigned char sha1[20];
456
d249b098
JH
457 if (s->relative_paths)
458 s->prefix = prefix;
f5bbc322
KH
459
460 if (amend) {
d249b098
JH
461 s->amend = 1;
462 s->reference = "HEAD^1";
f5bbc322 463 }
d249b098
JH
464 s->verbose = verbose;
465 s->index_file = index_file;
466 s->fp = fp;
467 s->nowarn = nowarn;
76e2f7ce 468 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
f5bbc322 469
76e2f7ce 470 wt_status_collect(s);
7c9f7038
JK
471
472 switch (status_format) {
473 case STATUS_FORMAT_SHORT:
d4a6bf1f 474 wt_shortstatus_print(s);
7c9f7038
JK
475 break;
476 case STATUS_FORMAT_PORCELAIN:
3207a3a2 477 wt_porcelain_print(s);
7c9f7038 478 break;
84b4202d
JH
479 case STATUS_FORMAT_UNSPECIFIED:
480 die("BUG: finalize_deferred_config() should have been called");
481 break;
f3f47a1e 482 case STATUS_FORMAT_NONE:
7c9f7038
JK
483 case STATUS_FORMAT_LONG:
484 wt_status_print(s);
485 break;
486 }
f5bbc322 487
d249b098 488 return s->commitable;
f5bbc322
KH
489}
490
06bb643b 491static int is_a_merge(const struct commit *current_head)
ec84bd00 492{
06bb643b 493 return !!(current_head->parents && current_head->parents->next);
ec84bd00
PB
494}
495
7dfe8ad6
JH
496static void export_one(const char *var, const char *s, const char *e, int hack)
497{
498 struct strbuf buf = STRBUF_INIT;
499 if (hack)
500 strbuf_addch(&buf, hack);
501 strbuf_addf(&buf, "%.*s", (int)(e - s), s);
502 setenv(var, buf.buf, 1);
503 strbuf_release(&buf);
504}
505
e27ddb64
JH
506static int sane_ident_split(struct ident_split *person)
507{
508 if (!person->name_begin || !person->name_end ||
509 person->name_begin == person->name_end)
510 return 0; /* no human readable name */
511 if (!person->mail_begin || !person->mail_end ||
512 person->mail_begin == person->mail_end)
513 return 0; /* no usable mail */
514 if (!person->date_begin || !person->date_end ||
515 !person->tz_begin || !person->tz_end)
516 return 0;
517 return 1;
518}
519
4c28e4ad 520static void determine_author_info(struct strbuf *author_ident)
a45d46ba
SB
521{
522 char *name, *email, *date;
7dfe8ad6 523 struct ident_split author;
a45d46ba
SB
524
525 name = getenv("GIT_AUTHOR_NAME");
526 email = getenv("GIT_AUTHOR_EMAIL");
527 date = getenv("GIT_AUTHOR_DATE");
528
37f7a857 529 if (author_message) {
a45d46ba 530 const char *a, *lb, *rb, *eol;
2c733fb2 531 size_t len;
a45d46ba 532
37f7a857 533 a = strstr(author_message_buffer, "\nauthor ");
a45d46ba 534 if (!a)
6c80cd29 535 die(_("invalid commit: %s"), author_message);
a45d46ba 536
fb7749e4
JN
537 lb = strchrnul(a + strlen("\nauthor "), '<');
538 rb = strchrnul(lb, '>');
539 eol = strchrnul(rb, '\n');
540 if (!*lb || !*rb || !*eol)
6c80cd29 541 die(_("invalid commit: %s"), author_message);
a45d46ba 542
fb7749e4
JN
543 if (lb == a + strlen("\nauthor "))
544 /* \nauthor <foo@example.com> */
545 name = xcalloc(1, 1);
546 else
547 name = xmemdupz(a + strlen("\nauthor "),
548 (lb - strlen(" ") -
549 (a + strlen("\nauthor "))));
550 email = xmemdupz(lb + strlen("<"), rb - (lb + strlen("<")));
2c733fb2
JH
551 len = eol - (rb + strlen("> "));
552 date = xmalloc(len + 2);
553 *date = '@';
554 memcpy(date + 1, rb + strlen("> "), len);
555 date[len + 1] = '\0';
a45d46ba
SB
556 }
557
558 if (force_author) {
559 const char *lb = strstr(force_author, " <");
560 const char *rb = strchr(force_author, '>');
561
562 if (!lb || !rb)
8a6179bc 563 die(_("malformed --author parameter"));
a45d46ba
SB
564 name = xstrndup(force_author, lb - force_author);
565 email = xstrndup(lb + 2, rb - (lb + 2));
566 }
567
02b47cd7
MV
568 if (force_date)
569 date = force_date;
f9bc573f 570 strbuf_addstr(author_ident, fmt_ident(name, email, date, IDENT_STRICT));
e27ddb64
JH
571 if (!split_ident_line(&author, author_ident->buf, author_ident->len) &&
572 sane_ident_split(&author)) {
7dfe8ad6
JH
573 export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
574 export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
575 export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@');
576 }
a45d46ba
SB
577}
578
4c28e4ad
JH
579static char *cut_ident_timestamp_part(char *string)
580{
581 char *ket = strrchr(string, '>');
582 if (!ket || ket[1] != ' ')
8a6179bc 583 die(_("Malformed ident string: '%s'"), string);
4c28e4ad
JH
584 *++ket = '\0';
585 return ket;
586}
587
d249b098 588static int prepare_to_commit(const char *index_file, const char *prefix,
06bb643b 589 struct commit *current_head,
4c28e4ad
JH
590 struct wt_status *s,
591 struct strbuf *author_ident)
f5bbc322
KH
592{
593 struct stat statbuf;
4c28e4ad 594 struct strbuf committer_ident = STRBUF_INIT;
bc5d248a 595 int commitable, saved_color_setting;
f285a2d7 596 struct strbuf sb = STRBUF_INIT;
f5bbc322 597 char *buffer;
8089c85b
PB
598 const char *hook_arg1 = NULL;
599 const char *hook_arg2 = NULL;
bb1ae3f6 600 int ident_shown = 0;
8b1ae678 601 int clean_message_contents = (cleanup_mode != CLEANUP_NONE);
f5bbc322 602
7dfe8ad6
JH
603 /* This checks and barfs if author is badly specified */
604 determine_author_info(author_ident);
605
ec84bd00
PB
606 if (!no_verify && run_hook(index_file, "pre-commit", NULL))
607 return 0;
f5bbc322 608
89ac1223
PN
609 if (squash_message) {
610 /*
611 * Insert the proper subject line before other commit
612 * message options add their content.
613 */
614 if (use_message && !strcmp(use_message, squash_message))
615 strbuf_addstr(&sb, "squash! ");
616 else {
617 struct pretty_print_context ctx = {0};
618 struct commit *c;
619 c = lookup_commit_reference_by_name(squash_message);
620 if (!c)
8a6179bc 621 die(_("could not lookup commit %s"), squash_message);
89ac1223
PN
622 ctx.output_encoding = get_commit_output_encoding();
623 format_commit_message(c, "squash! %s\n\n", &sb,
624 &ctx);
625 }
626 }
627
f9568530
JS
628 if (message.len) {
629 strbuf_addbuf(&sb, &message);
8089c85b 630 hook_arg1 = "message";
f5bbc322
KH
631 } else if (logfile && !strcmp(logfile, "-")) {
632 if (isatty(0))
8a6179bc 633 fprintf(stderr, _("(reading log message from standard input)\n"));
f5bbc322 634 if (strbuf_read(&sb, 0, 0) < 0)
8a6179bc 635 die_errno(_("could not read log from standard input"));
8089c85b 636 hook_arg1 = "message";
f5bbc322
KH
637 } else if (logfile) {
638 if (strbuf_read_file(&sb, logfile, 0) < 0)
8a6179bc 639 die_errno(_("could not read log file '%s'"),
d824cbba 640 logfile);
8089c85b 641 hook_arg1 = "message";
f5bbc322
KH
642 } else if (use_message) {
643 buffer = strstr(use_message_buffer, "\n\n");
d9a93575 644 if (!use_editor && (!buffer || buffer[2] == '\0'))
8a6179bc 645 die(_("commit has empty message"));
f5bbc322 646 strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
8089c85b
PB
647 hook_arg1 = "commit";
648 hook_arg2 = use_message;
d71b8ba7
PN
649 } else if (fixup_message) {
650 struct pretty_print_context ctx = {0};
651 struct commit *commit;
652 commit = lookup_commit_reference_by_name(fixup_message);
653 if (!commit)
8a6179bc 654 die(_("could not lookup commit %s"), fixup_message);
d71b8ba7
PN
655 ctx.output_encoding = get_commit_output_encoding();
656 format_commit_message(commit, "fixup! %s\n\n",
657 &sb, &ctx);
658 hook_arg1 = "message";
f5bbc322
KH
659 } else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
660 if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
8a6179bc 661 die_errno(_("could not read MERGE_MSG"));
8089c85b 662 hook_arg1 = "merge";
f5bbc322
KH
663 } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
664 if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
8a6179bc 665 die_errno(_("could not read SQUASH_MSG"));
8089c85b 666 hook_arg1 = "squash";
2140b140 667 } else if (template_file) {
f5bbc322 668 if (strbuf_read_file(&sb, template_file, 0) < 0)
8a6179bc 669 die_errno(_("could not read '%s'"), template_file);
8089c85b 670 hook_arg1 = "template";
8b1ae678 671 clean_message_contents = 0;
f5bbc322
KH
672 }
673
8089c85b 674 /*
37f7a857
JS
675 * The remaining cases don't modify the template message, but
676 * just set the argument(s) to the prepare-commit-msg hook.
8089c85b 677 */
37f7a857 678 else if (whence == FROM_MERGE)
8089c85b 679 hook_arg1 = "merge";
37f7a857
JS
680 else if (whence == FROM_CHERRY_PICK) {
681 hook_arg1 = "commit";
682 hook_arg2 = "CHERRY_PICK_HEAD";
683 }
8089c85b 684
89ac1223
PN
685 if (squash_message) {
686 /*
687 * If squash_commit was used for the commit subject,
688 * then we're possibly hijacking other commit log options.
689 * Reset the hook args to tell the real story.
690 */
691 hook_arg1 = "message";
692 hook_arg2 = "";
693 }
694
37f3012f
JN
695 s->fp = fopen(git_path(commit_editmsg), "w");
696 if (s->fp == NULL)
8a6179bc 697 die_errno(_("could not open '%s'"), git_path(commit_editmsg));
f5bbc322 698
8b1ae678 699 if (clean_message_contents)
5f065737 700 stripspace(&sb, 0);
f5bbc322
KH
701
702 if (signoff) {
5ed75e2a
MV
703 /*
704 * See if we have a Conflicts: block at the end. If yes, count
705 * its size, so we can ignore it.
706 */
707 int ignore_footer = 0;
708 int i, eol, previous = 0;
709 const char *nl;
710
711 for (i = 0; i < sb.len; i++) {
712 nl = memchr(sb.buf + i, '\n', sb.len - i);
713 if (nl)
714 eol = nl - sb.buf;
715 else
716 eol = sb.len;
717 if (!prefixcmp(sb.buf + previous, "\nConflicts:\n")) {
718 ignore_footer = sb.len - previous;
719 break;
720 }
721 while (i < eol)
722 i++;
723 previous = eol;
2150554b 724 }
5ed75e2a 725
bab4d109 726 append_signoff(&sb, ignore_footer, 0);
f5bbc322
KH
727 }
728
37f3012f 729 if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
8a6179bc 730 die_errno(_("could not write commit template"));
13208572 731
f5bbc322
KH
732 strbuf_release(&sb);
733
bb1ae3f6 734 /* This checks if committer ident is explicitly given */
f20f3878 735 strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
bed575e4 736 if (use_editor && include_status) {
4c28e4ad 737 char *ai_tmp, *ci_tmp;
37f7a857 738 if (whence != FROM_COMMIT)
b926c0d1 739 status_printf_ln(s, GIT_COLOR_NORMAL,
b0cea47e
ÆAB
740 whence == FROM_MERGE
741 ? _("\n"
742 "It looks like you may be committing a merge.\n"
743 "If this is not correct, please remove the file\n"
744 " %s\n"
745 "and try again.\n")
746 : _("\n"
747 "It looks like you may be committing a cherry-pick.\n"
748 "If this is not correct, please remove the file\n"
749 " %s\n"
750 "and try again.\n"),
37f7a857
JS
751 git_path(whence == FROM_MERGE
752 ? "MERGE_HEAD"
753 : "CHERRY_PICK_HEAD"));
ec84bd00 754
b926c0d1 755 fprintf(s->fp, "\n");
ec84bd00 756 if (cleanup_mode == CLEANUP_ALL)
4064e665
ÆAB
757 status_printf(s, GIT_COLOR_NORMAL,
758 _("Please enter the commit message for your changes."
eff80a9f
JH
759 " Lines starting\nwith '%c' will be ignored, and an empty"
760 " message aborts the commit.\n"), comment_line_char);
ec84bd00 761 else /* CLEANUP_SPACE, that is. */
4064e665
ÆAB
762 status_printf(s, GIT_COLOR_NORMAL,
763 _("Please enter the commit message for your changes."
eff80a9f
JH
764 " Lines starting\n"
765 "with '%c' will be kept; you may remove them"
766 " yourself if you want to.\n"
767 "An empty message aborts the commit.\n"), comment_line_char);
ec84bd00 768 if (only_include_assumed)
b926c0d1
JN
769 status_printf_ln(s, GIT_COLOR_NORMAL,
770 "%s", only_include_assumed);
ec84bd00 771
4c28e4ad
JH
772 ai_tmp = cut_ident_timestamp_part(author_ident->buf);
773 ci_tmp = cut_ident_timestamp_part(committer_ident.buf);
774 if (strcmp(author_ident->buf, committer_ident.buf))
b926c0d1 775 status_printf_ln(s, GIT_COLOR_NORMAL,
fe8165cd
ÆAB
776 _("%s"
777 "Author: %s"),
b926c0d1 778 ident_shown++ ? "" : "\n",
4c28e4ad 779 author_ident->buf);
e83dbe80 780
d6991cee 781 if (!committer_ident_sufficiently_given())
b926c0d1 782 status_printf_ln(s, GIT_COLOR_NORMAL,
fe8165cd
ÆAB
783 _("%s"
784 "Committer: %s"),
b926c0d1 785 ident_shown++ ? "" : "\n",
4c28e4ad 786 committer_ident.buf);
bb1ae3f6
SB
787
788 if (ident_shown)
b926c0d1 789 status_printf_ln(s, GIT_COLOR_NORMAL, "");
bb1ae3f6 790
d249b098
JH
791 saved_color_setting = s->use_color;
792 s->use_color = 0;
37f3012f 793 commitable = run_status(s->fp, index_file, prefix, 1, s);
d249b098 794 s->use_color = saved_color_setting;
4c28e4ad
JH
795
796 *ai_tmp = ' ';
797 *ci_tmp = ' ';
ec84bd00 798 } else {
d616a239 799 unsigned char sha1[20];
fbcf1184 800 const char *parent = "HEAD";
7168624c 801
7168624c 802 if (!active_nr && read_cache() < 0)
8a6179bc 803 die(_("Cannot read index"));
7168624c 804
fbcf1184
JH
805 if (amend)
806 parent = "HEAD^1";
807
d616a239 808 if (get_sha1(parent, sha1))
ec84bd00 809 commitable = !!active_nr;
75f3ff2e
SB
810 else
811 commitable = index_differs_from(parent, 0);
ec84bd00 812 }
4c28e4ad 813 strbuf_release(&committer_ident);
d616a239 814
37f3012f 815 fclose(s->fp);
7168624c 816
37f7a857
JS
817 /*
818 * Reject an attempt to record a non-merge empty commit without
819 * explicit --allow-empty. In the cherry-pick case, it may be
820 * empty due to conflict resolution, which the user should okay.
821 */
822 if (!commitable && whence != FROM_MERGE && !allow_empty &&
06bb643b 823 !(amend && is_a_merge(current_head))) {
d249b098 824 run_status(stdout, index_file, prefix, 0, s);
f197ed2f 825 if (amend)
fc88e316 826 fputs(_(empty_amend_advice), stderr);
c17592a7 827 else if (whence == FROM_CHERRY_PICK) {
6c80cd29 828 fputs(_(empty_cherry_pick_advice), stderr);
c17592a7
JK
829 if (!sequencer_in_use)
830 fputs(_(empty_cherry_pick_advice_single), stderr);
831 else
832 fputs(_(empty_cherry_pick_advice_multi), stderr);
833 }
ec84bd00 834 return 0;
7168624c
AR
835 }
836
ec84bd00
PB
837 /*
838 * Re-read the index as pre-commit hook could have updated it,
839 * and write it out as a tree. We must do this before we invoke
840 * the editor and after we invoke run_status above.
841 */
842 discard_cache();
843 read_cache_from(index_file);
996277c5 844 if (update_main_cache_tree(0)) {
8a6179bc 845 error(_("Error building trees"));
ec84bd00 846 return 0;
7168624c 847 }
f5bbc322 848
8089c85b
PB
849 if (run_hook(index_file, "prepare-commit-msg",
850 git_path(commit_editmsg), hook_arg1, hook_arg2, NULL))
851 return 0;
f5bbc322 852
ec84bd00
PB
853 if (use_editor) {
854 char index[PATH_MAX];
66dbfd55
GV
855 const char *env[2] = { NULL };
856 env[0] = index;
ec84bd00 857 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
7198203a
SB
858 if (launch_editor(git_path(commit_editmsg), NULL, env)) {
859 fprintf(stderr,
8a6179bc 860 _("Please supply the message using either -m or -F option.\n"));
7198203a
SB
861 exit(1);
862 }
ec84bd00 863 }
f5bbc322 864
ec84bd00
PB
865 if (!no_verify &&
866 run_hook(index_file, "commit-msg", git_path(commit_editmsg), NULL)) {
867 return 0;
868 }
f5bbc322 869
ec84bd00 870 return 1;
f5bbc322
KH
871}
872
b2eda9bd 873static int rest_is_empty(struct strbuf *sb, int start)
f5bbc322 874{
b2eda9bd 875 int i, eol;
f5bbc322 876 const char *nl;
f5bbc322
KH
877
878 /* Check if the rest is just whitespace and Signed-of-by's. */
879 for (i = start; i < sb->len; i++) {
880 nl = memchr(sb->buf + i, '\n', sb->len - i);
881 if (nl)
882 eol = nl - sb->buf;
883 else
884 eol = sb->len;
885
886 if (strlen(sign_off_header) <= eol - i &&
887 !prefixcmp(sb->buf + i, sign_off_header)) {
888 i = eol;
889 continue;
890 }
891 while (i < eol)
892 if (!isspace(sb->buf[i++]))
893 return 0;
894 }
895
896 return 1;
897}
898
b2eda9bd
JH
899/*
900 * Find out if the message in the strbuf contains only whitespace and
901 * Signed-off-by lines.
902 */
903static int message_is_empty(struct strbuf *sb)
904{
905 if (cleanup_mode == CLEANUP_NONE && sb->len)
906 return 0;
907 return rest_is_empty(sb, 0);
908}
909
910/*
911 * See if the user edited the message in the editor or left what
912 * was in the template intact
913 */
914static int template_untouched(struct strbuf *sb)
915{
916 struct strbuf tmpl = STRBUF_INIT;
917 char *start;
918
919 if (cleanup_mode == CLEANUP_NONE && sb->len)
920 return 0;
921
922 if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
923 return 0;
924
925 stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
926 start = (char *)skip_prefix(sb->buf, tmpl.buf);
927 if (!start)
928 start = sb->buf;
929 strbuf_release(&tmpl);
930 return rest_is_empty(sb, start - sb->buf);
931}
932
146ea068
JH
933static const char *find_author_by_nickname(const char *name)
934{
935 struct rev_info revs;
936 struct commit *commit;
937 struct strbuf buf = STRBUF_INIT;
938 const char *av[20];
939 int ac = 0;
940
941 init_revisions(&revs, NULL);
942 strbuf_addf(&buf, "--author=%s", name);
943 av[++ac] = "--all";
944 av[++ac] = "-i";
945 av[++ac] = buf.buf;
946 av[++ac] = NULL;
947 setup_revisions(ac, av, &revs, NULL);
948 prepare_revision_walk(&revs);
949 commit = get_revision(&revs);
950 if (commit) {
dd2e794a
TR
951 struct pretty_print_context ctx = {0};
952 ctx.date_mode = DATE_NORMAL;
146ea068 953 strbuf_release(&buf);
dd2e794a 954 format_commit_message(commit, "%an <%ae>", &buf, &ctx);
146ea068
JH
955 return strbuf_detach(&buf, NULL);
956 }
8a6179bc 957 die(_("No existing author found with '%s'"), name);
146ea068
JH
958}
959
76e2f7ce
JH
960
961static void handle_untracked_files_arg(struct wt_status *s)
962{
963 if (!untracked_files_arg)
964 ; /* default already initialized */
965 else if (!strcmp(untracked_files_arg, "no"))
966 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
967 else if (!strcmp(untracked_files_arg, "normal"))
968 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
969 else if (!strcmp(untracked_files_arg, "all"))
970 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
971 else
8a6179bc 972 die(_("Invalid untracked files mode '%s'"), untracked_files_arg);
76e2f7ce
JH
973}
974
37f7a857
JS
975static const char *read_commit_message(const char *name)
976{
dd0d388c 977 const char *out_enc;
37f7a857
JS
978 struct commit *commit;
979
980 commit = lookup_commit_reference_by_name(name);
981 if (!commit)
6c80cd29 982 die(_("could not lookup commit %s"), name);
37f7a857 983 out_enc = get_commit_output_encoding();
5a10d236 984 return logmsg_reencode(commit, NULL, out_enc);
37f7a857
JS
985}
986
84b4202d
JH
987/*
988 * Enumerate what needs to be propagated when --porcelain
989 * is not in effect here.
990 */
991static struct status_deferred_config {
992 enum status_format status_format;
993 int show_branch;
994} status_deferred_config = {
995 STATUS_FORMAT_UNSPECIFIED,
996 -1 /* unspecified */
997};
998
999static void finalize_deferred_config(struct wt_status *s)
1000{
1001 int use_deferred_config = (status_format != STATUS_FORMAT_PORCELAIN &&
1002 !s->null_termination);
1003
1004 if (s->null_termination) {
1005 if (status_format == STATUS_FORMAT_NONE ||
1006 status_format == STATUS_FORMAT_UNSPECIFIED)
1007 status_format = STATUS_FORMAT_PORCELAIN;
1008 else if (status_format == STATUS_FORMAT_LONG)
1009 die(_("--long and -z are incompatible"));
1010 }
1011
1012 if (use_deferred_config && status_format == STATUS_FORMAT_UNSPECIFIED)
1013 status_format = status_deferred_config.status_format;
1014 if (status_format == STATUS_FORMAT_UNSPECIFIED)
1015 status_format = STATUS_FORMAT_NONE;
1016
1017 if (use_deferred_config && s->show_branch < 0)
1018 s->show_branch = status_deferred_config.show_branch;
1019 if (s->show_branch < 0)
1020 s->show_branch = 0;
1021}
1022
2f02b25f 1023static int parse_and_validate_options(int argc, const char *argv[],
036dbbfb 1024 const struct option *options,
dbd0f5c7 1025 const char * const usage[],
d249b098 1026 const char *prefix,
06bb643b 1027 struct commit *current_head,
d249b098 1028 struct wt_status *s)
f5bbc322
KH
1029{
1030 int f = 0;
1031
036dbbfb 1032 argc = parse_options(argc, argv, prefix, options, usage, 0);
84b4202d 1033 finalize_deferred_config(s);
f5bbc322 1034
146ea068
JH
1035 if (force_author && !strchr(force_author, '>'))
1036 force_author = find_author_by_nickname(force_author);
1037
c51f6cee 1038 if (force_author && renew_authorship)
8a6179bc 1039 die(_("Using both --reset-author and --author does not make sense"));
c51f6cee 1040
25206778 1041 if (logfile || have_option_m || use_message || fixup_message)
4803466f 1042 use_editor = 0;
ca1ba201
JH
1043 if (0 <= edit_flag)
1044 use_editor = edit_flag;
406400ce
PB
1045 if (!use_editor)
1046 setenv("GIT_EDITOR", ":", 1);
f5bbc322 1047
f5bbc322 1048 /* Sanity check options */
06bb643b 1049 if (amend && !current_head)
8a6179bc 1050 die(_("You have nothing to amend."));
b0cea47e
ÆAB
1051 if (amend && whence != FROM_COMMIT) {
1052 if (whence == FROM_MERGE)
1053 die(_("You are in the middle of a merge -- cannot amend."));
1054 else if (whence == FROM_CHERRY_PICK)
1055 die(_("You are in the middle of a cherry-pick -- cannot amend."));
1056 }
89ac1223 1057 if (fixup_message && squash_message)
9c227655 1058 die(_("Options --squash and --fixup cannot be used together"));
f5bbc322
KH
1059 if (use_message)
1060 f++;
1061 if (edit_message)
1062 f++;
d71b8ba7
PN
1063 if (fixup_message)
1064 f++;
f5bbc322
KH
1065 if (logfile)
1066 f++;
1067 if (f > 1)
8a6179bc 1068 die(_("Only one of -c/-C/-F/--fixup can be used."));
f9568530 1069 if (message.len && f > 0)
8a6179bc 1070 die((_("Option -m cannot be combined with -c/-C/-F/--fixup.")));
010c7dbc
JH
1071 if (f || message.len)
1072 template_file = NULL;
f5bbc322
KH
1073 if (edit_message)
1074 use_message = edit_message;
d71b8ba7 1075 if (amend && !use_message && !fixup_message)
f5bbc322 1076 use_message = "HEAD";
37f7a857 1077 if (!use_message && whence != FROM_CHERRY_PICK && renew_authorship)
8a6179bc 1078 die(_("--reset-author can be used only with -C, -c or --amend."));
f5bbc322 1079 if (use_message) {
37f7a857
JS
1080 use_message_buffer = read_commit_message(use_message);
1081 if (!renew_authorship) {
1082 author_message = use_message;
1083 author_message_buffer = use_message_buffer;
1084 }
1085 }
1086 if (whence == FROM_CHERRY_PICK && !renew_authorship) {
1087 author_message = "CHERRY_PICK_HEAD";
1088 author_message_buffer = read_commit_message(author_message);
f5bbc322
KH
1089 }
1090
b4bd4668
CI
1091 if (patch_interactive)
1092 interactive = 1;
1093
f5bbc322 1094 if (!!also + !!only + !!all + !!interactive > 1)
b4bd4668 1095 die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
f5bbc322 1096 if (argc == 0 && (also || (only && !amend)))
8a6179bc 1097 die(_("No paths with --include/--only does not make sense."));
f5bbc322 1098 if (argc == 0 && only && amend)
8a6179bc 1099 only_include_assumed = _("Clever... amending the last one with dirty index.");
3c5283f8 1100 if (argc > 0 && !also && !only)
8a6179bc 1101 only_include_assumed = _("Explicit paths specified without -i nor -o; assuming --only paths...");
5f065737
AR
1102 if (!cleanup_arg || !strcmp(cleanup_arg, "default"))
1103 cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE;
1104 else if (!strcmp(cleanup_arg, "verbatim"))
1105 cleanup_mode = CLEANUP_NONE;
1106 else if (!strcmp(cleanup_arg, "whitespace"))
1107 cleanup_mode = CLEANUP_SPACE;
1108 else if (!strcmp(cleanup_arg, "strip"))
1109 cleanup_mode = CLEANUP_ALL;
1110 else
8a6179bc 1111 die(_("Invalid cleanup mode %s"), cleanup_arg);
f5bbc322 1112
76e2f7ce 1113 handle_untracked_files_arg(s);
4bfee30a 1114
f5bbc322 1115 if (all && argc > 0)
8a6179bc 1116 die(_("Paths with -a does not make sense."));
f5bbc322 1117
f3f47a1e 1118 if (status_format != STATUS_FORMAT_NONE)
7c9f7038
JK
1119 dry_run = 1;
1120
f5bbc322
KH
1121 return argc;
1122}
1123
d249b098 1124static int dry_run_commit(int argc, const char **argv, const char *prefix,
06bb643b 1125 const struct commit *current_head, struct wt_status *s)
f5bbc322 1126{
f5bbc322 1127 int commitable;
3a5d13a3 1128 const char *index_file;
f5bbc322 1129
06bb643b 1130 index_file = prepare_index(argc, argv, prefix, current_head, 1);
d249b098 1131 commitable = run_status(stdout, index_file, prefix, 0, s);
3a5d13a3 1132 rollback_index_files();
f5bbc322 1133
3a5d13a3
JH
1134 return commitable ? 0 : 1;
1135}
1136
f766b367
JH
1137static int parse_status_slot(const char *var, int offset)
1138{
1139 if (!strcasecmp(var+offset, "header"))
1140 return WT_STATUS_HEADER;
1d282327
AA
1141 if (!strcasecmp(var+offset, "branch"))
1142 return WT_STATUS_ONBRANCH;
f766b367
JH
1143 if (!strcasecmp(var+offset, "updated")
1144 || !strcasecmp(var+offset, "added"))
1145 return WT_STATUS_UPDATED;
1146 if (!strcasecmp(var+offset, "changed"))
1147 return WT_STATUS_CHANGED;
1148 if (!strcasecmp(var+offset, "untracked"))
1149 return WT_STATUS_UNTRACKED;
1150 if (!strcasecmp(var+offset, "nobranch"))
1151 return WT_STATUS_NOBRANCH;
1152 if (!strcasecmp(var+offset, "unmerged"))
1153 return WT_STATUS_UNMERGED;
8b8e8624 1154 return -1;
f766b367
JH
1155}
1156
1157static int git_status_config(const char *k, const char *v, void *cb)
1158{
1159 struct wt_status *s = cb;
1160
323d0530 1161 if (!prefixcmp(k, "column."))
4d2292e9 1162 return git_column_config(k, v, "status", &s->colopts);
f766b367
JH
1163 if (!strcmp(k, "status.submodulesummary")) {
1164 int is_bool;
1165 s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
1166 if (is_bool && s->submodule_summary)
1167 s->submodule_summary = -1;
1168 return 0;
1169 }
4fb5166a
JJGG
1170 if (!strcmp(k, "status.short")) {
1171 if (git_config_bool(k, v))
84b4202d 1172 status_deferred_config.status_format = STATUS_FORMAT_SHORT;
4fb5166a 1173 else
84b4202d 1174 status_deferred_config.status_format = STATUS_FORMAT_NONE;
4fb5166a
JJGG
1175 return 0;
1176 }
ec85d070 1177 if (!strcmp(k, "status.branch")) {
84b4202d 1178 status_deferred_config.show_branch = git_config_bool(k, v);
ec85d070
JJGG
1179 return 0;
1180 }
f766b367 1181 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
e269eb79 1182 s->use_color = git_config_colorbool(k, v);
f766b367
JH
1183 return 0;
1184 }
1185 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
1186 int slot = parse_status_slot(k, 13);
8b8e8624
JK
1187 if (slot < 0)
1188 return 0;
f766b367
JH
1189 if (!v)
1190 return config_error_nonbool(k);
1191 color_parse(v, k, s->color_palette[slot]);
1192 return 0;
1193 }
1194 if (!strcmp(k, "status.relativepaths")) {
1195 s->relative_paths = git_config_bool(k, v);
1196 return 0;
1197 }
1198 if (!strcmp(k, "status.showuntrackedfiles")) {
1199 if (!v)
1200 return config_error_nonbool(k);
1201 else if (!strcmp(v, "no"))
1202 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
1203 else if (!strcmp(v, "normal"))
1204 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
1205 else if (!strcmp(v, "all"))
1206 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
1207 else
8a6179bc 1208 return error(_("Invalid untracked files mode '%s'"), v);
f766b367
JH
1209 return 0;
1210 }
1211 return git_diff_ui_config(k, v, NULL);
1212}
1213
3a5d13a3
JH
1214int cmd_status(int argc, const char **argv, const char *prefix)
1215{
036dbbfb 1216 static struct wt_status s;
4bb6644d 1217 int fd;
76e2f7ce 1218 unsigned char sha1[20];
9e4b7ab6 1219 static struct option builtin_status_options[] = {
f2276316 1220 OPT__VERBOSE(&verbose, N_("be verbose")),
dd2be243 1221 OPT_SET_INT('s', "short", &status_format,
f2276316 1222 N_("show status concisely"), STATUS_FORMAT_SHORT),
84b4202d
JH
1223 OPT_BOOL('b', "branch", &s.show_branch,
1224 N_("show branch information")),
6f157871 1225 OPT_SET_INT(0, "porcelain", &status_format,
f2276316 1226 N_("machine-readable output"),
6f157871 1227 STATUS_FORMAT_PORCELAIN),
f3f47a1e
JK
1228 OPT_SET_INT(0, "long", &status_format,
1229 N_("show status in long format (default)"),
1230 STATUS_FORMAT_LONG),
3207a3a2 1231 OPT_BOOLEAN('z', "null", &s.null_termination,
f2276316 1232 N_("terminate entries with NUL")),
76e2f7ce 1233 { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
f2276316
NTND
1234 N_("mode"),
1235 N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
76e2f7ce 1236 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
2381e39e 1237 OPT_BOOLEAN(0, "ignored", &show_ignored_in_status,
f2276316
NTND
1238 N_("show ignored files")),
1239 { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, N_("when"),
1240 N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
46a958b3 1241 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
9c23f4c5 1242 OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")),
76e2f7ce
JH
1243 OPT_END(),
1244 };
1245
5d3dd915
NTND
1246 if (argc == 2 && !strcmp(argv[1], "-h"))
1247 usage_with_options(builtin_status_usage, builtin_status_options);
1248
d249b098 1249 wt_status_prepare(&s);
302ad7a9 1250 gitmodules_config();
d249b098 1251 git_config(git_status_config, &s);
37f7a857 1252 determine_whence(&s);
76e2f7ce 1253 argc = parse_options(argc, argv, prefix,
9e4b7ab6
JH
1254 builtin_status_options,
1255 builtin_status_usage, 0);
4d2292e9 1256 finalize_colopts(&s.colopts, -1);
84b4202d 1257 finalize_deferred_config(&s);
000f97bd 1258
76e2f7ce 1259 handle_untracked_files_arg(&s);
2381e39e
JH
1260 if (show_ignored_in_status)
1261 s.show_ignored_files = 1;
76e2f7ce
JH
1262 if (*argv)
1263 s.pathspec = get_pathspec(prefix, argv);
1264
149794dd 1265 read_cache_preload(s.pathspec);
688cd6d2 1266 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec, NULL, NULL);
4bb6644d
MH
1267
1268 fd = hold_locked_index(&index_lock, 0);
ccdc4ec3
JH
1269 if (0 <= fd)
1270 update_index_if_able(&the_index, &index_lock);
4bb6644d 1271
76e2f7ce 1272 s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
46a958b3 1273 s.ignore_submodule_arg = ignore_submodule_arg;
76e2f7ce
JH
1274 wt_status_collect(&s);
1275
8661768f
JK
1276 if (s.relative_paths)
1277 s.prefix = prefix;
38920dd6 1278
dd2be243
JK
1279 switch (status_format) {
1280 case STATUS_FORMAT_SHORT:
d4a6bf1f 1281 wt_shortstatus_print(&s);
dd2be243 1282 break;
6f157871 1283 case STATUS_FORMAT_PORCELAIN:
3207a3a2 1284 wt_porcelain_print(&s);
6f157871 1285 break;
84b4202d
JH
1286 case STATUS_FORMAT_UNSPECIFIED:
1287 die("BUG: finalize_deferred_config() should have been called");
1288 break;
f3f47a1e 1289 case STATUS_FORMAT_NONE:
dd2be243 1290 case STATUS_FORMAT_LONG:
173e6c88 1291 s.verbose = verbose;
46a958b3 1292 s.ignore_submodule_arg = ignore_submodule_arg;
173e6c88 1293 wt_status_print(&s);
dd2be243 1294 break;
173e6c88 1295 }
76e2f7ce 1296 return 0;
f5bbc322
KH
1297}
1298
06bb643b
JH
1299static void print_summary(const char *prefix, const unsigned char *sha1,
1300 int initial_commit)
f5bbc322
KH
1301{
1302 struct rev_info rev;
1303 struct commit *commit;
49ff9a7a 1304 struct strbuf format = STRBUF_INIT;
c85db254 1305 unsigned char junk_sha1[20];
d5a35c11 1306 const char *head;
49ff9a7a
JK
1307 struct pretty_print_context pctx = {0};
1308 struct strbuf author_ident = STRBUF_INIT;
1309 struct strbuf committer_ident = STRBUF_INIT;
f5bbc322
KH
1310
1311 commit = lookup_commit(sha1);
1312 if (!commit)
8a6179bc 1313 die(_("couldn't look up newly created commit"));
5e7d4d3e 1314 if (parse_commit(commit))
8a6179bc 1315 die(_("could not parse newly created commit"));
f5bbc322 1316
49ff9a7a
JK
1317 strbuf_addstr(&format, "format:%h] %s");
1318
1319 format_commit_message(commit, "%an <%ae>", &author_ident, &pctx);
1320 format_commit_message(commit, "%cn <%ce>", &committer_ident, &pctx);
1321 if (strbuf_cmp(&author_ident, &committer_ident)) {
1322 strbuf_addstr(&format, "\n Author: ");
1323 strbuf_addbuf_percentquote(&format, &author_ident);
1324 }
d6991cee 1325 if (!committer_ident_sufficiently_given()) {
49ff9a7a
JK
1326 strbuf_addstr(&format, "\n Committer: ");
1327 strbuf_addbuf_percentquote(&format, &committer_ident);
b706fcfe
JK
1328 if (advice_implicit_identity) {
1329 strbuf_addch(&format, '\n');
fc88e316 1330 strbuf_addstr(&format, _(implicit_ident_advice));
b706fcfe 1331 }
49ff9a7a
JK
1332 }
1333 strbuf_release(&author_ident);
1334 strbuf_release(&committer_ident);
1335
f5bbc322
KH
1336 init_revisions(&rev, prefix);
1337 setup_revisions(0, NULL, &rev, NULL);
1338
f5bbc322
KH
1339 rev.diff = 1;
1340 rev.diffopt.output_format =
1341 DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY;
1342
1343 rev.verbose_header = 1;
1344 rev.show_root_diff = 1;
49ff9a7a 1345 get_commit_format(format.buf, &rev);
bf82a150 1346 rev.always_show_header = 0;
3eb2a15e 1347 rev.diffopt.detect_rename = 1;
3eb2a15e 1348 rev.diffopt.break_opt = 0;
15964563 1349 diff_setup_done(&rev.diffopt);
f5bbc322 1350
8cad4744 1351 head = resolve_ref_unsafe("HEAD", junk_sha1, 0, NULL);
c5ee71fd 1352 printf("[%s%s ",
c85db254
JK
1353 !prefixcmp(head, "refs/heads/") ?
1354 head + 11 :
1355 !strcmp(head, "HEAD") ?
7f5673d7 1356 _("detached HEAD") :
c85db254 1357 head,
7f5673d7 1358 initial_commit ? _(" (root-commit)") : "");
f5bbc322 1359
bf82a150 1360 if (!log_tree_commit(&rev, commit)) {
a45e1a87
TRC
1361 rev.always_show_header = 1;
1362 rev.use_terminator = 1;
1363 log_tree_commit(&rev, commit);
bf82a150 1364 }
a45e1a87 1365
fc6f19fe 1366 strbuf_release(&format);
f5bbc322
KH
1367}
1368
186458b1 1369static int git_commit_config(const char *k, const char *v, void *cb)
f5bbc322 1370{
d249b098 1371 struct wt_status *s = cb;
ba3c69a9 1372 int status;
d249b098 1373
984c6e7e 1374 if (!strcmp(k, "commit.template"))
395de250 1375 return git_config_pathname(&template_file, k, v);
bed575e4
JHI
1376 if (!strcmp(k, "commit.status")) {
1377 include_status = git_config_bool(k, v);
1378 return 0;
1379 }
51fb3a3d
RT
1380 if (!strcmp(k, "commit.cleanup"))
1381 return git_config_string(&cleanup_arg, k, v);
f5bbc322 1382
ba3c69a9
JH
1383 status = git_gpg_config(k, v, NULL);
1384 if (status)
1385 return status;
d249b098 1386 return git_status_config(k, v, s);
f5bbc322
KH
1387}
1388
6f6bee3b
TR
1389static int run_rewrite_hook(const unsigned char *oldsha1,
1390 const unsigned char *newsha1)
1391{
1392 /* oldsha1 SP newsha1 LF NUL */
1393 static char buf[2*40 + 3];
1394 struct child_process proc;
1395 const char *argv[3];
1396 int code;
1397 size_t n;
1398
5a7da2dc
AS
1399 argv[0] = find_hook("post-rewrite");
1400 if (!argv[0])
6f6bee3b
TR
1401 return 0;
1402
6f6bee3b
TR
1403 argv[1] = "amend";
1404 argv[2] = NULL;
1405
1406 memset(&proc, 0, sizeof(proc));
1407 proc.argv = argv;
1408 proc.in = -1;
1409 proc.stdout_to_stderr = 1;
1410
1411 code = start_command(&proc);
1412 if (code)
1413 return code;
1414 n = snprintf(buf, sizeof(buf), "%s %s\n",
1415 sha1_to_hex(oldsha1), sha1_to_hex(newsha1));
1416 write_in_full(proc.in, buf, n);
1417 close(proc.in);
1418 return finish_command(&proc);
1419}
1420
f5bbc322
KH
1421int cmd_commit(int argc, const char **argv, const char *prefix)
1422{
036dbbfb
JK
1423 static struct wt_status s;
1424 static struct option builtin_commit_options[] = {
9c23f4c5
NTND
1425 OPT__QUIET(&quiet, N_("suppress summary after successful commit")),
1426 OPT__VERBOSE(&verbose, N_("show diff in commit message template")),
1427
1428 OPT_GROUP(N_("Commit message options")),
1429 OPT_FILENAME('F', "file", &logfile, N_("read message from file")),
1430 OPT_STRING(0, "author", &force_author, N_("author"), N_("override author for commit")),
1431 OPT_STRING(0, "date", &force_date, N_("date"), N_("override date for commit")),
1432 OPT_CALLBACK('m', "message", &message, N_("message"), N_("commit message"), opt_parse_m),
1433 OPT_STRING('c', "reedit-message", &edit_message, N_("commit"), N_("reuse and edit message from specified commit")),
1434 OPT_STRING('C', "reuse-message", &use_message, N_("commit"), N_("reuse message from specified commit")),
1435 OPT_STRING(0, "fixup", &fixup_message, N_("commit"), N_("use autosquash formatted message to fixup specified commit")),
1436 OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
1437 OPT_BOOLEAN(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")),
1438 OPT_BOOLEAN('s', "signoff", &signoff, N_("add Signed-off-by:")),
1439 OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
1440 OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),
1441 OPT_STRING(0, "cleanup", &cleanup_arg, N_("default"), N_("how to strip spaces and #comments from message")),
1442 OPT_BOOLEAN(0, "status", &include_status, N_("include status in commit message template")),
1443 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key id"),
1444 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
036dbbfb
JK
1445 /* end commit message options */
1446
9c23f4c5
NTND
1447 OPT_GROUP(N_("Commit contents options")),
1448 OPT_BOOLEAN('a', "all", &all, N_("commit all changed files")),
1449 OPT_BOOLEAN('i', "include", &also, N_("add specified files to index for commit")),
1450 OPT_BOOLEAN(0, "interactive", &interactive, N_("interactively add files")),
1451 OPT_BOOLEAN('p', "patch", &patch_interactive, N_("interactively add changes")),
1452 OPT_BOOLEAN('o', "only", &only, N_("commit only specified files")),
1453 OPT_BOOLEAN('n', "no-verify", &no_verify, N_("bypass pre-commit hook")),
1454 OPT_BOOLEAN(0, "dry-run", &dry_run, N_("show what would be committed")),
1455 OPT_SET_INT(0, "short", &status_format, N_("show status concisely"),
036dbbfb 1456 STATUS_FORMAT_SHORT),
84b4202d 1457 OPT_BOOL(0, "branch", &s.show_branch, N_("show branch information")),
036dbbfb 1458 OPT_SET_INT(0, "porcelain", &status_format,
9c23f4c5 1459 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN),
f3f47a1e
JK
1460 OPT_SET_INT(0, "long", &status_format,
1461 N_("show status in long format (default)"),
1462 STATUS_FORMAT_LONG),
3207a3a2 1463 OPT_BOOLEAN('z', "null", &s.null_termination,
9c23f4c5
NTND
1464 N_("terminate entries with NUL")),
1465 OPT_BOOLEAN(0, "amend", &amend, N_("amend previous commit")),
1466 OPT_BOOLEAN(0, "no-post-rewrite", &no_post_rewrite, N_("bypass post-rewrite hook")),
1467 { 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" },
036dbbfb
JK
1468 /* end commit contents options */
1469
1470 { OPTION_BOOLEAN, 0, "allow-empty", &allow_empty, NULL,
9c23f4c5 1471 N_("ok to record an empty change"),
036dbbfb
JK
1472 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
1473 { OPTION_BOOLEAN, 0, "allow-empty-message", &allow_empty_message, NULL,
9c23f4c5 1474 N_("ok to record a change with an empty message"),
036dbbfb
JK
1475 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
1476
1477 OPT_END()
1478 };
1479
f285a2d7 1480 struct strbuf sb = STRBUF_INIT;
4c28e4ad 1481 struct strbuf author_ident = STRBUF_INIT;
f5bbc322 1482 const char *index_file, *reflog_msg;
99a12694 1483 char *nl, *p;
06bb643b 1484 unsigned char sha1[20];
f5bbc322 1485 struct ref_lock *ref_lock;
6bb6b034 1486 struct commit_list *parents = NULL, **pptr = &parents;
cf10f9fd
MV
1487 struct stat statbuf;
1488 int allow_fast_forward = 1;
06bb643b 1489 struct commit *current_head = NULL;
ed7a42a0 1490 struct commit_extra_header *extra = NULL;
f5bbc322 1491
5d3dd915
NTND
1492 if (argc == 2 && !strcmp(argv[1], "-h"))
1493 usage_with_options(builtin_commit_usage, builtin_commit_options);
1494
d249b098 1495 wt_status_prepare(&s);
8f6811ef 1496 gitmodules_config();
d249b098 1497 git_config(git_commit_config, &s);
f0915cba 1498 status_format = STATUS_FORMAT_NONE; /* Ignore status.short */
37f7a857 1499 determine_whence(&s);
4d2292e9 1500 s.colopts = 0;
f5bbc322 1501
06bb643b
JH
1502 if (get_sha1("HEAD", sha1))
1503 current_head = NULL;
1504 else {
baf18fc2 1505 current_head = lookup_commit_or_die(sha1, "HEAD");
5e7d4d3e 1506 if (parse_commit(current_head))
06bb643b
JH
1507 die(_("could not parse HEAD commit"));
1508 }
036dbbfb
JK
1509 argc = parse_and_validate_options(argc, argv, builtin_commit_options,
1510 builtin_commit_usage,
06bb643b 1511 prefix, current_head, &s);
c9bfb953 1512 if (dry_run)
06bb643b 1513 return dry_run_commit(argc, argv, prefix, current_head, &s);
06bb643b 1514 index_file = prepare_index(argc, argv, prefix, current_head, 0);
f5bbc322 1515
ec84bd00
PB
1516 /* Set up everything for writing the commit object. This includes
1517 running hooks, writing the trees, and interacting with the user. */
06bb643b
JH
1518 if (!prepare_to_commit(index_file, prefix,
1519 current_head, &s, &author_ident)) {
2888605c 1520 rollback_index_files();
f5bbc322
KH
1521 return 1;
1522 }
1523
f5bbc322 1524 /* Determine parents */
643cb5f7 1525 reflog_msg = getenv("GIT_REFLOG_ACTION");
06bb643b 1526 if (!current_head) {
643cb5f7
CC
1527 if (!reflog_msg)
1528 reflog_msg = "commit (initial)";
f5bbc322
KH
1529 } else if (amend) {
1530 struct commit_list *c;
f5bbc322 1531
643cb5f7
CC
1532 if (!reflog_msg)
1533 reflog_msg = "commit (amend)";
06bb643b 1534 for (c = current_head->parents; c; c = c->next)
6bb6b034 1535 pptr = &commit_list_insert(c->item, pptr)->next;
37f7a857 1536 } else if (whence == FROM_MERGE) {
f285a2d7 1537 struct strbuf m = STRBUF_INIT;
f5bbc322
KH
1538 FILE *fp;
1539
643cb5f7
CC
1540 if (!reflog_msg)
1541 reflog_msg = "commit (merge)";
06bb643b 1542 pptr = &commit_list_insert(current_head, pptr)->next;
f5bbc322
KH
1543 fp = fopen(git_path("MERGE_HEAD"), "r");
1544 if (fp == NULL)
8a6179bc 1545 die_errno(_("could not open '%s' for reading"),
d824cbba 1546 git_path("MERGE_HEAD"));
7c3fd25d 1547 while (strbuf_getline(&m, fp, '\n') != EOF) {
5231c633
JH
1548 struct commit *parent;
1549
1550 parent = get_merge_parent(m.buf);
1551 if (!parent)
8a6179bc 1552 die(_("Corrupt MERGE_HEAD file (%s)"), m.buf);
5231c633 1553 pptr = &commit_list_insert(parent, pptr)->next;
7c3fd25d 1554 }
f5bbc322
KH
1555 fclose(fp);
1556 strbuf_release(&m);
cf10f9fd
MV
1557 if (!stat(git_path("MERGE_MODE"), &statbuf)) {
1558 if (strbuf_read_file(&sb, git_path("MERGE_MODE"), 0) < 0)
8a6179bc 1559 die_errno(_("could not read MERGE_MODE"));
cf10f9fd
MV
1560 if (!strcmp(sb.buf, "no-ff"))
1561 allow_fast_forward = 0;
1562 }
1563 if (allow_fast_forward)
1564 parents = reduce_heads(parents);
f5bbc322 1565 } else {
643cb5f7 1566 if (!reflog_msg)
37f7a857
JS
1567 reflog_msg = (whence == FROM_CHERRY_PICK)
1568 ? "commit (cherry-pick)"
1569 : "commit";
06bb643b 1570 pptr = &commit_list_insert(current_head, pptr)->next;
f5bbc322 1571 }
f5bbc322 1572
ec84bd00 1573 /* Finally, get the commit message */
cf10f9fd 1574 strbuf_reset(&sb);
740001a5 1575 if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
0721c314 1576 int saved_errno = errno;
740001a5 1577 rollback_index_files();
8a6179bc 1578 die(_("could not read commit message: %s"), strerror(saved_errno));
740001a5 1579 }
99a12694
KH
1580
1581 /* Truncate the message just before the diff, if any. */
0b38227f
JK
1582 if (verbose) {
1583 p = strstr(sb.buf, "\ndiff --git ");
1584 if (p != NULL)
1585 strbuf_setlen(&sb, p - sb.buf + 1);
1586 }
99a12694 1587
5f065737
AR
1588 if (cleanup_mode != CLEANUP_NONE)
1589 stripspace(&sb, cleanup_mode == CLEANUP_ALL);
b2eda9bd
JH
1590 if (template_untouched(&sb) && !allow_empty_message) {
1591 rollback_index_files();
1592 fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
1593 exit(1);
1594 }
c9b5fde7 1595 if (message_is_empty(&sb) && !allow_empty_message) {
2888605c 1596 rollback_index_files();
8a6179bc 1597 fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
fdc7c811 1598 exit(1);
2888605c 1599 }
f5bbc322 1600
0074d18d 1601 if (amend) {
c871a1d1
JH
1602 const char *exclude_gpgsig[2] = { "gpgsig", NULL };
1603 extra = read_commit_extra_headers(current_head, exclude_gpgsig);
0074d18d
JH
1604 } else {
1605 struct commit_extra_header **tail = &extra;
1606 append_merge_tag_headers(parents, &tail);
1607 }
ed7a42a0 1608
f35ccd9b 1609 if (commit_tree_extended(&sb, active_cache_tree->sha1, parents, sha1,
ba3c69a9 1610 author_ident.buf, sign_commit, extra)) {
2888605c 1611 rollback_index_files();
8a6179bc 1612 die(_("failed to write commit object"));
2888605c 1613 }
4c28e4ad 1614 strbuf_release(&author_ident);
ed7a42a0 1615 free_commit_extra_headers(extra);
f5bbc322
KH
1616
1617 ref_lock = lock_any_ref_for_update("HEAD",
06bb643b
JH
1618 !current_head
1619 ? NULL
1620 : current_head->object.sha1,
f5bbc322
KH
1621 0);
1622
6bb6b034 1623 nl = strchr(sb.buf, '\n');
741707b1
JS
1624 if (nl)
1625 strbuf_setlen(&sb, nl + 1 - sb.buf);
1626 else
1627 strbuf_addch(&sb, '\n');
741707b1
JS
1628 strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
1629 strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
f5bbc322 1630
2888605c
JH
1631 if (!ref_lock) {
1632 rollback_index_files();
8a6179bc 1633 die(_("cannot lock HEAD ref"));
2888605c 1634 }
06bb643b 1635 if (write_ref_sha1(ref_lock, sha1, sb.buf) < 0) {
2888605c 1636 rollback_index_files();
8a6179bc 1637 die(_("cannot update HEAD ref"));
2888605c 1638 }
f5bbc322 1639
d7e5c0cb 1640 unlink(git_path("CHERRY_PICK_HEAD"));
82433cdf 1641 unlink(git_path("REVERT_HEAD"));
f5bbc322
KH
1642 unlink(git_path("MERGE_HEAD"));
1643 unlink(git_path("MERGE_MSG"));
cf10f9fd 1644 unlink(git_path("MERGE_MODE"));
5a95b855 1645 unlink(git_path("SQUASH_MSG"));
f5bbc322 1646
5a9dd399 1647 if (commit_index_files())
8a6179bc 1648 die (_("Repository has been updated, but unable to write\n"
5a9dd399 1649 "new_index file. Check that disk is not full or quota is\n"
8a6179bc 1650 "not exceeded, and then \"git reset HEAD\" to recover."));
f5bbc322 1651
cb6020bb 1652 rerere(0);
2888605c 1653 run_hook(get_index_file(), "post-commit", NULL);
6f6bee3b 1654 if (amend && !no_post_rewrite) {
6360d343
TR
1655 struct notes_rewrite_cfg *cfg;
1656 cfg = init_copy_notes_for_rewrite("amend");
1657 if (cfg) {
06bb643b
JH
1658 /* we are amending, so current_head is not NULL */
1659 copy_note_for_rewrite(cfg, current_head->object.sha1, sha1);
80a14665 1660 finish_copy_notes_for_rewrite(cfg, "Notes added by 'git commit --amend'");
6360d343 1661 }
06bb643b 1662 run_rewrite_hook(current_head->object.sha1, sha1);
6f6bee3b 1663 }
f5bbc322 1664 if (!quiet)
06bb643b 1665 print_summary(prefix, sha1, !current_head);
f5bbc322
KH
1666
1667 return 0;
1668}