blob: f4c73442cfba9483a826dcfbf68f5466d43e8351 [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
Junio C Hamano30988302009-12-11 23:45:24 -080051static unsigned char head_sha1[20];
Jeff King49ff9a72010-01-13 12:39:51 -050052
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050053static char *use_message_buffer;
54static const char commit_editmsg[] = "COMMIT_EDITMSG";
Junio C Hamano28886052007-11-18 01:52:55 -080055static struct lock_file index_lock; /* real index */
56static struct lock_file false_lock; /* used only for partial commits */
57static enum {
58 COMMIT_AS_IS = 1,
59 COMMIT_NORMAL,
60 COMMIT_PARTIAL,
61} commit_style;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050062
Junio C Hamanodbd0f5c2008-08-06 11:43:47 -070063static const char *logfile, *force_author;
Brian Hetro984c6e72008-07-05 01:24:40 -040064static const char *template_file;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050065static char *edit_message, *use_message;
Santi Béjare83dbe82008-05-04 18:04:50 +020066static char *author_name, *author_email, *author_date;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050067static int all, edit_flag, also, interactive, only, amend, signoff;
Erick Mattosc51f6ce2009-11-04 01:20:11 -020068static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
Miklos Vajna02b47cd2009-12-02 23:16:18 +010069static char *untracked_files_arg, *force_date;
Alex Riesen5f065732007-12-22 19:46:24 +010070/*
71 * The default commit message cleanup mode will remove the lines
72 * beginning with # (shell comments) and leading and trailing
73 * whitespaces (empty lines or containing only whitespaces)
74 * if editor is used, and only the whitespaces if the message
75 * is specified explicitly.
76 */
77static enum {
78 CLEANUP_SPACE,
79 CLEANUP_NONE,
80 CLEANUP_ALL,
81} cleanup_mode;
82static char *cleanup_arg;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050083
James P. Howard, IIbed575e2009-12-07 17:45:27 -050084static int use_editor = 1, initial_commit, in_merge, include_status = 1;
Johannes Schindelin3b6aeb32008-07-22 21:40:41 +010085static const char *only_include_assumed;
86static struct strbuf message;
Johannes Schindelinf9568532007-11-11 17:36:39 +000087
Jeff King7c9f7032009-09-05 04:59:56 -040088static int null_termination;
89static enum {
90 STATUS_FORMAT_LONG,
91 STATUS_FORMAT_SHORT,
92 STATUS_FORMAT_PORCELAIN,
93} status_format = STATUS_FORMAT_LONG;
94
Johannes Schindelinf9568532007-11-11 17:36:39 +000095static int opt_parse_m(const struct option *opt, const char *arg, int unset)
96{
97 struct strbuf *buf = opt->value;
98 if (unset)
99 strbuf_setlen(buf, 0);
100 else {
101 strbuf_addstr(buf, arg);
Johannes Schindelin3b6aeb32008-07-22 21:40:41 +0100102 strbuf_addstr(buf, "\n\n");
Johannes Schindelinf9568532007-11-11 17:36:39 +0000103 }
104 return 0;
105}
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500106
107static struct option builtin_commit_options[] = {
108 OPT__QUIET(&quiet),
109 OPT__VERBOSE(&verbose),
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500110
Greg Pricee97ca7f2009-12-29 16:54:49 -0500111 OPT_GROUP("Commit message options"),
Stephen Boyddf217ed2009-05-23 11:53:13 -0700112 OPT_FILENAME('F', "file", &logfile, "read log from file"),
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500113 OPT_STRING(0, "author", &force_author, "AUTHOR", "override author for commit"),
Miklos Vajna02b47cd2009-12-02 23:16:18 +0100114 OPT_STRING(0, "date", &force_date, "DATE", "override date for commit"),
Johannes Schindelinf9568532007-11-11 17:36:39 +0000115 OPT_CALLBACK('m', "message", &message, "MESSAGE", "specify commit message", opt_parse_m),
Erick Mattosc51f6ce2009-11-04 01:20:11 -0200116 OPT_STRING('c', "reedit-message", &edit_message, "COMMIT", "reuse and edit message from specified commit"),
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500117 OPT_STRING('C', "reuse-message", &use_message, "COMMIT", "reuse message from specified commit"),
Erick Mattosc51f6ce2009-11-04 01:20:11 -0200118 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 -0500119 OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"),
Stephen Boyddf217ed2009-05-23 11:53:13 -0700120 OPT_FILENAME('t', "template", &template_file, "use specified template file"),
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500121 OPT_BOOLEAN('e', "edit", &edit_flag, "force edit of commit"),
Greg Pricee97ca7f2009-12-29 16:54:49 -0500122 OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"),
James P. Howard, IIbed575e2009-12-07 17:45:27 -0500123 OPT_BOOLEAN(0, "status", &include_status, "include status in commit message template"),
Greg Pricee97ca7f2009-12-29 16:54:49 -0500124 /* end commit message options */
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500125
126 OPT_GROUP("Commit contents options"),
127 OPT_BOOLEAN('a', "all", &all, "commit all changed files"),
128 OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"),
129 OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"),
Johannes Sixtd4ba07c2008-04-10 13:33:09 +0200130 OPT_BOOLEAN('o', "only", &only, "commit only specified files"),
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500131 OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"),
Junio C Hamano3a5d13a2009-08-07 23:03:36 -0700132 OPT_BOOLEAN(0, "dry-run", &dry_run, "show what would be committed"),
Jeff King7c9f7032009-09-05 04:59:56 -0400133 OPT_SET_INT(0, "short", &status_format, "show status concisely",
134 STATUS_FORMAT_SHORT),
135 OPT_SET_INT(0, "porcelain", &status_format,
136 "show porcelain output format", STATUS_FORMAT_PORCELAIN),
137 OPT_BOOLEAN('z', "null", &null_termination,
138 "terminate entries with NUL"),
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500139 OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
Marius Storm-Olsen6c2ce042008-06-05 14:22:56 +0200140 { 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" },
Junio C Hamano5241b6b2007-12-03 00:03:10 -0800141 OPT_BOOLEAN(0, "allow-empty", &allow_empty, "ok to record an empty change"),
Greg Pricee97ca7f2009-12-29 16:54:49 -0500142 /* end commit contents options */
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500143
144 OPT_END()
145};
146
Junio C Hamano28886052007-11-18 01:52:55 -0800147static void rollback_index_files(void)
148{
149 switch (commit_style) {
150 case COMMIT_AS_IS:
151 break; /* nothing to do */
152 case COMMIT_NORMAL:
153 rollback_lock_file(&index_lock);
154 break;
155 case COMMIT_PARTIAL:
156 rollback_lock_file(&index_lock);
157 rollback_lock_file(&false_lock);
158 break;
159 }
160}
161
Brandon Casey5a9dd392008-01-23 11:21:22 -0600162static int commit_index_files(void)
Junio C Hamano28886052007-11-18 01:52:55 -0800163{
Brandon Casey5a9dd392008-01-23 11:21:22 -0600164 int err = 0;
165
Junio C Hamano28886052007-11-18 01:52:55 -0800166 switch (commit_style) {
167 case COMMIT_AS_IS:
168 break; /* nothing to do */
169 case COMMIT_NORMAL:
Brandon Casey5a9dd392008-01-23 11:21:22 -0600170 err = commit_lock_file(&index_lock);
Junio C Hamano28886052007-11-18 01:52:55 -0800171 break;
172 case COMMIT_PARTIAL:
Brandon Casey5a9dd392008-01-23 11:21:22 -0600173 err = commit_lock_file(&index_lock);
Junio C Hamano28886052007-11-18 01:52:55 -0800174 rollback_lock_file(&false_lock);
175 break;
176 }
Brandon Casey5a9dd392008-01-23 11:21:22 -0600177
178 return err;
Junio C Hamano28886052007-11-18 01:52:55 -0800179}
180
181/*
182 * Take a union of paths in the index and the named tree (typically, "HEAD"),
183 * and return the paths that match the given pattern in list.
184 */
Johannes Schindelinc455c872008-07-21 19:03:49 +0100185static int list_paths(struct string_list *list, const char *with_tree,
Junio C Hamano28886052007-11-18 01:52:55 -0800186 const char *prefix, const char **pattern)
187{
188 int i;
189 char *m;
190
191 for (i = 0; pattern[i]; i++)
192 ;
193 m = xcalloc(1, i);
194
195 if (with_tree)
196 overlay_tree_on_cache(with_tree, prefix);
197
198 for (i = 0; i < active_nr; i++) {
199 struct cache_entry *ce = active_cache[i];
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700200 struct string_list_item *item;
201
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800202 if (ce->ce_flags & CE_UPDATE)
Junio C Hamanoe87e22d2008-01-14 13:54:24 -0800203 continue;
Clemens Buchacher0b509222009-01-14 15:54:35 +0100204 if (!match_pathspec(pattern, ce->name, ce_namelen(ce), 0, m))
Junio C Hamano28886052007-11-18 01:52:55 -0800205 continue;
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700206 item = string_list_insert(ce->name, list);
207 if (ce_skip_worktree(ce))
208 item->util = item; /* better a valid pointer than a fake one */
Junio C Hamano28886052007-11-18 01:52:55 -0800209 }
210
211 return report_path_error(m, pattern, prefix ? strlen(prefix) : 0);
212}
213
Johannes Schindelinc455c872008-07-21 19:03:49 +0100214static void add_remove_files(struct string_list *list)
Junio C Hamano28886052007-11-18 01:52:55 -0800215{
216 int i;
217 for (i = 0; i < list->nr; i++) {
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700218 struct stat st;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100219 struct string_list_item *p = &(list->items[i]);
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700220
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700221 /* p->util is skip-worktree */
222 if (p->util)
Nguyễn Thái Ngọc Duyb4d16902009-08-20 20:46:58 +0700223 continue;
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700224
Johannes Schindelinc455c872008-07-21 19:03:49 +0100225 if (!lstat(p->string, &st)) {
226 if (add_to_cache(p->string, &st, 0))
Alex Riesen960b8ad2008-05-12 19:57:45 +0200227 die("updating files failed");
228 } else
Johannes Schindelinc455c872008-07-21 19:03:49 +0100229 remove_file_from_cache(p->string);
Junio C Hamano28886052007-11-18 01:52:55 -0800230 }
231}
232
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800233static void create_base_index(void)
234{
235 struct tree *tree;
236 struct unpack_trees_options opts;
237 struct tree_desc t;
238
239 if (initial_commit) {
240 discard_cache();
241 return;
242 }
243
244 memset(&opts, 0, sizeof(opts));
245 opts.head_idx = 1;
246 opts.index_only = 1;
247 opts.merge = 1;
Linus Torvalds34110cd2008-03-06 18:12:28 -0800248 opts.src_index = &the_index;
249 opts.dst_index = &the_index;
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800250
251 opts.fn = oneway_merge;
252 tree = parse_tree_indirect(head_sha1);
253 if (!tree)
254 die("failed to unpack HEAD tree object");
255 parse_tree(tree);
256 init_tree_desc(&t, tree->buffer, tree->size);
Daniel Barkalow203a2fe2008-02-07 11:39:48 -0500257 if (unpack_trees(1, &t, &opts))
258 exit(128); /* We've already reported the error, finish dying */
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800259}
260
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100261static void refresh_cache_or_die(int refresh_flags)
262{
263 /*
264 * refresh_flags contains REFRESH_QUIET, so the only errors
265 * are for unmerged entries.
266 */
267 if (refresh_cache(refresh_flags | REFRESH_IN_PORCELAIN))
268 die_resolve_conflict("commit");
269}
270
Junio C Hamano50b7e702009-08-04 23:49:33 -0700271static char *prepare_index(int argc, const char **argv, const char *prefix, int is_status)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500272{
273 int fd;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100274 struct string_list partial;
Junio C Hamano28886052007-11-18 01:52:55 -0800275 const char **pathspec = NULL;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700276 int refresh_flags = REFRESH_QUIET;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500277
Junio C Hamano50b7e702009-08-04 23:49:33 -0700278 if (is_status)
279 refresh_flags |= REFRESH_UNMERGED;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500280 if (interactive) {
Jeff King4f6a32f2009-04-03 15:28:56 -0400281 if (interactive_add(argc, argv, prefix) != 0)
282 die("interactive add failed");
Linus Torvalds671c9b72008-11-13 16:36:30 -0800283 if (read_cache_preload(NULL) < 0)
Gerrit Paped5350fd2008-05-27 08:59:16 +0000284 die("index file corrupt");
Junio C Hamano28886052007-11-18 01:52:55 -0800285 commit_style = COMMIT_AS_IS;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500286 return get_index_file();
287 }
288
Junio C Hamanof64fe7b2007-11-25 08:46:29 -0800289 if (*argv)
290 pathspec = get_pathspec(prefix, argv);
Junio C Hamano28886052007-11-18 01:52:55 -0800291
Linus Torvalds671c9b72008-11-13 16:36:30 -0800292 if (read_cache_preload(pathspec) < 0)
293 die("index file corrupt");
294
Junio C Hamano28886052007-11-18 01:52:55 -0800295 /*
296 * Non partial, non as-is commit.
297 *
298 * (1) get the real index;
299 * (2) update the_index as necessary;
300 * (3) write the_index out to the real index (still locked);
301 * (4) return the name of the locked index file.
302 *
303 * The caller should run hooks on the locked real index, and
304 * (A) if all goes well, commit the real index;
305 * (B) on failure, rollback the real index.
306 */
307 if (all || (also && pathspec && *pathspec)) {
308 int fd = hold_locked_index(&index_lock, 1);
Alex Riesen7ae02a32008-05-12 19:58:10 +0200309 add_files_to_cache(also ? prefix : NULL, pathspec, 0);
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100310 refresh_cache_or_die(refresh_flags);
Brandon Casey4ed7cd32008-01-16 13:12:46 -0600311 if (write_cache(fd, active_cache, active_nr) ||
312 close_lock_file(&index_lock))
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500313 die("unable to write new_index file");
Junio C Hamano28886052007-11-18 01:52:55 -0800314 commit_style = COMMIT_NORMAL;
315 return index_lock.filename;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500316 }
317
Junio C Hamano28886052007-11-18 01:52:55 -0800318 /*
319 * As-is commit.
320 *
321 * (1) return the name of the real index file.
322 *
323 * The caller should run hooks on the real index, and run
324 * hooks on the real index, and create commit from the_index.
325 * We still need to refresh the index here.
326 */
327 if (!pathspec || !*pathspec) {
328 fd = hold_locked_index(&index_lock, 1);
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100329 refresh_cache_or_die(refresh_flags);
Junio C Hamano28886052007-11-18 01:52:55 -0800330 if (write_cache(fd, active_cache, active_nr) ||
Kristian Høgsberg44397512008-01-15 15:00:02 -0500331 commit_locked_index(&index_lock))
Junio C Hamano28886052007-11-18 01:52:55 -0800332 die("unable to write new_index file");
333 commit_style = COMMIT_AS_IS;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500334 return get_index_file();
335 }
336
Junio C Hamano28886052007-11-18 01:52:55 -0800337 /*
338 * A partial commit.
339 *
340 * (0) find the set of affected paths;
341 * (1) get lock on the real index file;
342 * (2) update the_index with the given paths;
343 * (3) write the_index out to the real index (still locked);
344 * (4) get lock on the false index file;
345 * (5) reset the_index from HEAD;
346 * (6) update the_index the same way as (2);
347 * (7) write the_index out to the false index file;
348 * (8) return the name of the false index file (still locked);
349 *
350 * The caller should run hooks on the locked false index, and
351 * create commit from it. Then
352 * (A) if all goes well, commit the real index;
353 * (B) on failure, rollback the real index;
354 * In either case, rollback the false index.
355 */
356 commit_style = COMMIT_PARTIAL;
357
Junio C Hamano30988302009-12-11 23:45:24 -0800358 if (in_merge)
Junio C Hamano28886052007-11-18 01:52:55 -0800359 die("cannot do a partial commit during a merge.");
360
361 memset(&partial, 0, sizeof(partial));
Johannes Schindelinc455c872008-07-21 19:03:49 +0100362 partial.strdup_strings = 1;
Junio C Hamano28886052007-11-18 01:52:55 -0800363 if (list_paths(&partial, initial_commit ? NULL : "HEAD", prefix, pathspec))
364 exit(1);
365
366 discard_cache();
367 if (read_cache() < 0)
368 die("cannot read the index");
369
370 fd = hold_locked_index(&index_lock, 1);
371 add_remove_files(&partial);
Kristian Høgsbergef12b502007-11-12 15:48:22 -0500372 refresh_cache(REFRESH_QUIET);
Brandon Casey4ed7cd32008-01-16 13:12:46 -0600373 if (write_cache(fd, active_cache, active_nr) ||
374 close_lock_file(&index_lock))
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500375 die("unable to write new_index file");
376
Junio C Hamano28886052007-11-18 01:52:55 -0800377 fd = hold_lock_file_for_update(&false_lock,
Junio C Hamanoa1574002008-10-21 17:58:11 -0700378 git_path("next-index-%"PRIuMAX,
379 (uintmax_t) getpid()),
Junio C Hamanoacd3b9e2008-10-17 15:44:39 -0700380 LOCK_DIE_ON_ERROR);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800381
382 create_base_index();
Junio C Hamano28886052007-11-18 01:52:55 -0800383 add_remove_files(&partial);
Kristian Høgsbergd37d3202007-11-09 11:40:27 -0500384 refresh_cache(REFRESH_QUIET);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500385
Brandon Casey4ed7cd32008-01-16 13:12:46 -0600386 if (write_cache(fd, active_cache, active_nr) ||
387 close_lock_file(&false_lock))
Junio C Hamano28886052007-11-18 01:52:55 -0800388 die("unable to write temporary index file");
Jeff King959ba672008-02-14 12:18:23 -0500389
390 discard_cache();
391 read_cache_from(false_lock.filename);
392
Junio C Hamano28886052007-11-18 01:52:55 -0800393 return false_lock.filename;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500394}
395
Junio C Hamanod249b092009-08-09 21:59:30 -0700396static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
397 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500398{
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700399 unsigned char sha1[20];
400
Junio C Hamanod249b092009-08-09 21:59:30 -0700401 if (s->relative_paths)
402 s->prefix = prefix;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500403
404 if (amend) {
Junio C Hamanod249b092009-08-09 21:59:30 -0700405 s->amend = 1;
406 s->reference = "HEAD^1";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500407 }
Junio C Hamanod249b092009-08-09 21:59:30 -0700408 s->verbose = verbose;
409 s->index_file = index_file;
410 s->fp = fp;
411 s->nowarn = nowarn;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700412 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500413
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700414 wt_status_collect(s);
Jeff King7c9f7032009-09-05 04:59:56 -0400415
416 switch (status_format) {
417 case STATUS_FORMAT_SHORT:
Michael J Gruber84dbe7b2009-12-05 16:04:37 +0100418 wt_shortstatus_print(s, null_termination);
Jeff King7c9f7032009-09-05 04:59:56 -0400419 break;
420 case STATUS_FORMAT_PORCELAIN:
Jeff King4a7cc2f2009-12-07 00:17:15 -0500421 wt_porcelain_print(s, null_termination);
Jeff King7c9f7032009-09-05 04:59:56 -0400422 break;
423 case STATUS_FORMAT_LONG:
424 wt_status_print(s);
425 break;
426 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500427
Junio C Hamanod249b092009-08-09 21:59:30 -0700428 return s->commitable;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500429}
430
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100431static int is_a_merge(const unsigned char *sha1)
432{
433 struct commit *commit = lookup_commit(sha1);
434 if (!commit || parse_commit(commit))
435 die("could not parse HEAD commit");
436 return !!(commit->parents && commit->parents->next);
437}
438
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500439static const char sign_off_header[] = "Signed-off-by: ";
440
Santi Béjare83dbe82008-05-04 18:04:50 +0200441static void determine_author_info(void)
Santi Béjara45d46b2008-05-04 18:04:49 +0200442{
443 char *name, *email, *date;
444
445 name = getenv("GIT_AUTHOR_NAME");
446 email = getenv("GIT_AUTHOR_EMAIL");
447 date = getenv("GIT_AUTHOR_DATE");
448
Erick Mattosc51f6ce2009-11-04 01:20:11 -0200449 if (use_message && !renew_authorship) {
Santi Béjara45d46b2008-05-04 18:04:49 +0200450 const char *a, *lb, *rb, *eol;
451
452 a = strstr(use_message_buffer, "\nauthor ");
453 if (!a)
454 die("invalid commit: %s", use_message);
455
456 lb = strstr(a + 8, " <");
457 rb = strstr(a + 8, "> ");
458 eol = strchr(a + 8, '\n');
459 if (!lb || !rb || !eol)
460 die("invalid commit: %s", use_message);
461
462 name = xstrndup(a + 8, lb - (a + 8));
463 email = xstrndup(lb + 2, rb - (lb + 2));
464 date = xstrndup(rb + 2, eol - (rb + 2));
465 }
466
467 if (force_author) {
468 const char *lb = strstr(force_author, " <");
469 const char *rb = strchr(force_author, '>');
470
471 if (!lb || !rb)
472 die("malformed --author parameter");
473 name = xstrndup(force_author, lb - force_author);
474 email = xstrndup(lb + 2, rb - (lb + 2));
475 }
476
Miklos Vajna02b47cd2009-12-02 23:16:18 +0100477 if (force_date)
478 date = force_date;
479
Santi Béjare83dbe82008-05-04 18:04:50 +0200480 author_name = name;
481 author_email = email;
482 author_date = date;
Santi Béjara45d46b2008-05-04 18:04:49 +0200483}
484
David Brownc1e01b02009-10-28 10:13:44 -0700485static int ends_rfc2822_footer(struct strbuf *sb)
486{
487 int ch;
488 int hit = 0;
489 int i, j, k;
490 int len = sb->len;
491 int first = 1;
492 const char *buf = sb->buf;
493
494 for (i = len - 1; i > 0; i--) {
495 if (hit && buf[i] == '\n')
496 break;
497 hit = (buf[i] == '\n');
498 }
499
500 while (i < len - 1 && buf[i] == '\n')
501 i++;
502
503 for (; i < len; i = k) {
504 for (k = i; k < len && buf[k] != '\n'; k++)
505 ; /* do nothing */
506 k++;
507
508 if ((buf[k] == ' ' || buf[k] == '\t') && !first)
509 continue;
510
511 first = 0;
512
513 for (j = 0; i + j < len; j++) {
514 ch = buf[i + j];
515 if (ch == ':')
516 break;
517 if (isalnum(ch) ||
518 (ch == '-'))
519 continue;
520 return 0;
521 }
522 }
523 return 1;
524}
525
Junio C Hamanod249b092009-08-09 21:59:30 -0700526static int prepare_to_commit(const char *index_file, const char *prefix,
527 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500528{
529 struct stat statbuf;
Junio C Hamanobc5d2482007-11-18 12:01:38 -0800530 int commitable, saved_color_setting;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500531 struct strbuf sb = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500532 char *buffer;
533 FILE *fp;
Paolo Bonzini8089c852008-02-05 08:04:18 +0100534 const char *hook_arg1 = NULL;
535 const char *hook_arg2 = NULL;
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200536 int ident_shown = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500537
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100538 if (!no_verify && run_hook(index_file, "pre-commit", NULL))
539 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500540
Johannes Schindelinf9568532007-11-11 17:36:39 +0000541 if (message.len) {
542 strbuf_addbuf(&sb, &message);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100543 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500544 } else if (logfile && !strcmp(logfile, "-")) {
545 if (isatty(0))
546 fprintf(stderr, "(reading log message from standard input)\n");
547 if (strbuf_read(&sb, 0, 0) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +0200548 die_errno("could not read log from standard input");
Paolo Bonzini8089c852008-02-05 08:04:18 +0100549 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500550 } else if (logfile) {
551 if (strbuf_read_file(&sb, logfile, 0) < 0)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200552 die_errno("could not read log file '%s'",
553 logfile);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100554 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500555 } else if (use_message) {
556 buffer = strstr(use_message_buffer, "\n\n");
557 if (!buffer || buffer[2] == '\0')
558 die("commit has empty message");
559 strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100560 hook_arg1 = "commit";
561 hook_arg2 = use_message;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500562 } else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
563 if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200564 die_errno("could not read MERGE_MSG");
Paolo Bonzini8089c852008-02-05 08:04:18 +0100565 hook_arg1 = "merge";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500566 } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
567 if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200568 die_errno("could not read SQUASH_MSG");
Paolo Bonzini8089c852008-02-05 08:04:18 +0100569 hook_arg1 = "squash";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500570 } else if (template_file && !stat(template_file, &statbuf)) {
571 if (strbuf_read_file(&sb, template_file, 0) < 0)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200572 die_errno("could not read '%s'", template_file);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100573 hook_arg1 = "template";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500574 }
575
Paolo Bonzini8089c852008-02-05 08:04:18 +0100576 /*
577 * This final case does not modify the template message,
578 * it just sets the argument to the prepare-commit-msg hook.
579 */
580 else if (in_merge)
581 hook_arg1 = "merge";
582
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500583 fp = fopen(git_path(commit_editmsg), "w");
584 if (fp == NULL)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200585 die_errno("could not open '%s'", git_path(commit_editmsg));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500586
Alex Riesen5f065732007-12-22 19:46:24 +0100587 if (cleanup_mode != CLEANUP_NONE)
588 stripspace(&sb, 0);
Johannes Schindelin13208572007-11-11 17:35:58 +0000589
590 if (signoff) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500591 struct strbuf sob = STRBUF_INIT;
Johannes Schindelin13208572007-11-11 17:35:58 +0000592 int i;
593
Johannes Schindelin13208572007-11-11 17:35:58 +0000594 strbuf_addstr(&sob, sign_off_header);
Junio C Hamanod9ccfe72007-12-02 13:43:34 -0800595 strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"),
596 getenv("GIT_COMMITTER_EMAIL")));
Johannes Schindelin13208572007-11-11 17:35:58 +0000597 strbuf_addch(&sob, '\n');
Johannes Schindelin13208572007-11-11 17:35:58 +0000598 for (i = sb.len - 1; i > 0 && sb.buf[i - 1] != '\n'; i--)
599 ; /* do nothing */
Johannes Schindelin21505542007-11-11 17:36:27 +0000600 if (prefixcmp(sb.buf + i, sob.buf)) {
Junio C Hamanoe5138432009-11-06 23:06:06 -0800601 if (!i || !ends_rfc2822_footer(&sb))
Johannes Schindelin21505542007-11-11 17:36:27 +0000602 strbuf_addch(&sb, '\n');
Johannes Schindelin13208572007-11-11 17:35:58 +0000603 strbuf_addbuf(&sb, &sob);
Johannes Schindelin21505542007-11-11 17:36:27 +0000604 }
Johannes Schindelin13208572007-11-11 17:35:58 +0000605 strbuf_release(&sob);
606 }
607
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500608 if (fwrite(sb.buf, 1, sb.len, fp) < sb.len)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200609 die_errno("could not write commit template");
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500610
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500611 strbuf_release(&sb);
612
Santi Béjare83dbe82008-05-04 18:04:50 +0200613 determine_author_info();
614
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200615 /* This checks if committer ident is explicitly given */
616 git_committer_info(0);
James P. Howard, IIbed575e2009-12-07 17:45:27 -0500617 if (use_editor && include_status) {
Santi Béjare83dbe82008-05-04 18:04:50 +0200618 char *author_ident;
619 const char *committer_ident;
620
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100621 if (in_merge)
622 fprintf(fp,
623 "#\n"
624 "# It looks like you may be committing a MERGE.\n"
625 "# If this is not correct, please remove the file\n"
626 "# %s\n"
627 "# and try again.\n"
628 "#\n",
629 git_path("MERGE_HEAD"));
630
631 fprintf(fp,
632 "\n"
Jeff Kingfdc7c812008-07-31 03:36:09 -0400633 "# Please enter the commit message for your changes.");
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100634 if (cleanup_mode == CLEANUP_ALL)
Jeff Kingfdc7c812008-07-31 03:36:09 -0400635 fprintf(fp,
636 " Lines starting\n"
637 "# with '#' will be ignored, and an empty"
638 " message aborts the commit.\n");
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100639 else /* CLEANUP_SPACE, that is. */
Jeff Kingfdc7c812008-07-31 03:36:09 -0400640 fprintf(fp,
641 " Lines starting\n"
642 "# with '#' will be kept; you may remove them"
643 " yourself if you want to.\n"
644 "# An empty message aborts the commit.\n");
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100645 if (only_include_assumed)
646 fprintf(fp, "# %s\n", only_include_assumed);
647
Santi Béjare83dbe82008-05-04 18:04:50 +0200648 author_ident = xstrdup(fmt_name(author_name, author_email));
649 committer_ident = fmt_name(getenv("GIT_COMMITTER_NAME"),
650 getenv("GIT_COMMITTER_EMAIL"));
651 if (strcmp(author_ident, committer_ident))
652 fprintf(fp,
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200653 "%s"
654 "# Author: %s\n",
655 ident_shown++ ? "" : "#\n",
Santi Béjare83dbe82008-05-04 18:04:50 +0200656 author_ident);
657 free(author_ident);
658
Junio C Hamano5aeb3a32010-01-17 13:54:28 -0800659 if (!user_ident_sufficiently_given())
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200660 fprintf(fp,
661 "%s"
662 "# Committer: %s\n",
663 ident_shown++ ? "" : "#\n",
664 committer_ident);
665
666 if (ident_shown)
667 fprintf(fp, "#\n");
668
Junio C Hamanod249b092009-08-09 21:59:30 -0700669 saved_color_setting = s->use_color;
670 s->use_color = 0;
671 commitable = run_status(fp, index_file, prefix, 1, s);
672 s->use_color = saved_color_setting;
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100673 } else {
Junio C Hamanod616a232007-12-22 19:22:29 -0800674 unsigned char sha1[20];
Junio C Hamanofbcf1182007-12-19 19:23:03 -0800675 const char *parent = "HEAD";
Alex Riesen71686242007-11-28 22:13:08 +0100676
Alex Riesen71686242007-11-28 22:13:08 +0100677 if (!active_nr && read_cache() < 0)
678 die("Cannot read index");
679
Junio C Hamanofbcf1182007-12-19 19:23:03 -0800680 if (amend)
681 parent = "HEAD^1";
682
Junio C Hamanod616a232007-12-22 19:22:29 -0800683 if (get_sha1(parent, sha1))
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100684 commitable = !!active_nr;
Stephan Beyer75f3ff22009-02-10 15:30:35 +0100685 else
686 commitable = index_differs_from(parent, 0);
Alex Riesen71686242007-11-28 22:13:08 +0100687 }
688
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500689 fclose(fp);
690
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100691 if (!commitable && !in_merge && !allow_empty &&
692 !(amend && is_a_merge(head_sha1))) {
Junio C Hamanod249b092009-08-09 21:59:30 -0700693 run_status(stdout, index_file, prefix, 0, s);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100694 return 0;
695 }
696
697 /*
698 * Re-read the index as pre-commit hook could have updated it,
699 * and write it out as a tree. We must do this before we invoke
700 * the editor and after we invoke run_status above.
701 */
702 discard_cache();
703 read_cache_from(index_file);
704 if (!active_cache_tree)
705 active_cache_tree = cache_tree();
706 if (cache_tree_update(active_cache_tree,
707 active_cache, active_nr, 0, 0) < 0) {
Junio C Hamano331fcb52008-11-28 19:56:34 -0800708 error("Error building trees");
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100709 return 0;
710 }
711
Paolo Bonzini8089c852008-02-05 08:04:18 +0100712 if (run_hook(index_file, "prepare-commit-msg",
713 git_path(commit_editmsg), hook_arg1, hook_arg2, NULL))
714 return 0;
715
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100716 if (use_editor) {
717 char index[PATH_MAX];
718 const char *env[2] = { index, NULL };
719 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
Stephan Beyer71982032008-07-25 18:28:42 +0200720 if (launch_editor(git_path(commit_editmsg), NULL, env)) {
721 fprintf(stderr,
722 "Please supply the message using either -m or -F option.\n");
723 exit(1);
724 }
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100725 }
726
727 if (!no_verify &&
728 run_hook(index_file, "commit-msg", git_path(commit_editmsg), NULL)) {
729 return 0;
730 }
731
732 return 1;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500733}
734
735/*
Miklos Vajna6bb6b032008-09-10 22:10:32 +0200736 * Find out if the message in the strbuf contains only whitespace and
737 * Signed-off-by lines.
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500738 */
Miklos Vajna6bb6b032008-09-10 22:10:32 +0200739static int message_is_empty(struct strbuf *sb)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500740{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500741 struct strbuf tmpl = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500742 const char *nl;
Miklos Vajna6bb6b032008-09-10 22:10:32 +0200743 int eol, i, start = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500744
Alex Riesen5f065732007-12-22 19:46:24 +0100745 if (cleanup_mode == CLEANUP_NONE && sb->len)
746 return 0;
747
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500748 /* See if the template is just a prefix of the message. */
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500749 if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) {
Alex Riesen5f065732007-12-22 19:46:24 +0100750 stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500751 if (start + tmpl.len <= sb->len &&
752 memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0)
753 start += tmpl.len;
754 }
755 strbuf_release(&tmpl);
756
757 /* Check if the rest is just whitespace and Signed-of-by's. */
758 for (i = start; i < sb->len; i++) {
759 nl = memchr(sb->buf + i, '\n', sb->len - i);
760 if (nl)
761 eol = nl - sb->buf;
762 else
763 eol = sb->len;
764
765 if (strlen(sign_off_header) <= eol - i &&
766 !prefixcmp(sb->buf + i, sign_off_header)) {
767 i = eol;
768 continue;
769 }
770 while (i < eol)
771 if (!isspace(sb->buf[i++]))
772 return 0;
773 }
774
775 return 1;
776}
777
Junio C Hamano146ea062008-08-26 23:13:13 -0700778static const char *find_author_by_nickname(const char *name)
779{
780 struct rev_info revs;
781 struct commit *commit;
782 struct strbuf buf = STRBUF_INIT;
783 const char *av[20];
784 int ac = 0;
785
786 init_revisions(&revs, NULL);
787 strbuf_addf(&buf, "--author=%s", name);
788 av[++ac] = "--all";
789 av[++ac] = "-i";
790 av[++ac] = buf.buf;
791 av[++ac] = NULL;
792 setup_revisions(ac, av, &revs, NULL);
793 prepare_revision_walk(&revs);
794 commit = get_revision(&revs);
795 if (commit) {
Thomas Rastdd2e7942009-10-19 17:48:08 +0200796 struct pretty_print_context ctx = {0};
797 ctx.date_mode = DATE_NORMAL;
Junio C Hamano146ea062008-08-26 23:13:13 -0700798 strbuf_release(&buf);
Thomas Rastdd2e7942009-10-19 17:48:08 +0200799 format_commit_message(commit, "%an <%ae>", &buf, &ctx);
Junio C Hamano146ea062008-08-26 23:13:13 -0700800 return strbuf_detach(&buf, NULL);
801 }
802 die("No existing author found with '%s'", name);
803}
804
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700805
806static void handle_untracked_files_arg(struct wt_status *s)
807{
808 if (!untracked_files_arg)
809 ; /* default already initialized */
810 else if (!strcmp(untracked_files_arg, "no"))
811 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
812 else if (!strcmp(untracked_files_arg, "normal"))
813 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
814 else if (!strcmp(untracked_files_arg, "all"))
815 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
816 else
817 die("Invalid untracked files mode '%s'", untracked_files_arg);
818}
819
Shawn Bohrer2f02b252007-12-02 23:02:09 -0600820static int parse_and_validate_options(int argc, const char *argv[],
Junio C Hamanodbd0f5c2008-08-06 11:43:47 -0700821 const char * const usage[],
Junio C Hamanod249b092009-08-09 21:59:30 -0700822 const char *prefix,
823 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500824{
825 int f = 0;
826
Stephen Boyd37782922009-05-23 11:53:12 -0700827 argc = parse_options(argc, argv, prefix, builtin_commit_options, usage,
828 0);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500829
Junio C Hamano146ea062008-08-26 23:13:13 -0700830 if (force_author && !strchr(force_author, '>'))
831 force_author = find_author_by_nickname(force_author);
832
Erick Mattosc51f6ce2009-11-04 01:20:11 -0200833 if (force_author && renew_authorship)
834 die("Using both --reset-author and --author does not make sense");
835
Johannes Schindelinf9568532007-11-11 17:36:39 +0000836 if (logfile || message.len || use_message)
Junio C Hamano48034662007-12-22 19:25:37 -0800837 use_editor = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500838 if (edit_flag)
Junio C Hamano48034662007-12-22 19:25:37 -0800839 use_editor = 1;
Paolo Bonzini406400c2008-02-05 11:01:45 +0100840 if (!use_editor)
841 setenv("GIT_EDITOR", ":", 1);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500842
843 if (get_sha1("HEAD", head_sha1))
844 initial_commit = 1;
845
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500846 /* Sanity check options */
847 if (amend && initial_commit)
848 die("You have nothing to amend.");
849 if (amend && in_merge)
Jeff Kingb5b644a2007-12-02 01:07:03 -0500850 die("You are in the middle of a merge -- cannot amend.");
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500851
852 if (use_message)
853 f++;
854 if (edit_message)
855 f++;
856 if (logfile)
857 f++;
858 if (f > 1)
859 die("Only one of -c/-C/-F can be used.");
Johannes Schindelinf9568532007-11-11 17:36:39 +0000860 if (message.len && f > 0)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500861 die("Option -m cannot be combined with -c/-C/-F.");
862 if (edit_message)
863 use_message = edit_message;
Junio C Hamano1eb1e9e2007-12-14 11:57:22 -0800864 if (amend && !use_message)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500865 use_message = "HEAD";
Erick Mattosc51f6ce2009-11-04 01:20:11 -0200866 if (!use_message && renew_authorship)
867 die("--reset-author can be used only with -C, -c or --amend.");
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500868 if (use_message) {
869 unsigned char sha1[20];
870 static char utf8[] = "UTF-8";
871 const char *out_enc;
872 char *enc, *end;
873 struct commit *commit;
874
875 if (get_sha1(use_message, sha1))
876 die("could not lookup commit %s", use_message);
Junio C Hamano8a2f8732008-02-03 00:00:09 -0800877 commit = lookup_commit_reference(sha1);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500878 if (!commit || parse_commit(commit))
879 die("could not parse commit %s", use_message);
880
881 enc = strstr(commit->buffer, "\nencoding");
882 if (enc) {
883 end = strchr(enc + 10, '\n');
884 enc = xstrndup(enc + 10, end - (enc + 10));
885 } else {
886 enc = utf8;
887 }
888 out_enc = git_commit_encoding ? git_commit_encoding : utf8;
889
890 if (strcmp(out_enc, enc))
891 use_message_buffer =
892 reencode_string(commit->buffer, out_enc, enc);
893
894 /*
895 * If we failed to reencode the buffer, just copy it
896 * byte for byte so the user can try to fix it up.
897 * This also handles the case where input and output
898 * encodings are identical.
899 */
900 if (use_message_buffer == NULL)
901 use_message_buffer = xstrdup(commit->buffer);
902 if (enc != utf8)
903 free(enc);
904 }
905
906 if (!!also + !!only + !!all + !!interactive > 1)
907 die("Only one of --include/--only/--all/--interactive can be used.");
908 if (argc == 0 && (also || (only && !amend)))
909 die("No paths with --include/--only does not make sense.");
910 if (argc == 0 && only && amend)
911 only_include_assumed = "Clever... amending the last one with dirty index.";
Johannes Sixt3c5283f2008-04-10 13:33:08 +0200912 if (argc > 0 && !also && !only)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500913 only_include_assumed = "Explicit paths specified without -i nor -o; assuming --only paths...";
Alex Riesen5f065732007-12-22 19:46:24 +0100914 if (!cleanup_arg || !strcmp(cleanup_arg, "default"))
915 cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE;
916 else if (!strcmp(cleanup_arg, "verbatim"))
917 cleanup_mode = CLEANUP_NONE;
918 else if (!strcmp(cleanup_arg, "whitespace"))
919 cleanup_mode = CLEANUP_SPACE;
920 else if (!strcmp(cleanup_arg, "strip"))
921 cleanup_mode = CLEANUP_ALL;
922 else
923 die("Invalid cleanup mode %s", cleanup_arg);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500924
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700925 handle_untracked_files_arg(s);
Marius Storm-Olsen4bfee302008-06-05 10:31:19 +0200926
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500927 if (all && argc > 0)
928 die("Paths with -a does not make sense.");
929 else if (interactive && argc > 0)
930 die("Paths with --interactive does not make sense.");
931
Jeff King7c9f7032009-09-05 04:59:56 -0400932 if (null_termination && status_format == STATUS_FORMAT_LONG)
933 status_format = STATUS_FORMAT_PORCELAIN;
934 if (status_format != STATUS_FORMAT_LONG)
935 dry_run = 1;
936
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500937 return argc;
938}
939
Junio C Hamanod249b092009-08-09 21:59:30 -0700940static int dry_run_commit(int argc, const char **argv, const char *prefix,
941 struct wt_status *s)
Junio C Hamano3a5d13a2009-08-07 23:03:36 -0700942{
943 int commitable;
944 const char *index_file;
945
946 index_file = prepare_index(argc, argv, prefix, 1);
Junio C Hamanod249b092009-08-09 21:59:30 -0700947 commitable = run_status(stdout, index_file, prefix, 0, s);
Junio C Hamano3a5d13a2009-08-07 23:03:36 -0700948 rollback_index_files();
949
950 return commitable ? 0 : 1;
951}
952
Junio C Hamanof766b362009-08-09 23:12:19 -0700953static int parse_status_slot(const char *var, int offset)
954{
955 if (!strcasecmp(var+offset, "header"))
956 return WT_STATUS_HEADER;
957 if (!strcasecmp(var+offset, "updated")
958 || !strcasecmp(var+offset, "added"))
959 return WT_STATUS_UPDATED;
960 if (!strcasecmp(var+offset, "changed"))
961 return WT_STATUS_CHANGED;
962 if (!strcasecmp(var+offset, "untracked"))
963 return WT_STATUS_UNTRACKED;
964 if (!strcasecmp(var+offset, "nobranch"))
965 return WT_STATUS_NOBRANCH;
966 if (!strcasecmp(var+offset, "unmerged"))
967 return WT_STATUS_UNMERGED;
Jeff King8b8e8622009-12-12 07:25:24 -0500968 return -1;
Junio C Hamanof766b362009-08-09 23:12:19 -0700969}
970
971static int git_status_config(const char *k, const char *v, void *cb)
972{
973 struct wt_status *s = cb;
974
975 if (!strcmp(k, "status.submodulesummary")) {
976 int is_bool;
977 s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
978 if (is_bool && s->submodule_summary)
979 s->submodule_summary = -1;
980 return 0;
981 }
982 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
983 s->use_color = git_config_colorbool(k, v, -1);
984 return 0;
985 }
986 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
987 int slot = parse_status_slot(k, 13);
Jeff King8b8e8622009-12-12 07:25:24 -0500988 if (slot < 0)
989 return 0;
Junio C Hamanof766b362009-08-09 23:12:19 -0700990 if (!v)
991 return config_error_nonbool(k);
992 color_parse(v, k, s->color_palette[slot]);
993 return 0;
994 }
995 if (!strcmp(k, "status.relativepaths")) {
996 s->relative_paths = git_config_bool(k, v);
997 return 0;
998 }
999 if (!strcmp(k, "status.showuntrackedfiles")) {
1000 if (!v)
1001 return config_error_nonbool(k);
1002 else if (!strcmp(v, "no"))
1003 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
1004 else if (!strcmp(v, "normal"))
1005 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
1006 else if (!strcmp(v, "all"))
1007 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
1008 else
1009 return error("Invalid untracked files mode '%s'", v);
1010 return 0;
1011 }
1012 return git_diff_ui_config(k, v, NULL);
1013}
1014
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001015int cmd_status(int argc, const char **argv, const char *prefix)
1016{
Junio C Hamanod249b092009-08-09 21:59:30 -07001017 struct wt_status s;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001018 unsigned char sha1[20];
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001019 static struct option builtin_status_options[] = {
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001020 OPT__VERBOSE(&verbose),
Jeff Kingdd2be242009-09-05 04:54:14 -04001021 OPT_SET_INT('s', "short", &status_format,
1022 "show status concisely", STATUS_FORMAT_SHORT),
Jeff King6f157872009-09-05 04:55:37 -04001023 OPT_SET_INT(0, "porcelain", &status_format,
1024 "show porcelain output format",
1025 STATUS_FORMAT_PORCELAIN),
Junio C Hamano173e6c82009-08-04 23:55:22 -07001026 OPT_BOOLEAN('z', "null", &null_termination,
1027 "terminate entries with NUL"),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001028 { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
1029 "mode",
1030 "show untracked files, optional modes: all, normal, no. (Default: all)",
1031 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
1032 OPT_END(),
1033 };
1034
Jeff Kingdd2be242009-09-05 04:54:14 -04001035 if (null_termination && status_format == STATUS_FORMAT_LONG)
Jeff King6f157872009-09-05 04:55:37 -04001036 status_format = STATUS_FORMAT_PORCELAIN;
Junio C Hamanod249b092009-08-09 21:59:30 -07001037
1038 wt_status_prepare(&s);
1039 git_config(git_status_config, &s);
Junio C Hamano30988302009-12-11 23:45:24 -08001040 in_merge = file_exists(git_path("MERGE_HEAD"));
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001041 argc = parse_options(argc, argv, prefix,
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001042 builtin_status_options,
1043 builtin_status_usage, 0);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001044 handle_untracked_files_arg(&s);
1045
1046 if (*argv)
1047 s.pathspec = get_pathspec(prefix, argv);
1048
Junio C Hamano149794d2010-02-17 12:30:41 -08001049 read_cache_preload(s.pathspec);
Nguyễn Thái Ngọc Duy688cd6d2010-01-14 22:02:21 +07001050 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec, NULL, NULL);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001051 s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
Junio C Hamano3c588452009-12-11 23:53:41 -08001052 s.in_merge = in_merge;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001053 wt_status_collect(&s);
1054
Jeff King86617682009-12-07 00:26:25 -05001055 if (s.relative_paths)
1056 s.prefix = prefix;
Junio C Hamanod249b092009-08-09 21:59:30 -07001057 if (s.use_color == -1)
1058 s.use_color = git_use_color_default;
Markus Heidelberg38920dd2009-01-08 19:53:05 +01001059 if (diff_use_color_default == -1)
1060 diff_use_color_default = git_use_color_default;
1061
Jeff Kingdd2be242009-09-05 04:54:14 -04001062 switch (status_format) {
1063 case STATUS_FORMAT_SHORT:
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001064 wt_shortstatus_print(&s, null_termination);
Jeff Kingdd2be242009-09-05 04:54:14 -04001065 break;
Jeff King6f157872009-09-05 04:55:37 -04001066 case STATUS_FORMAT_PORCELAIN:
Jeff King4a7cc2f2009-12-07 00:17:15 -05001067 wt_porcelain_print(&s, null_termination);
Jeff King6f157872009-09-05 04:55:37 -04001068 break;
Jeff Kingdd2be242009-09-05 04:54:14 -04001069 case STATUS_FORMAT_LONG:
Junio C Hamano173e6c82009-08-04 23:55:22 -07001070 s.verbose = verbose;
Junio C Hamano173e6c82009-08-04 23:55:22 -07001071 wt_status_print(&s);
Jeff Kingdd2be242009-09-05 04:54:14 -04001072 break;
Junio C Hamano173e6c82009-08-04 23:55:22 -07001073 }
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001074 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001075}
1076
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001077static void print_summary(const char *prefix, const unsigned char *sha1)
1078{
1079 struct rev_info rev;
1080 struct commit *commit;
Jeff King49ff9a72010-01-13 12:39:51 -05001081 struct strbuf format = STRBUF_INIT;
Jeff Kingc85db252008-10-01 18:31:25 -04001082 unsigned char junk_sha1[20];
1083 const char *head = resolve_ref("HEAD", junk_sha1, 0, NULL);
Jeff King49ff9a72010-01-13 12:39:51 -05001084 struct pretty_print_context pctx = {0};
1085 struct strbuf author_ident = STRBUF_INIT;
1086 struct strbuf committer_ident = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001087
1088 commit = lookup_commit(sha1);
1089 if (!commit)
Jeff Kingb5b644a2007-12-02 01:07:03 -05001090 die("couldn't look up newly created commit");
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001091 if (!commit || parse_commit(commit))
1092 die("could not parse newly created commit");
1093
Jeff King49ff9a72010-01-13 12:39:51 -05001094 strbuf_addstr(&format, "format:%h] %s");
1095
1096 format_commit_message(commit, "%an <%ae>", &author_ident, &pctx);
1097 format_commit_message(commit, "%cn <%ce>", &committer_ident, &pctx);
1098 if (strbuf_cmp(&author_ident, &committer_ident)) {
1099 strbuf_addstr(&format, "\n Author: ");
1100 strbuf_addbuf_percentquote(&format, &author_ident);
1101 }
Junio C Hamano1a893062010-01-17 13:59:36 -08001102 if (!user_ident_sufficiently_given()) {
Jeff King49ff9a72010-01-13 12:39:51 -05001103 strbuf_addstr(&format, "\n Committer: ");
1104 strbuf_addbuf_percentquote(&format, &committer_ident);
Jeff Kingb706fcf2010-01-13 15:17:08 -05001105 if (advice_implicit_identity) {
1106 strbuf_addch(&format, '\n');
1107 strbuf_addstr(&format, implicit_ident_advice);
1108 }
Jeff King49ff9a72010-01-13 12:39:51 -05001109 }
1110 strbuf_release(&author_ident);
1111 strbuf_release(&committer_ident);
1112
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001113 init_revisions(&rev, prefix);
1114 setup_revisions(0, NULL, &rev, NULL);
1115
1116 rev.abbrev = 0;
1117 rev.diff = 1;
1118 rev.diffopt.output_format =
1119 DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY;
1120
1121 rev.verbose_header = 1;
1122 rev.show_root_diff = 1;
Jeff King49ff9a72010-01-13 12:39:51 -05001123 get_commit_format(format.buf, &rev);
Junio C Hamanobf82a152007-12-10 21:02:26 -08001124 rev.always_show_header = 0;
Junio C Hamano3eb2a152007-12-16 15:05:39 -08001125 rev.diffopt.detect_rename = 1;
1126 rev.diffopt.rename_limit = 100;
1127 rev.diffopt.break_opt = 0;
Junio C Hamano15964562007-12-16 15:03:58 -08001128 diff_setup_done(&rev.diffopt);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001129
Santi Béjarc5ee71f2009-01-19 23:45:16 +01001130 printf("[%s%s ",
Jeff Kingc85db252008-10-01 18:31:25 -04001131 !prefixcmp(head, "refs/heads/") ?
1132 head + 11 :
1133 !strcmp(head, "HEAD") ?
1134 "detached HEAD" :
1135 head,
1136 initial_commit ? " (root-commit)" : "");
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001137
Junio C Hamanobf82a152007-12-10 21:02:26 -08001138 if (!log_tree_commit(&rev, commit)) {
Thomas Rastdd2e7942009-10-19 17:48:08 +02001139 struct pretty_print_context ctx = {0};
Junio C Hamanobf82a152007-12-10 21:02:26 -08001140 struct strbuf buf = STRBUF_INIT;
Thomas Rastdd2e7942009-10-19 17:48:08 +02001141 ctx.date_mode = DATE_NORMAL;
Jeff King49ff9a72010-01-13 12:39:51 -05001142 format_commit_message(commit, format.buf + 7, &buf, &ctx);
Junio C Hamanobf82a152007-12-10 21:02:26 -08001143 printf("%s\n", buf.buf);
1144 strbuf_release(&buf);
1145 }
Junio C Hamanofc6f19f2010-01-17 00:57:51 -08001146 strbuf_release(&format);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001147}
1148
Stephan Beyer186458b2008-07-24 01:09:35 +02001149static int git_commit_config(const char *k, const char *v, void *cb)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001150{
Junio C Hamanod249b092009-08-09 21:59:30 -07001151 struct wt_status *s = cb;
1152
Brian Hetro984c6e72008-07-05 01:24:40 -04001153 if (!strcmp(k, "commit.template"))
Matthieu Moy395de252009-11-17 18:24:25 +01001154 return git_config_pathname(&template_file, k, v);
James P. Howard, IIbed575e2009-12-07 17:45:27 -05001155 if (!strcmp(k, "commit.status")) {
1156 include_status = git_config_bool(k, v);
1157 return 0;
1158 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001159
Junio C Hamanod249b092009-08-09 21:59:30 -07001160 return git_status_config(k, v, s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001161}
1162
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001163int cmd_commit(int argc, const char **argv, const char *prefix)
1164{
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001165 struct strbuf sb = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001166 const char *index_file, *reflog_msg;
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001167 char *nl, *p;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001168 unsigned char commit_sha1[20];
1169 struct ref_lock *ref_lock;
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001170 struct commit_list *parents = NULL, **pptr = &parents;
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001171 struct stat statbuf;
1172 int allow_fast_forward = 1;
Junio C Hamanod249b092009-08-09 21:59:30 -07001173 struct wt_status s;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001174
Junio C Hamanod249b092009-08-09 21:59:30 -07001175 wt_status_prepare(&s);
1176 git_config(git_commit_config, &s);
Junio C Hamano30988302009-12-11 23:45:24 -08001177 in_merge = file_exists(git_path("MERGE_HEAD"));
Junio C Hamano3c588452009-12-11 23:53:41 -08001178 s.in_merge = in_merge;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001179
Junio C Hamanod249b092009-08-09 21:59:30 -07001180 if (s.use_color == -1)
1181 s.use_color = git_use_color_default;
Junio C Hamanod249b092009-08-09 21:59:30 -07001182 argc = parse_and_validate_options(argc, argv, builtin_commit_usage,
1183 prefix, &s);
Junio C Hamano3fa509d2009-08-15 02:14:14 -07001184 if (dry_run) {
1185 if (diff_use_color_default == -1)
1186 diff_use_color_default = git_use_color_default;
Junio C Hamanod249b092009-08-09 21:59:30 -07001187 return dry_run_commit(argc, argv, prefix, &s);
Junio C Hamano3fa509d2009-08-15 02:14:14 -07001188 }
Junio C Hamano50b7e702009-08-04 23:49:33 -07001189 index_file = prepare_index(argc, argv, prefix, 0);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001190
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001191 /* Set up everything for writing the commit object. This includes
1192 running hooks, writing the trees, and interacting with the user. */
Junio C Hamanod249b092009-08-09 21:59:30 -07001193 if (!prepare_to_commit(index_file, prefix, &s)) {
Junio C Hamano28886052007-11-18 01:52:55 -08001194 rollback_index_files();
1195 return 1;
1196 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001197
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001198 /* Determine parents */
1199 if (initial_commit) {
1200 reflog_msg = "commit (initial)";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001201 } else if (amend) {
1202 struct commit_list *c;
1203 struct commit *commit;
1204
1205 reflog_msg = "commit (amend)";
1206 commit = lookup_commit(head_sha1);
1207 if (!commit || parse_commit(commit))
1208 die("could not parse HEAD commit");
1209
1210 for (c = commit->parents; c; c = c->next)
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001211 pptr = &commit_list_insert(c->item, pptr)->next;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001212 } else if (in_merge) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001213 struct strbuf m = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001214 FILE *fp;
1215
1216 reflog_msg = "commit (merge)";
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001217 pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001218 fp = fopen(git_path("MERGE_HEAD"), "r");
1219 if (fp == NULL)
Thomas Rastd824cbb2009-06-27 17:58:46 +02001220 die_errno("could not open '%s' for reading",
1221 git_path("MERGE_HEAD"));
Linus Torvalds7c3fd252008-01-15 16:12:33 -08001222 while (strbuf_getline(&m, fp, '\n') != EOF) {
1223 unsigned char sha1[20];
1224 if (get_sha1_hex(m.buf, sha1) < 0)
1225 die("Corrupt MERGE_HEAD file (%s)", m.buf);
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001226 pptr = &commit_list_insert(lookup_commit(sha1), pptr)->next;
Linus Torvalds7c3fd252008-01-15 16:12:33 -08001227 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001228 fclose(fp);
1229 strbuf_release(&m);
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001230 if (!stat(git_path("MERGE_MODE"), &statbuf)) {
1231 if (strbuf_read_file(&sb, git_path("MERGE_MODE"), 0) < 0)
Thomas Rastd824cbb2009-06-27 17:58:46 +02001232 die_errno("could not read MERGE_MODE");
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001233 if (!strcmp(sb.buf, "no-ff"))
1234 allow_fast_forward = 0;
1235 }
1236 if (allow_fast_forward)
1237 parents = reduce_heads(parents);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001238 } else {
1239 reflog_msg = "commit";
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001240 pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001241 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001242
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001243 /* Finally, get the commit message */
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001244 strbuf_reset(&sb);
Junio C Hamano740001a2007-12-08 23:23:20 -08001245 if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
Thomas Rast0721c312009-06-27 17:58:47 +02001246 int saved_errno = errno;
Junio C Hamano740001a2007-12-08 23:23:20 -08001247 rollback_index_files();
Thomas Rast0721c312009-06-27 17:58:47 +02001248 die("could not read commit message: %s", strerror(saved_errno));
Junio C Hamano740001a2007-12-08 23:23:20 -08001249 }
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001250
1251 /* Truncate the message just before the diff, if any. */
Jeff King0b382272008-11-12 03:25:52 -05001252 if (verbose) {
1253 p = strstr(sb.buf, "\ndiff --git ");
1254 if (p != NULL)
1255 strbuf_setlen(&sb, p - sb.buf + 1);
1256 }
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001257
Alex Riesen5f065732007-12-22 19:46:24 +01001258 if (cleanup_mode != CLEANUP_NONE)
1259 stripspace(&sb, cleanup_mode == CLEANUP_ALL);
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001260 if (message_is_empty(&sb)) {
Junio C Hamano28886052007-11-18 01:52:55 -08001261 rollback_index_files();
Jeff Kingfdc7c812008-07-31 03:36:09 -04001262 fprintf(stderr, "Aborting commit due to empty commit message.\n");
1263 exit(1);
Junio C Hamano28886052007-11-18 01:52:55 -08001264 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001265
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001266 if (commit_tree(sb.buf, active_cache_tree->sha1, parents, commit_sha1,
1267 fmt_ident(author_name, author_email, author_date,
1268 IDENT_ERROR_ON_NO_NAME))) {
Junio C Hamano28886052007-11-18 01:52:55 -08001269 rollback_index_files();
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001270 die("failed to write commit object");
Junio C Hamano28886052007-11-18 01:52:55 -08001271 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001272
1273 ref_lock = lock_any_ref_for_update("HEAD",
1274 initial_commit ? NULL : head_sha1,
1275 0);
1276
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001277 nl = strchr(sb.buf, '\n');
Johannes Schindelin741707b2007-11-08 12:15:26 +00001278 if (nl)
1279 strbuf_setlen(&sb, nl + 1 - sb.buf);
1280 else
1281 strbuf_addch(&sb, '\n');
Johannes Schindelin741707b2007-11-08 12:15:26 +00001282 strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
1283 strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001284
Junio C Hamano28886052007-11-18 01:52:55 -08001285 if (!ref_lock) {
1286 rollback_index_files();
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001287 die("cannot lock HEAD ref");
Junio C Hamano28886052007-11-18 01:52:55 -08001288 }
1289 if (write_ref_sha1(ref_lock, commit_sha1, sb.buf) < 0) {
1290 rollback_index_files();
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001291 die("cannot update HEAD ref");
Junio C Hamano28886052007-11-18 01:52:55 -08001292 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001293
1294 unlink(git_path("MERGE_HEAD"));
1295 unlink(git_path("MERGE_MSG"));
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001296 unlink(git_path("MERGE_MODE"));
Gerrit Pape5a95b852008-02-08 09:53:58 +00001297 unlink(git_path("SQUASH_MSG"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001298
Brandon Casey5a9dd392008-01-23 11:21:22 -06001299 if (commit_index_files())
1300 die ("Repository has been updated, but unable to write\n"
1301 "new_index file. Check that disk is not full or quota is\n"
1302 "not exceeded, and then \"git reset HEAD\" to recover.");
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001303
Junio C Hamanocb6020b2009-12-04 00:20:48 -08001304 rerere(0);
Junio C Hamano28886052007-11-18 01:52:55 -08001305 run_hook(get_index_file(), "post-commit", NULL);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001306 if (!quiet)
1307 print_summary(prefix, commit_sha1);
1308
1309 return 0;
1310}