blob: a78dbd83bf04c7080487b8508321d79d848e9b1f [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"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050028
29static const char * const builtin_commit_usage[] = {
Stephan Beyer1b1dd232008-07-13 15:36:15 +020030 "git commit [options] [--] <filepattern>...",
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050031 NULL
32};
33
Shawn Bohrer2f02b252007-12-02 23:02:09 -060034static const char * const builtin_status_usage[] = {
Stephan Beyer1b1dd232008-07-13 15:36:15 +020035 "git status [options] [--] <filepattern>...",
Shawn Bohrer2f02b252007-12-02 23:02:09 -060036 NULL
37};
38
Jeff King49ff9a72010-01-13 12:39:51 -050039static const char implicit_ident_advice[] =
40"Your name and email address were configured automatically based\n"
41"on your username and hostname. Please check that they are accurate.\n"
42"You can suppress this message by setting them explicitly:\n"
43"\n"
Matt Kraai8bb45b22010-02-24 06:18:25 -080044" git config --global user.name \"Your Name\"\n"
Jeff King49ff9a72010-01-13 12:39:51 -050045" git config --global user.email you@example.com\n"
46"\n"
47"If the identity used for this commit is wrong, you can fix it with:\n"
48"\n"
49" git commit --amend --author='Your Name <you@example.com>'\n";
50
Jeff Kingf197ed22010-06-06 20:41:46 -040051static const char empty_amend_advice[] =
52"You asked to amend the most recent commit, but doing so would make\n"
53"it empty. You can repeat your command with --allow-empty, or you can\n"
54"remove the commit entirely with \"git reset HEAD^\".\n";
55
Junio C Hamano30988302009-12-11 23:45:24 -080056static unsigned char head_sha1[20];
Jeff King49ff9a72010-01-13 12:39:51 -050057
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050058static char *use_message_buffer;
59static const char commit_editmsg[] = "COMMIT_EDITMSG";
Junio C Hamano28886052007-11-18 01:52:55 -080060static struct lock_file index_lock; /* real index */
61static struct lock_file false_lock; /* used only for partial commits */
62static enum {
63 COMMIT_AS_IS = 1,
64 COMMIT_NORMAL,
Gary V. Vaughan4b055482010-05-14 09:31:35 +000065 COMMIT_PARTIAL
Junio C Hamano28886052007-11-18 01:52:55 -080066} commit_style;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050067
Junio C Hamanodbd0f5c2008-08-06 11:43:47 -070068static const char *logfile, *force_author;
Brian Hetro984c6e72008-07-05 01:24:40 -040069static const char *template_file;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050070static char *edit_message, *use_message;
Santi Béjare83dbe82008-05-04 18:04:50 +020071static char *author_name, *author_email, *author_date;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050072static int all, edit_flag, also, interactive, only, amend, signoff;
Erick Mattosc51f6ce2009-11-04 01:20:11 -020073static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
Ævar Arnfjörð Bjarmasonc9b5fde2010-04-06 08:40:35 +000074static int no_post_rewrite, allow_empty_message;
Jens Lehmann46a958b2010-06-25 16:56:47 +020075static char *untracked_files_arg, *force_date, *ignore_submodule_arg;
Alex Riesen5f065732007-12-22 19:46:24 +010076/*
77 * The default commit message cleanup mode will remove the lines
78 * beginning with # (shell comments) and leading and trailing
79 * whitespaces (empty lines or containing only whitespaces)
80 * if editor is used, and only the whitespaces if the message
81 * is specified explicitly.
82 */
83static enum {
84 CLEANUP_SPACE,
85 CLEANUP_NONE,
Gary V. Vaughan4b055482010-05-14 09:31:35 +000086 CLEANUP_ALL
Alex Riesen5f065732007-12-22 19:46:24 +010087} cleanup_mode;
88static char *cleanup_arg;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050089
James P. Howard, IIbed575e2009-12-07 17:45:27 -050090static int use_editor = 1, initial_commit, in_merge, include_status = 1;
Junio C Hamano2381e392010-04-10 00:33:17 -070091static int show_ignored_in_status;
Johannes Schindelin3b6aeb32008-07-22 21:40:41 +010092static const char *only_include_assumed;
93static struct strbuf message;
Johannes Schindelinf9568532007-11-11 17:36:39 +000094
Jeff King7c9f7032009-09-05 04:59:56 -040095static int null_termination;
96static enum {
97 STATUS_FORMAT_LONG,
98 STATUS_FORMAT_SHORT,
Gary V. Vaughan4b055482010-05-14 09:31:35 +000099 STATUS_FORMAT_PORCELAIN
Jeff King7c9f7032009-09-05 04:59:56 -0400100} status_format = STATUS_FORMAT_LONG;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +0200101static int status_show_branch;
Jeff King7c9f7032009-09-05 04:59:56 -0400102
Johannes Schindelinf9568532007-11-11 17:36:39 +0000103static int opt_parse_m(const struct option *opt, const char *arg, int unset)
104{
105 struct strbuf *buf = opt->value;
106 if (unset)
107 strbuf_setlen(buf, 0);
108 else {
109 strbuf_addstr(buf, arg);
Johannes Schindelin3b6aeb32008-07-22 21:40:41 +0100110 strbuf_addstr(buf, "\n\n");
Johannes Schindelinf9568532007-11-11 17:36:39 +0000111 }
112 return 0;
113}
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500114
115static struct option builtin_commit_options[] = {
116 OPT__QUIET(&quiet),
117 OPT__VERBOSE(&verbose),
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500118
Greg Pricee97ca7f2009-12-29 16:54:49 -0500119 OPT_GROUP("Commit message options"),
Stephen Boyddf217ed2009-05-23 11:53:13 -0700120 OPT_FILENAME('F', "file", &logfile, "read log from file"),
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500121 OPT_STRING(0, "author", &force_author, "AUTHOR", "override author for commit"),
Miklos Vajna02b47cd2009-12-02 23:16:18 +0100122 OPT_STRING(0, "date", &force_date, "DATE", "override date for commit"),
Johannes Schindelinf9568532007-11-11 17:36:39 +0000123 OPT_CALLBACK('m', "message", &message, "MESSAGE", "specify commit message", opt_parse_m),
Erick Mattosc51f6ce2009-11-04 01:20:11 -0200124 OPT_STRING('c', "reedit-message", &edit_message, "COMMIT", "reuse and edit message from specified commit"),
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500125 OPT_STRING('C', "reuse-message", &use_message, "COMMIT", "reuse message from specified commit"),
Erick Mattosc51f6ce2009-11-04 01:20:11 -0200126 OPT_BOOLEAN(0, "reset-author", &renew_authorship, "the commit is authored by me now (used with -C-c/--amend)"),
Dan McGee362b0dd2008-04-26 19:43:20 -0500127 OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"),
Stephen Boyddf217ed2009-05-23 11:53:13 -0700128 OPT_FILENAME('t', "template", &template_file, "use specified template file"),
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500129 OPT_BOOLEAN('e', "edit", &edit_flag, "force edit of commit"),
Greg Pricee97ca7f2009-12-29 16:54:49 -0500130 OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"),
James P. Howard, IIbed575e2009-12-07 17:45:27 -0500131 OPT_BOOLEAN(0, "status", &include_status, "include status in commit message template"),
Greg Pricee97ca7f2009-12-29 16:54:49 -0500132 /* end commit message options */
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500133
134 OPT_GROUP("Commit contents options"),
135 OPT_BOOLEAN('a', "all", &all, "commit all changed files"),
136 OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"),
137 OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"),
Johannes Sixtd4ba07c2008-04-10 13:33:09 +0200138 OPT_BOOLEAN('o', "only", &only, "commit only specified files"),
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500139 OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"),
Junio C Hamano3a5d13a2009-08-07 23:03:36 -0700140 OPT_BOOLEAN(0, "dry-run", &dry_run, "show what would be committed"),
Jeff King7c9f7032009-09-05 04:59:56 -0400141 OPT_SET_INT(0, "short", &status_format, "show status concisely",
142 STATUS_FORMAT_SHORT),
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +0200143 OPT_BOOLEAN(0, "branch", &status_show_branch, "show branch information"),
Jeff King7c9f7032009-09-05 04:59:56 -0400144 OPT_SET_INT(0, "porcelain", &status_format,
145 "show porcelain output format", STATUS_FORMAT_PORCELAIN),
146 OPT_BOOLEAN('z', "null", &null_termination,
147 "terminate entries with NUL"),
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500148 OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
Thomas Rast6f6bee32010-03-12 18:04:28 +0100149 OPT_BOOLEAN(0, "no-post-rewrite", &no_post_rewrite, "bypass post-rewrite hook"),
Marius Storm-Olsen6c2ce042008-06-05 14:22:56 +0200150 { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, "mode", "show untracked files, optional modes: all, normal, no. (Default: all)", PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
Greg Pricee97ca7f2009-12-29 16:54:49 -0500151 /* end commit contents options */
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500152
Ævar Arnfjörð Bjarmasonc9b5fde2010-04-06 08:40:35 +0000153 { OPTION_BOOLEAN, 0, "allow-empty", &allow_empty, NULL,
154 "ok to record an empty change",
155 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
156 { OPTION_BOOLEAN, 0, "allow-empty-message", &allow_empty_message, NULL,
157 "ok to record a change with an empty message",
158 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
159
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500160 OPT_END()
161};
162
Junio C Hamano28886052007-11-18 01:52:55 -0800163static void rollback_index_files(void)
164{
165 switch (commit_style) {
166 case COMMIT_AS_IS:
167 break; /* nothing to do */
168 case COMMIT_NORMAL:
169 rollback_lock_file(&index_lock);
170 break;
171 case COMMIT_PARTIAL:
172 rollback_lock_file(&index_lock);
173 rollback_lock_file(&false_lock);
174 break;
175 }
176}
177
Brandon Casey5a9dd392008-01-23 11:21:22 -0600178static int commit_index_files(void)
Junio C Hamano28886052007-11-18 01:52:55 -0800179{
Brandon Casey5a9dd392008-01-23 11:21:22 -0600180 int err = 0;
181
Junio C Hamano28886052007-11-18 01:52:55 -0800182 switch (commit_style) {
183 case COMMIT_AS_IS:
184 break; /* nothing to do */
185 case COMMIT_NORMAL:
Brandon Casey5a9dd392008-01-23 11:21:22 -0600186 err = commit_lock_file(&index_lock);
Junio C Hamano28886052007-11-18 01:52:55 -0800187 break;
188 case COMMIT_PARTIAL:
Brandon Casey5a9dd392008-01-23 11:21:22 -0600189 err = commit_lock_file(&index_lock);
Junio C Hamano28886052007-11-18 01:52:55 -0800190 rollback_lock_file(&false_lock);
191 break;
192 }
Brandon Casey5a9dd392008-01-23 11:21:22 -0600193
194 return err;
Junio C Hamano28886052007-11-18 01:52:55 -0800195}
196
197/*
198 * Take a union of paths in the index and the named tree (typically, "HEAD"),
199 * and return the paths that match the given pattern in list.
200 */
Johannes Schindelinc455c872008-07-21 19:03:49 +0100201static int list_paths(struct string_list *list, const char *with_tree,
Junio C Hamano28886052007-11-18 01:52:55 -0800202 const char *prefix, const char **pattern)
203{
204 int i;
205 char *m;
206
207 for (i = 0; pattern[i]; i++)
208 ;
209 m = xcalloc(1, i);
210
211 if (with_tree)
212 overlay_tree_on_cache(with_tree, prefix);
213
214 for (i = 0; i < active_nr; i++) {
215 struct cache_entry *ce = active_cache[i];
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700216 struct string_list_item *item;
217
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800218 if (ce->ce_flags & CE_UPDATE)
Junio C Hamanoe87e22d2008-01-14 13:54:24 -0800219 continue;
Clemens Buchacher0b509222009-01-14 15:54:35 +0100220 if (!match_pathspec(pattern, ce->name, ce_namelen(ce), 0, m))
Junio C Hamano28886052007-11-18 01:52:55 -0800221 continue;
Julian Phillips78a395d2010-06-26 00:41:35 +0100222 item = string_list_insert(list, ce->name);
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700223 if (ce_skip_worktree(ce))
224 item->util = item; /* better a valid pointer than a fake one */
Junio C Hamano28886052007-11-18 01:52:55 -0800225 }
226
227 return report_path_error(m, pattern, prefix ? strlen(prefix) : 0);
228}
229
Johannes Schindelinc455c872008-07-21 19:03:49 +0100230static void add_remove_files(struct string_list *list)
Junio C Hamano28886052007-11-18 01:52:55 -0800231{
232 int i;
233 for (i = 0; i < list->nr; i++) {
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700234 struct stat st;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100235 struct string_list_item *p = &(list->items[i]);
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700236
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700237 /* p->util is skip-worktree */
238 if (p->util)
Nguyễn Thái Ngọc Duyb4d16902009-08-20 20:46:58 +0700239 continue;
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700240
Johannes Schindelinc455c872008-07-21 19:03:49 +0100241 if (!lstat(p->string, &st)) {
242 if (add_to_cache(p->string, &st, 0))
Alex Riesen960b8ad2008-05-12 19:57:45 +0200243 die("updating files failed");
244 } else
Johannes Schindelinc455c872008-07-21 19:03:49 +0100245 remove_file_from_cache(p->string);
Junio C Hamano28886052007-11-18 01:52:55 -0800246 }
247}
248
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800249static void create_base_index(void)
250{
251 struct tree *tree;
252 struct unpack_trees_options opts;
253 struct tree_desc t;
254
255 if (initial_commit) {
256 discard_cache();
257 return;
258 }
259
260 memset(&opts, 0, sizeof(opts));
261 opts.head_idx = 1;
262 opts.index_only = 1;
263 opts.merge = 1;
Linus Torvalds34110cd2008-03-06 18:12:28 -0800264 opts.src_index = &the_index;
265 opts.dst_index = &the_index;
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800266
267 opts.fn = oneway_merge;
268 tree = parse_tree_indirect(head_sha1);
269 if (!tree)
270 die("failed to unpack HEAD tree object");
271 parse_tree(tree);
272 init_tree_desc(&t, tree->buffer, tree->size);
Daniel Barkalow203a2fe2008-02-07 11:39:48 -0500273 if (unpack_trees(1, &t, &opts))
274 exit(128); /* We've already reported the error, finish dying */
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800275}
276
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100277static void refresh_cache_or_die(int refresh_flags)
278{
279 /*
280 * refresh_flags contains REFRESH_QUIET, so the only errors
281 * are for unmerged entries.
282 */
283 if (refresh_cache(refresh_flags | REFRESH_IN_PORCELAIN))
284 die_resolve_conflict("commit");
285}
286
Junio C Hamano50b7e702009-08-04 23:49:33 -0700287static char *prepare_index(int argc, const char **argv, const char *prefix, int is_status)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500288{
289 int fd;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100290 struct string_list partial;
Junio C Hamano28886052007-11-18 01:52:55 -0800291 const char **pathspec = NULL;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700292 int refresh_flags = REFRESH_QUIET;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500293
Junio C Hamano50b7e702009-08-04 23:49:33 -0700294 if (is_status)
295 refresh_flags |= REFRESH_UNMERGED;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500296 if (interactive) {
Jeff King4f6a32f2009-04-03 15:28:56 -0400297 if (interactive_add(argc, argv, prefix) != 0)
298 die("interactive add failed");
Linus Torvalds671c9b72008-11-13 16:36:30 -0800299 if (read_cache_preload(NULL) < 0)
Gerrit Paped5350fd2008-05-27 08:59:16 +0000300 die("index file corrupt");
Junio C Hamano28886052007-11-18 01:52:55 -0800301 commit_style = COMMIT_AS_IS;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500302 return get_index_file();
303 }
304
Junio C Hamanof64fe7b2007-11-25 08:46:29 -0800305 if (*argv)
306 pathspec = get_pathspec(prefix, argv);
Junio C Hamano28886052007-11-18 01:52:55 -0800307
Linus Torvalds671c9b72008-11-13 16:36:30 -0800308 if (read_cache_preload(pathspec) < 0)
309 die("index file corrupt");
310
Junio C Hamano28886052007-11-18 01:52:55 -0800311 /*
312 * Non partial, non as-is commit.
313 *
314 * (1) get the real index;
315 * (2) update the_index as necessary;
316 * (3) write the_index out to the real index (still locked);
317 * (4) return the name of the locked index file.
318 *
319 * The caller should run hooks on the locked real index, and
320 * (A) if all goes well, commit the real index;
321 * (B) on failure, rollback the real index.
322 */
323 if (all || (also && pathspec && *pathspec)) {
Markus Heidelberg1f2362a2010-04-02 14:27:19 +0200324 fd = hold_locked_index(&index_lock, 1);
Alex Riesen7ae02a32008-05-12 19:58:10 +0200325 add_files_to_cache(also ? prefix : NULL, pathspec, 0);
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100326 refresh_cache_or_die(refresh_flags);
Brandon Casey4ed7cd32008-01-16 13:12:46 -0600327 if (write_cache(fd, active_cache, active_nr) ||
328 close_lock_file(&index_lock))
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500329 die("unable to write new_index file");
Junio C Hamano28886052007-11-18 01:52:55 -0800330 commit_style = COMMIT_NORMAL;
331 return index_lock.filename;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500332 }
333
Junio C Hamano28886052007-11-18 01:52:55 -0800334 /*
335 * As-is commit.
336 *
337 * (1) return the name of the real index file.
338 *
Markus Heidelberg73276232010-04-02 14:27:18 +0200339 * The caller should run hooks on the real index,
340 * and create commit from the_index.
Junio C Hamano28886052007-11-18 01:52:55 -0800341 * We still need to refresh the index here.
342 */
343 if (!pathspec || !*pathspec) {
344 fd = hold_locked_index(&index_lock, 1);
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100345 refresh_cache_or_die(refresh_flags);
Junio C Hamanod5f5d0a2010-07-06 21:53:11 -0700346 if (active_cache_changed) {
347 if (write_cache(fd, active_cache, active_nr) ||
348 commit_locked_index(&index_lock))
349 die("unable to write new_index file");
350 } else {
351 rollback_lock_file(&index_lock);
352 }
Junio C Hamano28886052007-11-18 01:52:55 -0800353 commit_style = COMMIT_AS_IS;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500354 return get_index_file();
355 }
356
Junio C Hamano28886052007-11-18 01:52:55 -0800357 /*
358 * A partial commit.
359 *
360 * (0) find the set of affected paths;
361 * (1) get lock on the real index file;
362 * (2) update the_index with the given paths;
363 * (3) write the_index out to the real index (still locked);
364 * (4) get lock on the false index file;
365 * (5) reset the_index from HEAD;
366 * (6) update the_index the same way as (2);
367 * (7) write the_index out to the false index file;
368 * (8) return the name of the false index file (still locked);
369 *
370 * The caller should run hooks on the locked false index, and
371 * create commit from it. Then
372 * (A) if all goes well, commit the real index;
373 * (B) on failure, rollback the real index;
374 * In either case, rollback the false index.
375 */
376 commit_style = COMMIT_PARTIAL;
377
Junio C Hamano30988302009-12-11 23:45:24 -0800378 if (in_merge)
Junio C Hamano28886052007-11-18 01:52:55 -0800379 die("cannot do a partial commit during a merge.");
380
381 memset(&partial, 0, sizeof(partial));
Johannes Schindelinc455c872008-07-21 19:03:49 +0100382 partial.strdup_strings = 1;
Junio C Hamano28886052007-11-18 01:52:55 -0800383 if (list_paths(&partial, initial_commit ? NULL : "HEAD", prefix, pathspec))
384 exit(1);
385
386 discard_cache();
387 if (read_cache() < 0)
388 die("cannot read the index");
389
390 fd = hold_locked_index(&index_lock, 1);
391 add_remove_files(&partial);
Kristian Høgsbergef12b502007-11-12 15:48:22 -0500392 refresh_cache(REFRESH_QUIET);
Brandon Casey4ed7cd32008-01-16 13:12:46 -0600393 if (write_cache(fd, active_cache, active_nr) ||
394 close_lock_file(&index_lock))
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500395 die("unable to write new_index file");
396
Junio C Hamano28886052007-11-18 01:52:55 -0800397 fd = hold_lock_file_for_update(&false_lock,
Junio C Hamanoa1574002008-10-21 17:58:11 -0700398 git_path("next-index-%"PRIuMAX,
399 (uintmax_t) getpid()),
Junio C Hamanoacd3b9e2008-10-17 15:44:39 -0700400 LOCK_DIE_ON_ERROR);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800401
402 create_base_index();
Junio C Hamano28886052007-11-18 01:52:55 -0800403 add_remove_files(&partial);
Kristian Høgsbergd37d3202007-11-09 11:40:27 -0500404 refresh_cache(REFRESH_QUIET);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500405
Brandon Casey4ed7cd32008-01-16 13:12:46 -0600406 if (write_cache(fd, active_cache, active_nr) ||
407 close_lock_file(&false_lock))
Junio C Hamano28886052007-11-18 01:52:55 -0800408 die("unable to write temporary index file");
Jeff King959ba672008-02-14 12:18:23 -0500409
410 discard_cache();
411 read_cache_from(false_lock.filename);
412
Junio C Hamano28886052007-11-18 01:52:55 -0800413 return false_lock.filename;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500414}
415
Junio C Hamanod249b092009-08-09 21:59:30 -0700416static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
417 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500418{
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700419 unsigned char sha1[20];
420
Junio C Hamanod249b092009-08-09 21:59:30 -0700421 if (s->relative_paths)
422 s->prefix = prefix;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500423
424 if (amend) {
Junio C Hamanod249b092009-08-09 21:59:30 -0700425 s->amend = 1;
426 s->reference = "HEAD^1";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500427 }
Junio C Hamanod249b092009-08-09 21:59:30 -0700428 s->verbose = verbose;
429 s->index_file = index_file;
430 s->fp = fp;
431 s->nowarn = nowarn;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700432 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500433
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700434 wt_status_collect(s);
Jeff King7c9f7032009-09-05 04:59:56 -0400435
436 switch (status_format) {
437 case STATUS_FORMAT_SHORT:
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +0200438 wt_shortstatus_print(s, null_termination, status_show_branch);
Jeff King7c9f7032009-09-05 04:59:56 -0400439 break;
440 case STATUS_FORMAT_PORCELAIN:
Jeff King4a7cc2f2009-12-07 00:17:15 -0500441 wt_porcelain_print(s, null_termination);
Jeff King7c9f7032009-09-05 04:59:56 -0400442 break;
443 case STATUS_FORMAT_LONG:
444 wt_status_print(s);
445 break;
446 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500447
Junio C Hamanod249b092009-08-09 21:59:30 -0700448 return s->commitable;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500449}
450
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100451static int is_a_merge(const unsigned char *sha1)
452{
453 struct commit *commit = lookup_commit(sha1);
454 if (!commit || parse_commit(commit))
455 die("could not parse HEAD commit");
456 return !!(commit->parents && commit->parents->next);
457}
458
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500459static const char sign_off_header[] = "Signed-off-by: ";
460
Santi Béjare83dbe82008-05-04 18:04:50 +0200461static void determine_author_info(void)
Santi Béjara45d46b2008-05-04 18:04:49 +0200462{
463 char *name, *email, *date;
464
465 name = getenv("GIT_AUTHOR_NAME");
466 email = getenv("GIT_AUTHOR_EMAIL");
467 date = getenv("GIT_AUTHOR_DATE");
468
Erick Mattosc51f6ce2009-11-04 01:20:11 -0200469 if (use_message && !renew_authorship) {
Santi Béjara45d46b2008-05-04 18:04:49 +0200470 const char *a, *lb, *rb, *eol;
471
472 a = strstr(use_message_buffer, "\nauthor ");
473 if (!a)
474 die("invalid commit: %s", use_message);
475
Jonathan Niederfb7749e2010-05-02 03:57:12 -0500476 lb = strchrnul(a + strlen("\nauthor "), '<');
477 rb = strchrnul(lb, '>');
478 eol = strchrnul(rb, '\n');
479 if (!*lb || !*rb || !*eol)
Santi Béjara45d46b2008-05-04 18:04:49 +0200480 die("invalid commit: %s", use_message);
481
Jonathan Niederfb7749e2010-05-02 03:57:12 -0500482 if (lb == a + strlen("\nauthor "))
483 /* \nauthor <foo@example.com> */
484 name = xcalloc(1, 1);
485 else
486 name = xmemdupz(a + strlen("\nauthor "),
487 (lb - strlen(" ") -
488 (a + strlen("\nauthor "))));
489 email = xmemdupz(lb + strlen("<"), rb - (lb + strlen("<")));
490 date = xmemdupz(rb + strlen("> "), eol - (rb + strlen("> ")));
Santi Béjara45d46b2008-05-04 18:04:49 +0200491 }
492
493 if (force_author) {
494 const char *lb = strstr(force_author, " <");
495 const char *rb = strchr(force_author, '>');
496
497 if (!lb || !rb)
498 die("malformed --author parameter");
499 name = xstrndup(force_author, lb - force_author);
500 email = xstrndup(lb + 2, rb - (lb + 2));
501 }
502
Miklos Vajna02b47cd2009-12-02 23:16:18 +0100503 if (force_date)
504 date = force_date;
505
Santi Béjare83dbe82008-05-04 18:04:50 +0200506 author_name = name;
507 author_email = email;
508 author_date = date;
Santi Béjara45d46b2008-05-04 18:04:49 +0200509}
510
David Brownc1e01b02009-10-28 10:13:44 -0700511static int ends_rfc2822_footer(struct strbuf *sb)
512{
513 int ch;
514 int hit = 0;
515 int i, j, k;
516 int len = sb->len;
517 int first = 1;
518 const char *buf = sb->buf;
519
520 for (i = len - 1; i > 0; i--) {
521 if (hit && buf[i] == '\n')
522 break;
523 hit = (buf[i] == '\n');
524 }
525
526 while (i < len - 1 && buf[i] == '\n')
527 i++;
528
529 for (; i < len; i = k) {
530 for (k = i; k < len && buf[k] != '\n'; k++)
531 ; /* do nothing */
532 k++;
533
534 if ((buf[k] == ' ' || buf[k] == '\t') && !first)
535 continue;
536
537 first = 0;
538
539 for (j = 0; i + j < len; j++) {
540 ch = buf[i + j];
541 if (ch == ':')
542 break;
543 if (isalnum(ch) ||
544 (ch == '-'))
545 continue;
546 return 0;
547 }
548 }
549 return 1;
550}
551
Junio C Hamanod249b092009-08-09 21:59:30 -0700552static int prepare_to_commit(const char *index_file, const char *prefix,
553 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500554{
555 struct stat statbuf;
Junio C Hamanobc5d2482007-11-18 12:01:38 -0800556 int commitable, saved_color_setting;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500557 struct strbuf sb = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500558 char *buffer;
559 FILE *fp;
Paolo Bonzini8089c852008-02-05 08:04:18 +0100560 const char *hook_arg1 = NULL;
561 const char *hook_arg2 = NULL;
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200562 int ident_shown = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500563
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100564 if (!no_verify && run_hook(index_file, "pre-commit", NULL))
565 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500566
Johannes Schindelinf9568532007-11-11 17:36:39 +0000567 if (message.len) {
568 strbuf_addbuf(&sb, &message);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100569 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500570 } else if (logfile && !strcmp(logfile, "-")) {
571 if (isatty(0))
572 fprintf(stderr, "(reading log message from standard input)\n");
573 if (strbuf_read(&sb, 0, 0) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +0200574 die_errno("could not read log from standard input");
Paolo Bonzini8089c852008-02-05 08:04:18 +0100575 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500576 } else if (logfile) {
577 if (strbuf_read_file(&sb, logfile, 0) < 0)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200578 die_errno("could not read log file '%s'",
579 logfile);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100580 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500581 } else if (use_message) {
582 buffer = strstr(use_message_buffer, "\n\n");
583 if (!buffer || buffer[2] == '\0')
584 die("commit has empty message");
585 strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100586 hook_arg1 = "commit";
587 hook_arg2 = use_message;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500588 } else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
589 if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200590 die_errno("could not read MERGE_MSG");
Paolo Bonzini8089c852008-02-05 08:04:18 +0100591 hook_arg1 = "merge";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500592 } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
593 if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200594 die_errno("could not read SQUASH_MSG");
Paolo Bonzini8089c852008-02-05 08:04:18 +0100595 hook_arg1 = "squash";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500596 } else if (template_file && !stat(template_file, &statbuf)) {
597 if (strbuf_read_file(&sb, template_file, 0) < 0)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200598 die_errno("could not read '%s'", template_file);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100599 hook_arg1 = "template";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500600 }
601
Paolo Bonzini8089c852008-02-05 08:04:18 +0100602 /*
603 * This final case does not modify the template message,
604 * it just sets the argument to the prepare-commit-msg hook.
605 */
606 else if (in_merge)
607 hook_arg1 = "merge";
608
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500609 fp = fopen(git_path(commit_editmsg), "w");
610 if (fp == NULL)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200611 die_errno("could not open '%s'", git_path(commit_editmsg));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500612
Alex Riesen5f065732007-12-22 19:46:24 +0100613 if (cleanup_mode != CLEANUP_NONE)
614 stripspace(&sb, 0);
Johannes Schindelin13208572007-11-11 17:35:58 +0000615
616 if (signoff) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500617 struct strbuf sob = STRBUF_INIT;
Johannes Schindelin13208572007-11-11 17:35:58 +0000618 int i;
619
Johannes Schindelin13208572007-11-11 17:35:58 +0000620 strbuf_addstr(&sob, sign_off_header);
Junio C Hamanod9ccfe72007-12-02 13:43:34 -0800621 strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"),
622 getenv("GIT_COMMITTER_EMAIL")));
Johannes Schindelin13208572007-11-11 17:35:58 +0000623 strbuf_addch(&sob, '\n');
Johannes Schindelin13208572007-11-11 17:35:58 +0000624 for (i = sb.len - 1; i > 0 && sb.buf[i - 1] != '\n'; i--)
625 ; /* do nothing */
Johannes Schindelin21505542007-11-11 17:36:27 +0000626 if (prefixcmp(sb.buf + i, sob.buf)) {
Junio C Hamanoe5138432009-11-06 23:06:06 -0800627 if (!i || !ends_rfc2822_footer(&sb))
Johannes Schindelin21505542007-11-11 17:36:27 +0000628 strbuf_addch(&sb, '\n');
Johannes Schindelin13208572007-11-11 17:35:58 +0000629 strbuf_addbuf(&sb, &sob);
Johannes Schindelin21505542007-11-11 17:36:27 +0000630 }
Johannes Schindelin13208572007-11-11 17:35:58 +0000631 strbuf_release(&sob);
632 }
633
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500634 if (fwrite(sb.buf, 1, sb.len, fp) < sb.len)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200635 die_errno("could not write commit template");
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500636
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500637 strbuf_release(&sb);
638
Santi Béjare83dbe82008-05-04 18:04:50 +0200639 determine_author_info();
640
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200641 /* This checks if committer ident is explicitly given */
642 git_committer_info(0);
James P. Howard, IIbed575e2009-12-07 17:45:27 -0500643 if (use_editor && include_status) {
Santi Béjare83dbe82008-05-04 18:04:50 +0200644 char *author_ident;
645 const char *committer_ident;
646
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100647 if (in_merge)
648 fprintf(fp,
649 "#\n"
650 "# It looks like you may be committing a MERGE.\n"
651 "# If this is not correct, please remove the file\n"
652 "# %s\n"
653 "# and try again.\n"
654 "#\n",
655 git_path("MERGE_HEAD"));
656
657 fprintf(fp,
658 "\n"
Jeff Kingfdc7c812008-07-31 03:36:09 -0400659 "# Please enter the commit message for your changes.");
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100660 if (cleanup_mode == CLEANUP_ALL)
Jeff Kingfdc7c812008-07-31 03:36:09 -0400661 fprintf(fp,
662 " Lines starting\n"
663 "# with '#' will be ignored, and an empty"
664 " message aborts the commit.\n");
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100665 else /* CLEANUP_SPACE, that is. */
Jeff Kingfdc7c812008-07-31 03:36:09 -0400666 fprintf(fp,
667 " Lines starting\n"
668 "# with '#' will be kept; you may remove them"
669 " yourself if you want to.\n"
670 "# An empty message aborts the commit.\n");
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100671 if (only_include_assumed)
672 fprintf(fp, "# %s\n", only_include_assumed);
673
Santi Béjare83dbe82008-05-04 18:04:50 +0200674 author_ident = xstrdup(fmt_name(author_name, author_email));
675 committer_ident = fmt_name(getenv("GIT_COMMITTER_NAME"),
676 getenv("GIT_COMMITTER_EMAIL"));
677 if (strcmp(author_ident, committer_ident))
678 fprintf(fp,
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200679 "%s"
680 "# Author: %s\n",
681 ident_shown++ ? "" : "#\n",
Santi Béjare83dbe82008-05-04 18:04:50 +0200682 author_ident);
683 free(author_ident);
684
Junio C Hamano5aeb3a32010-01-17 13:54:28 -0800685 if (!user_ident_sufficiently_given())
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200686 fprintf(fp,
687 "%s"
688 "# Committer: %s\n",
689 ident_shown++ ? "" : "#\n",
690 committer_ident);
691
692 if (ident_shown)
693 fprintf(fp, "#\n");
694
Junio C Hamanod249b092009-08-09 21:59:30 -0700695 saved_color_setting = s->use_color;
696 s->use_color = 0;
697 commitable = run_status(fp, index_file, prefix, 1, s);
698 s->use_color = saved_color_setting;
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100699 } else {
Junio C Hamanod616a232007-12-22 19:22:29 -0800700 unsigned char sha1[20];
Junio C Hamanofbcf1182007-12-19 19:23:03 -0800701 const char *parent = "HEAD";
Alex Riesen71686242007-11-28 22:13:08 +0100702
Alex Riesen71686242007-11-28 22:13:08 +0100703 if (!active_nr && read_cache() < 0)
704 die("Cannot read index");
705
Junio C Hamanofbcf1182007-12-19 19:23:03 -0800706 if (amend)
707 parent = "HEAD^1";
708
Junio C Hamanod616a232007-12-22 19:22:29 -0800709 if (get_sha1(parent, sha1))
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100710 commitable = !!active_nr;
Stephan Beyer75f3ff22009-02-10 15:30:35 +0100711 else
712 commitable = index_differs_from(parent, 0);
Alex Riesen71686242007-11-28 22:13:08 +0100713 }
714
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500715 fclose(fp);
716
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100717 if (!commitable && !in_merge && !allow_empty &&
718 !(amend && is_a_merge(head_sha1))) {
Junio C Hamanod249b092009-08-09 21:59:30 -0700719 run_status(stdout, index_file, prefix, 0, s);
Jeff Kingf197ed22010-06-06 20:41:46 -0400720 if (amend)
721 fputs(empty_amend_advice, stderr);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100722 return 0;
723 }
724
725 /*
726 * Re-read the index as pre-commit hook could have updated it,
727 * and write it out as a tree. We must do this before we invoke
728 * the editor and after we invoke run_status above.
729 */
730 discard_cache();
731 read_cache_from(index_file);
732 if (!active_cache_tree)
733 active_cache_tree = cache_tree();
734 if (cache_tree_update(active_cache_tree,
735 active_cache, active_nr, 0, 0) < 0) {
Junio C Hamano331fcb52008-11-28 19:56:34 -0800736 error("Error building trees");
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100737 return 0;
738 }
739
Paolo Bonzini8089c852008-02-05 08:04:18 +0100740 if (run_hook(index_file, "prepare-commit-msg",
741 git_path(commit_editmsg), hook_arg1, hook_arg2, NULL))
742 return 0;
743
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100744 if (use_editor) {
745 char index[PATH_MAX];
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000746 const char *env[2] = { NULL };
747 env[0] = index;
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100748 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
Stephan Beyer71982032008-07-25 18:28:42 +0200749 if (launch_editor(git_path(commit_editmsg), NULL, env)) {
750 fprintf(stderr,
751 "Please supply the message using either -m or -F option.\n");
752 exit(1);
753 }
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100754 }
755
756 if (!no_verify &&
757 run_hook(index_file, "commit-msg", git_path(commit_editmsg), NULL)) {
758 return 0;
759 }
760
761 return 1;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500762}
763
764/*
Miklos Vajna6bb6b032008-09-10 22:10:32 +0200765 * Find out if the message in the strbuf contains only whitespace and
766 * Signed-off-by lines.
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500767 */
Miklos Vajna6bb6b032008-09-10 22:10:32 +0200768static int message_is_empty(struct strbuf *sb)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500769{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500770 struct strbuf tmpl = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500771 const char *nl;
Miklos Vajna6bb6b032008-09-10 22:10:32 +0200772 int eol, i, start = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500773
Alex Riesen5f065732007-12-22 19:46:24 +0100774 if (cleanup_mode == CLEANUP_NONE && sb->len)
775 return 0;
776
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500777 /* See if the template is just a prefix of the message. */
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500778 if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) {
Alex Riesen5f065732007-12-22 19:46:24 +0100779 stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500780 if (start + tmpl.len <= sb->len &&
781 memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0)
782 start += tmpl.len;
783 }
784 strbuf_release(&tmpl);
785
786 /* Check if the rest is just whitespace and Signed-of-by's. */
787 for (i = start; i < sb->len; i++) {
788 nl = memchr(sb->buf + i, '\n', sb->len - i);
789 if (nl)
790 eol = nl - sb->buf;
791 else
792 eol = sb->len;
793
794 if (strlen(sign_off_header) <= eol - i &&
795 !prefixcmp(sb->buf + i, sign_off_header)) {
796 i = eol;
797 continue;
798 }
799 while (i < eol)
800 if (!isspace(sb->buf[i++]))
801 return 0;
802 }
803
804 return 1;
805}
806
Junio C Hamano146ea062008-08-26 23:13:13 -0700807static const char *find_author_by_nickname(const char *name)
808{
809 struct rev_info revs;
810 struct commit *commit;
811 struct strbuf buf = STRBUF_INIT;
812 const char *av[20];
813 int ac = 0;
814
815 init_revisions(&revs, NULL);
816 strbuf_addf(&buf, "--author=%s", name);
817 av[++ac] = "--all";
818 av[++ac] = "-i";
819 av[++ac] = buf.buf;
820 av[++ac] = NULL;
821 setup_revisions(ac, av, &revs, NULL);
822 prepare_revision_walk(&revs);
823 commit = get_revision(&revs);
824 if (commit) {
Thomas Rastdd2e7942009-10-19 17:48:08 +0200825 struct pretty_print_context ctx = {0};
826 ctx.date_mode = DATE_NORMAL;
Junio C Hamano146ea062008-08-26 23:13:13 -0700827 strbuf_release(&buf);
Thomas Rastdd2e7942009-10-19 17:48:08 +0200828 format_commit_message(commit, "%an <%ae>", &buf, &ctx);
Junio C Hamano146ea062008-08-26 23:13:13 -0700829 return strbuf_detach(&buf, NULL);
830 }
831 die("No existing author found with '%s'", name);
832}
833
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700834
835static void handle_untracked_files_arg(struct wt_status *s)
836{
837 if (!untracked_files_arg)
838 ; /* default already initialized */
839 else if (!strcmp(untracked_files_arg, "no"))
840 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
841 else if (!strcmp(untracked_files_arg, "normal"))
842 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
843 else if (!strcmp(untracked_files_arg, "all"))
844 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
845 else
846 die("Invalid untracked files mode '%s'", untracked_files_arg);
847}
848
Shawn Bohrer2f02b252007-12-02 23:02:09 -0600849static int parse_and_validate_options(int argc, const char *argv[],
Junio C Hamanodbd0f5c2008-08-06 11:43:47 -0700850 const char * const usage[],
Junio C Hamanod249b092009-08-09 21:59:30 -0700851 const char *prefix,
852 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500853{
854 int f = 0;
855
Stephen Boyd37782922009-05-23 11:53:12 -0700856 argc = parse_options(argc, argv, prefix, builtin_commit_options, usage,
857 0);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500858
Junio C Hamano146ea062008-08-26 23:13:13 -0700859 if (force_author && !strchr(force_author, '>'))
860 force_author = find_author_by_nickname(force_author);
861
Erick Mattosc51f6ce2009-11-04 01:20:11 -0200862 if (force_author && renew_authorship)
863 die("Using both --reset-author and --author does not make sense");
864
Johannes Schindelinf9568532007-11-11 17:36:39 +0000865 if (logfile || message.len || use_message)
Junio C Hamano48034662007-12-22 19:25:37 -0800866 use_editor = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500867 if (edit_flag)
Junio C Hamano48034662007-12-22 19:25:37 -0800868 use_editor = 1;
Paolo Bonzini406400c2008-02-05 11:01:45 +0100869 if (!use_editor)
870 setenv("GIT_EDITOR", ":", 1);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500871
872 if (get_sha1("HEAD", head_sha1))
873 initial_commit = 1;
874
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500875 /* Sanity check options */
876 if (amend && initial_commit)
877 die("You have nothing to amend.");
878 if (amend && in_merge)
Jeff Kingb5b644a2007-12-02 01:07:03 -0500879 die("You are in the middle of a merge -- cannot amend.");
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500880
881 if (use_message)
882 f++;
883 if (edit_message)
884 f++;
885 if (logfile)
886 f++;
887 if (f > 1)
888 die("Only one of -c/-C/-F can be used.");
Johannes Schindelinf9568532007-11-11 17:36:39 +0000889 if (message.len && f > 0)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500890 die("Option -m cannot be combined with -c/-C/-F.");
891 if (edit_message)
892 use_message = edit_message;
Junio C Hamano1eb1e9e2007-12-14 11:57:22 -0800893 if (amend && !use_message)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500894 use_message = "HEAD";
Erick Mattosc51f6ce2009-11-04 01:20:11 -0200895 if (!use_message && renew_authorship)
896 die("--reset-author can be used only with -C, -c or --amend.");
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500897 if (use_message) {
898 unsigned char sha1[20];
899 static char utf8[] = "UTF-8";
900 const char *out_enc;
901 char *enc, *end;
902 struct commit *commit;
903
904 if (get_sha1(use_message, sha1))
905 die("could not lookup commit %s", use_message);
Junio C Hamano8a2f8732008-02-03 00:00:09 -0800906 commit = lookup_commit_reference(sha1);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500907 if (!commit || parse_commit(commit))
908 die("could not parse commit %s", use_message);
909
910 enc = strstr(commit->buffer, "\nencoding");
911 if (enc) {
912 end = strchr(enc + 10, '\n');
913 enc = xstrndup(enc + 10, end - (enc + 10));
914 } else {
915 enc = utf8;
916 }
917 out_enc = git_commit_encoding ? git_commit_encoding : utf8;
918
919 if (strcmp(out_enc, enc))
920 use_message_buffer =
921 reencode_string(commit->buffer, out_enc, enc);
922
923 /*
924 * If we failed to reencode the buffer, just copy it
925 * byte for byte so the user can try to fix it up.
926 * This also handles the case where input and output
927 * encodings are identical.
928 */
929 if (use_message_buffer == NULL)
930 use_message_buffer = xstrdup(commit->buffer);
931 if (enc != utf8)
932 free(enc);
933 }
934
935 if (!!also + !!only + !!all + !!interactive > 1)
936 die("Only one of --include/--only/--all/--interactive can be used.");
937 if (argc == 0 && (also || (only && !amend)))
938 die("No paths with --include/--only does not make sense.");
939 if (argc == 0 && only && amend)
940 only_include_assumed = "Clever... amending the last one with dirty index.";
Johannes Sixt3c5283f2008-04-10 13:33:08 +0200941 if (argc > 0 && !also && !only)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500942 only_include_assumed = "Explicit paths specified without -i nor -o; assuming --only paths...";
Alex Riesen5f065732007-12-22 19:46:24 +0100943 if (!cleanup_arg || !strcmp(cleanup_arg, "default"))
944 cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE;
945 else if (!strcmp(cleanup_arg, "verbatim"))
946 cleanup_mode = CLEANUP_NONE;
947 else if (!strcmp(cleanup_arg, "whitespace"))
948 cleanup_mode = CLEANUP_SPACE;
949 else if (!strcmp(cleanup_arg, "strip"))
950 cleanup_mode = CLEANUP_ALL;
951 else
952 die("Invalid cleanup mode %s", cleanup_arg);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500953
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700954 handle_untracked_files_arg(s);
Marius Storm-Olsen4bfee302008-06-05 10:31:19 +0200955
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500956 if (all && argc > 0)
957 die("Paths with -a does not make sense.");
958 else if (interactive && argc > 0)
959 die("Paths with --interactive does not make sense.");
960
Jeff King7c9f7032009-09-05 04:59:56 -0400961 if (null_termination && status_format == STATUS_FORMAT_LONG)
962 status_format = STATUS_FORMAT_PORCELAIN;
963 if (status_format != STATUS_FORMAT_LONG)
964 dry_run = 1;
965
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500966 return argc;
967}
968
Junio C Hamanod249b092009-08-09 21:59:30 -0700969static int dry_run_commit(int argc, const char **argv, const char *prefix,
970 struct wt_status *s)
Junio C Hamano3a5d13a2009-08-07 23:03:36 -0700971{
972 int commitable;
973 const char *index_file;
974
975 index_file = prepare_index(argc, argv, prefix, 1);
Junio C Hamanod249b092009-08-09 21:59:30 -0700976 commitable = run_status(stdout, index_file, prefix, 0, s);
Junio C Hamano3a5d13a2009-08-07 23:03:36 -0700977 rollback_index_files();
978
979 return commitable ? 0 : 1;
980}
981
Junio C Hamanof766b362009-08-09 23:12:19 -0700982static int parse_status_slot(const char *var, int offset)
983{
984 if (!strcasecmp(var+offset, "header"))
985 return WT_STATUS_HEADER;
986 if (!strcasecmp(var+offset, "updated")
987 || !strcasecmp(var+offset, "added"))
988 return WT_STATUS_UPDATED;
989 if (!strcasecmp(var+offset, "changed"))
990 return WT_STATUS_CHANGED;
991 if (!strcasecmp(var+offset, "untracked"))
992 return WT_STATUS_UNTRACKED;
993 if (!strcasecmp(var+offset, "nobranch"))
994 return WT_STATUS_NOBRANCH;
995 if (!strcasecmp(var+offset, "unmerged"))
996 return WT_STATUS_UNMERGED;
Jeff King8b8e8622009-12-12 07:25:24 -0500997 return -1;
Junio C Hamanof766b362009-08-09 23:12:19 -0700998}
999
1000static int git_status_config(const char *k, const char *v, void *cb)
1001{
1002 struct wt_status *s = cb;
1003
1004 if (!strcmp(k, "status.submodulesummary")) {
1005 int is_bool;
1006 s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
1007 if (is_bool && s->submodule_summary)
1008 s->submodule_summary = -1;
1009 return 0;
1010 }
1011 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
1012 s->use_color = git_config_colorbool(k, v, -1);
1013 return 0;
1014 }
1015 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
1016 int slot = parse_status_slot(k, 13);
Jeff King8b8e8622009-12-12 07:25:24 -05001017 if (slot < 0)
1018 return 0;
Junio C Hamanof766b362009-08-09 23:12:19 -07001019 if (!v)
1020 return config_error_nonbool(k);
1021 color_parse(v, k, s->color_palette[slot]);
1022 return 0;
1023 }
1024 if (!strcmp(k, "status.relativepaths")) {
1025 s->relative_paths = git_config_bool(k, v);
1026 return 0;
1027 }
1028 if (!strcmp(k, "status.showuntrackedfiles")) {
1029 if (!v)
1030 return config_error_nonbool(k);
1031 else if (!strcmp(v, "no"))
1032 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
1033 else if (!strcmp(v, "normal"))
1034 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
1035 else if (!strcmp(v, "all"))
1036 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
1037 else
1038 return error("Invalid untracked files mode '%s'", v);
1039 return 0;
1040 }
1041 return git_diff_ui_config(k, v, NULL);
1042}
1043
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001044int cmd_status(int argc, const char **argv, const char *prefix)
1045{
Junio C Hamanod249b092009-08-09 21:59:30 -07001046 struct wt_status s;
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001047 int fd;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001048 unsigned char sha1[20];
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001049 static struct option builtin_status_options[] = {
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001050 OPT__VERBOSE(&verbose),
Jeff Kingdd2be242009-09-05 04:54:14 -04001051 OPT_SET_INT('s', "short", &status_format,
1052 "show status concisely", STATUS_FORMAT_SHORT),
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001053 OPT_BOOLEAN('b', "branch", &status_show_branch,
1054 "show branch information"),
Jeff King6f157872009-09-05 04:55:37 -04001055 OPT_SET_INT(0, "porcelain", &status_format,
1056 "show porcelain output format",
1057 STATUS_FORMAT_PORCELAIN),
Junio C Hamano173e6c82009-08-04 23:55:22 -07001058 OPT_BOOLEAN('z', "null", &null_termination,
1059 "terminate entries with NUL"),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001060 { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
1061 "mode",
1062 "show untracked files, optional modes: all, normal, no. (Default: all)",
1063 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
Junio C Hamano2381e392010-04-10 00:33:17 -07001064 OPT_BOOLEAN(0, "ignored", &show_ignored_in_status,
1065 "show ignored files"),
Jens Lehmann46a958b2010-06-25 16:56:47 +02001066 { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, "when",
1067 "ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)",
1068 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001069 OPT_END(),
1070 };
1071
Jeff Kingdd2be242009-09-05 04:54:14 -04001072 if (null_termination && status_format == STATUS_FORMAT_LONG)
Jeff King6f157872009-09-05 04:55:37 -04001073 status_format = STATUS_FORMAT_PORCELAIN;
Junio C Hamanod249b092009-08-09 21:59:30 -07001074
1075 wt_status_prepare(&s);
1076 git_config(git_status_config, &s);
Junio C Hamano30988302009-12-11 23:45:24 -08001077 in_merge = file_exists(git_path("MERGE_HEAD"));
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001078 argc = parse_options(argc, argv, prefix,
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001079 builtin_status_options,
1080 builtin_status_usage, 0);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001081 handle_untracked_files_arg(&s);
Junio C Hamano2381e392010-04-10 00:33:17 -07001082 if (show_ignored_in_status)
1083 s.show_ignored_files = 1;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001084 if (*argv)
1085 s.pathspec = get_pathspec(prefix, argv);
1086
Junio C Hamano149794d2010-02-17 12:30:41 -08001087 read_cache_preload(s.pathspec);
Nguyễn Thái Ngọc Duy688cd6d2010-01-14 22:02:21 +07001088 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec, NULL, NULL);
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001089
1090 fd = hold_locked_index(&index_lock, 0);
1091 if (0 <= fd) {
Junio C Hamanod5f5d0a2010-07-06 21:53:11 -07001092 if (active_cache_changed &&
1093 !write_cache(fd, active_cache, active_nr))
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001094 commit_locked_index(&index_lock);
Junio C Hamanod5f5d0a2010-07-06 21:53:11 -07001095 else
1096 rollback_lock_file(&index_lock);
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001097 }
1098
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001099 s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
Junio C Hamano3c588452009-12-11 23:53:41 -08001100 s.in_merge = in_merge;
Jens Lehmann46a958b2010-06-25 16:56:47 +02001101 s.ignore_submodule_arg = ignore_submodule_arg;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001102 wt_status_collect(&s);
1103
Jeff King86617682009-12-07 00:26:25 -05001104 if (s.relative_paths)
1105 s.prefix = prefix;
Junio C Hamanod249b092009-08-09 21:59:30 -07001106 if (s.use_color == -1)
1107 s.use_color = git_use_color_default;
Markus Heidelberg38920dd2009-01-08 19:53:05 +01001108 if (diff_use_color_default == -1)
1109 diff_use_color_default = git_use_color_default;
1110
Jeff Kingdd2be242009-09-05 04:54:14 -04001111 switch (status_format) {
1112 case STATUS_FORMAT_SHORT:
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001113 wt_shortstatus_print(&s, null_termination, status_show_branch);
Jeff Kingdd2be242009-09-05 04:54:14 -04001114 break;
Jeff King6f157872009-09-05 04:55:37 -04001115 case STATUS_FORMAT_PORCELAIN:
Jeff King4a7cc2f2009-12-07 00:17:15 -05001116 wt_porcelain_print(&s, null_termination);
Jeff King6f157872009-09-05 04:55:37 -04001117 break;
Jeff Kingdd2be242009-09-05 04:54:14 -04001118 case STATUS_FORMAT_LONG:
Junio C Hamano173e6c82009-08-04 23:55:22 -07001119 s.verbose = verbose;
Jens Lehmann46a958b2010-06-25 16:56:47 +02001120 s.ignore_submodule_arg = ignore_submodule_arg;
Junio C Hamano173e6c82009-08-04 23:55:22 -07001121 wt_status_print(&s);
Jeff Kingdd2be242009-09-05 04:54:14 -04001122 break;
Junio C Hamano173e6c82009-08-04 23:55:22 -07001123 }
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001124 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001125}
1126
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001127static void print_summary(const char *prefix, const unsigned char *sha1)
1128{
1129 struct rev_info rev;
1130 struct commit *commit;
Jeff King49ff9a72010-01-13 12:39:51 -05001131 struct strbuf format = STRBUF_INIT;
Jeff Kingc85db252008-10-01 18:31:25 -04001132 unsigned char junk_sha1[20];
1133 const char *head = resolve_ref("HEAD", junk_sha1, 0, NULL);
Jeff King49ff9a72010-01-13 12:39:51 -05001134 struct pretty_print_context pctx = {0};
1135 struct strbuf author_ident = STRBUF_INIT;
1136 struct strbuf committer_ident = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001137
1138 commit = lookup_commit(sha1);
1139 if (!commit)
Jeff Kingb5b644a2007-12-02 01:07:03 -05001140 die("couldn't look up newly created commit");
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001141 if (!commit || parse_commit(commit))
1142 die("could not parse newly created commit");
1143
Jeff King49ff9a72010-01-13 12:39:51 -05001144 strbuf_addstr(&format, "format:%h] %s");
1145
1146 format_commit_message(commit, "%an <%ae>", &author_ident, &pctx);
1147 format_commit_message(commit, "%cn <%ce>", &committer_ident, &pctx);
1148 if (strbuf_cmp(&author_ident, &committer_ident)) {
1149 strbuf_addstr(&format, "\n Author: ");
1150 strbuf_addbuf_percentquote(&format, &author_ident);
1151 }
Junio C Hamano1a893062010-01-17 13:59:36 -08001152 if (!user_ident_sufficiently_given()) {
Jeff King49ff9a72010-01-13 12:39:51 -05001153 strbuf_addstr(&format, "\n Committer: ");
1154 strbuf_addbuf_percentquote(&format, &committer_ident);
Jeff Kingb706fcf2010-01-13 15:17:08 -05001155 if (advice_implicit_identity) {
1156 strbuf_addch(&format, '\n');
1157 strbuf_addstr(&format, implicit_ident_advice);
1158 }
Jeff King49ff9a72010-01-13 12:39:51 -05001159 }
1160 strbuf_release(&author_ident);
1161 strbuf_release(&committer_ident);
1162
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001163 init_revisions(&rev, prefix);
1164 setup_revisions(0, NULL, &rev, NULL);
1165
1166 rev.abbrev = 0;
1167 rev.diff = 1;
1168 rev.diffopt.output_format =
1169 DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY;
1170
1171 rev.verbose_header = 1;
1172 rev.show_root_diff = 1;
Jeff King49ff9a72010-01-13 12:39:51 -05001173 get_commit_format(format.buf, &rev);
Junio C Hamanobf82a152007-12-10 21:02:26 -08001174 rev.always_show_header = 0;
Junio C Hamano3eb2a152007-12-16 15:05:39 -08001175 rev.diffopt.detect_rename = 1;
1176 rev.diffopt.rename_limit = 100;
1177 rev.diffopt.break_opt = 0;
Junio C Hamano15964562007-12-16 15:03:58 -08001178 diff_setup_done(&rev.diffopt);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001179
Santi Béjarc5ee71f2009-01-19 23:45:16 +01001180 printf("[%s%s ",
Jeff Kingc85db252008-10-01 18:31:25 -04001181 !prefixcmp(head, "refs/heads/") ?
1182 head + 11 :
1183 !strcmp(head, "HEAD") ?
1184 "detached HEAD" :
1185 head,
1186 initial_commit ? " (root-commit)" : "");
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001187
Junio C Hamanobf82a152007-12-10 21:02:26 -08001188 if (!log_tree_commit(&rev, commit)) {
Tay Ray Chuana45e1a82010-06-12 22:15:39 +08001189 rev.always_show_header = 1;
1190 rev.use_terminator = 1;
1191 log_tree_commit(&rev, commit);
Junio C Hamanobf82a152007-12-10 21:02:26 -08001192 }
Tay Ray Chuana45e1a82010-06-12 22:15:39 +08001193
Junio C Hamanofc6f19f2010-01-17 00:57:51 -08001194 strbuf_release(&format);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001195}
1196
Stephan Beyer186458b2008-07-24 01:09:35 +02001197static int git_commit_config(const char *k, const char *v, void *cb)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001198{
Junio C Hamanod249b092009-08-09 21:59:30 -07001199 struct wt_status *s = cb;
1200
Brian Hetro984c6e72008-07-05 01:24:40 -04001201 if (!strcmp(k, "commit.template"))
Matthieu Moy395de252009-11-17 18:24:25 +01001202 return git_config_pathname(&template_file, k, v);
James P. Howard, IIbed575e2009-12-07 17:45:27 -05001203 if (!strcmp(k, "commit.status")) {
1204 include_status = git_config_bool(k, v);
1205 return 0;
1206 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001207
Junio C Hamanod249b092009-08-09 21:59:30 -07001208 return git_status_config(k, v, s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001209}
1210
Thomas Rast6f6bee32010-03-12 18:04:28 +01001211static const char post_rewrite_hook[] = "hooks/post-rewrite";
1212
1213static int run_rewrite_hook(const unsigned char *oldsha1,
1214 const unsigned char *newsha1)
1215{
1216 /* oldsha1 SP newsha1 LF NUL */
1217 static char buf[2*40 + 3];
1218 struct child_process proc;
1219 const char *argv[3];
1220 int code;
1221 size_t n;
1222
1223 if (access(git_path(post_rewrite_hook), X_OK) < 0)
1224 return 0;
1225
1226 argv[0] = git_path(post_rewrite_hook);
1227 argv[1] = "amend";
1228 argv[2] = NULL;
1229
1230 memset(&proc, 0, sizeof(proc));
1231 proc.argv = argv;
1232 proc.in = -1;
1233 proc.stdout_to_stderr = 1;
1234
1235 code = start_command(&proc);
1236 if (code)
1237 return code;
1238 n = snprintf(buf, sizeof(buf), "%s %s\n",
1239 sha1_to_hex(oldsha1), sha1_to_hex(newsha1));
1240 write_in_full(proc.in, buf, n);
1241 close(proc.in);
1242 return finish_command(&proc);
1243}
1244
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001245int cmd_commit(int argc, const char **argv, const char *prefix)
1246{
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001247 struct strbuf sb = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001248 const char *index_file, *reflog_msg;
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001249 char *nl, *p;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001250 unsigned char commit_sha1[20];
1251 struct ref_lock *ref_lock;
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001252 struct commit_list *parents = NULL, **pptr = &parents;
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001253 struct stat statbuf;
1254 int allow_fast_forward = 1;
Junio C Hamanod249b092009-08-09 21:59:30 -07001255 struct wt_status s;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001256
Junio C Hamanod249b092009-08-09 21:59:30 -07001257 wt_status_prepare(&s);
1258 git_config(git_commit_config, &s);
Junio C Hamano30988302009-12-11 23:45:24 -08001259 in_merge = file_exists(git_path("MERGE_HEAD"));
Junio C Hamano3c588452009-12-11 23:53:41 -08001260 s.in_merge = in_merge;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001261
Junio C Hamanod249b092009-08-09 21:59:30 -07001262 if (s.use_color == -1)
1263 s.use_color = git_use_color_default;
Junio C Hamanod249b092009-08-09 21:59:30 -07001264 argc = parse_and_validate_options(argc, argv, builtin_commit_usage,
1265 prefix, &s);
Junio C Hamano3fa509d2009-08-15 02:14:14 -07001266 if (dry_run) {
1267 if (diff_use_color_default == -1)
1268 diff_use_color_default = git_use_color_default;
Junio C Hamanod249b092009-08-09 21:59:30 -07001269 return dry_run_commit(argc, argv, prefix, &s);
Junio C Hamano3fa509d2009-08-15 02:14:14 -07001270 }
Junio C Hamano50b7e702009-08-04 23:49:33 -07001271 index_file = prepare_index(argc, argv, prefix, 0);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001272
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001273 /* Set up everything for writing the commit object. This includes
1274 running hooks, writing the trees, and interacting with the user. */
Junio C Hamanod249b092009-08-09 21:59:30 -07001275 if (!prepare_to_commit(index_file, prefix, &s)) {
Junio C Hamano28886052007-11-18 01:52:55 -08001276 rollback_index_files();
1277 return 1;
1278 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001279
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001280 /* Determine parents */
Christian Couder643cb5f2010-06-12 18:05:12 +02001281 reflog_msg = getenv("GIT_REFLOG_ACTION");
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001282 if (initial_commit) {
Christian Couder643cb5f2010-06-12 18:05:12 +02001283 if (!reflog_msg)
1284 reflog_msg = "commit (initial)";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001285 } else if (amend) {
1286 struct commit_list *c;
1287 struct commit *commit;
1288
Christian Couder643cb5f2010-06-12 18:05:12 +02001289 if (!reflog_msg)
1290 reflog_msg = "commit (amend)";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001291 commit = lookup_commit(head_sha1);
1292 if (!commit || parse_commit(commit))
1293 die("could not parse HEAD commit");
1294
1295 for (c = commit->parents; c; c = c->next)
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001296 pptr = &commit_list_insert(c->item, pptr)->next;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001297 } else if (in_merge) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001298 struct strbuf m = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001299 FILE *fp;
1300
Christian Couder643cb5f2010-06-12 18:05:12 +02001301 if (!reflog_msg)
1302 reflog_msg = "commit (merge)";
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001303 pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001304 fp = fopen(git_path("MERGE_HEAD"), "r");
1305 if (fp == NULL)
Thomas Rastd824cbb2009-06-27 17:58:46 +02001306 die_errno("could not open '%s' for reading",
1307 git_path("MERGE_HEAD"));
Linus Torvalds7c3fd252008-01-15 16:12:33 -08001308 while (strbuf_getline(&m, fp, '\n') != EOF) {
1309 unsigned char sha1[20];
1310 if (get_sha1_hex(m.buf, sha1) < 0)
1311 die("Corrupt MERGE_HEAD file (%s)", m.buf);
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001312 pptr = &commit_list_insert(lookup_commit(sha1), pptr)->next;
Linus Torvalds7c3fd252008-01-15 16:12:33 -08001313 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001314 fclose(fp);
1315 strbuf_release(&m);
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001316 if (!stat(git_path("MERGE_MODE"), &statbuf)) {
1317 if (strbuf_read_file(&sb, git_path("MERGE_MODE"), 0) < 0)
Thomas Rastd824cbb2009-06-27 17:58:46 +02001318 die_errno("could not read MERGE_MODE");
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001319 if (!strcmp(sb.buf, "no-ff"))
1320 allow_fast_forward = 0;
1321 }
1322 if (allow_fast_forward)
1323 parents = reduce_heads(parents);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001324 } else {
Christian Couder643cb5f2010-06-12 18:05:12 +02001325 if (!reflog_msg)
1326 reflog_msg = "commit";
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001327 pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001328 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001329
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001330 /* Finally, get the commit message */
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001331 strbuf_reset(&sb);
Junio C Hamano740001a2007-12-08 23:23:20 -08001332 if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
Thomas Rast0721c312009-06-27 17:58:47 +02001333 int saved_errno = errno;
Junio C Hamano740001a2007-12-08 23:23:20 -08001334 rollback_index_files();
Thomas Rast0721c312009-06-27 17:58:47 +02001335 die("could not read commit message: %s", strerror(saved_errno));
Junio C Hamano740001a2007-12-08 23:23:20 -08001336 }
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001337
1338 /* Truncate the message just before the diff, if any. */
Jeff King0b382272008-11-12 03:25:52 -05001339 if (verbose) {
1340 p = strstr(sb.buf, "\ndiff --git ");
1341 if (p != NULL)
1342 strbuf_setlen(&sb, p - sb.buf + 1);
1343 }
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001344
Alex Riesen5f065732007-12-22 19:46:24 +01001345 if (cleanup_mode != CLEANUP_NONE)
1346 stripspace(&sb, cleanup_mode == CLEANUP_ALL);
Ævar Arnfjörð Bjarmasonc9b5fde2010-04-06 08:40:35 +00001347 if (message_is_empty(&sb) && !allow_empty_message) {
Junio C Hamano28886052007-11-18 01:52:55 -08001348 rollback_index_files();
Jeff Kingfdc7c812008-07-31 03:36:09 -04001349 fprintf(stderr, "Aborting commit due to empty commit message.\n");
1350 exit(1);
Junio C Hamano28886052007-11-18 01:52:55 -08001351 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001352
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001353 if (commit_tree(sb.buf, active_cache_tree->sha1, parents, commit_sha1,
1354 fmt_ident(author_name, author_email, author_date,
1355 IDENT_ERROR_ON_NO_NAME))) {
Junio C Hamano28886052007-11-18 01:52:55 -08001356 rollback_index_files();
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001357 die("failed to write commit object");
Junio C Hamano28886052007-11-18 01:52:55 -08001358 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001359
1360 ref_lock = lock_any_ref_for_update("HEAD",
1361 initial_commit ? NULL : head_sha1,
1362 0);
1363
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001364 nl = strchr(sb.buf, '\n');
Johannes Schindelin741707b2007-11-08 12:15:26 +00001365 if (nl)
1366 strbuf_setlen(&sb, nl + 1 - sb.buf);
1367 else
1368 strbuf_addch(&sb, '\n');
Johannes Schindelin741707b2007-11-08 12:15:26 +00001369 strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
1370 strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001371
Junio C Hamano28886052007-11-18 01:52:55 -08001372 if (!ref_lock) {
1373 rollback_index_files();
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001374 die("cannot lock HEAD ref");
Junio C Hamano28886052007-11-18 01:52:55 -08001375 }
1376 if (write_ref_sha1(ref_lock, commit_sha1, sb.buf) < 0) {
1377 rollback_index_files();
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001378 die("cannot update HEAD ref");
Junio C Hamano28886052007-11-18 01:52:55 -08001379 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001380
1381 unlink(git_path("MERGE_HEAD"));
1382 unlink(git_path("MERGE_MSG"));
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001383 unlink(git_path("MERGE_MODE"));
Gerrit Pape5a95b852008-02-08 09:53:58 +00001384 unlink(git_path("SQUASH_MSG"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001385
Brandon Casey5a9dd392008-01-23 11:21:22 -06001386 if (commit_index_files())
1387 die ("Repository has been updated, but unable to write\n"
1388 "new_index file. Check that disk is not full or quota is\n"
1389 "not exceeded, and then \"git reset HEAD\" to recover.");
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001390
Junio C Hamanocb6020b2009-12-04 00:20:48 -08001391 rerere(0);
Junio C Hamano28886052007-11-18 01:52:55 -08001392 run_hook(get_index_file(), "post-commit", NULL);
Thomas Rast6f6bee32010-03-12 18:04:28 +01001393 if (amend && !no_post_rewrite) {
Thomas Rast6360d342010-03-12 18:04:34 +01001394 struct notes_rewrite_cfg *cfg;
1395 cfg = init_copy_notes_for_rewrite("amend");
1396 if (cfg) {
1397 copy_note_for_rewrite(cfg, head_sha1, commit_sha1);
1398 finish_copy_notes_for_rewrite(cfg);
1399 }
Thomas Rast6f6bee32010-03-12 18:04:28 +01001400 run_rewrite_hook(head_sha1, commit_sha1);
1401 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001402 if (!quiet)
1403 print_summary(prefix, commit_sha1);
1404
1405 return 0;
1406}