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