blob: d2f30d960a71e41da88d6cc4201cbdca168b8648 [file] [log] [blame]
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001/*
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"
Matthias Kestenholz6b2f2d92008-02-18 08:26:03 +010010#include "color.h"
Junio C Hamano28886052007-11-18 01:52:55 -080011#include "dir.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050012#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"
Johannes Schindelinc455c872008-07-21 19:03:49 +010024#include "string-list.h"
Stephan Beyer5b2fd952008-07-09 14:58:57 +020025#include "rerere.h"
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -080026#include "unpack-trees.h"
Junio C Hamano76e2f7c2009-08-07 23:31:57 -070027#include "quote.h"
Jens Lehmann302ad7a2010-08-06 00:40:48 +020028#include "submodule.h"
Junio C Hamanoba3c69a2011-10-05 17:23:20 -070029#include "gpg-interface.h"
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +070030#include "column.h"
Miklos Vajna5ed75e22012-09-14 08:52:03 +020031#include "sequencer.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050032
33static const char * const builtin_commit_usage[] = {
Matthieu Moyd32805d2013-02-12 10:24:44 +010034 N_("git commit [options] [--] <pathspec>..."),
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050035 NULL
36};
37
Shawn Bohrer2f02b252007-12-02 23:02:09 -060038static const char * const builtin_status_usage[] = {
Matthieu Moyd32805d2013-02-12 10:24:44 +010039 N_("git status [options] [--] <pathspec>..."),
Shawn Bohrer2f02b252007-12-02 23:02:09 -060040 NULL
41};
42
Jeff King49ff9a72010-01-13 12:39:51 -050043static const char implicit_ident_advice[] =
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +000044N_("Your name and email address were configured automatically based\n"
Jeff King49ff9a72010-01-13 12:39:51 -050045"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"
Matt Kraai8bb45b22010-02-24 06:18:25 -080048" git config --global user.name \"Your Name\"\n"
Jeff King49ff9a72010-01-13 12:39:51 -050049" git config --global user.email you@example.com\n"
50"\n"
Matthieu Moy3f142462011-01-12 19:29:14 +010051"After doing this, you may fix the identity used for this commit with:\n"
Jeff King49ff9a72010-01-13 12:39:51 -050052"\n"
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +000053" git commit --amend --reset-author\n");
Jeff King49ff9a72010-01-13 12:39:51 -050054
Jeff Kingf197ed22010-06-06 20:41:46 -040055static const char empty_amend_advice[] =
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +000056N_("You asked to amend the most recent commit, but doing so would make\n"
Jeff Kingf197ed22010-06-06 20:41:46 -040057"it empty. You can repeat your command with --allow-empty, or you can\n"
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +000058"remove the commit entirely with \"git reset HEAD^\".\n");
Jeff Kingf197ed22010-06-06 20:41:46 -040059
Jay Soffian37f7a852011-02-19 23:12:29 -050060static const char empty_cherry_pick_advice[] =
Junio C Hamano6c80cd22011-04-01 17:55:55 -070061N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n"
Jay Soffian37f7a852011-02-19 23:12:29 -050062"If you wish to commit it anyway, use:\n"
63"\n"
64" git commit --allow-empty\n"
65"\n"
Junio C Hamano6c80cd22011-04-01 17:55:55 -070066"Otherwise, please use 'git reset'\n");
Jay Soffian37f7a852011-02-19 23:12:29 -050067
Jay Soffian37f7a852011-02-19 23:12:29 -050068static const char *use_message_buffer;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050069static const char commit_editmsg[] = "COMMIT_EDITMSG";
Junio C Hamano28886052007-11-18 01:52:55 -080070static 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,
Gary V. Vaughan4b055482010-05-14 09:31:35 +000075 COMMIT_PARTIAL
Junio C Hamano28886052007-11-18 01:52:55 -080076} commit_style;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050077
Junio C Hamanodbd0f5c2008-08-06 11:43:47 -070078static const char *logfile, *force_author;
Brian Hetro984c6e72008-07-05 01:24:40 -040079static const char *template_file;
Jay Soffian37f7a852011-02-19 23:12:29 -050080/*
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;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050085static char *edit_message, *use_message;
Pat Notz89ac1222010-11-02 13:59:11 -060086static char *fixup_message, *squash_message;
Junio C Hamanoca1ba202011-12-06 13:09:55 -080087static int all, also, interactive, patch_interactive, only, amend, signoff;
88static int edit_flag = -1; /* unspecified */
Erick Mattosc51f6ce2009-11-04 01:20:11 -020089static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
Ævar Arnfjörð Bjarmasonc9b5fde2010-04-06 08:40:35 +000090static int no_post_rewrite, allow_empty_message;
Jens Lehmann46a958b2010-06-25 16:56:47 +020091static char *untracked_files_arg, *force_date, *ignore_submodule_arg;
Junio C Hamanoba3c69a2011-10-05 17:23:20 -070092static char *sign_commit;
93
Alex Riesen5f065732007-12-22 19:46:24 +010094/*
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,
Gary V. Vaughan4b055482010-05-14 09:31:35 +0000104 CLEANUP_ALL
Alex Riesen5f065732007-12-22 19:46:24 +0100105} cleanup_mode;
Ralf Thielow51fb3a32013-01-10 18:45:59 +0100106static const char *cleanup_arg;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500107
Jay Soffian37f7a852011-02-19 23:12:29 -0500108static enum commit_whence whence;
Junio C Hamano06bb6432011-08-19 11:58:18 -0700109static int use_editor = 1, include_status = 1;
Junio C Hamano2381e392010-04-10 00:33:17 -0700110static int show_ignored_in_status;
Johannes Schindelin3b6aeb32008-07-22 21:40:41 +0100111static const char *only_include_assumed;
Jeff King2c477892011-12-18 00:03:22 -0500112static struct strbuf message = STRBUF_INIT;
Johannes Schindelinf9568532007-11-11 17:36:39 +0000113
Jeff King7c9f7032009-09-05 04:59:56 -0400114static enum {
Jeff Kingf3f47a12012-10-18 21:15:50 +0700115 STATUS_FORMAT_NONE = 0,
Jeff King7c9f7032009-09-05 04:59:56 -0400116 STATUS_FORMAT_LONG,
117 STATUS_FORMAT_SHORT,
Gary V. Vaughan4b055482010-05-14 09:31:35 +0000118 STATUS_FORMAT_PORCELAIN
Jeff Kingf3f47a12012-10-18 21:15:50 +0700119} status_format;
Jeff King7c9f7032009-09-05 04:59:56 -0400120
Johannes Schindelinf9568532007-11-11 17:36:39 +0000121static int opt_parse_m(const struct option *opt, const char *arg, int unset)
122{
123 struct strbuf *buf = opt->value;
124 if (unset)
125 strbuf_setlen(buf, 0);
126 else {
Brandon Caseya24a41e2013-02-18 20:17:06 -0800127 if (buf->len)
128 strbuf_addch(buf, '\n');
Johannes Schindelinf9568532007-11-11 17:36:39 +0000129 strbuf_addstr(buf, arg);
Brandon Caseya24a41e2013-02-18 20:17:06 -0800130 strbuf_complete_line(buf);
Johannes Schindelinf9568532007-11-11 17:36:39 +0000131 }
132 return 0;
133}
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500134
Jay Soffian37f7a852011-02-19 23:12:29 -0500135static void determine_whence(struct wt_status *s)
136{
137 if (file_exists(git_path("MERGE_HEAD")))
138 whence = FROM_MERGE;
139 else if (file_exists(git_path("CHERRY_PICK_HEAD")))
140 whence = FROM_CHERRY_PICK;
141 else
142 whence = FROM_COMMIT;
143 if (s)
144 s->whence = whence;
145}
146
Junio C Hamano28886052007-11-18 01:52:55 -0800147static void rollback_index_files(void)
148{
149 switch (commit_style) {
150 case COMMIT_AS_IS:
151 break; /* nothing to do */
152 case COMMIT_NORMAL:
153 rollback_lock_file(&index_lock);
154 break;
155 case COMMIT_PARTIAL:
156 rollback_lock_file(&index_lock);
157 rollback_lock_file(&false_lock);
158 break;
159 }
160}
161
Brandon Casey5a9dd392008-01-23 11:21:22 -0600162static int commit_index_files(void)
Junio C Hamano28886052007-11-18 01:52:55 -0800163{
Brandon Casey5a9dd392008-01-23 11:21:22 -0600164 int err = 0;
165
Junio C Hamano28886052007-11-18 01:52:55 -0800166 switch (commit_style) {
167 case COMMIT_AS_IS:
168 break; /* nothing to do */
169 case COMMIT_NORMAL:
Brandon Casey5a9dd392008-01-23 11:21:22 -0600170 err = commit_lock_file(&index_lock);
Junio C Hamano28886052007-11-18 01:52:55 -0800171 break;
172 case COMMIT_PARTIAL:
Brandon Casey5a9dd392008-01-23 11:21:22 -0600173 err = commit_lock_file(&index_lock);
Junio C Hamano28886052007-11-18 01:52:55 -0800174 rollback_lock_file(&false_lock);
175 break;
176 }
Brandon Casey5a9dd392008-01-23 11:21:22 -0600177
178 return err;
Junio C Hamano28886052007-11-18 01:52:55 -0800179}
180
181/*
182 * Take a union of paths in the index and the named tree (typically, "HEAD"),
183 * and return the paths that match the given pattern in list.
184 */
Johannes Schindelinc455c872008-07-21 19:03:49 +0100185static int list_paths(struct string_list *list, const char *with_tree,
Junio C Hamano28886052007-11-18 01:52:55 -0800186 const char *prefix, const char **pattern)
187{
188 int i;
189 char *m;
190
Junio C Hamanob9a08012012-07-15 21:39:48 -0700191 if (!pattern)
192 return 0;
193
Junio C Hamano28886052007-11-18 01:52:55 -0800194 for (i = 0; pattern[i]; i++)
195 ;
196 m = xcalloc(1, i);
197
Clemens Buchacher8894d532011-07-30 19:13:47 +0200198 if (with_tree) {
Clemens Buchacherf950eb92011-09-04 12:42:01 +0200199 char *max_prefix = common_prefix(pattern);
Clemens Buchacher5879f562011-09-04 12:41:59 +0200200 overlay_tree_on_cache(with_tree, max_prefix ? max_prefix : prefix);
201 free(max_prefix);
Clemens Buchacher8894d532011-07-30 19:13:47 +0200202 }
Junio C Hamano28886052007-11-18 01:52:55 -0800203
204 for (i = 0; i < active_nr; i++) {
205 struct cache_entry *ce = active_cache[i];
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700206 struct string_list_item *item;
207
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800208 if (ce->ce_flags & CE_UPDATE)
Junio C Hamanoe87e22d2008-01-14 13:54:24 -0800209 continue;
Clemens Buchacher0b509222009-01-14 15:54:35 +0100210 if (!match_pathspec(pattern, ce->name, ce_namelen(ce), 0, m))
Junio C Hamano28886052007-11-18 01:52:55 -0800211 continue;
Julian Phillips78a395d2010-06-26 00:41:35 +0100212 item = string_list_insert(list, ce->name);
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700213 if (ce_skip_worktree(ce))
214 item->util = item; /* better a valid pointer than a fake one */
Junio C Hamano28886052007-11-18 01:52:55 -0800215 }
216
Clemens Buchacher0f64bfa2011-08-01 23:19:58 +0200217 return report_path_error(m, pattern, prefix);
Junio C Hamano28886052007-11-18 01:52:55 -0800218}
219
Johannes Schindelinc455c872008-07-21 19:03:49 +0100220static void add_remove_files(struct string_list *list)
Junio C Hamano28886052007-11-18 01:52:55 -0800221{
222 int i;
223 for (i = 0; i < list->nr; i++) {
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700224 struct stat st;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100225 struct string_list_item *p = &(list->items[i]);
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700226
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700227 /* p->util is skip-worktree */
228 if (p->util)
Nguyễn Thái Ngọc Duyb4d16902009-08-20 20:46:58 +0700229 continue;
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700230
Johannes Schindelinc455c872008-07-21 19:03:49 +0100231 if (!lstat(p->string, &st)) {
232 if (add_to_cache(p->string, &st, 0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000233 die(_("updating files failed"));
Alex Riesen960b8ad2008-05-12 19:57:45 +0200234 } else
Johannes Schindelinc455c872008-07-21 19:03:49 +0100235 remove_file_from_cache(p->string);
Junio C Hamano28886052007-11-18 01:52:55 -0800236 }
237}
238
Junio C Hamano06bb6432011-08-19 11:58:18 -0700239static void create_base_index(const struct commit *current_head)
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800240{
241 struct tree *tree;
242 struct unpack_trees_options opts;
243 struct tree_desc t;
244
Junio C Hamano06bb6432011-08-19 11:58:18 -0700245 if (!current_head) {
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800246 discard_cache();
247 return;
248 }
249
250 memset(&opts, 0, sizeof(opts));
251 opts.head_idx = 1;
252 opts.index_only = 1;
253 opts.merge = 1;
Linus Torvalds34110cd2008-03-06 18:12:28 -0800254 opts.src_index = &the_index;
255 opts.dst_index = &the_index;
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800256
257 opts.fn = oneway_merge;
Junio C Hamano06bb6432011-08-19 11:58:18 -0700258 tree = parse_tree_indirect(current_head->object.sha1);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800259 if (!tree)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000260 die(_("failed to unpack HEAD tree object"));
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800261 parse_tree(tree);
262 init_tree_desc(&t, tree->buffer, tree->size);
Daniel Barkalow203a2fe2008-02-07 11:39:48 -0500263 if (unpack_trees(1, &t, &opts))
264 exit(128); /* We've already reported the error, finish dying */
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800265}
266
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100267static void refresh_cache_or_die(int refresh_flags)
268{
269 /*
270 * refresh_flags contains REFRESH_QUIET, so the only errors
271 * are for unmerged entries.
272 */
273 if (refresh_cache(refresh_flags | REFRESH_IN_PORCELAIN))
274 die_resolve_conflict("commit");
275}
276
Junio C Hamano06bb6432011-08-19 11:58:18 -0700277static char *prepare_index(int argc, const char **argv, const char *prefix,
278 const struct commit *current_head, int is_status)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500279{
280 int fd;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100281 struct string_list partial;
Junio C Hamano28886052007-11-18 01:52:55 -0800282 const char **pathspec = NULL;
Conrad Irwin1020d082011-05-06 22:59:59 -0700283 char *old_index_env = NULL;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700284 int refresh_flags = REFRESH_QUIET;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500285
Junio C Hamano50b7e702009-08-04 23:49:33 -0700286 if (is_status)
287 refresh_flags |= REFRESH_UNMERGED;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500288
Junio C Hamanof64fe7b2007-11-25 08:46:29 -0800289 if (*argv)
290 pathspec = get_pathspec(prefix, argv);
Junio C Hamano28886052007-11-18 01:52:55 -0800291
Linus Torvalds671c9b72008-11-13 16:36:30 -0800292 if (read_cache_preload(pathspec) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000293 die(_("index file corrupt"));
Linus Torvalds671c9b72008-11-13 16:36:30 -0800294
Conrad Irwin1020d082011-05-06 22:59:59 -0700295 if (interactive) {
296 fd = hold_locked_index(&index_lock, 1);
297
298 refresh_cache_or_die(refresh_flags);
299
300 if (write_cache(fd, active_cache, active_nr) ||
301 close_lock_file(&index_lock))
302 die(_("unable to create temporary index"));
303
304 old_index_env = getenv(INDEX_ENVIRONMENT);
305 setenv(INDEX_ENVIRONMENT, index_lock.filename, 1);
306
Conrad Irwinb4bd4662011-05-07 10:58:07 -0700307 if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
Conrad Irwin1020d082011-05-06 22:59:59 -0700308 die(_("interactive add failed"));
309
310 if (old_index_env && *old_index_env)
311 setenv(INDEX_ENVIRONMENT, old_index_env, 1);
312 else
313 unsetenv(INDEX_ENVIRONMENT);
314
315 discard_cache();
316 read_cache_from(index_lock.filename);
317
318 commit_style = COMMIT_NORMAL;
319 return index_lock.filename;
320 }
321
Junio C Hamano28886052007-11-18 01:52:55 -0800322 /*
323 * Non partial, non as-is commit.
324 *
325 * (1) get the real index;
326 * (2) update the_index as necessary;
327 * (3) write the_index out to the real index (still locked);
328 * (4) return the name of the locked index file.
329 *
330 * The caller should run hooks on the locked real index, and
331 * (A) if all goes well, commit the real index;
332 * (B) on failure, rollback the real index.
333 */
334 if (all || (also && pathspec && *pathspec)) {
Markus Heidelberg1f2362a2010-04-02 14:27:19 +0200335 fd = hold_locked_index(&index_lock, 1);
Alex Riesen7ae02a32008-05-12 19:58:10 +0200336 add_files_to_cache(also ? prefix : NULL, pathspec, 0);
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100337 refresh_cache_or_die(refresh_flags);
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700338 update_main_cache_tree(WRITE_TREE_SILENT);
Brandon Casey4ed7cd32008-01-16 13:12:46 -0600339 if (write_cache(fd, active_cache, active_nr) ||
340 close_lock_file(&index_lock))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000341 die(_("unable to write new_index file"));
Junio C Hamano28886052007-11-18 01:52:55 -0800342 commit_style = COMMIT_NORMAL;
343 return index_lock.filename;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500344 }
345
Junio C Hamano28886052007-11-18 01:52:55 -0800346 /*
347 * As-is commit.
348 *
349 * (1) return the name of the real index file.
350 *
Markus Heidelberg73276232010-04-02 14:27:18 +0200351 * The caller should run hooks on the real index,
352 * and create commit from the_index.
Junio C Hamano28886052007-11-18 01:52:55 -0800353 * We still need to refresh the index here.
354 */
Junio C Hamanob9a08012012-07-15 21:39:48 -0700355 if (!only && (!pathspec || !*pathspec)) {
Junio C Hamano28886052007-11-18 01:52:55 -0800356 fd = hold_locked_index(&index_lock, 1);
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100357 refresh_cache_or_die(refresh_flags);
Junio C Hamanod5f5d0a2010-07-06 21:53:11 -0700358 if (active_cache_changed) {
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700359 update_main_cache_tree(WRITE_TREE_SILENT);
Junio C Hamanod5f5d0a2010-07-06 21:53:11 -0700360 if (write_cache(fd, active_cache, active_nr) ||
361 commit_locked_index(&index_lock))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000362 die(_("unable to write new_index file"));
Junio C Hamanod5f5d0a2010-07-06 21:53:11 -0700363 } else {
364 rollback_lock_file(&index_lock);
365 }
Junio C Hamano28886052007-11-18 01:52:55 -0800366 commit_style = COMMIT_AS_IS;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500367 return get_index_file();
368 }
369
Junio C Hamano28886052007-11-18 01:52:55 -0800370 /*
371 * A partial commit.
372 *
373 * (0) find the set of affected paths;
374 * (1) get lock on the real index file;
375 * (2) update the_index with the given paths;
376 * (3) write the_index out to the real index (still locked);
377 * (4) get lock on the false index file;
378 * (5) reset the_index from HEAD;
379 * (6) update the_index the same way as (2);
380 * (7) write the_index out to the false index file;
381 * (8) return the name of the false index file (still locked);
382 *
383 * The caller should run hooks on the locked false index, and
384 * create commit from it. Then
385 * (A) if all goes well, commit the real index;
386 * (B) on failure, rollback the real index;
387 * In either case, rollback the false index.
388 */
389 commit_style = COMMIT_PARTIAL;
390
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000391 if (whence != FROM_COMMIT) {
392 if (whence == FROM_MERGE)
393 die(_("cannot do a partial commit during a merge."));
394 else if (whence == FROM_CHERRY_PICK)
395 die(_("cannot do a partial commit during a cherry-pick."));
396 }
Junio C Hamano28886052007-11-18 01:52:55 -0800397
398 memset(&partial, 0, sizeof(partial));
Johannes Schindelinc455c872008-07-21 19:03:49 +0100399 partial.strdup_strings = 1;
Junio C Hamano06bb6432011-08-19 11:58:18 -0700400 if (list_paths(&partial, !current_head ? NULL : "HEAD", prefix, pathspec))
Junio C Hamano28886052007-11-18 01:52:55 -0800401 exit(1);
402
403 discard_cache();
404 if (read_cache() < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000405 die(_("cannot read the index"));
Junio C Hamano28886052007-11-18 01:52:55 -0800406
407 fd = hold_locked_index(&index_lock, 1);
408 add_remove_files(&partial);
Kristian Høgsbergef12b502007-11-12 15:48:22 -0500409 refresh_cache(REFRESH_QUIET);
Brandon Casey4ed7cd32008-01-16 13:12:46 -0600410 if (write_cache(fd, active_cache, active_nr) ||
411 close_lock_file(&index_lock))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000412 die(_("unable to write new_index file"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500413
Junio C Hamano28886052007-11-18 01:52:55 -0800414 fd = hold_lock_file_for_update(&false_lock,
Junio C Hamanoa1574002008-10-21 17:58:11 -0700415 git_path("next-index-%"PRIuMAX,
416 (uintmax_t) getpid()),
Junio C Hamanoacd3b9e2008-10-17 15:44:39 -0700417 LOCK_DIE_ON_ERROR);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800418
Junio C Hamano06bb6432011-08-19 11:58:18 -0700419 create_base_index(current_head);
Junio C Hamano28886052007-11-18 01:52:55 -0800420 add_remove_files(&partial);
Kristian Høgsbergd37d3202007-11-09 11:40:27 -0500421 refresh_cache(REFRESH_QUIET);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500422
Brandon Casey4ed7cd32008-01-16 13:12:46 -0600423 if (write_cache(fd, active_cache, active_nr) ||
424 close_lock_file(&false_lock))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000425 die(_("unable to write temporary index file"));
Jeff King959ba672008-02-14 12:18:23 -0500426
427 discard_cache();
428 read_cache_from(false_lock.filename);
429
Junio C Hamano28886052007-11-18 01:52:55 -0800430 return false_lock.filename;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500431}
432
Junio C Hamanod249b092009-08-09 21:59:30 -0700433static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
434 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500435{
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700436 unsigned char sha1[20];
437
Junio C Hamanod249b092009-08-09 21:59:30 -0700438 if (s->relative_paths)
439 s->prefix = prefix;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500440
441 if (amend) {
Junio C Hamanod249b092009-08-09 21:59:30 -0700442 s->amend = 1;
443 s->reference = "HEAD^1";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500444 }
Junio C Hamanod249b092009-08-09 21:59:30 -0700445 s->verbose = verbose;
446 s->index_file = index_file;
447 s->fp = fp;
448 s->nowarn = nowarn;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700449 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500450
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700451 wt_status_collect(s);
Jeff King7c9f7032009-09-05 04:59:56 -0400452
453 switch (status_format) {
454 case STATUS_FORMAT_SHORT:
Jeff Kingd4a6bf12012-05-07 17:09:04 -0400455 wt_shortstatus_print(s);
Jeff King7c9f7032009-09-05 04:59:56 -0400456 break;
457 case STATUS_FORMAT_PORCELAIN:
Jeff King3207a3a2012-05-07 15:44:44 -0400458 wt_porcelain_print(s);
Jeff King7c9f7032009-09-05 04:59:56 -0400459 break;
Jeff Kingf3f47a12012-10-18 21:15:50 +0700460 case STATUS_FORMAT_NONE:
Jeff King7c9f7032009-09-05 04:59:56 -0400461 case STATUS_FORMAT_LONG:
462 wt_status_print(s);
463 break;
464 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500465
Junio C Hamanod249b092009-08-09 21:59:30 -0700466 return s->commitable;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500467}
468
Junio C Hamano06bb6432011-08-19 11:58:18 -0700469static int is_a_merge(const struct commit *current_head)
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100470{
Junio C Hamano06bb6432011-08-19 11:58:18 -0700471 return !!(current_head->parents && current_head->parents->next);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100472}
473
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700474static void export_one(const char *var, const char *s, const char *e, int hack)
475{
476 struct strbuf buf = STRBUF_INIT;
477 if (hack)
478 strbuf_addch(&buf, hack);
479 strbuf_addf(&buf, "%.*s", (int)(e - s), s);
480 setenv(var, buf.buf, 1);
481 strbuf_release(&buf);
482}
483
Junio C Hamanoe27ddb62012-08-31 14:54:18 -0700484static int sane_ident_split(struct ident_split *person)
485{
486 if (!person->name_begin || !person->name_end ||
487 person->name_begin == person->name_end)
488 return 0; /* no human readable name */
489 if (!person->mail_begin || !person->mail_end ||
490 person->mail_begin == person->mail_end)
491 return 0; /* no usable mail */
492 if (!person->date_begin || !person->date_end ||
493 !person->tz_begin || !person->tz_end)
494 return 0;
495 return 1;
496}
497
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800498static void determine_author_info(struct strbuf *author_ident)
Santi Béjara45d46b2008-05-04 18:04:49 +0200499{
500 char *name, *email, *date;
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700501 struct ident_split author;
Santi Béjara45d46b2008-05-04 18:04:49 +0200502
503 name = getenv("GIT_AUTHOR_NAME");
504 email = getenv("GIT_AUTHOR_EMAIL");
505 date = getenv("GIT_AUTHOR_DATE");
506
Jay Soffian37f7a852011-02-19 23:12:29 -0500507 if (author_message) {
Santi Béjara45d46b2008-05-04 18:04:49 +0200508 const char *a, *lb, *rb, *eol;
Junio C Hamano2c733fb2012-02-02 13:41:43 -0800509 size_t len;
Santi Béjara45d46b2008-05-04 18:04:49 +0200510
Jay Soffian37f7a852011-02-19 23:12:29 -0500511 a = strstr(author_message_buffer, "\nauthor ");
Santi Béjara45d46b2008-05-04 18:04:49 +0200512 if (!a)
Junio C Hamano6c80cd22011-04-01 17:55:55 -0700513 die(_("invalid commit: %s"), author_message);
Santi Béjara45d46b2008-05-04 18:04:49 +0200514
Jonathan Niederfb7749e2010-05-02 03:57:12 -0500515 lb = strchrnul(a + strlen("\nauthor "), '<');
516 rb = strchrnul(lb, '>');
517 eol = strchrnul(rb, '\n');
518 if (!*lb || !*rb || !*eol)
Junio C Hamano6c80cd22011-04-01 17:55:55 -0700519 die(_("invalid commit: %s"), author_message);
Santi Béjara45d46b2008-05-04 18:04:49 +0200520
Jonathan Niederfb7749e2010-05-02 03:57:12 -0500521 if (lb == a + strlen("\nauthor "))
522 /* \nauthor <foo@example.com> */
523 name = xcalloc(1, 1);
524 else
525 name = xmemdupz(a + strlen("\nauthor "),
526 (lb - strlen(" ") -
527 (a + strlen("\nauthor "))));
528 email = xmemdupz(lb + strlen("<"), rb - (lb + strlen("<")));
529 date = xmemdupz(rb + strlen("> "), eol - (rb + strlen("> ")));
Junio C Hamano2c733fb2012-02-02 13:41:43 -0800530 len = eol - (rb + strlen("> "));
531 date = xmalloc(len + 2);
532 *date = '@';
533 memcpy(date + 1, rb + strlen("> "), len);
534 date[len + 1] = '\0';
Santi Béjara45d46b2008-05-04 18:04:49 +0200535 }
536
537 if (force_author) {
538 const char *lb = strstr(force_author, " <");
539 const char *rb = strchr(force_author, '>');
540
541 if (!lb || !rb)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000542 die(_("malformed --author parameter"));
Santi Béjara45d46b2008-05-04 18:04:49 +0200543 name = xstrndup(force_author, lb - force_author);
544 email = xstrndup(lb + 2, rb - (lb + 2));
545 }
546
Miklos Vajna02b47cd2009-12-02 23:16:18 +0100547 if (force_date)
548 date = force_date;
Jeff Kingf9bc5732012-05-24 19:28:40 -0400549 strbuf_addstr(author_ident, fmt_ident(name, email, date, IDENT_STRICT));
Junio C Hamanoe27ddb62012-08-31 14:54:18 -0700550 if (!split_ident_line(&author, author_ident->buf, author_ident->len) &&
551 sane_ident_split(&author)) {
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700552 export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
553 export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
554 export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@');
555 }
Santi Béjara45d46b2008-05-04 18:04:49 +0200556}
557
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800558static char *cut_ident_timestamp_part(char *string)
559{
560 char *ket = strrchr(string, '>');
561 if (!ket || ket[1] != ' ')
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000562 die(_("Malformed ident string: '%s'"), string);
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800563 *++ket = '\0';
564 return ket;
565}
566
Junio C Hamanod249b092009-08-09 21:59:30 -0700567static int prepare_to_commit(const char *index_file, const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -0700568 struct commit *current_head,
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800569 struct wt_status *s,
570 struct strbuf *author_ident)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500571{
572 struct stat statbuf;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800573 struct strbuf committer_ident = STRBUF_INIT;
Junio C Hamanobc5d2482007-11-18 12:01:38 -0800574 int commitable, saved_color_setting;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500575 struct strbuf sb = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500576 char *buffer;
Paolo Bonzini8089c852008-02-05 08:04:18 +0100577 const char *hook_arg1 = NULL;
578 const char *hook_arg2 = NULL;
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200579 int ident_shown = 0;
Boris Faure8b1ae672011-05-08 12:31:02 +0200580 int clean_message_contents = (cleanup_mode != CLEANUP_NONE);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500581
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700582 /* This checks and barfs if author is badly specified */
583 determine_author_info(author_ident);
584
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100585 if (!no_verify && run_hook(index_file, "pre-commit", NULL))
586 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500587
Pat Notz89ac1222010-11-02 13:59:11 -0600588 if (squash_message) {
589 /*
590 * Insert the proper subject line before other commit
591 * message options add their content.
592 */
593 if (use_message && !strcmp(use_message, squash_message))
594 strbuf_addstr(&sb, "squash! ");
595 else {
596 struct pretty_print_context ctx = {0};
597 struct commit *c;
598 c = lookup_commit_reference_by_name(squash_message);
599 if (!c)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000600 die(_("could not lookup commit %s"), squash_message);
Pat Notz89ac1222010-11-02 13:59:11 -0600601 ctx.output_encoding = get_commit_output_encoding();
602 format_commit_message(c, "squash! %s\n\n", &sb,
603 &ctx);
604 }
605 }
606
Johannes Schindelinf9568532007-11-11 17:36:39 +0000607 if (message.len) {
608 strbuf_addbuf(&sb, &message);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100609 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500610 } else if (logfile && !strcmp(logfile, "-")) {
611 if (isatty(0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000612 fprintf(stderr, _("(reading log message from standard input)\n"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500613 if (strbuf_read(&sb, 0, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000614 die_errno(_("could not read log from standard input"));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100615 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500616 } else if (logfile) {
617 if (strbuf_read_file(&sb, logfile, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000618 die_errno(_("could not read log file '%s'"),
Thomas Rastd824cbb2009-06-27 17:58:46 +0200619 logfile);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100620 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500621 } else if (use_message) {
622 buffer = strstr(use_message_buffer, "\n\n");
Chris Webbd9a93572012-07-09 19:53:26 +0100623 if (!use_editor && (!buffer || buffer[2] == '\0'))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000624 die(_("commit has empty message"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500625 strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100626 hook_arg1 = "commit";
627 hook_arg2 = use_message;
Pat Notzd71b8ba2010-11-02 13:59:09 -0600628 } else if (fixup_message) {
629 struct pretty_print_context ctx = {0};
630 struct commit *commit;
631 commit = lookup_commit_reference_by_name(fixup_message);
632 if (!commit)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000633 die(_("could not lookup commit %s"), fixup_message);
Pat Notzd71b8ba2010-11-02 13:59:09 -0600634 ctx.output_encoding = get_commit_output_encoding();
635 format_commit_message(commit, "fixup! %s\n\n",
636 &sb, &ctx);
637 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500638 } else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
639 if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000640 die_errno(_("could not read MERGE_MSG"));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100641 hook_arg1 = "merge";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500642 } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
643 if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000644 die_errno(_("could not read SQUASH_MSG"));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100645 hook_arg1 = "squash";
Jonathan Nieder2140b142011-02-25 03:07:57 -0600646 } else if (template_file) {
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500647 if (strbuf_read_file(&sb, template_file, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000648 die_errno(_("could not read '%s'"), template_file);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100649 hook_arg1 = "template";
Boris Faure8b1ae672011-05-08 12:31:02 +0200650 clean_message_contents = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500651 }
652
Paolo Bonzini8089c852008-02-05 08:04:18 +0100653 /*
Jay Soffian37f7a852011-02-19 23:12:29 -0500654 * The remaining cases don't modify the template message, but
655 * just set the argument(s) to the prepare-commit-msg hook.
Paolo Bonzini8089c852008-02-05 08:04:18 +0100656 */
Jay Soffian37f7a852011-02-19 23:12:29 -0500657 else if (whence == FROM_MERGE)
Paolo Bonzini8089c852008-02-05 08:04:18 +0100658 hook_arg1 = "merge";
Jay Soffian37f7a852011-02-19 23:12:29 -0500659 else if (whence == FROM_CHERRY_PICK) {
660 hook_arg1 = "commit";
661 hook_arg2 = "CHERRY_PICK_HEAD";
662 }
Paolo Bonzini8089c852008-02-05 08:04:18 +0100663
Pat Notz89ac1222010-11-02 13:59:11 -0600664 if (squash_message) {
665 /*
666 * If squash_commit was used for the commit subject,
667 * then we're possibly hijacking other commit log options.
668 * Reset the hook args to tell the real story.
669 */
670 hook_arg1 = "message";
671 hook_arg2 = "";
672 }
673
Jonathan Nieder37f30122011-02-25 23:10:49 -0600674 s->fp = fopen(git_path(commit_editmsg), "w");
675 if (s->fp == NULL)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000676 die_errno(_("could not open '%s'"), git_path(commit_editmsg));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500677
Boris Faure8b1ae672011-05-08 12:31:02 +0200678 if (clean_message_contents)
Alex Riesen5f065732007-12-22 19:46:24 +0100679 stripspace(&sb, 0);
Johannes Schindelin13208572007-11-11 17:35:58 +0000680
681 if (signoff) {
Miklos Vajna5ed75e22012-09-14 08:52:03 +0200682 /*
683 * See if we have a Conflicts: block at the end. If yes, count
684 * its size, so we can ignore it.
685 */
686 int ignore_footer = 0;
687 int i, eol, previous = 0;
688 const char *nl;
Johannes Schindelin13208572007-11-11 17:35:58 +0000689
Miklos Vajna5ed75e22012-09-14 08:52:03 +0200690 for (i = 0; i < sb.len; i++) {
691 nl = memchr(sb.buf + i, '\n', sb.len - i);
692 if (nl)
693 eol = nl - sb.buf;
694 else
695 eol = sb.len;
696 if (!prefixcmp(sb.buf + previous, "\nConflicts:\n")) {
697 ignore_footer = sb.len - previous;
698 break;
699 }
700 while (i < eol)
701 i++;
702 previous = eol;
Johannes Schindelin21505542007-11-11 17:36:27 +0000703 }
Miklos Vajna5ed75e22012-09-14 08:52:03 +0200704
Brandon Caseybab4d102013-02-12 02:17:35 -0800705 append_signoff(&sb, ignore_footer, 0);
Johannes Schindelin13208572007-11-11 17:35:58 +0000706 }
707
Jonathan Nieder37f30122011-02-25 23:10:49 -0600708 if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000709 die_errno(_("could not write commit template"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500710
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500711 strbuf_release(&sb);
712
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200713 /* This checks if committer ident is explicitly given */
Jeff Kingf20f3872012-07-23 14:50:35 -0400714 strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
James P. Howard, IIbed575e2009-12-07 17:45:27 -0500715 if (use_editor && include_status) {
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800716 char *ai_tmp, *ci_tmp;
Jay Soffian37f7a852011-02-19 23:12:29 -0500717 if (whence != FROM_COMMIT)
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600718 status_printf_ln(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000719 whence == FROM_MERGE
720 ? _("\n"
721 "It looks like you may be committing a merge.\n"
722 "If this is not correct, please remove the file\n"
723 " %s\n"
724 "and try again.\n")
725 : _("\n"
726 "It looks like you may be committing a cherry-pick.\n"
727 "If this is not correct, please remove the file\n"
728 " %s\n"
729 "and try again.\n"),
Jay Soffian37f7a852011-02-19 23:12:29 -0500730 git_path(whence == FROM_MERGE
731 ? "MERGE_HEAD"
732 : "CHERRY_PICK_HEAD"));
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100733
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600734 fprintf(s->fp, "\n");
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100735 if (cleanup_mode == CLEANUP_ALL)
Ævar Arnfjörð Bjarmason4064e662012-04-30 15:33:14 +0000736 status_printf(s, GIT_COLOR_NORMAL,
737 _("Please enter the commit message for your changes."
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100738 " Lines starting\nwith '%c' will be ignored, and an empty"
739 " message aborts the commit.\n"), comment_line_char);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100740 else /* CLEANUP_SPACE, that is. */
Ævar Arnfjörð Bjarmason4064e662012-04-30 15:33:14 +0000741 status_printf(s, GIT_COLOR_NORMAL,
742 _("Please enter the commit message for your changes."
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100743 " Lines starting\n"
744 "with '%c' will be kept; you may remove them"
745 " yourself if you want to.\n"
746 "An empty message aborts the commit.\n"), comment_line_char);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100747 if (only_include_assumed)
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600748 status_printf_ln(s, GIT_COLOR_NORMAL,
749 "%s", only_include_assumed);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100750
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800751 ai_tmp = cut_ident_timestamp_part(author_ident->buf);
752 ci_tmp = cut_ident_timestamp_part(committer_ident.buf);
753 if (strcmp(author_ident->buf, committer_ident.buf))
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600754 status_printf_ln(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmasonfe8165c2011-02-22 23:41:46 +0000755 _("%s"
756 "Author: %s"),
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600757 ident_shown++ ? "" : "\n",
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800758 author_ident->buf);
Santi Béjare83dbe82008-05-04 18:04:50 +0200759
Jeff Kingd6991ce2012-11-14 16:34:13 -0800760 if (!committer_ident_sufficiently_given())
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600761 status_printf_ln(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmasonfe8165c2011-02-22 23:41:46 +0000762 _("%s"
763 "Committer: %s"),
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600764 ident_shown++ ? "" : "\n",
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800765 committer_ident.buf);
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200766
767 if (ident_shown)
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600768 status_printf_ln(s, GIT_COLOR_NORMAL, "");
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200769
Junio C Hamanod249b092009-08-09 21:59:30 -0700770 saved_color_setting = s->use_color;
771 s->use_color = 0;
Jonathan Nieder37f30122011-02-25 23:10:49 -0600772 commitable = run_status(s->fp, index_file, prefix, 1, s);
Junio C Hamanod249b092009-08-09 21:59:30 -0700773 s->use_color = saved_color_setting;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800774
775 *ai_tmp = ' ';
776 *ci_tmp = ' ';
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100777 } else {
Junio C Hamanod616a232007-12-22 19:22:29 -0800778 unsigned char sha1[20];
Junio C Hamanofbcf1182007-12-19 19:23:03 -0800779 const char *parent = "HEAD";
Alex Riesen71686242007-11-28 22:13:08 +0100780
Alex Riesen71686242007-11-28 22:13:08 +0100781 if (!active_nr && read_cache() < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000782 die(_("Cannot read index"));
Alex Riesen71686242007-11-28 22:13:08 +0100783
Junio C Hamanofbcf1182007-12-19 19:23:03 -0800784 if (amend)
785 parent = "HEAD^1";
786
Junio C Hamanod616a232007-12-22 19:22:29 -0800787 if (get_sha1(parent, sha1))
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100788 commitable = !!active_nr;
Stephan Beyer75f3ff22009-02-10 15:30:35 +0100789 else
790 commitable = index_differs_from(parent, 0);
Alex Riesen71686242007-11-28 22:13:08 +0100791 }
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800792 strbuf_release(&committer_ident);
Alex Riesen71686242007-11-28 22:13:08 +0100793
Jonathan Nieder37f30122011-02-25 23:10:49 -0600794 fclose(s->fp);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500795
Jay Soffian37f7a852011-02-19 23:12:29 -0500796 /*
797 * Reject an attempt to record a non-merge empty commit without
798 * explicit --allow-empty. In the cherry-pick case, it may be
799 * empty due to conflict resolution, which the user should okay.
800 */
801 if (!commitable && whence != FROM_MERGE && !allow_empty &&
Junio C Hamano06bb6432011-08-19 11:58:18 -0700802 !(amend && is_a_merge(current_head))) {
Junio C Hamanod249b092009-08-09 21:59:30 -0700803 run_status(stdout, index_file, prefix, 0, s);
Jeff Kingf197ed22010-06-06 20:41:46 -0400804 if (amend)
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +0000805 fputs(_(empty_amend_advice), stderr);
Jay Soffian37f7a852011-02-19 23:12:29 -0500806 else if (whence == FROM_CHERRY_PICK)
Junio C Hamano6c80cd22011-04-01 17:55:55 -0700807 fputs(_(empty_cherry_pick_advice), stderr);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100808 return 0;
809 }
810
811 /*
812 * Re-read the index as pre-commit hook could have updated it,
813 * and write it out as a tree. We must do this before we invoke
814 * the editor and after we invoke run_status above.
815 */
816 discard_cache();
817 read_cache_from(index_file);
Thomas Rast996277c2011-12-06 18:43:37 +0100818 if (update_main_cache_tree(0)) {
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000819 error(_("Error building trees"));
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100820 return 0;
821 }
822
Paolo Bonzini8089c852008-02-05 08:04:18 +0100823 if (run_hook(index_file, "prepare-commit-msg",
824 git_path(commit_editmsg), hook_arg1, hook_arg2, NULL))
825 return 0;
826
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100827 if (use_editor) {
828 char index[PATH_MAX];
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000829 const char *env[2] = { NULL };
830 env[0] = index;
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100831 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
Stephan Beyer71982032008-07-25 18:28:42 +0200832 if (launch_editor(git_path(commit_editmsg), NULL, env)) {
833 fprintf(stderr,
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000834 _("Please supply the message using either -m or -F option.\n"));
Stephan Beyer71982032008-07-25 18:28:42 +0200835 exit(1);
836 }
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100837 }
838
839 if (!no_verify &&
840 run_hook(index_file, "commit-msg", git_path(commit_editmsg), NULL)) {
841 return 0;
842 }
843
844 return 1;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500845}
846
Junio C Hamanob2eda9b2012-03-30 12:14:33 -0700847static int rest_is_empty(struct strbuf *sb, int start)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500848{
Junio C Hamanob2eda9b2012-03-30 12:14:33 -0700849 int i, eol;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500850 const char *nl;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500851
852 /* Check if the rest is just whitespace and Signed-of-by's. */
853 for (i = start; i < sb->len; i++) {
854 nl = memchr(sb->buf + i, '\n', sb->len - i);
855 if (nl)
856 eol = nl - sb->buf;
857 else
858 eol = sb->len;
859
860 if (strlen(sign_off_header) <= eol - i &&
861 !prefixcmp(sb->buf + i, sign_off_header)) {
862 i = eol;
863 continue;
864 }
865 while (i < eol)
866 if (!isspace(sb->buf[i++]))
867 return 0;
868 }
869
870 return 1;
871}
872
Junio C Hamanob2eda9b2012-03-30 12:14:33 -0700873/*
874 * Find out if the message in the strbuf contains only whitespace and
875 * Signed-off-by lines.
876 */
877static int message_is_empty(struct strbuf *sb)
878{
879 if (cleanup_mode == CLEANUP_NONE && sb->len)
880 return 0;
881 return rest_is_empty(sb, 0);
882}
883
884/*
885 * See if the user edited the message in the editor or left what
886 * was in the template intact
887 */
888static int template_untouched(struct strbuf *sb)
889{
890 struct strbuf tmpl = STRBUF_INIT;
891 char *start;
892
893 if (cleanup_mode == CLEANUP_NONE && sb->len)
894 return 0;
895
896 if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
897 return 0;
898
899 stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
900 start = (char *)skip_prefix(sb->buf, tmpl.buf);
901 if (!start)
902 start = sb->buf;
903 strbuf_release(&tmpl);
904 return rest_is_empty(sb, start - sb->buf);
905}
906
Junio C Hamano146ea062008-08-26 23:13:13 -0700907static const char *find_author_by_nickname(const char *name)
908{
909 struct rev_info revs;
910 struct commit *commit;
911 struct strbuf buf = STRBUF_INIT;
912 const char *av[20];
913 int ac = 0;
914
915 init_revisions(&revs, NULL);
916 strbuf_addf(&buf, "--author=%s", name);
917 av[++ac] = "--all";
918 av[++ac] = "-i";
919 av[++ac] = buf.buf;
920 av[++ac] = NULL;
921 setup_revisions(ac, av, &revs, NULL);
922 prepare_revision_walk(&revs);
923 commit = get_revision(&revs);
924 if (commit) {
Thomas Rastdd2e7942009-10-19 17:48:08 +0200925 struct pretty_print_context ctx = {0};
926 ctx.date_mode = DATE_NORMAL;
Junio C Hamano146ea062008-08-26 23:13:13 -0700927 strbuf_release(&buf);
Thomas Rastdd2e7942009-10-19 17:48:08 +0200928 format_commit_message(commit, "%an <%ae>", &buf, &ctx);
Junio C Hamano146ea062008-08-26 23:13:13 -0700929 return strbuf_detach(&buf, NULL);
930 }
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000931 die(_("No existing author found with '%s'"), name);
Junio C Hamano146ea062008-08-26 23:13:13 -0700932}
933
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700934
935static void handle_untracked_files_arg(struct wt_status *s)
936{
937 if (!untracked_files_arg)
938 ; /* default already initialized */
939 else if (!strcmp(untracked_files_arg, "no"))
940 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
941 else if (!strcmp(untracked_files_arg, "normal"))
942 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
943 else if (!strcmp(untracked_files_arg, "all"))
944 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
945 else
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000946 die(_("Invalid untracked files mode '%s'"), untracked_files_arg);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700947}
948
Jay Soffian37f7a852011-02-19 23:12:29 -0500949static const char *read_commit_message(const char *name)
950{
Jeff Kingdd0d3882013-01-26 04:44:06 -0500951 const char *out_enc;
Jay Soffian37f7a852011-02-19 23:12:29 -0500952 struct commit *commit;
953
954 commit = lookup_commit_reference_by_name(name);
955 if (!commit)
Junio C Hamano6c80cd22011-04-01 17:55:55 -0700956 die(_("could not lookup commit %s"), name);
Jay Soffian37f7a852011-02-19 23:12:29 -0500957 out_enc = get_commit_output_encoding();
Nguyễn Thái Ngọc Duy5a10d232013-04-19 09:08:40 +1000958 return logmsg_reencode(commit, NULL, out_enc);
Jay Soffian37f7a852011-02-19 23:12:29 -0500959}
960
Shawn Bohrer2f02b252007-12-02 23:02:09 -0600961static int parse_and_validate_options(int argc, const char *argv[],
Jeff King036dbbf2012-05-07 15:18:26 -0400962 const struct option *options,
Junio C Hamanodbd0f5c2008-08-06 11:43:47 -0700963 const char * const usage[],
Junio C Hamanod249b092009-08-09 21:59:30 -0700964 const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -0700965 struct commit *current_head,
Junio C Hamanod249b092009-08-09 21:59:30 -0700966 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500967{
968 int f = 0;
969
Jeff King036dbbf2012-05-07 15:18:26 -0400970 argc = parse_options(argc, argv, prefix, options, usage, 0);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500971
Junio C Hamano146ea062008-08-26 23:13:13 -0700972 if (force_author && !strchr(force_author, '>'))
973 force_author = find_author_by_nickname(force_author);
974
Erick Mattosc51f6ce2009-11-04 01:20:11 -0200975 if (force_author && renew_authorship)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000976 die(_("Using both --reset-author and --author does not make sense"));
Erick Mattosc51f6ce2009-11-04 01:20:11 -0200977
Pat Notzd71b8ba2010-11-02 13:59:09 -0600978 if (logfile || message.len || use_message || fixup_message)
Junio C Hamano48034662007-12-22 19:25:37 -0800979 use_editor = 0;
Junio C Hamanoca1ba202011-12-06 13:09:55 -0800980 if (0 <= edit_flag)
981 use_editor = edit_flag;
Paolo Bonzini406400c2008-02-05 11:01:45 +0100982 if (!use_editor)
983 setenv("GIT_EDITOR", ":", 1);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500984
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500985 /* Sanity check options */
Junio C Hamano06bb6432011-08-19 11:58:18 -0700986 if (amend && !current_head)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000987 die(_("You have nothing to amend."));
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000988 if (amend && whence != FROM_COMMIT) {
989 if (whence == FROM_MERGE)
990 die(_("You are in the middle of a merge -- cannot amend."));
991 else if (whence == FROM_CHERRY_PICK)
992 die(_("You are in the middle of a cherry-pick -- cannot amend."));
993 }
Pat Notz89ac1222010-11-02 13:59:11 -0600994 if (fixup_message && squash_message)
Ævar Arnfjörð Bjarmason9c227652011-02-22 23:41:45 +0000995 die(_("Options --squash and --fixup cannot be used together"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500996 if (use_message)
997 f++;
998 if (edit_message)
999 f++;
Pat Notzd71b8ba2010-11-02 13:59:09 -06001000 if (fixup_message)
1001 f++;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001002 if (logfile)
1003 f++;
1004 if (f > 1)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001005 die(_("Only one of -c/-C/-F/--fixup can be used."));
Johannes Schindelinf9568532007-11-11 17:36:39 +00001006 if (message.len && f > 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001007 die((_("Option -m cannot be combined with -c/-C/-F/--fixup.")));
Junio C Hamano010c7db2012-03-30 11:30:59 -07001008 if (f || message.len)
1009 template_file = NULL;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001010 if (edit_message)
1011 use_message = edit_message;
Pat Notzd71b8ba2010-11-02 13:59:09 -06001012 if (amend && !use_message && !fixup_message)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001013 use_message = "HEAD";
Jay Soffian37f7a852011-02-19 23:12:29 -05001014 if (!use_message && whence != FROM_CHERRY_PICK && renew_authorship)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001015 die(_("--reset-author can be used only with -C, -c or --amend."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001016 if (use_message) {
Jay Soffian37f7a852011-02-19 23:12:29 -05001017 use_message_buffer = read_commit_message(use_message);
1018 if (!renew_authorship) {
1019 author_message = use_message;
1020 author_message_buffer = use_message_buffer;
1021 }
1022 }
1023 if (whence == FROM_CHERRY_PICK && !renew_authorship) {
1024 author_message = "CHERRY_PICK_HEAD";
1025 author_message_buffer = read_commit_message(author_message);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001026 }
1027
Conrad Irwinb4bd4662011-05-07 10:58:07 -07001028 if (patch_interactive)
1029 interactive = 1;
1030
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001031 if (!!also + !!only + !!all + !!interactive > 1)
Conrad Irwinb4bd4662011-05-07 10:58:07 -07001032 die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001033 if (argc == 0 && (also || (only && !amend)))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001034 die(_("No paths with --include/--only does not make sense."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001035 if (argc == 0 && only && amend)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001036 only_include_assumed = _("Clever... amending the last one with dirty index.");
Johannes Sixt3c5283f2008-04-10 13:33:08 +02001037 if (argc > 0 && !also && !only)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001038 only_include_assumed = _("Explicit paths specified without -i nor -o; assuming --only paths...");
Alex Riesen5f065732007-12-22 19:46:24 +01001039 if (!cleanup_arg || !strcmp(cleanup_arg, "default"))
1040 cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE;
1041 else if (!strcmp(cleanup_arg, "verbatim"))
1042 cleanup_mode = CLEANUP_NONE;
1043 else if (!strcmp(cleanup_arg, "whitespace"))
1044 cleanup_mode = CLEANUP_SPACE;
1045 else if (!strcmp(cleanup_arg, "strip"))
1046 cleanup_mode = CLEANUP_ALL;
1047 else
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001048 die(_("Invalid cleanup mode %s"), cleanup_arg);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001049
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001050 handle_untracked_files_arg(s);
Marius Storm-Olsen4bfee302008-06-05 10:31:19 +02001051
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001052 if (all && argc > 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001053 die(_("Paths with -a does not make sense."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001054
Jeff Kingf3f47a12012-10-18 21:15:50 +07001055 if (s->null_termination) {
1056 if (status_format == STATUS_FORMAT_NONE)
1057 status_format = STATUS_FORMAT_PORCELAIN;
1058 else if (status_format == STATUS_FORMAT_LONG)
1059 die(_("--long and -z are incompatible"));
1060 }
1061 if (status_format != STATUS_FORMAT_NONE)
Jeff King7c9f7032009-09-05 04:59:56 -04001062 dry_run = 1;
1063
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001064 return argc;
1065}
1066
Junio C Hamanod249b092009-08-09 21:59:30 -07001067static int dry_run_commit(int argc, const char **argv, const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001068 const struct commit *current_head, struct wt_status *s)
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001069{
1070 int commitable;
1071 const char *index_file;
1072
Junio C Hamano06bb6432011-08-19 11:58:18 -07001073 index_file = prepare_index(argc, argv, prefix, current_head, 1);
Junio C Hamanod249b092009-08-09 21:59:30 -07001074 commitable = run_status(stdout, index_file, prefix, 0, s);
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001075 rollback_index_files();
1076
1077 return commitable ? 0 : 1;
1078}
1079
Junio C Hamanof766b362009-08-09 23:12:19 -07001080static int parse_status_slot(const char *var, int offset)
1081{
1082 if (!strcasecmp(var+offset, "header"))
1083 return WT_STATUS_HEADER;
Aleksi Aalto1d282322010-11-18 01:40:05 +02001084 if (!strcasecmp(var+offset, "branch"))
1085 return WT_STATUS_ONBRANCH;
Junio C Hamanof766b362009-08-09 23:12:19 -07001086 if (!strcasecmp(var+offset, "updated")
1087 || !strcasecmp(var+offset, "added"))
1088 return WT_STATUS_UPDATED;
1089 if (!strcasecmp(var+offset, "changed"))
1090 return WT_STATUS_CHANGED;
1091 if (!strcasecmp(var+offset, "untracked"))
1092 return WT_STATUS_UNTRACKED;
1093 if (!strcasecmp(var+offset, "nobranch"))
1094 return WT_STATUS_NOBRANCH;
1095 if (!strcasecmp(var+offset, "unmerged"))
1096 return WT_STATUS_UNMERGED;
Jeff King8b8e8622009-12-12 07:25:24 -05001097 return -1;
Junio C Hamanof766b362009-08-09 23:12:19 -07001098}
1099
1100static int git_status_config(const char *k, const char *v, void *cb)
1101{
1102 struct wt_status *s = cb;
1103
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +07001104 if (!prefixcmp(k, "column."))
Jeff King4d2292e2012-05-07 15:35:03 -04001105 return git_column_config(k, v, "status", &s->colopts);
Junio C Hamanof766b362009-08-09 23:12:19 -07001106 if (!strcmp(k, "status.submodulesummary")) {
1107 int is_bool;
1108 s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
1109 if (is_bool && s->submodule_summary)
1110 s->submodule_summary = -1;
1111 return 0;
1112 }
1113 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
Jeff Kinge269eb72011-08-17 22:03:48 -07001114 s->use_color = git_config_colorbool(k, v);
Junio C Hamanof766b362009-08-09 23:12:19 -07001115 return 0;
1116 }
1117 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
1118 int slot = parse_status_slot(k, 13);
Jeff King8b8e8622009-12-12 07:25:24 -05001119 if (slot < 0)
1120 return 0;
Junio C Hamanof766b362009-08-09 23:12:19 -07001121 if (!v)
1122 return config_error_nonbool(k);
1123 color_parse(v, k, s->color_palette[slot]);
1124 return 0;
1125 }
1126 if (!strcmp(k, "status.relativepaths")) {
1127 s->relative_paths = git_config_bool(k, v);
1128 return 0;
1129 }
1130 if (!strcmp(k, "status.showuntrackedfiles")) {
1131 if (!v)
1132 return config_error_nonbool(k);
1133 else if (!strcmp(v, "no"))
1134 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
1135 else if (!strcmp(v, "normal"))
1136 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
1137 else if (!strcmp(v, "all"))
1138 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
1139 else
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001140 return error(_("Invalid untracked files mode '%s'"), v);
Junio C Hamanof766b362009-08-09 23:12:19 -07001141 return 0;
1142 }
1143 return git_diff_ui_config(k, v, NULL);
1144}
1145
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001146int cmd_status(int argc, const char **argv, const char *prefix)
1147{
Jeff King036dbbf2012-05-07 15:18:26 -04001148 static struct wt_status s;
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001149 int fd;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001150 unsigned char sha1[20];
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001151 static struct option builtin_status_options[] = {
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001152 OPT__VERBOSE(&verbose, N_("be verbose")),
Jeff Kingdd2be242009-09-05 04:54:14 -04001153 OPT_SET_INT('s', "short", &status_format,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001154 N_("show status concisely"), STATUS_FORMAT_SHORT),
Jeff Kingd4a6bf12012-05-07 17:09:04 -04001155 OPT_BOOLEAN('b', "branch", &s.show_branch,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001156 N_("show branch information")),
Jeff King6f157872009-09-05 04:55:37 -04001157 OPT_SET_INT(0, "porcelain", &status_format,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001158 N_("machine-readable output"),
Jeff King6f157872009-09-05 04:55:37 -04001159 STATUS_FORMAT_PORCELAIN),
Jeff Kingf3f47a12012-10-18 21:15:50 +07001160 OPT_SET_INT(0, "long", &status_format,
1161 N_("show status in long format (default)"),
1162 STATUS_FORMAT_LONG),
Jeff King3207a3a2012-05-07 15:44:44 -04001163 OPT_BOOLEAN('z', "null", &s.null_termination,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001164 N_("terminate entries with NUL")),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001165 { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001166 N_("mode"),
1167 N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001168 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
Junio C Hamano2381e392010-04-10 00:33:17 -07001169 OPT_BOOLEAN(0, "ignored", &show_ignored_in_status,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001170 N_("show ignored files")),
1171 { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, N_("when"),
1172 N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
Jens Lehmann46a958b2010-06-25 16:56:47 +02001173 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001174 OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001175 OPT_END(),
1176 };
1177
Nguyễn Thái Ngọc Duy5d3dd912010-10-22 01:45:47 -05001178 if (argc == 2 && !strcmp(argv[1], "-h"))
1179 usage_with_options(builtin_status_usage, builtin_status_options);
1180
Junio C Hamanod249b092009-08-09 21:59:30 -07001181 wt_status_prepare(&s);
Jens Lehmann302ad7a2010-08-06 00:40:48 +02001182 gitmodules_config();
Junio C Hamanod249b092009-08-09 21:59:30 -07001183 git_config(git_status_config, &s);
Jay Soffian37f7a852011-02-19 23:12:29 -05001184 determine_whence(&s);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001185 argc = parse_options(argc, argv, prefix,
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001186 builtin_status_options,
1187 builtin_status_usage, 0);
Jeff King4d2292e2012-05-07 15:35:03 -04001188 finalize_colopts(&s.colopts, -1);
Junio C Hamano6c929722011-06-06 11:40:08 -07001189
Jeff Kingf3f47a12012-10-18 21:15:50 +07001190 if (s.null_termination) {
1191 if (status_format == STATUS_FORMAT_NONE)
1192 status_format = STATUS_FORMAT_PORCELAIN;
1193 else if (status_format == STATUS_FORMAT_LONG)
1194 die(_("--long and -z are incompatible"));
1195 }
Junio C Hamano6c929722011-06-06 11:40:08 -07001196
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001197 handle_untracked_files_arg(&s);
Junio C Hamano2381e392010-04-10 00:33:17 -07001198 if (show_ignored_in_status)
1199 s.show_ignored_files = 1;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001200 if (*argv)
1201 s.pathspec = get_pathspec(prefix, argv);
1202
Junio C Hamano149794d2010-02-17 12:30:41 -08001203 read_cache_preload(s.pathspec);
Nguyễn Thái Ngọc Duy688cd6d2010-01-14 22:02:21 +07001204 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec, NULL, NULL);
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001205
1206 fd = hold_locked_index(&index_lock, 0);
Junio C Hamanoccdc4ec2011-03-21 10:16:10 -07001207 if (0 <= fd)
1208 update_index_if_able(&the_index, &index_lock);
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001209
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001210 s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
Jens Lehmann46a958b2010-06-25 16:56:47 +02001211 s.ignore_submodule_arg = ignore_submodule_arg;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001212 wt_status_collect(&s);
1213
Jeff King86617682009-12-07 00:26:25 -05001214 if (s.relative_paths)
1215 s.prefix = prefix;
Markus Heidelberg38920dd2009-01-08 19:53:05 +01001216
Jeff Kingdd2be242009-09-05 04:54:14 -04001217 switch (status_format) {
1218 case STATUS_FORMAT_SHORT:
Jeff Kingd4a6bf12012-05-07 17:09:04 -04001219 wt_shortstatus_print(&s);
Jeff Kingdd2be242009-09-05 04:54:14 -04001220 break;
Jeff King6f157872009-09-05 04:55:37 -04001221 case STATUS_FORMAT_PORCELAIN:
Jeff King3207a3a2012-05-07 15:44:44 -04001222 wt_porcelain_print(&s);
Jeff King6f157872009-09-05 04:55:37 -04001223 break;
Jeff Kingf3f47a12012-10-18 21:15:50 +07001224 case STATUS_FORMAT_NONE:
Jeff Kingdd2be242009-09-05 04:54:14 -04001225 case STATUS_FORMAT_LONG:
Junio C Hamano173e6c82009-08-04 23:55:22 -07001226 s.verbose = verbose;
Jens Lehmann46a958b2010-06-25 16:56:47 +02001227 s.ignore_submodule_arg = ignore_submodule_arg;
Junio C Hamano173e6c82009-08-04 23:55:22 -07001228 wt_status_print(&s);
Jeff Kingdd2be242009-09-05 04:54:14 -04001229 break;
Junio C Hamano173e6c82009-08-04 23:55:22 -07001230 }
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001231 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001232}
1233
Junio C Hamano06bb6432011-08-19 11:58:18 -07001234static void print_summary(const char *prefix, const unsigned char *sha1,
1235 int initial_commit)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001236{
1237 struct rev_info rev;
1238 struct commit *commit;
Jeff King49ff9a72010-01-13 12:39:51 -05001239 struct strbuf format = STRBUF_INIT;
Jeff Kingc85db252008-10-01 18:31:25 -04001240 unsigned char junk_sha1[20];
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +07001241 const char *head;
Jeff King49ff9a72010-01-13 12:39:51 -05001242 struct pretty_print_context pctx = {0};
1243 struct strbuf author_ident = STRBUF_INIT;
1244 struct strbuf committer_ident = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001245
1246 commit = lookup_commit(sha1);
1247 if (!commit)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001248 die(_("couldn't look up newly created commit"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001249 if (!commit || parse_commit(commit))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001250 die(_("could not parse newly created commit"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001251
Jeff King49ff9a72010-01-13 12:39:51 -05001252 strbuf_addstr(&format, "format:%h] %s");
1253
1254 format_commit_message(commit, "%an <%ae>", &author_ident, &pctx);
1255 format_commit_message(commit, "%cn <%ce>", &committer_ident, &pctx);
1256 if (strbuf_cmp(&author_ident, &committer_ident)) {
1257 strbuf_addstr(&format, "\n Author: ");
1258 strbuf_addbuf_percentquote(&format, &author_ident);
1259 }
Jeff Kingd6991ce2012-11-14 16:34:13 -08001260 if (!committer_ident_sufficiently_given()) {
Jeff King49ff9a72010-01-13 12:39:51 -05001261 strbuf_addstr(&format, "\n Committer: ");
1262 strbuf_addbuf_percentquote(&format, &committer_ident);
Jeff Kingb706fcf2010-01-13 15:17:08 -05001263 if (advice_implicit_identity) {
1264 strbuf_addch(&format, '\n');
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +00001265 strbuf_addstr(&format, _(implicit_ident_advice));
Jeff Kingb706fcf2010-01-13 15:17:08 -05001266 }
Jeff King49ff9a72010-01-13 12:39:51 -05001267 }
1268 strbuf_release(&author_ident);
1269 strbuf_release(&committer_ident);
1270
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001271 init_revisions(&rev, prefix);
1272 setup_revisions(0, NULL, &rev, NULL);
1273
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001274 rev.diff = 1;
1275 rev.diffopt.output_format =
1276 DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY;
1277
1278 rev.verbose_header = 1;
1279 rev.show_root_diff = 1;
Jeff King49ff9a72010-01-13 12:39:51 -05001280 get_commit_format(format.buf, &rev);
Junio C Hamanobf82a152007-12-10 21:02:26 -08001281 rev.always_show_header = 0;
Junio C Hamano3eb2a152007-12-16 15:05:39 -08001282 rev.diffopt.detect_rename = 1;
Junio C Hamano3eb2a152007-12-16 15:05:39 -08001283 rev.diffopt.break_opt = 0;
Junio C Hamano15964562007-12-16 15:03:58 -08001284 diff_setup_done(&rev.diffopt);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001285
Nguyễn Thái Ngọc Duy8cad4742011-12-12 18:20:32 +07001286 head = resolve_ref_unsafe("HEAD", junk_sha1, 0, NULL);
Santi Béjarc5ee71f2009-01-19 23:45:16 +01001287 printf("[%s%s ",
Jeff Kingc85db252008-10-01 18:31:25 -04001288 !prefixcmp(head, "refs/heads/") ?
1289 head + 11 :
1290 !strcmp(head, "HEAD") ?
Ævar Arnfjörð Bjarmason7f5673d2011-02-22 23:41:47 +00001291 _("detached HEAD") :
Jeff Kingc85db252008-10-01 18:31:25 -04001292 head,
Ævar Arnfjörð Bjarmason7f5673d2011-02-22 23:41:47 +00001293 initial_commit ? _(" (root-commit)") : "");
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001294
Junio C Hamanobf82a152007-12-10 21:02:26 -08001295 if (!log_tree_commit(&rev, commit)) {
Tay Ray Chuana45e1a82010-06-12 22:15:39 +08001296 rev.always_show_header = 1;
1297 rev.use_terminator = 1;
1298 log_tree_commit(&rev, commit);
Junio C Hamanobf82a152007-12-10 21:02:26 -08001299 }
Tay Ray Chuana45e1a82010-06-12 22:15:39 +08001300
Junio C Hamanofc6f19f2010-01-17 00:57:51 -08001301 strbuf_release(&format);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001302}
1303
Stephan Beyer186458b2008-07-24 01:09:35 +02001304static int git_commit_config(const char *k, const char *v, void *cb)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001305{
Junio C Hamanod249b092009-08-09 21:59:30 -07001306 struct wt_status *s = cb;
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001307 int status;
Junio C Hamanod249b092009-08-09 21:59:30 -07001308
Brian Hetro984c6e72008-07-05 01:24:40 -04001309 if (!strcmp(k, "commit.template"))
Matthieu Moy395de252009-11-17 18:24:25 +01001310 return git_config_pathname(&template_file, k, v);
James P. Howard, IIbed575e2009-12-07 17:45:27 -05001311 if (!strcmp(k, "commit.status")) {
1312 include_status = git_config_bool(k, v);
1313 return 0;
1314 }
Ralf Thielow51fb3a32013-01-10 18:45:59 +01001315 if (!strcmp(k, "commit.cleanup"))
1316 return git_config_string(&cleanup_arg, k, v);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001317
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001318 status = git_gpg_config(k, v, NULL);
1319 if (status)
1320 return status;
Junio C Hamanod249b092009-08-09 21:59:30 -07001321 return git_status_config(k, v, s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001322}
1323
Thomas Rast6f6bee32010-03-12 18:04:28 +01001324static int run_rewrite_hook(const unsigned char *oldsha1,
1325 const unsigned char *newsha1)
1326{
1327 /* oldsha1 SP newsha1 LF NUL */
1328 static char buf[2*40 + 3];
1329 struct child_process proc;
1330 const char *argv[3];
1331 int code;
1332 size_t n;
1333
Aaron Schrab5a7da2d2013-01-13 00:17:02 -05001334 argv[0] = find_hook("post-rewrite");
1335 if (!argv[0])
Thomas Rast6f6bee32010-03-12 18:04:28 +01001336 return 0;
1337
Thomas Rast6f6bee32010-03-12 18:04:28 +01001338 argv[1] = "amend";
1339 argv[2] = NULL;
1340
1341 memset(&proc, 0, sizeof(proc));
1342 proc.argv = argv;
1343 proc.in = -1;
1344 proc.stdout_to_stderr = 1;
1345
1346 code = start_command(&proc);
1347 if (code)
1348 return code;
1349 n = snprintf(buf, sizeof(buf), "%s %s\n",
1350 sha1_to_hex(oldsha1), sha1_to_hex(newsha1));
1351 write_in_full(proc.in, buf, n);
1352 close(proc.in);
1353 return finish_command(&proc);
1354}
1355
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001356int cmd_commit(int argc, const char **argv, const char *prefix)
1357{
Jeff King036dbbf2012-05-07 15:18:26 -04001358 static struct wt_status s;
1359 static struct option builtin_commit_options[] = {
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001360 OPT__QUIET(&quiet, N_("suppress summary after successful commit")),
1361 OPT__VERBOSE(&verbose, N_("show diff in commit message template")),
Jeff King036dbbf2012-05-07 15:18:26 -04001362
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001363 OPT_GROUP(N_("Commit message options")),
1364 OPT_FILENAME('F', "file", &logfile, N_("read message from file")),
1365 OPT_STRING(0, "author", &force_author, N_("author"), N_("override author for commit")),
1366 OPT_STRING(0, "date", &force_date, N_("date"), N_("override date for commit")),
1367 OPT_CALLBACK('m', "message", &message, N_("message"), N_("commit message"), opt_parse_m),
1368 OPT_STRING('c', "reedit-message", &edit_message, N_("commit"), N_("reuse and edit message from specified commit")),
1369 OPT_STRING('C', "reuse-message", &use_message, N_("commit"), N_("reuse message from specified commit")),
1370 OPT_STRING(0, "fixup", &fixup_message, N_("commit"), N_("use autosquash formatted message to fixup specified commit")),
1371 OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
1372 OPT_BOOLEAN(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")),
1373 OPT_BOOLEAN('s', "signoff", &signoff, N_("add Signed-off-by:")),
1374 OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
1375 OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),
1376 OPT_STRING(0, "cleanup", &cleanup_arg, N_("default"), N_("how to strip spaces and #comments from message")),
1377 OPT_BOOLEAN(0, "status", &include_status, N_("include status in commit message template")),
1378 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key id"),
1379 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
Jeff King036dbbf2012-05-07 15:18:26 -04001380 /* end commit message options */
1381
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001382 OPT_GROUP(N_("Commit contents options")),
1383 OPT_BOOLEAN('a', "all", &all, N_("commit all changed files")),
1384 OPT_BOOLEAN('i', "include", &also, N_("add specified files to index for commit")),
1385 OPT_BOOLEAN(0, "interactive", &interactive, N_("interactively add files")),
1386 OPT_BOOLEAN('p', "patch", &patch_interactive, N_("interactively add changes")),
1387 OPT_BOOLEAN('o', "only", &only, N_("commit only specified files")),
1388 OPT_BOOLEAN('n', "no-verify", &no_verify, N_("bypass pre-commit hook")),
1389 OPT_BOOLEAN(0, "dry-run", &dry_run, N_("show what would be committed")),
1390 OPT_SET_INT(0, "short", &status_format, N_("show status concisely"),
Jeff King036dbbf2012-05-07 15:18:26 -04001391 STATUS_FORMAT_SHORT),
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001392 OPT_BOOLEAN(0, "branch", &s.show_branch, N_("show branch information")),
Jeff King036dbbf2012-05-07 15:18:26 -04001393 OPT_SET_INT(0, "porcelain", &status_format,
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001394 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN),
Jeff Kingf3f47a12012-10-18 21:15:50 +07001395 OPT_SET_INT(0, "long", &status_format,
1396 N_("show status in long format (default)"),
1397 STATUS_FORMAT_LONG),
Jeff King3207a3a2012-05-07 15:44:44 -04001398 OPT_BOOLEAN('z', "null", &s.null_termination,
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001399 N_("terminate entries with NUL")),
1400 OPT_BOOLEAN(0, "amend", &amend, N_("amend previous commit")),
1401 OPT_BOOLEAN(0, "no-post-rewrite", &no_post_rewrite, N_("bypass post-rewrite hook")),
1402 { 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" },
Jeff King036dbbf2012-05-07 15:18:26 -04001403 /* end commit contents options */
1404
1405 { OPTION_BOOLEAN, 0, "allow-empty", &allow_empty, NULL,
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001406 N_("ok to record an empty change"),
Jeff King036dbbf2012-05-07 15:18:26 -04001407 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
1408 { OPTION_BOOLEAN, 0, "allow-empty-message", &allow_empty_message, NULL,
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001409 N_("ok to record a change with an empty message"),
Jeff King036dbbf2012-05-07 15:18:26 -04001410 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
1411
1412 OPT_END()
1413 };
1414
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001415 struct strbuf sb = STRBUF_INIT;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -08001416 struct strbuf author_ident = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001417 const char *index_file, *reflog_msg;
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001418 char *nl, *p;
Junio C Hamano06bb6432011-08-19 11:58:18 -07001419 unsigned char sha1[20];
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001420 struct ref_lock *ref_lock;
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001421 struct commit_list *parents = NULL, **pptr = &parents;
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001422 struct stat statbuf;
1423 int allow_fast_forward = 1;
Junio C Hamano06bb6432011-08-19 11:58:18 -07001424 struct commit *current_head = NULL;
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001425 struct commit_extra_header *extra = NULL;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001426
Nguyễn Thái Ngọc Duy5d3dd912010-10-22 01:45:47 -05001427 if (argc == 2 && !strcmp(argv[1], "-h"))
1428 usage_with_options(builtin_commit_usage, builtin_commit_options);
1429
Junio C Hamanod249b092009-08-09 21:59:30 -07001430 wt_status_prepare(&s);
Orgad Shaneh8f6811e2012-09-23 09:37:47 +02001431 gitmodules_config();
Junio C Hamanod249b092009-08-09 21:59:30 -07001432 git_config(git_commit_config, &s);
Jay Soffian37f7a852011-02-19 23:12:29 -05001433 determine_whence(&s);
Jeff King4d2292e2012-05-07 15:35:03 -04001434 s.colopts = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001435
Junio C Hamano06bb6432011-08-19 11:58:18 -07001436 if (get_sha1("HEAD", sha1))
1437 current_head = NULL;
1438 else {
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +10001439 current_head = lookup_commit_or_die(sha1, "HEAD");
Junio C Hamano06bb6432011-08-19 11:58:18 -07001440 if (!current_head || parse_commit(current_head))
1441 die(_("could not parse HEAD commit"));
1442 }
Jeff King036dbbf2012-05-07 15:18:26 -04001443 argc = parse_and_validate_options(argc, argv, builtin_commit_options,
1444 builtin_commit_usage,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001445 prefix, current_head, &s);
Jeff Kingc9bfb952011-08-17 22:05:35 -07001446 if (dry_run)
Junio C Hamano06bb6432011-08-19 11:58:18 -07001447 return dry_run_commit(argc, argv, prefix, current_head, &s);
Junio C Hamano06bb6432011-08-19 11:58:18 -07001448 index_file = prepare_index(argc, argv, prefix, current_head, 0);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001449
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001450 /* Set up everything for writing the commit object. This includes
1451 running hooks, writing the trees, and interacting with the user. */
Junio C Hamano06bb6432011-08-19 11:58:18 -07001452 if (!prepare_to_commit(index_file, prefix,
1453 current_head, &s, &author_ident)) {
Junio C Hamano28886052007-11-18 01:52:55 -08001454 rollback_index_files();
1455 return 1;
1456 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001457
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001458 /* Determine parents */
Christian Couder643cb5f2010-06-12 18:05:12 +02001459 reflog_msg = getenv("GIT_REFLOG_ACTION");
Junio C Hamano06bb6432011-08-19 11:58:18 -07001460 if (!current_head) {
Christian Couder643cb5f2010-06-12 18:05:12 +02001461 if (!reflog_msg)
1462 reflog_msg = "commit (initial)";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001463 } else if (amend) {
1464 struct commit_list *c;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001465
Christian Couder643cb5f2010-06-12 18:05:12 +02001466 if (!reflog_msg)
1467 reflog_msg = "commit (amend)";
Junio C Hamano06bb6432011-08-19 11:58:18 -07001468 for (c = current_head->parents; c; c = c->next)
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001469 pptr = &commit_list_insert(c->item, pptr)->next;
Jay Soffian37f7a852011-02-19 23:12:29 -05001470 } else if (whence == FROM_MERGE) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001471 struct strbuf m = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001472 FILE *fp;
1473
Christian Couder643cb5f2010-06-12 18:05:12 +02001474 if (!reflog_msg)
1475 reflog_msg = "commit (merge)";
Junio C Hamano06bb6432011-08-19 11:58:18 -07001476 pptr = &commit_list_insert(current_head, pptr)->next;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001477 fp = fopen(git_path("MERGE_HEAD"), "r");
1478 if (fp == NULL)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001479 die_errno(_("could not open '%s' for reading"),
Thomas Rastd824cbb2009-06-27 17:58:46 +02001480 git_path("MERGE_HEAD"));
Linus Torvalds7c3fd252008-01-15 16:12:33 -08001481 while (strbuf_getline(&m, fp, '\n') != EOF) {
Junio C Hamano5231c632011-11-07 16:21:32 -08001482 struct commit *parent;
1483
1484 parent = get_merge_parent(m.buf);
1485 if (!parent)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001486 die(_("Corrupt MERGE_HEAD file (%s)"), m.buf);
Junio C Hamano5231c632011-11-07 16:21:32 -08001487 pptr = &commit_list_insert(parent, pptr)->next;
Linus Torvalds7c3fd252008-01-15 16:12:33 -08001488 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001489 fclose(fp);
1490 strbuf_release(&m);
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001491 if (!stat(git_path("MERGE_MODE"), &statbuf)) {
1492 if (strbuf_read_file(&sb, git_path("MERGE_MODE"), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001493 die_errno(_("could not read MERGE_MODE"));
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001494 if (!strcmp(sb.buf, "no-ff"))
1495 allow_fast_forward = 0;
1496 }
1497 if (allow_fast_forward)
1498 parents = reduce_heads(parents);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001499 } else {
Christian Couder643cb5f2010-06-12 18:05:12 +02001500 if (!reflog_msg)
Jay Soffian37f7a852011-02-19 23:12:29 -05001501 reflog_msg = (whence == FROM_CHERRY_PICK)
1502 ? "commit (cherry-pick)"
1503 : "commit";
Junio C Hamano06bb6432011-08-19 11:58:18 -07001504 pptr = &commit_list_insert(current_head, pptr)->next;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001505 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001506
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001507 /* Finally, get the commit message */
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001508 strbuf_reset(&sb);
Junio C Hamano740001a2007-12-08 23:23:20 -08001509 if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
Thomas Rast0721c312009-06-27 17:58:47 +02001510 int saved_errno = errno;
Junio C Hamano740001a2007-12-08 23:23:20 -08001511 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001512 die(_("could not read commit message: %s"), strerror(saved_errno));
Junio C Hamano740001a2007-12-08 23:23:20 -08001513 }
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001514
1515 /* Truncate the message just before the diff, if any. */
Jeff King0b382272008-11-12 03:25:52 -05001516 if (verbose) {
1517 p = strstr(sb.buf, "\ndiff --git ");
1518 if (p != NULL)
1519 strbuf_setlen(&sb, p - sb.buf + 1);
1520 }
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001521
Alex Riesen5f065732007-12-22 19:46:24 +01001522 if (cleanup_mode != CLEANUP_NONE)
1523 stripspace(&sb, cleanup_mode == CLEANUP_ALL);
Junio C Hamanob2eda9b2012-03-30 12:14:33 -07001524 if (template_untouched(&sb) && !allow_empty_message) {
1525 rollback_index_files();
1526 fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
1527 exit(1);
1528 }
Ævar Arnfjörð Bjarmasonc9b5fde2010-04-06 08:40:35 +00001529 if (message_is_empty(&sb) && !allow_empty_message) {
Junio C Hamano28886052007-11-18 01:52:55 -08001530 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001531 fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
Jeff Kingfdc7c812008-07-31 03:36:09 -04001532 exit(1);
Junio C Hamano28886052007-11-18 01:52:55 -08001533 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001534
Junio C Hamano0074d182011-12-20 13:20:56 -08001535 if (amend) {
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001536 const char *exclude_gpgsig[2] = { "gpgsig", NULL };
1537 extra = read_commit_extra_headers(current_head, exclude_gpgsig);
Junio C Hamano0074d182011-12-20 13:20:56 -08001538 } else {
1539 struct commit_extra_header **tail = &extra;
1540 append_merge_tag_headers(parents, &tail);
1541 }
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001542
Junio C Hamanof35ccd92011-12-22 11:27:26 -08001543 if (commit_tree_extended(&sb, active_cache_tree->sha1, parents, sha1,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001544 author_ident.buf, sign_commit, extra)) {
Junio C Hamano28886052007-11-18 01:52:55 -08001545 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001546 die(_("failed to write commit object"));
Junio C Hamano28886052007-11-18 01:52:55 -08001547 }
Junio C Hamano4c28e4a2010-12-20 17:00:36 -08001548 strbuf_release(&author_ident);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001549 free_commit_extra_headers(extra);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001550
1551 ref_lock = lock_any_ref_for_update("HEAD",
Junio C Hamano06bb6432011-08-19 11:58:18 -07001552 !current_head
1553 ? NULL
1554 : current_head->object.sha1,
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001555 0);
1556
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001557 nl = strchr(sb.buf, '\n');
Johannes Schindelin741707b2007-11-08 12:15:26 +00001558 if (nl)
1559 strbuf_setlen(&sb, nl + 1 - sb.buf);
1560 else
1561 strbuf_addch(&sb, '\n');
Johannes Schindelin741707b2007-11-08 12:15:26 +00001562 strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
1563 strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001564
Junio C Hamano28886052007-11-18 01:52:55 -08001565 if (!ref_lock) {
1566 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001567 die(_("cannot lock HEAD ref"));
Junio C Hamano28886052007-11-18 01:52:55 -08001568 }
Junio C Hamano06bb6432011-08-19 11:58:18 -07001569 if (write_ref_sha1(ref_lock, sha1, sb.buf) < 0) {
Junio C Hamano28886052007-11-18 01:52:55 -08001570 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001571 die(_("cannot update HEAD ref"));
Junio C Hamano28886052007-11-18 01:52:55 -08001572 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001573
Jay Soffiand7e5c0c2011-02-19 23:12:27 -05001574 unlink(git_path("CHERRY_PICK_HEAD"));
Jonathan Nieder82433cd2011-11-22 05:17:36 -06001575 unlink(git_path("REVERT_HEAD"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001576 unlink(git_path("MERGE_HEAD"));
1577 unlink(git_path("MERGE_MSG"));
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001578 unlink(git_path("MERGE_MODE"));
Gerrit Pape5a95b852008-02-08 09:53:58 +00001579 unlink(git_path("SQUASH_MSG"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001580
Brandon Casey5a9dd392008-01-23 11:21:22 -06001581 if (commit_index_files())
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001582 die (_("Repository has been updated, but unable to write\n"
Brandon Casey5a9dd392008-01-23 11:21:22 -06001583 "new_index file. Check that disk is not full or quota is\n"
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001584 "not exceeded, and then \"git reset HEAD\" to recover."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001585
Junio C Hamanocb6020b2009-12-04 00:20:48 -08001586 rerere(0);
Junio C Hamano28886052007-11-18 01:52:55 -08001587 run_hook(get_index_file(), "post-commit", NULL);
Thomas Rast6f6bee32010-03-12 18:04:28 +01001588 if (amend && !no_post_rewrite) {
Thomas Rast6360d342010-03-12 18:04:34 +01001589 struct notes_rewrite_cfg *cfg;
1590 cfg = init_copy_notes_for_rewrite("amend");
1591 if (cfg) {
Junio C Hamano06bb6432011-08-19 11:58:18 -07001592 /* we are amending, so current_head is not NULL */
1593 copy_note_for_rewrite(cfg, current_head->object.sha1, sha1);
Thomas Rast6360d342010-03-12 18:04:34 +01001594 finish_copy_notes_for_rewrite(cfg);
1595 }
Junio C Hamano06bb6432011-08-19 11:58:18 -07001596 run_rewrite_hook(current_head->object.sha1, sha1);
Thomas Rast6f6bee32010-03-12 18:04:28 +01001597 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001598 if (!quiet)
Junio C Hamano06bb6432011-08-19 11:58:18 -07001599 print_summary(prefix, sha1, !current_head);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001600
1601 return 0;
1602}