blob: 4d50484837ece50276ca7a09239815dcca5b5f7d [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[] = {
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +070034 N_("git commit [options] [--] <filepattern>..."),
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[] = {
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +070039 N_("git status [options] [--] <filepattern>..."),
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;
106static 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 {
115 STATUS_FORMAT_LONG,
116 STATUS_FORMAT_SHORT,
Gary V. Vaughan4b055482010-05-14 09:31:35 +0000117 STATUS_FORMAT_PORCELAIN
Jeff King7c9f7032009-09-05 04:59:56 -0400118} status_format = STATUS_FORMAT_LONG;
119
Johannes Schindelinf9568532007-11-11 17:36:39 +0000120static 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);
Johannes Schindelin3b6aeb32008-07-22 21:40:41 +0100127 strbuf_addstr(buf, "\n\n");
Johannes Schindelinf9568532007-11-11 17:36:39 +0000128 }
129 return 0;
130}
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500131
Jay Soffian37f7a852011-02-19 23:12:29 -0500132static 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
Junio C Hamano28886052007-11-18 01:52:55 -0800144static 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
Brandon Casey5a9dd392008-01-23 11:21:22 -0600159static int commit_index_files(void)
Junio C Hamano28886052007-11-18 01:52:55 -0800160{
Brandon Casey5a9dd392008-01-23 11:21:22 -0600161 int err = 0;
162
Junio C Hamano28886052007-11-18 01:52:55 -0800163 switch (commit_style) {
164 case COMMIT_AS_IS:
165 break; /* nothing to do */
166 case COMMIT_NORMAL:
Brandon Casey5a9dd392008-01-23 11:21:22 -0600167 err = commit_lock_file(&index_lock);
Junio C Hamano28886052007-11-18 01:52:55 -0800168 break;
169 case COMMIT_PARTIAL:
Brandon Casey5a9dd392008-01-23 11:21:22 -0600170 err = commit_lock_file(&index_lock);
Junio C Hamano28886052007-11-18 01:52:55 -0800171 rollback_lock_file(&false_lock);
172 break;
173 }
Brandon Casey5a9dd392008-01-23 11:21:22 -0600174
175 return err;
Junio C Hamano28886052007-11-18 01:52:55 -0800176}
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 */
Johannes Schindelinc455c872008-07-21 19:03:49 +0100182static int list_paths(struct string_list *list, const char *with_tree,
Junio C Hamano28886052007-11-18 01:52:55 -0800183 const char *prefix, const char **pattern)
184{
185 int i;
186 char *m;
187
Junio C Hamanob9a08012012-07-15 21:39:48 -0700188 if (!pattern)
189 return 0;
190
Junio C Hamano28886052007-11-18 01:52:55 -0800191 for (i = 0; pattern[i]; i++)
192 ;
193 m = xcalloc(1, i);
194
Clemens Buchacher8894d532011-07-30 19:13:47 +0200195 if (with_tree) {
Clemens Buchacherf950eb92011-09-04 12:42:01 +0200196 char *max_prefix = common_prefix(pattern);
Clemens Buchacher5879f562011-09-04 12:41:59 +0200197 overlay_tree_on_cache(with_tree, max_prefix ? max_prefix : prefix);
198 free(max_prefix);
Clemens Buchacher8894d532011-07-30 19:13:47 +0200199 }
Junio C Hamano28886052007-11-18 01:52:55 -0800200
201 for (i = 0; i < active_nr; i++) {
202 struct cache_entry *ce = active_cache[i];
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700203 struct string_list_item *item;
204
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800205 if (ce->ce_flags & CE_UPDATE)
Junio C Hamanoe87e22d2008-01-14 13:54:24 -0800206 continue;
Clemens Buchacher0b509222009-01-14 15:54:35 +0100207 if (!match_pathspec(pattern, ce->name, ce_namelen(ce), 0, m))
Junio C Hamano28886052007-11-18 01:52:55 -0800208 continue;
Julian Phillips78a395d2010-06-26 00:41:35 +0100209 item = string_list_insert(list, ce->name);
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700210 if (ce_skip_worktree(ce))
211 item->util = item; /* better a valid pointer than a fake one */
Junio C Hamano28886052007-11-18 01:52:55 -0800212 }
213
Clemens Buchacher0f64bfa2011-08-01 23:19:58 +0200214 return report_path_error(m, pattern, prefix);
Junio C Hamano28886052007-11-18 01:52:55 -0800215}
216
Johannes Schindelinc455c872008-07-21 19:03:49 +0100217static void add_remove_files(struct string_list *list)
Junio C Hamano28886052007-11-18 01:52:55 -0800218{
219 int i;
220 for (i = 0; i < list->nr; i++) {
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700221 struct stat st;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100222 struct string_list_item *p = &(list->items[i]);
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700223
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700224 /* p->util is skip-worktree */
225 if (p->util)
Nguyễn Thái Ngọc Duyb4d16902009-08-20 20:46:58 +0700226 continue;
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700227
Johannes Schindelinc455c872008-07-21 19:03:49 +0100228 if (!lstat(p->string, &st)) {
229 if (add_to_cache(p->string, &st, 0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000230 die(_("updating files failed"));
Alex Riesen960b8ad2008-05-12 19:57:45 +0200231 } else
Johannes Schindelinc455c872008-07-21 19:03:49 +0100232 remove_file_from_cache(p->string);
Junio C Hamano28886052007-11-18 01:52:55 -0800233 }
234}
235
Junio C Hamano06bb6432011-08-19 11:58:18 -0700236static void create_base_index(const struct commit *current_head)
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800237{
238 struct tree *tree;
239 struct unpack_trees_options opts;
240 struct tree_desc t;
241
Junio C Hamano06bb6432011-08-19 11:58:18 -0700242 if (!current_head) {
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800243 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;
Linus Torvalds34110cd2008-03-06 18:12:28 -0800251 opts.src_index = &the_index;
252 opts.dst_index = &the_index;
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800253
254 opts.fn = oneway_merge;
Junio C Hamano06bb6432011-08-19 11:58:18 -0700255 tree = parse_tree_indirect(current_head->object.sha1);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800256 if (!tree)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000257 die(_("failed to unpack HEAD tree object"));
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800258 parse_tree(tree);
259 init_tree_desc(&t, tree->buffer, tree->size);
Daniel Barkalow203a2fe2008-02-07 11:39:48 -0500260 if (unpack_trees(1, &t, &opts))
261 exit(128); /* We've already reported the error, finish dying */
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800262}
263
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100264static 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
Junio C Hamano06bb6432011-08-19 11:58:18 -0700274static char *prepare_index(int argc, const char **argv, const char *prefix,
275 const struct commit *current_head, int is_status)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500276{
277 int fd;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100278 struct string_list partial;
Junio C Hamano28886052007-11-18 01:52:55 -0800279 const char **pathspec = NULL;
Conrad Irwin1020d082011-05-06 22:59:59 -0700280 char *old_index_env = NULL;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700281 int refresh_flags = REFRESH_QUIET;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500282
Junio C Hamano50b7e702009-08-04 23:49:33 -0700283 if (is_status)
284 refresh_flags |= REFRESH_UNMERGED;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500285
Junio C Hamanof64fe7b2007-11-25 08:46:29 -0800286 if (*argv)
287 pathspec = get_pathspec(prefix, argv);
Junio C Hamano28886052007-11-18 01:52:55 -0800288
Linus Torvalds671c9b72008-11-13 16:36:30 -0800289 if (read_cache_preload(pathspec) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000290 die(_("index file corrupt"));
Linus Torvalds671c9b72008-11-13 16:36:30 -0800291
Conrad Irwin1020d082011-05-06 22:59:59 -0700292 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
Conrad Irwinb4bd4662011-05-07 10:58:07 -0700304 if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
Conrad Irwin1020d082011-05-06 22:59:59 -0700305 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
Junio C Hamano28886052007-11-18 01:52:55 -0800319 /*
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)) {
Markus Heidelberg1f2362a2010-04-02 14:27:19 +0200332 fd = hold_locked_index(&index_lock, 1);
Alex Riesen7ae02a32008-05-12 19:58:10 +0200333 add_files_to_cache(also ? prefix : NULL, pathspec, 0);
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100334 refresh_cache_or_die(refresh_flags);
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700335 update_main_cache_tree(WRITE_TREE_SILENT);
Brandon Casey4ed7cd32008-01-16 13:12:46 -0600336 if (write_cache(fd, active_cache, active_nr) ||
337 close_lock_file(&index_lock))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000338 die(_("unable to write new_index file"));
Junio C Hamano28886052007-11-18 01:52:55 -0800339 commit_style = COMMIT_NORMAL;
340 return index_lock.filename;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500341 }
342
Junio C Hamano28886052007-11-18 01:52:55 -0800343 /*
344 * As-is commit.
345 *
346 * (1) return the name of the real index file.
347 *
Markus Heidelberg73276232010-04-02 14:27:18 +0200348 * The caller should run hooks on the real index,
349 * and create commit from the_index.
Junio C Hamano28886052007-11-18 01:52:55 -0800350 * We still need to refresh the index here.
351 */
Junio C Hamanob9a08012012-07-15 21:39:48 -0700352 if (!only && (!pathspec || !*pathspec)) {
Junio C Hamano28886052007-11-18 01:52:55 -0800353 fd = hold_locked_index(&index_lock, 1);
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100354 refresh_cache_or_die(refresh_flags);
Junio C Hamanod5f5d0a2010-07-06 21:53:11 -0700355 if (active_cache_changed) {
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700356 update_main_cache_tree(WRITE_TREE_SILENT);
Junio C Hamanod5f5d0a2010-07-06 21:53:11 -0700357 if (write_cache(fd, active_cache, active_nr) ||
358 commit_locked_index(&index_lock))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000359 die(_("unable to write new_index file"));
Junio C Hamanod5f5d0a2010-07-06 21:53:11 -0700360 } else {
361 rollback_lock_file(&index_lock);
362 }
Junio C Hamano28886052007-11-18 01:52:55 -0800363 commit_style = COMMIT_AS_IS;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500364 return get_index_file();
365 }
366
Junio C Hamano28886052007-11-18 01:52:55 -0800367 /*
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
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000388 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 }
Junio C Hamano28886052007-11-18 01:52:55 -0800394
395 memset(&partial, 0, sizeof(partial));
Johannes Schindelinc455c872008-07-21 19:03:49 +0100396 partial.strdup_strings = 1;
Junio C Hamano06bb6432011-08-19 11:58:18 -0700397 if (list_paths(&partial, !current_head ? NULL : "HEAD", prefix, pathspec))
Junio C Hamano28886052007-11-18 01:52:55 -0800398 exit(1);
399
400 discard_cache();
401 if (read_cache() < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000402 die(_("cannot read the index"));
Junio C Hamano28886052007-11-18 01:52:55 -0800403
404 fd = hold_locked_index(&index_lock, 1);
405 add_remove_files(&partial);
Kristian Høgsbergef12b502007-11-12 15:48:22 -0500406 refresh_cache(REFRESH_QUIET);
Brandon Casey4ed7cd32008-01-16 13:12:46 -0600407 if (write_cache(fd, active_cache, active_nr) ||
408 close_lock_file(&index_lock))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000409 die(_("unable to write new_index file"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500410
Junio C Hamano28886052007-11-18 01:52:55 -0800411 fd = hold_lock_file_for_update(&false_lock,
Junio C Hamanoa1574002008-10-21 17:58:11 -0700412 git_path("next-index-%"PRIuMAX,
413 (uintmax_t) getpid()),
Junio C Hamanoacd3b9e2008-10-17 15:44:39 -0700414 LOCK_DIE_ON_ERROR);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800415
Junio C Hamano06bb6432011-08-19 11:58:18 -0700416 create_base_index(current_head);
Junio C Hamano28886052007-11-18 01:52:55 -0800417 add_remove_files(&partial);
Kristian Høgsbergd37d3202007-11-09 11:40:27 -0500418 refresh_cache(REFRESH_QUIET);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500419
Brandon Casey4ed7cd32008-01-16 13:12:46 -0600420 if (write_cache(fd, active_cache, active_nr) ||
421 close_lock_file(&false_lock))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000422 die(_("unable to write temporary index file"));
Jeff King959ba672008-02-14 12:18:23 -0500423
424 discard_cache();
425 read_cache_from(false_lock.filename);
426
Junio C Hamano28886052007-11-18 01:52:55 -0800427 return false_lock.filename;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500428}
429
Junio C Hamanod249b092009-08-09 21:59:30 -0700430static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
431 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500432{
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700433 unsigned char sha1[20];
434
Junio C Hamanod249b092009-08-09 21:59:30 -0700435 if (s->relative_paths)
436 s->prefix = prefix;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500437
438 if (amend) {
Junio C Hamanod249b092009-08-09 21:59:30 -0700439 s->amend = 1;
440 s->reference = "HEAD^1";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500441 }
Junio C Hamanod249b092009-08-09 21:59:30 -0700442 s->verbose = verbose;
443 s->index_file = index_file;
444 s->fp = fp;
445 s->nowarn = nowarn;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700446 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500447
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700448 wt_status_collect(s);
Jeff King7c9f7032009-09-05 04:59:56 -0400449
450 switch (status_format) {
451 case STATUS_FORMAT_SHORT:
Jeff Kingd4a6bf12012-05-07 17:09:04 -0400452 wt_shortstatus_print(s);
Jeff King7c9f7032009-09-05 04:59:56 -0400453 break;
454 case STATUS_FORMAT_PORCELAIN:
Jeff King3207a3a2012-05-07 15:44:44 -0400455 wt_porcelain_print(s);
Jeff King7c9f7032009-09-05 04:59:56 -0400456 break;
457 case STATUS_FORMAT_LONG:
458 wt_status_print(s);
459 break;
460 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500461
Junio C Hamanod249b092009-08-09 21:59:30 -0700462 return s->commitable;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500463}
464
Junio C Hamano06bb6432011-08-19 11:58:18 -0700465static int is_a_merge(const struct commit *current_head)
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100466{
Junio C Hamano06bb6432011-08-19 11:58:18 -0700467 return !!(current_head->parents && current_head->parents->next);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100468}
469
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700470static 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
Junio C Hamanoe27ddb62012-08-31 14:54:18 -0700480static 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
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800494static void determine_author_info(struct strbuf *author_ident)
Santi Béjara45d46b2008-05-04 18:04:49 +0200495{
496 char *name, *email, *date;
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700497 struct ident_split author;
Santi Béjara45d46b2008-05-04 18:04:49 +0200498
499 name = getenv("GIT_AUTHOR_NAME");
500 email = getenv("GIT_AUTHOR_EMAIL");
501 date = getenv("GIT_AUTHOR_DATE");
502
Jay Soffian37f7a852011-02-19 23:12:29 -0500503 if (author_message) {
Santi Béjara45d46b2008-05-04 18:04:49 +0200504 const char *a, *lb, *rb, *eol;
Junio C Hamano2c733fb2012-02-02 13:41:43 -0800505 size_t len;
Santi Béjara45d46b2008-05-04 18:04:49 +0200506
Jay Soffian37f7a852011-02-19 23:12:29 -0500507 a = strstr(author_message_buffer, "\nauthor ");
Santi Béjara45d46b2008-05-04 18:04:49 +0200508 if (!a)
Junio C Hamano6c80cd22011-04-01 17:55:55 -0700509 die(_("invalid commit: %s"), author_message);
Santi Béjara45d46b2008-05-04 18:04:49 +0200510
Jonathan Niederfb7749e2010-05-02 03:57:12 -0500511 lb = strchrnul(a + strlen("\nauthor "), '<');
512 rb = strchrnul(lb, '>');
513 eol = strchrnul(rb, '\n');
514 if (!*lb || !*rb || !*eol)
Junio C Hamano6c80cd22011-04-01 17:55:55 -0700515 die(_("invalid commit: %s"), author_message);
Santi Béjara45d46b2008-05-04 18:04:49 +0200516
Jonathan Niederfb7749e2010-05-02 03:57:12 -0500517 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("> ")));
Junio C Hamano2c733fb2012-02-02 13:41:43 -0800526 len = eol - (rb + strlen("> "));
527 date = xmalloc(len + 2);
528 *date = '@';
529 memcpy(date + 1, rb + strlen("> "), len);
530 date[len + 1] = '\0';
Santi Béjara45d46b2008-05-04 18:04:49 +0200531 }
532
533 if (force_author) {
534 const char *lb = strstr(force_author, " <");
535 const char *rb = strchr(force_author, '>');
536
537 if (!lb || !rb)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000538 die(_("malformed --author parameter"));
Santi Béjara45d46b2008-05-04 18:04:49 +0200539 name = xstrndup(force_author, lb - force_author);
540 email = xstrndup(lb + 2, rb - (lb + 2));
541 }
542
Miklos Vajna02b47cd2009-12-02 23:16:18 +0100543 if (force_date)
544 date = force_date;
Jeff Kingf9bc5732012-05-24 19:28:40 -0400545 strbuf_addstr(author_ident, fmt_ident(name, email, date, IDENT_STRICT));
Junio C Hamanoe27ddb62012-08-31 14:54:18 -0700546 if (!split_ident_line(&author, author_ident->buf, author_ident->len) &&
547 sane_ident_split(&author)) {
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700548 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 }
Santi Béjara45d46b2008-05-04 18:04:49 +0200552}
553
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800554static char *cut_ident_timestamp_part(char *string)
555{
556 char *ket = strrchr(string, '>');
557 if (!ket || ket[1] != ' ')
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000558 die(_("Malformed ident string: '%s'"), string);
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800559 *++ket = '\0';
560 return ket;
561}
562
Junio C Hamanod249b092009-08-09 21:59:30 -0700563static int prepare_to_commit(const char *index_file, const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -0700564 struct commit *current_head,
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800565 struct wt_status *s,
566 struct strbuf *author_ident)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500567{
568 struct stat statbuf;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800569 struct strbuf committer_ident = STRBUF_INIT;
Junio C Hamanobc5d2482007-11-18 12:01:38 -0800570 int commitable, saved_color_setting;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500571 struct strbuf sb = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500572 char *buffer;
Paolo Bonzini8089c852008-02-05 08:04:18 +0100573 const char *hook_arg1 = NULL;
574 const char *hook_arg2 = NULL;
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200575 int ident_shown = 0;
Boris Faure8b1ae672011-05-08 12:31:02 +0200576 int clean_message_contents = (cleanup_mode != CLEANUP_NONE);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500577
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700578 /* This checks and barfs if author is badly specified */
579 determine_author_info(author_ident);
580
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100581 if (!no_verify && run_hook(index_file, "pre-commit", NULL))
582 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500583
Pat Notz89ac1222010-11-02 13:59:11 -0600584 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)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000596 die(_("could not lookup commit %s"), squash_message);
Pat Notz89ac1222010-11-02 13:59:11 -0600597 ctx.output_encoding = get_commit_output_encoding();
598 format_commit_message(c, "squash! %s\n\n", &sb,
599 &ctx);
600 }
601 }
602
Johannes Schindelinf9568532007-11-11 17:36:39 +0000603 if (message.len) {
604 strbuf_addbuf(&sb, &message);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100605 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500606 } else if (logfile && !strcmp(logfile, "-")) {
607 if (isatty(0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000608 fprintf(stderr, _("(reading log message from standard input)\n"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500609 if (strbuf_read(&sb, 0, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000610 die_errno(_("could not read log from standard input"));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100611 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500612 } else if (logfile) {
613 if (strbuf_read_file(&sb, logfile, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000614 die_errno(_("could not read log file '%s'"),
Thomas Rastd824cbb2009-06-27 17:58:46 +0200615 logfile);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100616 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500617 } else if (use_message) {
618 buffer = strstr(use_message_buffer, "\n\n");
Chris Webbd9a93572012-07-09 19:53:26 +0100619 if (!use_editor && (!buffer || buffer[2] == '\0'))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000620 die(_("commit has empty message"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500621 strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100622 hook_arg1 = "commit";
623 hook_arg2 = use_message;
Pat Notzd71b8ba2010-11-02 13:59:09 -0600624 } 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)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000629 die(_("could not lookup commit %s"), fixup_message);
Pat Notzd71b8ba2010-11-02 13:59:09 -0600630 ctx.output_encoding = get_commit_output_encoding();
631 format_commit_message(commit, "fixup! %s\n\n",
632 &sb, &ctx);
633 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500634 } else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
635 if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000636 die_errno(_("could not read MERGE_MSG"));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100637 hook_arg1 = "merge";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500638 } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
639 if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000640 die_errno(_("could not read SQUASH_MSG"));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100641 hook_arg1 = "squash";
Jonathan Nieder2140b142011-02-25 03:07:57 -0600642 } else if (template_file) {
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500643 if (strbuf_read_file(&sb, template_file, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000644 die_errno(_("could not read '%s'"), template_file);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100645 hook_arg1 = "template";
Boris Faure8b1ae672011-05-08 12:31:02 +0200646 clean_message_contents = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500647 }
648
Paolo Bonzini8089c852008-02-05 08:04:18 +0100649 /*
Jay Soffian37f7a852011-02-19 23:12:29 -0500650 * The remaining cases don't modify the template message, but
651 * just set the argument(s) to the prepare-commit-msg hook.
Paolo Bonzini8089c852008-02-05 08:04:18 +0100652 */
Jay Soffian37f7a852011-02-19 23:12:29 -0500653 else if (whence == FROM_MERGE)
Paolo Bonzini8089c852008-02-05 08:04:18 +0100654 hook_arg1 = "merge";
Jay Soffian37f7a852011-02-19 23:12:29 -0500655 else if (whence == FROM_CHERRY_PICK) {
656 hook_arg1 = "commit";
657 hook_arg2 = "CHERRY_PICK_HEAD";
658 }
Paolo Bonzini8089c852008-02-05 08:04:18 +0100659
Pat Notz89ac1222010-11-02 13:59:11 -0600660 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
Jonathan Nieder37f30122011-02-25 23:10:49 -0600670 s->fp = fopen(git_path(commit_editmsg), "w");
671 if (s->fp == NULL)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000672 die_errno(_("could not open '%s'"), git_path(commit_editmsg));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500673
Boris Faure8b1ae672011-05-08 12:31:02 +0200674 if (clean_message_contents)
Alex Riesen5f065732007-12-22 19:46:24 +0100675 stripspace(&sb, 0);
Johannes Schindelin13208572007-11-11 17:35:58 +0000676
677 if (signoff) {
Miklos Vajna5ed75e22012-09-14 08:52:03 +0200678 /*
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;
Johannes Schindelin13208572007-11-11 17:35:58 +0000685
Miklos Vajna5ed75e22012-09-14 08:52:03 +0200686 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;
Johannes Schindelin21505542007-11-11 17:36:27 +0000699 }
Miklos Vajna5ed75e22012-09-14 08:52:03 +0200700
701 append_signoff(&sb, ignore_footer);
Johannes Schindelin13208572007-11-11 17:35:58 +0000702 }
703
Jonathan Nieder37f30122011-02-25 23:10:49 -0600704 if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000705 die_errno(_("could not write commit template"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500706
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500707 strbuf_release(&sb);
708
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200709 /* This checks if committer ident is explicitly given */
Jeff Kingf20f3872012-07-23 14:50:35 -0400710 strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
James P. Howard, IIbed575e2009-12-07 17:45:27 -0500711 if (use_editor && include_status) {
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800712 char *ai_tmp, *ci_tmp;
Jay Soffian37f7a852011-02-19 23:12:29 -0500713 if (whence != FROM_COMMIT)
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600714 status_printf_ln(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000715 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"),
Jay Soffian37f7a852011-02-19 23:12:29 -0500726 git_path(whence == FROM_MERGE
727 ? "MERGE_HEAD"
728 : "CHERRY_PICK_HEAD"));
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100729
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600730 fprintf(s->fp, "\n");
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100731 if (cleanup_mode == CLEANUP_ALL)
Ævar Arnfjörð Bjarmason4064e662012-04-30 15:33:14 +0000732 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"
Ævar Arnfjörð Bjarmason0b430a12011-02-22 23:41:48 +0000735 " message aborts the commit.\n"));
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100736 else /* CLEANUP_SPACE, that is. */
Ævar Arnfjörð Bjarmason4064e662012-04-30 15:33:14 +0000737 status_printf(s, GIT_COLOR_NORMAL,
738 _("Please enter the commit message for your changes."
739 " Lines starting\n"
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600740 "with '#' will be kept; you may remove them"
Jeff Kingfdc7c812008-07-31 03:36:09 -0400741 " yourself if you want to.\n"
Ævar Arnfjörð Bjarmason0b430a12011-02-22 23:41:48 +0000742 "An empty message aborts the commit.\n"));
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100743 if (only_include_assumed)
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600744 status_printf_ln(s, GIT_COLOR_NORMAL,
745 "%s", only_include_assumed);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100746
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800747 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))
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600750 status_printf_ln(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmasonfe8165c2011-02-22 23:41:46 +0000751 _("%s"
752 "Author: %s"),
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600753 ident_shown++ ? "" : "\n",
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800754 author_ident->buf);
Santi Béjare83dbe82008-05-04 18:04:50 +0200755
Junio C Hamano5aeb3a32010-01-17 13:54:28 -0800756 if (!user_ident_sufficiently_given())
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600757 status_printf_ln(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmasonfe8165c2011-02-22 23:41:46 +0000758 _("%s"
759 "Committer: %s"),
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600760 ident_shown++ ? "" : "\n",
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800761 committer_ident.buf);
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200762
763 if (ident_shown)
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600764 status_printf_ln(s, GIT_COLOR_NORMAL, "");
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200765
Junio C Hamanod249b092009-08-09 21:59:30 -0700766 saved_color_setting = s->use_color;
767 s->use_color = 0;
Jonathan Nieder37f30122011-02-25 23:10:49 -0600768 commitable = run_status(s->fp, index_file, prefix, 1, s);
Junio C Hamanod249b092009-08-09 21:59:30 -0700769 s->use_color = saved_color_setting;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800770
771 *ai_tmp = ' ';
772 *ci_tmp = ' ';
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100773 } else {
Junio C Hamanod616a232007-12-22 19:22:29 -0800774 unsigned char sha1[20];
Junio C Hamanofbcf1182007-12-19 19:23:03 -0800775 const char *parent = "HEAD";
Alex Riesen71686242007-11-28 22:13:08 +0100776
Alex Riesen71686242007-11-28 22:13:08 +0100777 if (!active_nr && read_cache() < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000778 die(_("Cannot read index"));
Alex Riesen71686242007-11-28 22:13:08 +0100779
Junio C Hamanofbcf1182007-12-19 19:23:03 -0800780 if (amend)
781 parent = "HEAD^1";
782
Junio C Hamanod616a232007-12-22 19:22:29 -0800783 if (get_sha1(parent, sha1))
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100784 commitable = !!active_nr;
Stephan Beyer75f3ff22009-02-10 15:30:35 +0100785 else
786 commitable = index_differs_from(parent, 0);
Alex Riesen71686242007-11-28 22:13:08 +0100787 }
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800788 strbuf_release(&committer_ident);
Alex Riesen71686242007-11-28 22:13:08 +0100789
Jonathan Nieder37f30122011-02-25 23:10:49 -0600790 fclose(s->fp);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500791
Jay Soffian37f7a852011-02-19 23:12:29 -0500792 /*
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 &&
Junio C Hamano06bb6432011-08-19 11:58:18 -0700798 !(amend && is_a_merge(current_head))) {
Junio C Hamanod249b092009-08-09 21:59:30 -0700799 run_status(stdout, index_file, prefix, 0, s);
Jeff Kingf197ed22010-06-06 20:41:46 -0400800 if (amend)
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +0000801 fputs(_(empty_amend_advice), stderr);
Jay Soffian37f7a852011-02-19 23:12:29 -0500802 else if (whence == FROM_CHERRY_PICK)
Junio C Hamano6c80cd22011-04-01 17:55:55 -0700803 fputs(_(empty_cherry_pick_advice), stderr);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100804 return 0;
805 }
806
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);
Thomas Rast996277c2011-12-06 18:43:37 +0100814 if (update_main_cache_tree(0)) {
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000815 error(_("Error building trees"));
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100816 return 0;
817 }
818
Paolo Bonzini8089c852008-02-05 08:04:18 +0100819 if (run_hook(index_file, "prepare-commit-msg",
820 git_path(commit_editmsg), hook_arg1, hook_arg2, NULL))
821 return 0;
822
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100823 if (use_editor) {
824 char index[PATH_MAX];
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000825 const char *env[2] = { NULL };
826 env[0] = index;
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100827 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
Stephan Beyer71982032008-07-25 18:28:42 +0200828 if (launch_editor(git_path(commit_editmsg), NULL, env)) {
829 fprintf(stderr,
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000830 _("Please supply the message using either -m or -F option.\n"));
Stephan Beyer71982032008-07-25 18:28:42 +0200831 exit(1);
832 }
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100833 }
834
835 if (!no_verify &&
836 run_hook(index_file, "commit-msg", git_path(commit_editmsg), NULL)) {
837 return 0;
838 }
839
840 return 1;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500841}
842
Junio C Hamanob2eda9b2012-03-30 12:14:33 -0700843static int rest_is_empty(struct strbuf *sb, int start)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500844{
Junio C Hamanob2eda9b2012-03-30 12:14:33 -0700845 int i, eol;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500846 const char *nl;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500847
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
Junio C Hamanob2eda9b2012-03-30 12:14:33 -0700869/*
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
Junio C Hamano146ea062008-08-26 23:13:13 -0700903static 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) {
Thomas Rastdd2e7942009-10-19 17:48:08 +0200921 struct pretty_print_context ctx = {0};
922 ctx.date_mode = DATE_NORMAL;
Junio C Hamano146ea062008-08-26 23:13:13 -0700923 strbuf_release(&buf);
Thomas Rastdd2e7942009-10-19 17:48:08 +0200924 format_commit_message(commit, "%an <%ae>", &buf, &ctx);
Junio C Hamano146ea062008-08-26 23:13:13 -0700925 return strbuf_detach(&buf, NULL);
926 }
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000927 die(_("No existing author found with '%s'"), name);
Junio C Hamano146ea062008-08-26 23:13:13 -0700928}
929
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700930
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
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000942 die(_("Invalid untracked files mode '%s'"), untracked_files_arg);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700943}
944
Jay Soffian37f7a852011-02-19 23:12:29 -0500945static 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)
Junio C Hamano6c80cd22011-04-01 17:55:55 -0700952 die(_("could not lookup commit %s"), name);
Jay Soffian37f7a852011-02-19 23:12:29 -0500953 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
Shawn Bohrer2f02b252007-12-02 23:02:09 -0600967static int parse_and_validate_options(int argc, const char *argv[],
Jeff King036dbbf2012-05-07 15:18:26 -0400968 const struct option *options,
Junio C Hamanodbd0f5c2008-08-06 11:43:47 -0700969 const char * const usage[],
Junio C Hamanod249b092009-08-09 21:59:30 -0700970 const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -0700971 struct commit *current_head,
Junio C Hamanod249b092009-08-09 21:59:30 -0700972 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500973{
974 int f = 0;
975
Jeff King036dbbf2012-05-07 15:18:26 -0400976 argc = parse_options(argc, argv, prefix, options, usage, 0);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500977
Junio C Hamano146ea062008-08-26 23:13:13 -0700978 if (force_author && !strchr(force_author, '>'))
979 force_author = find_author_by_nickname(force_author);
980
Erick Mattosc51f6ce2009-11-04 01:20:11 -0200981 if (force_author && renew_authorship)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000982 die(_("Using both --reset-author and --author does not make sense"));
Erick Mattosc51f6ce2009-11-04 01:20:11 -0200983
Pat Notzd71b8ba2010-11-02 13:59:09 -0600984 if (logfile || message.len || use_message || fixup_message)
Junio C Hamano48034662007-12-22 19:25:37 -0800985 use_editor = 0;
Junio C Hamanoca1ba202011-12-06 13:09:55 -0800986 if (0 <= edit_flag)
987 use_editor = edit_flag;
Paolo Bonzini406400c2008-02-05 11:01:45 +0100988 if (!use_editor)
989 setenv("GIT_EDITOR", ":", 1);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500990
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500991 /* Sanity check options */
Junio C Hamano06bb6432011-08-19 11:58:18 -0700992 if (amend && !current_head)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000993 die(_("You have nothing to amend."));
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000994 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 }
Pat Notz89ac1222010-11-02 13:59:11 -06001000 if (fixup_message && squash_message)
Ævar Arnfjörð Bjarmason9c227652011-02-22 23:41:45 +00001001 die(_("Options --squash and --fixup cannot be used together"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001002 if (use_message)
1003 f++;
1004 if (edit_message)
1005 f++;
Pat Notzd71b8ba2010-11-02 13:59:09 -06001006 if (fixup_message)
1007 f++;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001008 if (logfile)
1009 f++;
1010 if (f > 1)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001011 die(_("Only one of -c/-C/-F/--fixup can be used."));
Johannes Schindelinf9568532007-11-11 17:36:39 +00001012 if (message.len && f > 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001013 die((_("Option -m cannot be combined with -c/-C/-F/--fixup.")));
Junio C Hamano010c7db2012-03-30 11:30:59 -07001014 if (f || message.len)
1015 template_file = NULL;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001016 if (edit_message)
1017 use_message = edit_message;
Pat Notzd71b8ba2010-11-02 13:59:09 -06001018 if (amend && !use_message && !fixup_message)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001019 use_message = "HEAD";
Jay Soffian37f7a852011-02-19 23:12:29 -05001020 if (!use_message && whence != FROM_CHERRY_PICK && renew_authorship)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001021 die(_("--reset-author can be used only with -C, -c or --amend."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001022 if (use_message) {
Jay Soffian37f7a852011-02-19 23:12:29 -05001023 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);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001032 }
1033
Conrad Irwinb4bd4662011-05-07 10:58:07 -07001034 if (patch_interactive)
1035 interactive = 1;
1036
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001037 if (!!also + !!only + !!all + !!interactive > 1)
Conrad Irwinb4bd4662011-05-07 10:58:07 -07001038 die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001039 if (argc == 0 && (also || (only && !amend)))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001040 die(_("No paths with --include/--only does not make sense."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001041 if (argc == 0 && only && amend)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001042 only_include_assumed = _("Clever... amending the last one with dirty index.");
Johannes Sixt3c5283f2008-04-10 13:33:08 +02001043 if (argc > 0 && !also && !only)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001044 only_include_assumed = _("Explicit paths specified without -i nor -o; assuming --only paths...");
Alex Riesen5f065732007-12-22 19:46:24 +01001045 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
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001054 die(_("Invalid cleanup mode %s"), cleanup_arg);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001055
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001056 handle_untracked_files_arg(s);
Marius Storm-Olsen4bfee302008-06-05 10:31:19 +02001057
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001058 if (all && argc > 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001059 die(_("Paths with -a does not make sense."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001060
Jeff King3207a3a2012-05-07 15:44:44 -04001061 if (s->null_termination && status_format == STATUS_FORMAT_LONG)
Jeff King7c9f7032009-09-05 04:59:56 -04001062 status_format = STATUS_FORMAT_PORCELAIN;
1063 if (status_format != STATUS_FORMAT_LONG)
1064 dry_run = 1;
1065
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001066 return argc;
1067}
1068
Junio C Hamanod249b092009-08-09 21:59:30 -07001069static int dry_run_commit(int argc, const char **argv, const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001070 const struct commit *current_head, struct wt_status *s)
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001071{
1072 int commitable;
1073 const char *index_file;
1074
Junio C Hamano06bb6432011-08-19 11:58:18 -07001075 index_file = prepare_index(argc, argv, prefix, current_head, 1);
Junio C Hamanod249b092009-08-09 21:59:30 -07001076 commitable = run_status(stdout, index_file, prefix, 0, s);
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001077 rollback_index_files();
1078
1079 return commitable ? 0 : 1;
1080}
1081
Junio C Hamanof766b362009-08-09 23:12:19 -07001082static int parse_status_slot(const char *var, int offset)
1083{
1084 if (!strcasecmp(var+offset, "header"))
1085 return WT_STATUS_HEADER;
Aleksi Aalto1d282322010-11-18 01:40:05 +02001086 if (!strcasecmp(var+offset, "branch"))
1087 return WT_STATUS_ONBRANCH;
Junio C Hamanof766b362009-08-09 23:12:19 -07001088 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;
Jeff King8b8e8622009-12-12 07:25:24 -05001099 return -1;
Junio C Hamanof766b362009-08-09 23:12:19 -07001100}
1101
1102static int git_status_config(const char *k, const char *v, void *cb)
1103{
1104 struct wt_status *s = cb;
1105
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +07001106 if (!prefixcmp(k, "column."))
Jeff King4d2292e2012-05-07 15:35:03 -04001107 return git_column_config(k, v, "status", &s->colopts);
Junio C Hamanof766b362009-08-09 23:12:19 -07001108 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")) {
Jeff Kinge269eb72011-08-17 22:03:48 -07001116 s->use_color = git_config_colorbool(k, v);
Junio C Hamanof766b362009-08-09 23:12:19 -07001117 return 0;
1118 }
1119 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
1120 int slot = parse_status_slot(k, 13);
Jeff King8b8e8622009-12-12 07:25:24 -05001121 if (slot < 0)
1122 return 0;
Junio C Hamanof766b362009-08-09 23:12:19 -07001123 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
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001142 return error(_("Invalid untracked files mode '%s'"), v);
Junio C Hamanof766b362009-08-09 23:12:19 -07001143 return 0;
1144 }
1145 return git_diff_ui_config(k, v, NULL);
1146}
1147
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001148int cmd_status(int argc, const char **argv, const char *prefix)
1149{
Jeff King036dbbf2012-05-07 15:18:26 -04001150 static struct wt_status s;
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001151 int fd;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001152 unsigned char sha1[20];
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001153 static struct option builtin_status_options[] = {
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001154 OPT__VERBOSE(&verbose, N_("be verbose")),
Jeff Kingdd2be242009-09-05 04:54:14 -04001155 OPT_SET_INT('s', "short", &status_format,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001156 N_("show status concisely"), STATUS_FORMAT_SHORT),
Jeff Kingd4a6bf12012-05-07 17:09:04 -04001157 OPT_BOOLEAN('b', "branch", &s.show_branch,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001158 N_("show branch information")),
Jeff King6f157872009-09-05 04:55:37 -04001159 OPT_SET_INT(0, "porcelain", &status_format,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001160 N_("machine-readable output"),
Jeff King6f157872009-09-05 04:55:37 -04001161 STATUS_FORMAT_PORCELAIN),
Jeff King3207a3a2012-05-07 15:44:44 -04001162 OPT_BOOLEAN('z', "null", &s.null_termination,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001163 N_("terminate entries with NUL")),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001164 { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001165 N_("mode"),
1166 N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001167 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
Junio C Hamano2381e392010-04-10 00:33:17 -07001168 OPT_BOOLEAN(0, "ignored", &show_ignored_in_status,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001169 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)"),
Jens Lehmann46a958b2010-06-25 16:56:47 +02001172 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001173 OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001174 OPT_END(),
1175 };
1176
Nguyễn Thái Ngọc Duy5d3dd912010-10-22 01:45:47 -05001177 if (argc == 2 && !strcmp(argv[1], "-h"))
1178 usage_with_options(builtin_status_usage, builtin_status_options);
1179
Junio C Hamanod249b092009-08-09 21:59:30 -07001180 wt_status_prepare(&s);
Jens Lehmann302ad7a2010-08-06 00:40:48 +02001181 gitmodules_config();
Junio C Hamanod249b092009-08-09 21:59:30 -07001182 git_config(git_status_config, &s);
Jay Soffian37f7a852011-02-19 23:12:29 -05001183 determine_whence(&s);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001184 argc = parse_options(argc, argv, prefix,
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001185 builtin_status_options,
1186 builtin_status_usage, 0);
Jeff King4d2292e2012-05-07 15:35:03 -04001187 finalize_colopts(&s.colopts, -1);
Junio C Hamano6c929722011-06-06 11:40:08 -07001188
Jeff King3207a3a2012-05-07 15:44:44 -04001189 if (s.null_termination && status_format == STATUS_FORMAT_LONG)
Junio C Hamano6c929722011-06-06 11:40:08 -07001190 status_format = STATUS_FORMAT_PORCELAIN;
1191
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001192 handle_untracked_files_arg(&s);
Junio C Hamano2381e392010-04-10 00:33:17 -07001193 if (show_ignored_in_status)
1194 s.show_ignored_files = 1;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001195 if (*argv)
1196 s.pathspec = get_pathspec(prefix, argv);
1197
Junio C Hamano149794d2010-02-17 12:30:41 -08001198 read_cache_preload(s.pathspec);
Nguyễn Thái Ngọc Duy688cd6d2010-01-14 22:02:21 +07001199 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec, NULL, NULL);
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001200
1201 fd = hold_locked_index(&index_lock, 0);
Junio C Hamanoccdc4ec2011-03-21 10:16:10 -07001202 if (0 <= fd)
1203 update_index_if_able(&the_index, &index_lock);
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001204
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001205 s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
Jens Lehmann46a958b2010-06-25 16:56:47 +02001206 s.ignore_submodule_arg = ignore_submodule_arg;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001207 wt_status_collect(&s);
1208
Jeff King86617682009-12-07 00:26:25 -05001209 if (s.relative_paths)
1210 s.prefix = prefix;
Markus Heidelberg38920dd2009-01-08 19:53:05 +01001211
Jeff Kingdd2be242009-09-05 04:54:14 -04001212 switch (status_format) {
1213 case STATUS_FORMAT_SHORT:
Jeff Kingd4a6bf12012-05-07 17:09:04 -04001214 wt_shortstatus_print(&s);
Jeff Kingdd2be242009-09-05 04:54:14 -04001215 break;
Jeff King6f157872009-09-05 04:55:37 -04001216 case STATUS_FORMAT_PORCELAIN:
Jeff King3207a3a2012-05-07 15:44:44 -04001217 wt_porcelain_print(&s);
Jeff King6f157872009-09-05 04:55:37 -04001218 break;
Jeff Kingdd2be242009-09-05 04:54:14 -04001219 case STATUS_FORMAT_LONG:
Junio C Hamano173e6c82009-08-04 23:55:22 -07001220 s.verbose = verbose;
Jens Lehmann46a958b2010-06-25 16:56:47 +02001221 s.ignore_submodule_arg = ignore_submodule_arg;
Junio C Hamano173e6c82009-08-04 23:55:22 -07001222 wt_status_print(&s);
Jeff Kingdd2be242009-09-05 04:54:14 -04001223 break;
Junio C Hamano173e6c82009-08-04 23:55:22 -07001224 }
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001225 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001226}
1227
Junio C Hamano06bb6432011-08-19 11:58:18 -07001228static void print_summary(const char *prefix, const unsigned char *sha1,
1229 int initial_commit)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001230{
1231 struct rev_info rev;
1232 struct commit *commit;
Jeff King49ff9a72010-01-13 12:39:51 -05001233 struct strbuf format = STRBUF_INIT;
Jeff Kingc85db252008-10-01 18:31:25 -04001234 unsigned char junk_sha1[20];
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +07001235 const char *head;
Jeff King49ff9a72010-01-13 12:39:51 -05001236 struct pretty_print_context pctx = {0};
1237 struct strbuf author_ident = STRBUF_INIT;
1238 struct strbuf committer_ident = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001239
1240 commit = lookup_commit(sha1);
1241 if (!commit)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001242 die(_("couldn't look up newly created commit"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001243 if (!commit || parse_commit(commit))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001244 die(_("could not parse newly created commit"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001245
Jeff King49ff9a72010-01-13 12:39:51 -05001246 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 }
Junio C Hamano1a893062010-01-17 13:59:36 -08001254 if (!user_ident_sufficiently_given()) {
Jeff King49ff9a72010-01-13 12:39:51 -05001255 strbuf_addstr(&format, "\n Committer: ");
1256 strbuf_addbuf_percentquote(&format, &committer_ident);
Jeff Kingb706fcf2010-01-13 15:17:08 -05001257 if (advice_implicit_identity) {
1258 strbuf_addch(&format, '\n');
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +00001259 strbuf_addstr(&format, _(implicit_ident_advice));
Jeff Kingb706fcf2010-01-13 15:17:08 -05001260 }
Jeff King49ff9a72010-01-13 12:39:51 -05001261 }
1262 strbuf_release(&author_ident);
1263 strbuf_release(&committer_ident);
1264
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001265 init_revisions(&rev, prefix);
1266 setup_revisions(0, NULL, &rev, NULL);
1267
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001268 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;
Jeff King49ff9a72010-01-13 12:39:51 -05001274 get_commit_format(format.buf, &rev);
Junio C Hamanobf82a152007-12-10 21:02:26 -08001275 rev.always_show_header = 0;
Junio C Hamano3eb2a152007-12-16 15:05:39 -08001276 rev.diffopt.detect_rename = 1;
Junio C Hamano3eb2a152007-12-16 15:05:39 -08001277 rev.diffopt.break_opt = 0;
Junio C Hamano15964562007-12-16 15:03:58 -08001278 diff_setup_done(&rev.diffopt);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001279
Nguyễn Thái Ngọc Duy8cad4742011-12-12 18:20:32 +07001280 head = resolve_ref_unsafe("HEAD", junk_sha1, 0, NULL);
Santi Béjarc5ee71f2009-01-19 23:45:16 +01001281 printf("[%s%s ",
Jeff Kingc85db252008-10-01 18:31:25 -04001282 !prefixcmp(head, "refs/heads/") ?
1283 head + 11 :
1284 !strcmp(head, "HEAD") ?
Ævar Arnfjörð Bjarmason7f5673d2011-02-22 23:41:47 +00001285 _("detached HEAD") :
Jeff Kingc85db252008-10-01 18:31:25 -04001286 head,
Ævar Arnfjörð Bjarmason7f5673d2011-02-22 23:41:47 +00001287 initial_commit ? _(" (root-commit)") : "");
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001288
Junio C Hamanobf82a152007-12-10 21:02:26 -08001289 if (!log_tree_commit(&rev, commit)) {
Tay Ray Chuana45e1a82010-06-12 22:15:39 +08001290 rev.always_show_header = 1;
1291 rev.use_terminator = 1;
1292 log_tree_commit(&rev, commit);
Junio C Hamanobf82a152007-12-10 21:02:26 -08001293 }
Tay Ray Chuana45e1a82010-06-12 22:15:39 +08001294
Junio C Hamanofc6f19f2010-01-17 00:57:51 -08001295 strbuf_release(&format);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001296}
1297
Stephan Beyer186458b2008-07-24 01:09:35 +02001298static int git_commit_config(const char *k, const char *v, void *cb)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001299{
Junio C Hamanod249b092009-08-09 21:59:30 -07001300 struct wt_status *s = cb;
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001301 int status;
Junio C Hamanod249b092009-08-09 21:59:30 -07001302
Brian Hetro984c6e72008-07-05 01:24:40 -04001303 if (!strcmp(k, "commit.template"))
Matthieu Moy395de252009-11-17 18:24:25 +01001304 return git_config_pathname(&template_file, k, v);
James P. Howard, IIbed575e2009-12-07 17:45:27 -05001305 if (!strcmp(k, "commit.status")) {
1306 include_status = git_config_bool(k, v);
1307 return 0;
1308 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001309
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001310 status = git_gpg_config(k, v, NULL);
1311 if (status)
1312 return status;
Junio C Hamanod249b092009-08-09 21:59:30 -07001313 return git_status_config(k, v, s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001314}
1315
Thomas Rast6f6bee32010-03-12 18:04:28 +01001316static 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
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001350int cmd_commit(int argc, const char **argv, const char *prefix)
1351{
Jeff King036dbbf2012-05-07 15:18:26 -04001352 static struct wt_status s;
1353 static struct option builtin_commit_options[] = {
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001354 OPT__QUIET(&quiet, N_("suppress summary after successful commit")),
1355 OPT__VERBOSE(&verbose, N_("show diff in commit message template")),
Jeff King036dbbf2012-05-07 15:18:26 -04001356
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001357 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) "" },
Jeff King036dbbf2012-05-07 15:18:26 -04001374 /* end commit message options */
1375
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001376 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"),
Jeff King036dbbf2012-05-07 15:18:26 -04001385 STATUS_FORMAT_SHORT),
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001386 OPT_BOOLEAN(0, "branch", &s.show_branch, N_("show branch information")),
Jeff King036dbbf2012-05-07 15:18:26 -04001387 OPT_SET_INT(0, "porcelain", &status_format,
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001388 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN),
Jeff King3207a3a2012-05-07 15:44:44 -04001389 OPT_BOOLEAN('z', "null", &s.null_termination,
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001390 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" },
Jeff King036dbbf2012-05-07 15:18:26 -04001394 /* end commit contents options */
1395
1396 { OPTION_BOOLEAN, 0, "allow-empty", &allow_empty, NULL,
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001397 N_("ok to record an empty change"),
Jeff King036dbbf2012-05-07 15:18:26 -04001398 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
1399 { OPTION_BOOLEAN, 0, "allow-empty-message", &allow_empty_message, NULL,
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001400 N_("ok to record a change with an empty message"),
Jeff King036dbbf2012-05-07 15:18:26 -04001401 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
1402
1403 OPT_END()
1404 };
1405
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001406 struct strbuf sb = STRBUF_INIT;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -08001407 struct strbuf author_ident = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001408 const char *index_file, *reflog_msg;
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001409 char *nl, *p;
Junio C Hamano06bb6432011-08-19 11:58:18 -07001410 unsigned char sha1[20];
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001411 struct ref_lock *ref_lock;
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001412 struct commit_list *parents = NULL, **pptr = &parents;
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001413 struct stat statbuf;
1414 int allow_fast_forward = 1;
Junio C Hamano06bb6432011-08-19 11:58:18 -07001415 struct commit *current_head = NULL;
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001416 struct commit_extra_header *extra = NULL;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001417
Nguyễn Thái Ngọc Duy5d3dd912010-10-22 01:45:47 -05001418 if (argc == 2 && !strcmp(argv[1], "-h"))
1419 usage_with_options(builtin_commit_usage, builtin_commit_options);
1420
Junio C Hamanod249b092009-08-09 21:59:30 -07001421 wt_status_prepare(&s);
1422 git_config(git_commit_config, &s);
Jay Soffian37f7a852011-02-19 23:12:29 -05001423 determine_whence(&s);
Jeff King4d2292e2012-05-07 15:35:03 -04001424 s.colopts = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001425
Junio C Hamano06bb6432011-08-19 11:58:18 -07001426 if (get_sha1("HEAD", sha1))
1427 current_head = NULL;
1428 else {
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +10001429 current_head = lookup_commit_or_die(sha1, "HEAD");
Junio C Hamano06bb6432011-08-19 11:58:18 -07001430 if (!current_head || parse_commit(current_head))
1431 die(_("could not parse HEAD commit"));
1432 }
Jeff King036dbbf2012-05-07 15:18:26 -04001433 argc = parse_and_validate_options(argc, argv, builtin_commit_options,
1434 builtin_commit_usage,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001435 prefix, current_head, &s);
Jeff Kingc9bfb952011-08-17 22:05:35 -07001436 if (dry_run)
Junio C Hamano06bb6432011-08-19 11:58:18 -07001437 return dry_run_commit(argc, argv, prefix, current_head, &s);
Junio C Hamano06bb6432011-08-19 11:58:18 -07001438 index_file = prepare_index(argc, argv, prefix, current_head, 0);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001439
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001440 /* Set up everything for writing the commit object. This includes
1441 running hooks, writing the trees, and interacting with the user. */
Junio C Hamano06bb6432011-08-19 11:58:18 -07001442 if (!prepare_to_commit(index_file, prefix,
1443 current_head, &s, &author_ident)) {
Junio C Hamano28886052007-11-18 01:52:55 -08001444 rollback_index_files();
1445 return 1;
1446 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001447
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001448 /* Determine parents */
Christian Couder643cb5f2010-06-12 18:05:12 +02001449 reflog_msg = getenv("GIT_REFLOG_ACTION");
Junio C Hamano06bb6432011-08-19 11:58:18 -07001450 if (!current_head) {
Christian Couder643cb5f2010-06-12 18:05:12 +02001451 if (!reflog_msg)
1452 reflog_msg = "commit (initial)";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001453 } else if (amend) {
1454 struct commit_list *c;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001455
Christian Couder643cb5f2010-06-12 18:05:12 +02001456 if (!reflog_msg)
1457 reflog_msg = "commit (amend)";
Junio C Hamano06bb6432011-08-19 11:58:18 -07001458 for (c = current_head->parents; c; c = c->next)
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001459 pptr = &commit_list_insert(c->item, pptr)->next;
Jay Soffian37f7a852011-02-19 23:12:29 -05001460 } else if (whence == FROM_MERGE) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001461 struct strbuf m = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001462 FILE *fp;
1463
Christian Couder643cb5f2010-06-12 18:05:12 +02001464 if (!reflog_msg)
1465 reflog_msg = "commit (merge)";
Junio C Hamano06bb6432011-08-19 11:58:18 -07001466 pptr = &commit_list_insert(current_head, pptr)->next;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001467 fp = fopen(git_path("MERGE_HEAD"), "r");
1468 if (fp == NULL)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001469 die_errno(_("could not open '%s' for reading"),
Thomas Rastd824cbb2009-06-27 17:58:46 +02001470 git_path("MERGE_HEAD"));
Linus Torvalds7c3fd252008-01-15 16:12:33 -08001471 while (strbuf_getline(&m, fp, '\n') != EOF) {
Junio C Hamano5231c632011-11-07 16:21:32 -08001472 struct commit *parent;
1473
1474 parent = get_merge_parent(m.buf);
1475 if (!parent)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001476 die(_("Corrupt MERGE_HEAD file (%s)"), m.buf);
Junio C Hamano5231c632011-11-07 16:21:32 -08001477 pptr = &commit_list_insert(parent, pptr)->next;
Linus Torvalds7c3fd252008-01-15 16:12:33 -08001478 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001479 fclose(fp);
1480 strbuf_release(&m);
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001481 if (!stat(git_path("MERGE_MODE"), &statbuf)) {
1482 if (strbuf_read_file(&sb, git_path("MERGE_MODE"), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001483 die_errno(_("could not read MERGE_MODE"));
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001484 if (!strcmp(sb.buf, "no-ff"))
1485 allow_fast_forward = 0;
1486 }
1487 if (allow_fast_forward)
1488 parents = reduce_heads(parents);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001489 } else {
Christian Couder643cb5f2010-06-12 18:05:12 +02001490 if (!reflog_msg)
Jay Soffian37f7a852011-02-19 23:12:29 -05001491 reflog_msg = (whence == FROM_CHERRY_PICK)
1492 ? "commit (cherry-pick)"
1493 : "commit";
Junio C Hamano06bb6432011-08-19 11:58:18 -07001494 pptr = &commit_list_insert(current_head, pptr)->next;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001495 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001496
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001497 /* Finally, get the commit message */
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001498 strbuf_reset(&sb);
Junio C Hamano740001a2007-12-08 23:23:20 -08001499 if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
Thomas Rast0721c312009-06-27 17:58:47 +02001500 int saved_errno = errno;
Junio C Hamano740001a2007-12-08 23:23:20 -08001501 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001502 die(_("could not read commit message: %s"), strerror(saved_errno));
Junio C Hamano740001a2007-12-08 23:23:20 -08001503 }
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001504
1505 /* Truncate the message just before the diff, if any. */
Jeff King0b382272008-11-12 03:25:52 -05001506 if (verbose) {
1507 p = strstr(sb.buf, "\ndiff --git ");
1508 if (p != NULL)
1509 strbuf_setlen(&sb, p - sb.buf + 1);
1510 }
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001511
Alex Riesen5f065732007-12-22 19:46:24 +01001512 if (cleanup_mode != CLEANUP_NONE)
1513 stripspace(&sb, cleanup_mode == CLEANUP_ALL);
Junio C Hamanob2eda9b2012-03-30 12:14:33 -07001514 if (template_untouched(&sb) && !allow_empty_message) {
1515 rollback_index_files();
1516 fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
1517 exit(1);
1518 }
Ævar Arnfjörð Bjarmasonc9b5fde2010-04-06 08:40:35 +00001519 if (message_is_empty(&sb) && !allow_empty_message) {
Junio C Hamano28886052007-11-18 01:52:55 -08001520 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001521 fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
Jeff Kingfdc7c812008-07-31 03:36:09 -04001522 exit(1);
Junio C Hamano28886052007-11-18 01:52:55 -08001523 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001524
Junio C Hamano0074d182011-12-20 13:20:56 -08001525 if (amend) {
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001526 const char *exclude_gpgsig[2] = { "gpgsig", NULL };
1527 extra = read_commit_extra_headers(current_head, exclude_gpgsig);
Junio C Hamano0074d182011-12-20 13:20:56 -08001528 } else {
1529 struct commit_extra_header **tail = &extra;
1530 append_merge_tag_headers(parents, &tail);
1531 }
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001532
Junio C Hamanof35ccd92011-12-22 11:27:26 -08001533 if (commit_tree_extended(&sb, active_cache_tree->sha1, parents, sha1,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001534 author_ident.buf, sign_commit, extra)) {
Junio C Hamano28886052007-11-18 01:52:55 -08001535 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001536 die(_("failed to write commit object"));
Junio C Hamano28886052007-11-18 01:52:55 -08001537 }
Junio C Hamano4c28e4a2010-12-20 17:00:36 -08001538 strbuf_release(&author_ident);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001539 free_commit_extra_headers(extra);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001540
1541 ref_lock = lock_any_ref_for_update("HEAD",
Junio C Hamano06bb6432011-08-19 11:58:18 -07001542 !current_head
1543 ? NULL
1544 : current_head->object.sha1,
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001545 0);
1546
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001547 nl = strchr(sb.buf, '\n');
Johannes Schindelin741707b2007-11-08 12:15:26 +00001548 if (nl)
1549 strbuf_setlen(&sb, nl + 1 - sb.buf);
1550 else
1551 strbuf_addch(&sb, '\n');
Johannes Schindelin741707b2007-11-08 12:15:26 +00001552 strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
1553 strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001554
Junio C Hamano28886052007-11-18 01:52:55 -08001555 if (!ref_lock) {
1556 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001557 die(_("cannot lock HEAD ref"));
Junio C Hamano28886052007-11-18 01:52:55 -08001558 }
Junio C Hamano06bb6432011-08-19 11:58:18 -07001559 if (write_ref_sha1(ref_lock, sha1, sb.buf) < 0) {
Junio C Hamano28886052007-11-18 01:52:55 -08001560 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001561 die(_("cannot update HEAD ref"));
Junio C Hamano28886052007-11-18 01:52:55 -08001562 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001563
Jay Soffiand7e5c0c2011-02-19 23:12:27 -05001564 unlink(git_path("CHERRY_PICK_HEAD"));
Jonathan Nieder82433cd2011-11-22 05:17:36 -06001565 unlink(git_path("REVERT_HEAD"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001566 unlink(git_path("MERGE_HEAD"));
1567 unlink(git_path("MERGE_MSG"));
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001568 unlink(git_path("MERGE_MODE"));
Gerrit Pape5a95b852008-02-08 09:53:58 +00001569 unlink(git_path("SQUASH_MSG"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001570
Brandon Casey5a9dd392008-01-23 11:21:22 -06001571 if (commit_index_files())
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001572 die (_("Repository has been updated, but unable to write\n"
Brandon Casey5a9dd392008-01-23 11:21:22 -06001573 "new_index file. Check that disk is not full or quota is\n"
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001574 "not exceeded, and then \"git reset HEAD\" to recover."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001575
Junio C Hamanocb6020b2009-12-04 00:20:48 -08001576 rerere(0);
Junio C Hamano28886052007-11-18 01:52:55 -08001577 run_hook(get_index_file(), "post-commit", NULL);
Thomas Rast6f6bee32010-03-12 18:04:28 +01001578 if (amend && !no_post_rewrite) {
Thomas Rast6360d342010-03-12 18:04:34 +01001579 struct notes_rewrite_cfg *cfg;
1580 cfg = init_copy_notes_for_rewrite("amend");
1581 if (cfg) {
Junio C Hamano06bb6432011-08-19 11:58:18 -07001582 /* we are amending, so current_head is not NULL */
1583 copy_note_for_rewrite(cfg, current_head->object.sha1, sha1);
Thomas Rast6360d342010-03-12 18:04:34 +01001584 finish_copy_notes_for_rewrite(cfg);
1585 }
Junio C Hamano06bb6432011-08-19 11:58:18 -07001586 run_rewrite_hook(current_head->object.sha1, sha1);
Thomas Rast6f6bee32010-03-12 18:04:28 +01001587 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001588 if (!quiet)
Junio C Hamano06bb6432011-08-19 11:58:18 -07001589 print_summary(prefix, sha1, !current_head);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001590
1591 return 0;
1592}