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