blob: 970a83662a67a8f1c6cb8a67fd2e6636a5260948 [file] [log] [blame]
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001/*
2 * Builtin "git commit"
3 *
4 * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
6 */
7
8#include "cache.h"
9#include "cache-tree.h"
Matthias Kestenholz6b2f2d92008-02-18 08:26:03 +010010#include "color.h"
Junio C Hamano28886052007-11-18 01:52:55 -080011#include "dir.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050012#include "builtin.h"
13#include "diff.h"
14#include "diffcore.h"
15#include "commit.h"
16#include "revision.h"
17#include "wt-status.h"
18#include "run-command.h"
19#include "refs.h"
20#include "log-tree.h"
21#include "strbuf.h"
22#include "utf8.h"
23#include "parse-options.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +010024#include "string-list.h"
Stephan Beyer5b2fd952008-07-09 14:58:57 +020025#include "rerere.h"
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -080026#include "unpack-trees.h"
Junio C Hamano76e2f7c2009-08-07 23:31:57 -070027#include "quote.h"
Jens Lehmann302ad7a2010-08-06 00:40:48 +020028#include "submodule.h"
Junio C Hamanoba3c69a2011-10-05 17:23:20 -070029#include "gpg-interface.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050030
31static const char * const builtin_commit_usage[] = {
Stephan Beyer1b1dd232008-07-13 15:36:15 +020032 "git commit [options] [--] <filepattern>...",
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050033 NULL
34};
35
Shawn Bohrer2f02b252007-12-02 23:02:09 -060036static const char * const builtin_status_usage[] = {
Stephan Beyer1b1dd232008-07-13 15:36:15 +020037 "git status [options] [--] <filepattern>...",
Shawn Bohrer2f02b252007-12-02 23:02:09 -060038 NULL
39};
40
Jeff King49ff9a72010-01-13 12:39:51 -050041static const char implicit_ident_advice[] =
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +000042N_("Your name and email address were configured automatically based\n"
Jeff King49ff9a72010-01-13 12:39:51 -050043"on your username and hostname. Please check that they are accurate.\n"
44"You can suppress this message by setting them explicitly:\n"
45"\n"
Matt Kraai8bb45b22010-02-24 06:18:25 -080046" git config --global user.name \"Your Name\"\n"
Jeff King49ff9a72010-01-13 12:39:51 -050047" git config --global user.email you@example.com\n"
48"\n"
Matthieu Moy3f142462011-01-12 19:29:14 +010049"After doing this, you may fix the identity used for this commit with:\n"
Jeff King49ff9a72010-01-13 12:39:51 -050050"\n"
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +000051" git commit --amend --reset-author\n");
Jeff King49ff9a72010-01-13 12:39:51 -050052
Jeff Kingf197ed22010-06-06 20:41:46 -040053static const char empty_amend_advice[] =
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +000054N_("You asked to amend the most recent commit, but doing so would make\n"
Jeff Kingf197ed22010-06-06 20:41:46 -040055"it empty. You can repeat your command with --allow-empty, or you can\n"
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +000056"remove the commit entirely with \"git reset HEAD^\".\n");
Jeff Kingf197ed22010-06-06 20:41:46 -040057
Jay Soffian37f7a852011-02-19 23:12:29 -050058static const char empty_cherry_pick_advice[] =
Junio C Hamano6c80cd22011-04-01 17:55:55 -070059N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n"
Jay Soffian37f7a852011-02-19 23:12:29 -050060"If you wish to commit it anyway, use:\n"
61"\n"
62" git commit --allow-empty\n"
63"\n"
Junio C Hamano6c80cd22011-04-01 17:55:55 -070064"Otherwise, please use 'git reset'\n");
Jay Soffian37f7a852011-02-19 23:12:29 -050065
Jay Soffian37f7a852011-02-19 23:12:29 -050066static const char *use_message_buffer;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050067static const char commit_editmsg[] = "COMMIT_EDITMSG";
Junio C Hamano28886052007-11-18 01:52:55 -080068static struct lock_file index_lock; /* real index */
69static struct lock_file false_lock; /* used only for partial commits */
70static enum {
71 COMMIT_AS_IS = 1,
72 COMMIT_NORMAL,
Gary V. Vaughan4b055482010-05-14 09:31:35 +000073 COMMIT_PARTIAL
Junio C Hamano28886052007-11-18 01:52:55 -080074} commit_style;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050075
Junio C Hamanodbd0f5c2008-08-06 11:43:47 -070076static const char *logfile, *force_author;
Brian Hetro984c6e72008-07-05 01:24:40 -040077static const char *template_file;
Jay Soffian37f7a852011-02-19 23:12:29 -050078/*
79 * The _message variables are commit names from which to take
80 * the commit message and/or authorship.
81 */
82static const char *author_message, *author_message_buffer;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050083static char *edit_message, *use_message;
Pat Notz89ac1222010-11-02 13:59:11 -060084static char *fixup_message, *squash_message;
Conrad Irwinb4bd4662011-05-07 10:58:07 -070085static int all, edit_flag, also, interactive, patch_interactive, only, amend, signoff;
Erick Mattosc51f6ce2009-11-04 01:20:11 -020086static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
Ævar Arnfjörð Bjarmasonc9b5fde2010-04-06 08:40:35 +000087static int no_post_rewrite, allow_empty_message;
Jens Lehmann46a958b2010-06-25 16:56:47 +020088static char *untracked_files_arg, *force_date, *ignore_submodule_arg;
Junio C Hamanoba3c69a2011-10-05 17:23:20 -070089static char *sign_commit;
90
Alex Riesen5f065732007-12-22 19:46:24 +010091/*
92 * The default commit message cleanup mode will remove the lines
93 * beginning with # (shell comments) and leading and trailing
94 * whitespaces (empty lines or containing only whitespaces)
95 * if editor is used, and only the whitespaces if the message
96 * is specified explicitly.
97 */
98static enum {
99 CLEANUP_SPACE,
100 CLEANUP_NONE,
Gary V. Vaughan4b055482010-05-14 09:31:35 +0000101 CLEANUP_ALL
Alex Riesen5f065732007-12-22 19:46:24 +0100102} cleanup_mode;
103static char *cleanup_arg;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500104
Jay Soffian37f7a852011-02-19 23:12:29 -0500105static enum commit_whence whence;
Junio C Hamano06bb6432011-08-19 11:58:18 -0700106static int use_editor = 1, include_status = 1;
Junio C Hamano2381e392010-04-10 00:33:17 -0700107static int show_ignored_in_status;
Johannes Schindelin3b6aeb32008-07-22 21:40:41 +0100108static const char *only_include_assumed;
109static struct strbuf message;
Johannes Schindelinf9568532007-11-11 17:36:39 +0000110
Jeff King7c9f7032009-09-05 04:59:56 -0400111static int null_termination;
112static enum {
113 STATUS_FORMAT_LONG,
114 STATUS_FORMAT_SHORT,
Gary V. Vaughan4b055482010-05-14 09:31:35 +0000115 STATUS_FORMAT_PORCELAIN
Jeff King7c9f7032009-09-05 04:59:56 -0400116} status_format = STATUS_FORMAT_LONG;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +0200117static int status_show_branch;
Jeff King7c9f7032009-09-05 04:59:56 -0400118
Johannes Schindelinf9568532007-11-11 17:36:39 +0000119static int opt_parse_m(const struct option *opt, const char *arg, int unset)
120{
121 struct strbuf *buf = opt->value;
122 if (unset)
123 strbuf_setlen(buf, 0);
124 else {
125 strbuf_addstr(buf, arg);
Johannes Schindelin3b6aeb32008-07-22 21:40:41 +0100126 strbuf_addstr(buf, "\n\n");
Johannes Schindelinf9568532007-11-11 17:36:39 +0000127 }
128 return 0;
129}
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500130
131static struct option builtin_commit_options[] = {
Jonathan Nieder8c839682010-11-08 13:54:48 -0600132 OPT__QUIET(&quiet, "suppress summary after successful commit"),
133 OPT__VERBOSE(&verbose, "show diff in commit message template"),
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500134
Greg Pricee97ca7f2009-12-29 16:54:49 -0500135 OPT_GROUP("Commit message options"),
Michael J Gruber726c4e32011-02-15 14:09:04 +0100136 OPT_FILENAME('F', "file", &logfile, "read message from file"),
Michael J Gruber23c6a802011-02-15 14:09:12 +0100137 OPT_STRING(0, "author", &force_author, "author", "override author for commit"),
138 OPT_STRING(0, "date", &force_date, "date", "override date for commit"),
139 OPT_CALLBACK('m', "message", &message, "message", "commit message", opt_parse_m),
140 OPT_STRING('c', "reedit-message", &edit_message, "commit", "reuse and edit message from specified commit"),
141 OPT_STRING('C', "reuse-message", &use_message, "commit", "reuse message from specified commit"),
142 OPT_STRING(0, "fixup", &fixup_message, "commit", "use autosquash formatted message to fixup specified commit"),
143 OPT_STRING(0, "squash", &squash_message, "commit", "use autosquash formatted message to squash specified commit"),
Erick Mattosc51f6ce2009-11-04 01:20:11 -0200144 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 -0500145 OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"),
Stephen Boyddf217ed2009-05-23 11:53:13 -0700146 OPT_FILENAME('t', "template", &template_file, "use specified template file"),
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500147 OPT_BOOLEAN('e', "edit", &edit_flag, "force edit of commit"),
Greg Pricee97ca7f2009-12-29 16:54:49 -0500148 OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"),
James P. Howard, IIbed575e2009-12-07 17:45:27 -0500149 OPT_BOOLEAN(0, "status", &include_status, "include status in commit message template"),
Junio C Hamanoba3c69a2011-10-05 17:23:20 -0700150 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, "key id",
151 "GPG sign commit", PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
Greg Pricee97ca7f2009-12-29 16:54:49 -0500152 /* end commit message options */
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500153
154 OPT_GROUP("Commit contents options"),
155 OPT_BOOLEAN('a', "all", &all, "commit all changed files"),
156 OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"),
157 OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"),
Conrad Irwinb4bd4662011-05-07 10:58:07 -0700158 OPT_BOOLEAN('p', "patch", &patch_interactive, "interactively add changes"),
Johannes Sixtd4ba07c2008-04-10 13:33:09 +0200159 OPT_BOOLEAN('o', "only", &only, "commit only specified files"),
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500160 OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"),
Junio C Hamano3a5d13a2009-08-07 23:03:36 -0700161 OPT_BOOLEAN(0, "dry-run", &dry_run, "show what would be committed"),
Jeff King7c9f7032009-09-05 04:59:56 -0400162 OPT_SET_INT(0, "short", &status_format, "show status concisely",
163 STATUS_FORMAT_SHORT),
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +0200164 OPT_BOOLEAN(0, "branch", &status_show_branch, "show branch information"),
Jeff King7c9f7032009-09-05 04:59:56 -0400165 OPT_SET_INT(0, "porcelain", &status_format,
Michael J Gruberba9d7fe2011-02-15 14:09:05 +0100166 "machine-readable output", STATUS_FORMAT_PORCELAIN),
Jeff King7c9f7032009-09-05 04:59:56 -0400167 OPT_BOOLEAN('z', "null", &null_termination,
168 "terminate entries with NUL"),
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500169 OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
Thomas Rast6f6bee32010-03-12 18:04:28 +0100170 OPT_BOOLEAN(0, "no-post-rewrite", &no_post_rewrite, "bypass post-rewrite hook"),
Michael J Gruber85470902011-02-15 14:09:09 +0100171 { 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 -0500172 /* end commit contents options */
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500173
Ævar Arnfjörð Bjarmasonc9b5fde2010-04-06 08:40:35 +0000174 { OPTION_BOOLEAN, 0, "allow-empty", &allow_empty, NULL,
175 "ok to record an empty change",
176 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
177 { OPTION_BOOLEAN, 0, "allow-empty-message", &allow_empty_message, NULL,
178 "ok to record a change with an empty message",
179 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
180
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500181 OPT_END()
182};
183
Jay Soffian37f7a852011-02-19 23:12:29 -0500184static void determine_whence(struct wt_status *s)
185{
186 if (file_exists(git_path("MERGE_HEAD")))
187 whence = FROM_MERGE;
188 else if (file_exists(git_path("CHERRY_PICK_HEAD")))
189 whence = FROM_CHERRY_PICK;
190 else
191 whence = FROM_COMMIT;
192 if (s)
193 s->whence = whence;
194}
195
196static const char *whence_s(void)
197{
198 char *s = "";
199
200 switch (whence) {
201 case FROM_COMMIT:
202 break;
203 case FROM_MERGE:
204 s = "merge";
205 break;
206 case FROM_CHERRY_PICK:
207 s = "cherry-pick";
208 break;
209 }
210
211 return s;
212}
213
Junio C Hamano28886052007-11-18 01:52:55 -0800214static void rollback_index_files(void)
215{
216 switch (commit_style) {
217 case COMMIT_AS_IS:
218 break; /* nothing to do */
219 case COMMIT_NORMAL:
220 rollback_lock_file(&index_lock);
221 break;
222 case COMMIT_PARTIAL:
223 rollback_lock_file(&index_lock);
224 rollback_lock_file(&false_lock);
225 break;
226 }
227}
228
Brandon Casey5a9dd392008-01-23 11:21:22 -0600229static int commit_index_files(void)
Junio C Hamano28886052007-11-18 01:52:55 -0800230{
Brandon Casey5a9dd392008-01-23 11:21:22 -0600231 int err = 0;
232
Junio C Hamano28886052007-11-18 01:52:55 -0800233 switch (commit_style) {
234 case COMMIT_AS_IS:
235 break; /* nothing to do */
236 case COMMIT_NORMAL:
Brandon Casey5a9dd392008-01-23 11:21:22 -0600237 err = commit_lock_file(&index_lock);
Junio C Hamano28886052007-11-18 01:52:55 -0800238 break;
239 case COMMIT_PARTIAL:
Brandon Casey5a9dd392008-01-23 11:21:22 -0600240 err = commit_lock_file(&index_lock);
Junio C Hamano28886052007-11-18 01:52:55 -0800241 rollback_lock_file(&false_lock);
242 break;
243 }
Brandon Casey5a9dd392008-01-23 11:21:22 -0600244
245 return err;
Junio C Hamano28886052007-11-18 01:52:55 -0800246}
247
248/*
249 * Take a union of paths in the index and the named tree (typically, "HEAD"),
250 * and return the paths that match the given pattern in list.
251 */
Johannes Schindelinc455c872008-07-21 19:03:49 +0100252static int list_paths(struct string_list *list, const char *with_tree,
Junio C Hamano28886052007-11-18 01:52:55 -0800253 const char *prefix, const char **pattern)
254{
255 int i;
256 char *m;
257
258 for (i = 0; pattern[i]; i++)
259 ;
260 m = xcalloc(1, i);
261
Clemens Buchacher8894d532011-07-30 19:13:47 +0200262 if (with_tree) {
Clemens Buchacherf950eb92011-09-04 12:42:01 +0200263 char *max_prefix = common_prefix(pattern);
Clemens Buchacher5879f562011-09-04 12:41:59 +0200264 overlay_tree_on_cache(with_tree, max_prefix ? max_prefix : prefix);
265 free(max_prefix);
Clemens Buchacher8894d532011-07-30 19:13:47 +0200266 }
Junio C Hamano28886052007-11-18 01:52:55 -0800267
268 for (i = 0; i < active_nr; i++) {
269 struct cache_entry *ce = active_cache[i];
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700270 struct string_list_item *item;
271
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800272 if (ce->ce_flags & CE_UPDATE)
Junio C Hamanoe87e22d2008-01-14 13:54:24 -0800273 continue;
Clemens Buchacher0b509222009-01-14 15:54:35 +0100274 if (!match_pathspec(pattern, ce->name, ce_namelen(ce), 0, m))
Junio C Hamano28886052007-11-18 01:52:55 -0800275 continue;
Julian Phillips78a395d2010-06-26 00:41:35 +0100276 item = string_list_insert(list, ce->name);
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700277 if (ce_skip_worktree(ce))
278 item->util = item; /* better a valid pointer than a fake one */
Junio C Hamano28886052007-11-18 01:52:55 -0800279 }
280
Clemens Buchacher0f64bfa2011-08-01 23:19:58 +0200281 return report_path_error(m, pattern, prefix);
Junio C Hamano28886052007-11-18 01:52:55 -0800282}
283
Johannes Schindelinc455c872008-07-21 19:03:49 +0100284static void add_remove_files(struct string_list *list)
Junio C Hamano28886052007-11-18 01:52:55 -0800285{
286 int i;
287 for (i = 0; i < list->nr; i++) {
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700288 struct stat st;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100289 struct string_list_item *p = &(list->items[i]);
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700290
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700291 /* p->util is skip-worktree */
292 if (p->util)
Nguyễn Thái Ngọc Duyb4d16902009-08-20 20:46:58 +0700293 continue;
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700294
Johannes Schindelinc455c872008-07-21 19:03:49 +0100295 if (!lstat(p->string, &st)) {
296 if (add_to_cache(p->string, &st, 0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000297 die(_("updating files failed"));
Alex Riesen960b8ad2008-05-12 19:57:45 +0200298 } else
Johannes Schindelinc455c872008-07-21 19:03:49 +0100299 remove_file_from_cache(p->string);
Junio C Hamano28886052007-11-18 01:52:55 -0800300 }
301}
302
Junio C Hamano06bb6432011-08-19 11:58:18 -0700303static void create_base_index(const struct commit *current_head)
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800304{
305 struct tree *tree;
306 struct unpack_trees_options opts;
307 struct tree_desc t;
308
Junio C Hamano06bb6432011-08-19 11:58:18 -0700309 if (!current_head) {
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800310 discard_cache();
311 return;
312 }
313
314 memset(&opts, 0, sizeof(opts));
315 opts.head_idx = 1;
316 opts.index_only = 1;
317 opts.merge = 1;
Linus Torvalds34110cd2008-03-06 18:12:28 -0800318 opts.src_index = &the_index;
319 opts.dst_index = &the_index;
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800320
321 opts.fn = oneway_merge;
Junio C Hamano06bb6432011-08-19 11:58:18 -0700322 tree = parse_tree_indirect(current_head->object.sha1);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800323 if (!tree)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000324 die(_("failed to unpack HEAD tree object"));
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800325 parse_tree(tree);
326 init_tree_desc(&t, tree->buffer, tree->size);
Daniel Barkalow203a2fe2008-02-07 11:39:48 -0500327 if (unpack_trees(1, &t, &opts))
328 exit(128); /* We've already reported the error, finish dying */
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800329}
330
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100331static void refresh_cache_or_die(int refresh_flags)
332{
333 /*
334 * refresh_flags contains REFRESH_QUIET, so the only errors
335 * are for unmerged entries.
336 */
337 if (refresh_cache(refresh_flags | REFRESH_IN_PORCELAIN))
338 die_resolve_conflict("commit");
339}
340
Junio C Hamano06bb6432011-08-19 11:58:18 -0700341static char *prepare_index(int argc, const char **argv, const char *prefix,
342 const struct commit *current_head, int is_status)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500343{
344 int fd;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100345 struct string_list partial;
Junio C Hamano28886052007-11-18 01:52:55 -0800346 const char **pathspec = NULL;
Conrad Irwin1020d082011-05-06 22:59:59 -0700347 char *old_index_env = NULL;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700348 int refresh_flags = REFRESH_QUIET;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500349
Junio C Hamano50b7e702009-08-04 23:49:33 -0700350 if (is_status)
351 refresh_flags |= REFRESH_UNMERGED;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500352
Junio C Hamanof64fe7b2007-11-25 08:46:29 -0800353 if (*argv)
354 pathspec = get_pathspec(prefix, argv);
Junio C Hamano28886052007-11-18 01:52:55 -0800355
Linus Torvalds671c9b72008-11-13 16:36:30 -0800356 if (read_cache_preload(pathspec) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000357 die(_("index file corrupt"));
Linus Torvalds671c9b72008-11-13 16:36:30 -0800358
Conrad Irwin1020d082011-05-06 22:59:59 -0700359 if (interactive) {
360 fd = hold_locked_index(&index_lock, 1);
361
362 refresh_cache_or_die(refresh_flags);
363
364 if (write_cache(fd, active_cache, active_nr) ||
365 close_lock_file(&index_lock))
366 die(_("unable to create temporary index"));
367
368 old_index_env = getenv(INDEX_ENVIRONMENT);
369 setenv(INDEX_ENVIRONMENT, index_lock.filename, 1);
370
Conrad Irwinb4bd4662011-05-07 10:58:07 -0700371 if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
Conrad Irwin1020d082011-05-06 22:59:59 -0700372 die(_("interactive add failed"));
373
374 if (old_index_env && *old_index_env)
375 setenv(INDEX_ENVIRONMENT, old_index_env, 1);
376 else
377 unsetenv(INDEX_ENVIRONMENT);
378
379 discard_cache();
380 read_cache_from(index_lock.filename);
381
382 commit_style = COMMIT_NORMAL;
383 return index_lock.filename;
384 }
385
Junio C Hamano28886052007-11-18 01:52:55 -0800386 /*
387 * Non partial, non as-is commit.
388 *
389 * (1) get the real index;
390 * (2) update the_index as necessary;
391 * (3) write the_index out to the real index (still locked);
392 * (4) return the name of the locked index file.
393 *
394 * The caller should run hooks on the locked real index, and
395 * (A) if all goes well, commit the real index;
396 * (B) on failure, rollback the real index.
397 */
398 if (all || (also && pathspec && *pathspec)) {
Markus Heidelberg1f2362a2010-04-02 14:27:19 +0200399 fd = hold_locked_index(&index_lock, 1);
Alex Riesen7ae02a32008-05-12 19:58:10 +0200400 add_files_to_cache(also ? prefix : NULL, pathspec, 0);
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100401 refresh_cache_or_die(refresh_flags);
Brandon Casey4ed7cd32008-01-16 13:12:46 -0600402 if (write_cache(fd, active_cache, active_nr) ||
403 close_lock_file(&index_lock))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000404 die(_("unable to write new_index file"));
Junio C Hamano28886052007-11-18 01:52:55 -0800405 commit_style = COMMIT_NORMAL;
406 return index_lock.filename;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500407 }
408
Junio C Hamano28886052007-11-18 01:52:55 -0800409 /*
410 * As-is commit.
411 *
412 * (1) return the name of the real index file.
413 *
Markus Heidelberg73276232010-04-02 14:27:18 +0200414 * The caller should run hooks on the real index,
415 * and create commit from the_index.
Junio C Hamano28886052007-11-18 01:52:55 -0800416 * We still need to refresh the index here.
417 */
418 if (!pathspec || !*pathspec) {
419 fd = hold_locked_index(&index_lock, 1);
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100420 refresh_cache_or_die(refresh_flags);
Junio C Hamanod5f5d0a2010-07-06 21:53:11 -0700421 if (active_cache_changed) {
422 if (write_cache(fd, active_cache, active_nr) ||
423 commit_locked_index(&index_lock))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000424 die(_("unable to write new_index file"));
Junio C Hamanod5f5d0a2010-07-06 21:53:11 -0700425 } else {
426 rollback_lock_file(&index_lock);
427 }
Junio C Hamano28886052007-11-18 01:52:55 -0800428 commit_style = COMMIT_AS_IS;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500429 return get_index_file();
430 }
431
Junio C Hamano28886052007-11-18 01:52:55 -0800432 /*
433 * A partial commit.
434 *
435 * (0) find the set of affected paths;
436 * (1) get lock on the real index file;
437 * (2) update the_index with the given paths;
438 * (3) write the_index out to the real index (still locked);
439 * (4) get lock on the false index file;
440 * (5) reset the_index from HEAD;
441 * (6) update the_index the same way as (2);
442 * (7) write the_index out to the false index file;
443 * (8) return the name of the false index file (still locked);
444 *
445 * The caller should run hooks on the locked false index, and
446 * create commit from it. Then
447 * (A) if all goes well, commit the real index;
448 * (B) on failure, rollback the real index;
449 * In either case, rollback the false index.
450 */
451 commit_style = COMMIT_PARTIAL;
452
Jay Soffian37f7a852011-02-19 23:12:29 -0500453 if (whence != FROM_COMMIT)
Junio C Hamano6c80cd22011-04-01 17:55:55 -0700454 die(_("cannot do a partial commit during a %s."), whence_s());
Junio C Hamano28886052007-11-18 01:52:55 -0800455
456 memset(&partial, 0, sizeof(partial));
Johannes Schindelinc455c872008-07-21 19:03:49 +0100457 partial.strdup_strings = 1;
Junio C Hamano06bb6432011-08-19 11:58:18 -0700458 if (list_paths(&partial, !current_head ? NULL : "HEAD", prefix, pathspec))
Junio C Hamano28886052007-11-18 01:52:55 -0800459 exit(1);
460
461 discard_cache();
462 if (read_cache() < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000463 die(_("cannot read the index"));
Junio C Hamano28886052007-11-18 01:52:55 -0800464
465 fd = hold_locked_index(&index_lock, 1);
466 add_remove_files(&partial);
Kristian Høgsbergef12b502007-11-12 15:48:22 -0500467 refresh_cache(REFRESH_QUIET);
Brandon Casey4ed7cd32008-01-16 13:12:46 -0600468 if (write_cache(fd, active_cache, active_nr) ||
469 close_lock_file(&index_lock))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000470 die(_("unable to write new_index file"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500471
Junio C Hamano28886052007-11-18 01:52:55 -0800472 fd = hold_lock_file_for_update(&false_lock,
Junio C Hamanoa1574002008-10-21 17:58:11 -0700473 git_path("next-index-%"PRIuMAX,
474 (uintmax_t) getpid()),
Junio C Hamanoacd3b9e2008-10-17 15:44:39 -0700475 LOCK_DIE_ON_ERROR);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800476
Junio C Hamano06bb6432011-08-19 11:58:18 -0700477 create_base_index(current_head);
Junio C Hamano28886052007-11-18 01:52:55 -0800478 add_remove_files(&partial);
Kristian Høgsbergd37d3202007-11-09 11:40:27 -0500479 refresh_cache(REFRESH_QUIET);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500480
Brandon Casey4ed7cd32008-01-16 13:12:46 -0600481 if (write_cache(fd, active_cache, active_nr) ||
482 close_lock_file(&false_lock))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000483 die(_("unable to write temporary index file"));
Jeff King959ba672008-02-14 12:18:23 -0500484
485 discard_cache();
486 read_cache_from(false_lock.filename);
487
Junio C Hamano28886052007-11-18 01:52:55 -0800488 return false_lock.filename;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500489}
490
Junio C Hamanod249b092009-08-09 21:59:30 -0700491static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
492 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500493{
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700494 unsigned char sha1[20];
495
Junio C Hamanod249b092009-08-09 21:59:30 -0700496 if (s->relative_paths)
497 s->prefix = prefix;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500498
499 if (amend) {
Junio C Hamanod249b092009-08-09 21:59:30 -0700500 s->amend = 1;
501 s->reference = "HEAD^1";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500502 }
Junio C Hamanod249b092009-08-09 21:59:30 -0700503 s->verbose = verbose;
504 s->index_file = index_file;
505 s->fp = fp;
506 s->nowarn = nowarn;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700507 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500508
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700509 wt_status_collect(s);
Jeff King7c9f7032009-09-05 04:59:56 -0400510
511 switch (status_format) {
512 case STATUS_FORMAT_SHORT:
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +0200513 wt_shortstatus_print(s, null_termination, status_show_branch);
Jeff King7c9f7032009-09-05 04:59:56 -0400514 break;
515 case STATUS_FORMAT_PORCELAIN:
Jeff King4a7cc2f2009-12-07 00:17:15 -0500516 wt_porcelain_print(s, null_termination);
Jeff King7c9f7032009-09-05 04:59:56 -0400517 break;
518 case STATUS_FORMAT_LONG:
519 wt_status_print(s);
520 break;
521 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500522
Junio C Hamanod249b092009-08-09 21:59:30 -0700523 return s->commitable;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500524}
525
Junio C Hamano06bb6432011-08-19 11:58:18 -0700526static int is_a_merge(const struct commit *current_head)
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100527{
Junio C Hamano06bb6432011-08-19 11:58:18 -0700528 return !!(current_head->parents && current_head->parents->next);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100529}
530
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500531static const char sign_off_header[] = "Signed-off-by: ";
532
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800533static void determine_author_info(struct strbuf *author_ident)
Santi Béjara45d46b2008-05-04 18:04:49 +0200534{
535 char *name, *email, *date;
536
537 name = getenv("GIT_AUTHOR_NAME");
538 email = getenv("GIT_AUTHOR_EMAIL");
539 date = getenv("GIT_AUTHOR_DATE");
540
Jay Soffian37f7a852011-02-19 23:12:29 -0500541 if (author_message) {
Santi Béjara45d46b2008-05-04 18:04:49 +0200542 const char *a, *lb, *rb, *eol;
543
Jay Soffian37f7a852011-02-19 23:12:29 -0500544 a = strstr(author_message_buffer, "\nauthor ");
Santi Béjara45d46b2008-05-04 18:04:49 +0200545 if (!a)
Junio C Hamano6c80cd22011-04-01 17:55:55 -0700546 die(_("invalid commit: %s"), author_message);
Santi Béjara45d46b2008-05-04 18:04:49 +0200547
Jonathan Niederfb7749e2010-05-02 03:57:12 -0500548 lb = strchrnul(a + strlen("\nauthor "), '<');
549 rb = strchrnul(lb, '>');
550 eol = strchrnul(rb, '\n');
551 if (!*lb || !*rb || !*eol)
Junio C Hamano6c80cd22011-04-01 17:55:55 -0700552 die(_("invalid commit: %s"), author_message);
Santi Béjara45d46b2008-05-04 18:04:49 +0200553
Jonathan Niederfb7749e2010-05-02 03:57:12 -0500554 if (lb == a + strlen("\nauthor "))
555 /* \nauthor <foo@example.com> */
556 name = xcalloc(1, 1);
557 else
558 name = xmemdupz(a + strlen("\nauthor "),
559 (lb - strlen(" ") -
560 (a + strlen("\nauthor "))));
561 email = xmemdupz(lb + strlen("<"), rb - (lb + strlen("<")));
562 date = xmemdupz(rb + strlen("> "), eol - (rb + strlen("> ")));
Santi Béjara45d46b2008-05-04 18:04:49 +0200563 }
564
565 if (force_author) {
566 const char *lb = strstr(force_author, " <");
567 const char *rb = strchr(force_author, '>');
568
569 if (!lb || !rb)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000570 die(_("malformed --author parameter"));
Santi Béjara45d46b2008-05-04 18:04:49 +0200571 name = xstrndup(force_author, lb - force_author);
572 email = xstrndup(lb + 2, rb - (lb + 2));
573 }
574
Miklos Vajna02b47cd2009-12-02 23:16:18 +0100575 if (force_date)
576 date = force_date;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800577 strbuf_addstr(author_ident, fmt_ident(name, email, date,
578 IDENT_ERROR_ON_NO_NAME));
Santi Béjara45d46b2008-05-04 18:04:49 +0200579}
580
David Brownc1e01b02009-10-28 10:13:44 -0700581static int ends_rfc2822_footer(struct strbuf *sb)
582{
583 int ch;
584 int hit = 0;
585 int i, j, k;
586 int len = sb->len;
587 int first = 1;
588 const char *buf = sb->buf;
589
590 for (i = len - 1; i > 0; i--) {
591 if (hit && buf[i] == '\n')
592 break;
593 hit = (buf[i] == '\n');
594 }
595
596 while (i < len - 1 && buf[i] == '\n')
597 i++;
598
599 for (; i < len; i = k) {
600 for (k = i; k < len && buf[k] != '\n'; k++)
601 ; /* do nothing */
602 k++;
603
604 if ((buf[k] == ' ' || buf[k] == '\t') && !first)
605 continue;
606
607 first = 0;
608
609 for (j = 0; i + j < len; j++) {
610 ch = buf[i + j];
611 if (ch == ':')
612 break;
613 if (isalnum(ch) ||
614 (ch == '-'))
615 continue;
616 return 0;
617 }
618 }
619 return 1;
620}
621
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800622static char *cut_ident_timestamp_part(char *string)
623{
624 char *ket = strrchr(string, '>');
625 if (!ket || ket[1] != ' ')
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000626 die(_("Malformed ident string: '%s'"), string);
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800627 *++ket = '\0';
628 return ket;
629}
630
Junio C Hamanod249b092009-08-09 21:59:30 -0700631static int prepare_to_commit(const char *index_file, const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -0700632 struct commit *current_head,
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800633 struct wt_status *s,
634 struct strbuf *author_ident)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500635{
636 struct stat statbuf;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800637 struct strbuf committer_ident = STRBUF_INIT;
Junio C Hamanobc5d2482007-11-18 12:01:38 -0800638 int commitable, saved_color_setting;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500639 struct strbuf sb = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500640 char *buffer;
Paolo Bonzini8089c852008-02-05 08:04:18 +0100641 const char *hook_arg1 = NULL;
642 const char *hook_arg2 = NULL;
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200643 int ident_shown = 0;
Boris Faure8b1ae672011-05-08 12:31:02 +0200644 int clean_message_contents = (cleanup_mode != CLEANUP_NONE);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500645
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100646 if (!no_verify && run_hook(index_file, "pre-commit", NULL))
647 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500648
Pat Notz89ac1222010-11-02 13:59:11 -0600649 if (squash_message) {
650 /*
651 * Insert the proper subject line before other commit
652 * message options add their content.
653 */
654 if (use_message && !strcmp(use_message, squash_message))
655 strbuf_addstr(&sb, "squash! ");
656 else {
657 struct pretty_print_context ctx = {0};
658 struct commit *c;
659 c = lookup_commit_reference_by_name(squash_message);
660 if (!c)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000661 die(_("could not lookup commit %s"), squash_message);
Pat Notz89ac1222010-11-02 13:59:11 -0600662 ctx.output_encoding = get_commit_output_encoding();
663 format_commit_message(c, "squash! %s\n\n", &sb,
664 &ctx);
665 }
666 }
667
Johannes Schindelinf9568532007-11-11 17:36:39 +0000668 if (message.len) {
669 strbuf_addbuf(&sb, &message);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100670 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500671 } else if (logfile && !strcmp(logfile, "-")) {
672 if (isatty(0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000673 fprintf(stderr, _("(reading log message from standard input)\n"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500674 if (strbuf_read(&sb, 0, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000675 die_errno(_("could not read log from standard input"));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100676 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500677 } else if (logfile) {
678 if (strbuf_read_file(&sb, logfile, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000679 die_errno(_("could not read log file '%s'"),
Thomas Rastd824cbb2009-06-27 17:58:46 +0200680 logfile);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100681 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500682 } else if (use_message) {
683 buffer = strstr(use_message_buffer, "\n\n");
684 if (!buffer || buffer[2] == '\0')
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000685 die(_("commit has empty message"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500686 strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100687 hook_arg1 = "commit";
688 hook_arg2 = use_message;
Pat Notzd71b8ba2010-11-02 13:59:09 -0600689 } else if (fixup_message) {
690 struct pretty_print_context ctx = {0};
691 struct commit *commit;
692 commit = lookup_commit_reference_by_name(fixup_message);
693 if (!commit)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000694 die(_("could not lookup commit %s"), fixup_message);
Pat Notzd71b8ba2010-11-02 13:59:09 -0600695 ctx.output_encoding = get_commit_output_encoding();
696 format_commit_message(commit, "fixup! %s\n\n",
697 &sb, &ctx);
698 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500699 } else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
700 if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000701 die_errno(_("could not read MERGE_MSG"));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100702 hook_arg1 = "merge";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500703 } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
704 if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000705 die_errno(_("could not read SQUASH_MSG"));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100706 hook_arg1 = "squash";
Jonathan Nieder2140b142011-02-25 03:07:57 -0600707 } else if (template_file) {
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500708 if (strbuf_read_file(&sb, template_file, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000709 die_errno(_("could not read '%s'"), template_file);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100710 hook_arg1 = "template";
Boris Faure8b1ae672011-05-08 12:31:02 +0200711 clean_message_contents = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500712 }
713
Paolo Bonzini8089c852008-02-05 08:04:18 +0100714 /*
Jay Soffian37f7a852011-02-19 23:12:29 -0500715 * The remaining cases don't modify the template message, but
716 * just set the argument(s) to the prepare-commit-msg hook.
Paolo Bonzini8089c852008-02-05 08:04:18 +0100717 */
Jay Soffian37f7a852011-02-19 23:12:29 -0500718 else if (whence == FROM_MERGE)
Paolo Bonzini8089c852008-02-05 08:04:18 +0100719 hook_arg1 = "merge";
Jay Soffian37f7a852011-02-19 23:12:29 -0500720 else if (whence == FROM_CHERRY_PICK) {
721 hook_arg1 = "commit";
722 hook_arg2 = "CHERRY_PICK_HEAD";
723 }
Paolo Bonzini8089c852008-02-05 08:04:18 +0100724
Pat Notz89ac1222010-11-02 13:59:11 -0600725 if (squash_message) {
726 /*
727 * If squash_commit was used for the commit subject,
728 * then we're possibly hijacking other commit log options.
729 * Reset the hook args to tell the real story.
730 */
731 hook_arg1 = "message";
732 hook_arg2 = "";
733 }
734
Jonathan Nieder37f30122011-02-25 23:10:49 -0600735 s->fp = fopen(git_path(commit_editmsg), "w");
736 if (s->fp == NULL)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000737 die_errno(_("could not open '%s'"), git_path(commit_editmsg));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500738
Boris Faure8b1ae672011-05-08 12:31:02 +0200739 if (clean_message_contents)
Alex Riesen5f065732007-12-22 19:46:24 +0100740 stripspace(&sb, 0);
Johannes Schindelin13208572007-11-11 17:35:58 +0000741
742 if (signoff) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500743 struct strbuf sob = STRBUF_INIT;
Johannes Schindelin13208572007-11-11 17:35:58 +0000744 int i;
745
Johannes Schindelin13208572007-11-11 17:35:58 +0000746 strbuf_addstr(&sob, sign_off_header);
Junio C Hamanod9ccfe72007-12-02 13:43:34 -0800747 strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"),
748 getenv("GIT_COMMITTER_EMAIL")));
Johannes Schindelin13208572007-11-11 17:35:58 +0000749 strbuf_addch(&sob, '\n');
Johannes Schindelin13208572007-11-11 17:35:58 +0000750 for (i = sb.len - 1; i > 0 && sb.buf[i - 1] != '\n'; i--)
751 ; /* do nothing */
Johannes Schindelin21505542007-11-11 17:36:27 +0000752 if (prefixcmp(sb.buf + i, sob.buf)) {
Junio C Hamanoe5138432009-11-06 23:06:06 -0800753 if (!i || !ends_rfc2822_footer(&sb))
Johannes Schindelin21505542007-11-11 17:36:27 +0000754 strbuf_addch(&sb, '\n');
Johannes Schindelin13208572007-11-11 17:35:58 +0000755 strbuf_addbuf(&sb, &sob);
Johannes Schindelin21505542007-11-11 17:36:27 +0000756 }
Johannes Schindelin13208572007-11-11 17:35:58 +0000757 strbuf_release(&sob);
758 }
759
Jonathan Nieder37f30122011-02-25 23:10:49 -0600760 if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000761 die_errno(_("could not write commit template"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500762
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500763 strbuf_release(&sb);
764
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800765 /* This checks and barfs if author is badly specified */
766 determine_author_info(author_ident);
Santi Béjare83dbe82008-05-04 18:04:50 +0200767
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200768 /* This checks if committer ident is explicitly given */
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800769 strbuf_addstr(&committer_ident, git_committer_info(0));
James P. Howard, IIbed575e2009-12-07 17:45:27 -0500770 if (use_editor && include_status) {
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800771 char *ai_tmp, *ci_tmp;
Jay Soffian37f7a852011-02-19 23:12:29 -0500772 if (whence != FROM_COMMIT)
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600773 status_printf_ln(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmasonfe8165c2011-02-22 23:41:46 +0000774 _("\n"
Junio C Hamanof4784b32011-03-19 23:24:19 -0700775 "It looks like you may be committing a %s.\n"
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600776 "If this is not correct, please remove the file\n"
777 " %s\n"
778 "and try again.\n"
Ævar Arnfjörð Bjarmasonfe8165c2011-02-22 23:41:46 +0000779 ""),
Jay Soffian37f7a852011-02-19 23:12:29 -0500780 whence_s(),
781 git_path(whence == FROM_MERGE
782 ? "MERGE_HEAD"
783 : "CHERRY_PICK_HEAD"));
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100784
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600785 fprintf(s->fp, "\n");
786 status_printf(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmason0b430a12011-02-22 23:41:48 +0000787 _("Please enter the commit message for your changes."));
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100788 if (cleanup_mode == CLEANUP_ALL)
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600789 status_printf_more(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmason0b430a12011-02-22 23:41:48 +0000790 _(" Lines starting\n"
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600791 "with '#' will be ignored, and an empty"
Ævar Arnfjörð Bjarmason0b430a12011-02-22 23:41:48 +0000792 " message aborts the commit.\n"));
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100793 else /* CLEANUP_SPACE, that is. */
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600794 status_printf_more(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmason0b430a12011-02-22 23:41:48 +0000795 _(" Lines starting\n"
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600796 "with '#' will be kept; you may remove them"
Jeff Kingfdc7c812008-07-31 03:36:09 -0400797 " yourself if you want to.\n"
Ævar Arnfjörð Bjarmason0b430a12011-02-22 23:41:48 +0000798 "An empty message aborts the commit.\n"));
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100799 if (only_include_assumed)
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600800 status_printf_ln(s, GIT_COLOR_NORMAL,
801 "%s", only_include_assumed);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100802
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800803 ai_tmp = cut_ident_timestamp_part(author_ident->buf);
804 ci_tmp = cut_ident_timestamp_part(committer_ident.buf);
805 if (strcmp(author_ident->buf, committer_ident.buf))
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600806 status_printf_ln(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmasonfe8165c2011-02-22 23:41:46 +0000807 _("%s"
808 "Author: %s"),
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600809 ident_shown++ ? "" : "\n",
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800810 author_ident->buf);
Santi Béjare83dbe82008-05-04 18:04:50 +0200811
Junio C Hamano5aeb3a32010-01-17 13:54:28 -0800812 if (!user_ident_sufficiently_given())
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600813 status_printf_ln(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmasonfe8165c2011-02-22 23:41:46 +0000814 _("%s"
815 "Committer: %s"),
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600816 ident_shown++ ? "" : "\n",
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800817 committer_ident.buf);
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200818
819 if (ident_shown)
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600820 status_printf_ln(s, GIT_COLOR_NORMAL, "");
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200821
Junio C Hamanod249b092009-08-09 21:59:30 -0700822 saved_color_setting = s->use_color;
823 s->use_color = 0;
Jonathan Nieder37f30122011-02-25 23:10:49 -0600824 commitable = run_status(s->fp, index_file, prefix, 1, s);
Junio C Hamanod249b092009-08-09 21:59:30 -0700825 s->use_color = saved_color_setting;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800826
827 *ai_tmp = ' ';
828 *ci_tmp = ' ';
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100829 } else {
Junio C Hamanod616a232007-12-22 19:22:29 -0800830 unsigned char sha1[20];
Junio C Hamanofbcf1182007-12-19 19:23:03 -0800831 const char *parent = "HEAD";
Alex Riesen71686242007-11-28 22:13:08 +0100832
Alex Riesen71686242007-11-28 22:13:08 +0100833 if (!active_nr && read_cache() < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000834 die(_("Cannot read index"));
Alex Riesen71686242007-11-28 22:13:08 +0100835
Junio C Hamanofbcf1182007-12-19 19:23:03 -0800836 if (amend)
837 parent = "HEAD^1";
838
Junio C Hamanod616a232007-12-22 19:22:29 -0800839 if (get_sha1(parent, sha1))
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100840 commitable = !!active_nr;
Stephan Beyer75f3ff22009-02-10 15:30:35 +0100841 else
842 commitable = index_differs_from(parent, 0);
Alex Riesen71686242007-11-28 22:13:08 +0100843 }
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800844 strbuf_release(&committer_ident);
Alex Riesen71686242007-11-28 22:13:08 +0100845
Jonathan Nieder37f30122011-02-25 23:10:49 -0600846 fclose(s->fp);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500847
Jay Soffian37f7a852011-02-19 23:12:29 -0500848 /*
849 * Reject an attempt to record a non-merge empty commit without
850 * explicit --allow-empty. In the cherry-pick case, it may be
851 * empty due to conflict resolution, which the user should okay.
852 */
853 if (!commitable && whence != FROM_MERGE && !allow_empty &&
Junio C Hamano06bb6432011-08-19 11:58:18 -0700854 !(amend && is_a_merge(current_head))) {
Junio C Hamanod249b092009-08-09 21:59:30 -0700855 run_status(stdout, index_file, prefix, 0, s);
Jeff Kingf197ed22010-06-06 20:41:46 -0400856 if (amend)
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +0000857 fputs(_(empty_amend_advice), stderr);
Jay Soffian37f7a852011-02-19 23:12:29 -0500858 else if (whence == FROM_CHERRY_PICK)
Junio C Hamano6c80cd22011-04-01 17:55:55 -0700859 fputs(_(empty_cherry_pick_advice), stderr);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100860 return 0;
861 }
862
863 /*
864 * Re-read the index as pre-commit hook could have updated it,
865 * and write it out as a tree. We must do this before we invoke
866 * the editor and after we invoke run_status above.
867 */
868 discard_cache();
869 read_cache_from(index_file);
870 if (!active_cache_tree)
871 active_cache_tree = cache_tree();
872 if (cache_tree_update(active_cache_tree,
873 active_cache, active_nr, 0, 0) < 0) {
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000874 error(_("Error building trees"));
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100875 return 0;
876 }
877
Paolo Bonzini8089c852008-02-05 08:04:18 +0100878 if (run_hook(index_file, "prepare-commit-msg",
879 git_path(commit_editmsg), hook_arg1, hook_arg2, NULL))
880 return 0;
881
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100882 if (use_editor) {
883 char index[PATH_MAX];
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000884 const char *env[2] = { NULL };
885 env[0] = index;
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100886 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
Stephan Beyer71982032008-07-25 18:28:42 +0200887 if (launch_editor(git_path(commit_editmsg), NULL, env)) {
888 fprintf(stderr,
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000889 _("Please supply the message using either -m or -F option.\n"));
Stephan Beyer71982032008-07-25 18:28:42 +0200890 exit(1);
891 }
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100892 }
893
894 if (!no_verify &&
895 run_hook(index_file, "commit-msg", git_path(commit_editmsg), NULL)) {
896 return 0;
897 }
898
899 return 1;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500900}
901
902/*
Miklos Vajna6bb6b032008-09-10 22:10:32 +0200903 * Find out if the message in the strbuf contains only whitespace and
904 * Signed-off-by lines.
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500905 */
Miklos Vajna6bb6b032008-09-10 22:10:32 +0200906static int message_is_empty(struct strbuf *sb)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500907{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500908 struct strbuf tmpl = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500909 const char *nl;
Miklos Vajna6bb6b032008-09-10 22:10:32 +0200910 int eol, i, start = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500911
Alex Riesen5f065732007-12-22 19:46:24 +0100912 if (cleanup_mode == CLEANUP_NONE && sb->len)
913 return 0;
914
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500915 /* See if the template is just a prefix of the message. */
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500916 if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) {
Alex Riesen5f065732007-12-22 19:46:24 +0100917 stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500918 if (start + tmpl.len <= sb->len &&
919 memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0)
920 start += tmpl.len;
921 }
922 strbuf_release(&tmpl);
923
924 /* Check if the rest is just whitespace and Signed-of-by's. */
925 for (i = start; i < sb->len; i++) {
926 nl = memchr(sb->buf + i, '\n', sb->len - i);
927 if (nl)
928 eol = nl - sb->buf;
929 else
930 eol = sb->len;
931
932 if (strlen(sign_off_header) <= eol - i &&
933 !prefixcmp(sb->buf + i, sign_off_header)) {
934 i = eol;
935 continue;
936 }
937 while (i < eol)
938 if (!isspace(sb->buf[i++]))
939 return 0;
940 }
941
942 return 1;
943}
944
Junio C Hamano146ea062008-08-26 23:13:13 -0700945static const char *find_author_by_nickname(const char *name)
946{
947 struct rev_info revs;
948 struct commit *commit;
949 struct strbuf buf = STRBUF_INIT;
950 const char *av[20];
951 int ac = 0;
952
953 init_revisions(&revs, NULL);
954 strbuf_addf(&buf, "--author=%s", name);
955 av[++ac] = "--all";
956 av[++ac] = "-i";
957 av[++ac] = buf.buf;
958 av[++ac] = NULL;
959 setup_revisions(ac, av, &revs, NULL);
960 prepare_revision_walk(&revs);
961 commit = get_revision(&revs);
962 if (commit) {
Thomas Rastdd2e7942009-10-19 17:48:08 +0200963 struct pretty_print_context ctx = {0};
964 ctx.date_mode = DATE_NORMAL;
Junio C Hamano146ea062008-08-26 23:13:13 -0700965 strbuf_release(&buf);
Thomas Rastdd2e7942009-10-19 17:48:08 +0200966 format_commit_message(commit, "%an <%ae>", &buf, &ctx);
Junio C Hamano146ea062008-08-26 23:13:13 -0700967 return strbuf_detach(&buf, NULL);
968 }
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000969 die(_("No existing author found with '%s'"), name);
Junio C Hamano146ea062008-08-26 23:13:13 -0700970}
971
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700972
973static void handle_untracked_files_arg(struct wt_status *s)
974{
975 if (!untracked_files_arg)
976 ; /* default already initialized */
977 else if (!strcmp(untracked_files_arg, "no"))
978 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
979 else if (!strcmp(untracked_files_arg, "normal"))
980 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
981 else if (!strcmp(untracked_files_arg, "all"))
982 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
983 else
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000984 die(_("Invalid untracked files mode '%s'"), untracked_files_arg);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700985}
986
Jay Soffian37f7a852011-02-19 23:12:29 -0500987static const char *read_commit_message(const char *name)
988{
989 const char *out_enc, *out;
990 struct commit *commit;
991
992 commit = lookup_commit_reference_by_name(name);
993 if (!commit)
Junio C Hamano6c80cd22011-04-01 17:55:55 -0700994 die(_("could not lookup commit %s"), name);
Jay Soffian37f7a852011-02-19 23:12:29 -0500995 out_enc = get_commit_output_encoding();
996 out = logmsg_reencode(commit, out_enc);
997
998 /*
999 * If we failed to reencode the buffer, just copy it
1000 * byte for byte so the user can try to fix it up.
1001 * This also handles the case where input and output
1002 * encodings are identical.
1003 */
1004 if (out == NULL)
1005 out = xstrdup(commit->buffer);
1006 return out;
1007}
1008
Shawn Bohrer2f02b252007-12-02 23:02:09 -06001009static int parse_and_validate_options(int argc, const char *argv[],
Junio C Hamanodbd0f5c2008-08-06 11:43:47 -07001010 const char * const usage[],
Junio C Hamanod249b092009-08-09 21:59:30 -07001011 const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001012 struct commit *current_head,
Junio C Hamanod249b092009-08-09 21:59:30 -07001013 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001014{
1015 int f = 0;
1016
Stephen Boyd37782922009-05-23 11:53:12 -07001017 argc = parse_options(argc, argv, prefix, builtin_commit_options, usage,
1018 0);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001019
Junio C Hamano146ea062008-08-26 23:13:13 -07001020 if (force_author && !strchr(force_author, '>'))
1021 force_author = find_author_by_nickname(force_author);
1022
Erick Mattosc51f6ce2009-11-04 01:20:11 -02001023 if (force_author && renew_authorship)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001024 die(_("Using both --reset-author and --author does not make sense"));
Erick Mattosc51f6ce2009-11-04 01:20:11 -02001025
Pat Notzd71b8ba2010-11-02 13:59:09 -06001026 if (logfile || message.len || use_message || fixup_message)
Junio C Hamano48034662007-12-22 19:25:37 -08001027 use_editor = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001028 if (edit_flag)
Junio C Hamano48034662007-12-22 19:25:37 -08001029 use_editor = 1;
Paolo Bonzini406400c2008-02-05 11:01:45 +01001030 if (!use_editor)
1031 setenv("GIT_EDITOR", ":", 1);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001032
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001033 /* Sanity check options */
Junio C Hamano06bb6432011-08-19 11:58:18 -07001034 if (amend && !current_head)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001035 die(_("You have nothing to amend."));
Jay Soffian37f7a852011-02-19 23:12:29 -05001036 if (amend && whence != FROM_COMMIT)
Junio C Hamano6c80cd22011-04-01 17:55:55 -07001037 die(_("You are in the middle of a %s -- cannot amend."), whence_s());
Pat Notz89ac1222010-11-02 13:59:11 -06001038 if (fixup_message && squash_message)
Ævar Arnfjörð Bjarmason9c227652011-02-22 23:41:45 +00001039 die(_("Options --squash and --fixup cannot be used together"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001040 if (use_message)
1041 f++;
1042 if (edit_message)
1043 f++;
Pat Notzd71b8ba2010-11-02 13:59:09 -06001044 if (fixup_message)
1045 f++;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001046 if (logfile)
1047 f++;
1048 if (f > 1)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001049 die(_("Only one of -c/-C/-F/--fixup can be used."));
Johannes Schindelinf9568532007-11-11 17:36:39 +00001050 if (message.len && f > 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001051 die((_("Option -m cannot be combined with -c/-C/-F/--fixup.")));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001052 if (edit_message)
1053 use_message = edit_message;
Pat Notzd71b8ba2010-11-02 13:59:09 -06001054 if (amend && !use_message && !fixup_message)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001055 use_message = "HEAD";
Jay Soffian37f7a852011-02-19 23:12:29 -05001056 if (!use_message && whence != FROM_CHERRY_PICK && renew_authorship)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001057 die(_("--reset-author can be used only with -C, -c or --amend."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001058 if (use_message) {
Jay Soffian37f7a852011-02-19 23:12:29 -05001059 use_message_buffer = read_commit_message(use_message);
1060 if (!renew_authorship) {
1061 author_message = use_message;
1062 author_message_buffer = use_message_buffer;
1063 }
1064 }
1065 if (whence == FROM_CHERRY_PICK && !renew_authorship) {
1066 author_message = "CHERRY_PICK_HEAD";
1067 author_message_buffer = read_commit_message(author_message);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001068 }
1069
Conrad Irwinb4bd4662011-05-07 10:58:07 -07001070 if (patch_interactive)
1071 interactive = 1;
1072
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001073 if (!!also + !!only + !!all + !!interactive > 1)
Conrad Irwinb4bd4662011-05-07 10:58:07 -07001074 die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001075 if (argc == 0 && (also || (only && !amend)))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001076 die(_("No paths with --include/--only does not make sense."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001077 if (argc == 0 && only && amend)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001078 only_include_assumed = _("Clever... amending the last one with dirty index.");
Johannes Sixt3c5283f2008-04-10 13:33:08 +02001079 if (argc > 0 && !also && !only)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001080 only_include_assumed = _("Explicit paths specified without -i nor -o; assuming --only paths...");
Alex Riesen5f065732007-12-22 19:46:24 +01001081 if (!cleanup_arg || !strcmp(cleanup_arg, "default"))
1082 cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE;
1083 else if (!strcmp(cleanup_arg, "verbatim"))
1084 cleanup_mode = CLEANUP_NONE;
1085 else if (!strcmp(cleanup_arg, "whitespace"))
1086 cleanup_mode = CLEANUP_SPACE;
1087 else if (!strcmp(cleanup_arg, "strip"))
1088 cleanup_mode = CLEANUP_ALL;
1089 else
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001090 die(_("Invalid cleanup mode %s"), cleanup_arg);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001091
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001092 handle_untracked_files_arg(s);
Marius Storm-Olsen4bfee302008-06-05 10:31:19 +02001093
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001094 if (all && argc > 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001095 die(_("Paths with -a does not make sense."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001096
Jeff King7c9f7032009-09-05 04:59:56 -04001097 if (null_termination && status_format == STATUS_FORMAT_LONG)
1098 status_format = STATUS_FORMAT_PORCELAIN;
1099 if (status_format != STATUS_FORMAT_LONG)
1100 dry_run = 1;
1101
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001102 return argc;
1103}
1104
Junio C Hamanod249b092009-08-09 21:59:30 -07001105static int dry_run_commit(int argc, const char **argv, const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001106 const struct commit *current_head, struct wt_status *s)
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001107{
1108 int commitable;
1109 const char *index_file;
1110
Junio C Hamano06bb6432011-08-19 11:58:18 -07001111 index_file = prepare_index(argc, argv, prefix, current_head, 1);
Junio C Hamanod249b092009-08-09 21:59:30 -07001112 commitable = run_status(stdout, index_file, prefix, 0, s);
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001113 rollback_index_files();
1114
1115 return commitable ? 0 : 1;
1116}
1117
Junio C Hamanof766b362009-08-09 23:12:19 -07001118static int parse_status_slot(const char *var, int offset)
1119{
1120 if (!strcasecmp(var+offset, "header"))
1121 return WT_STATUS_HEADER;
Aleksi Aalto1d282322010-11-18 01:40:05 +02001122 if (!strcasecmp(var+offset, "branch"))
1123 return WT_STATUS_ONBRANCH;
Junio C Hamanof766b362009-08-09 23:12:19 -07001124 if (!strcasecmp(var+offset, "updated")
1125 || !strcasecmp(var+offset, "added"))
1126 return WT_STATUS_UPDATED;
1127 if (!strcasecmp(var+offset, "changed"))
1128 return WT_STATUS_CHANGED;
1129 if (!strcasecmp(var+offset, "untracked"))
1130 return WT_STATUS_UNTRACKED;
1131 if (!strcasecmp(var+offset, "nobranch"))
1132 return WT_STATUS_NOBRANCH;
1133 if (!strcasecmp(var+offset, "unmerged"))
1134 return WT_STATUS_UNMERGED;
Jeff King8b8e8622009-12-12 07:25:24 -05001135 return -1;
Junio C Hamanof766b362009-08-09 23:12:19 -07001136}
1137
1138static int git_status_config(const char *k, const char *v, void *cb)
1139{
1140 struct wt_status *s = cb;
1141
1142 if (!strcmp(k, "status.submodulesummary")) {
1143 int is_bool;
1144 s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
1145 if (is_bool && s->submodule_summary)
1146 s->submodule_summary = -1;
1147 return 0;
1148 }
1149 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
Jeff Kinge269eb72011-08-17 22:03:48 -07001150 s->use_color = git_config_colorbool(k, v);
Junio C Hamanof766b362009-08-09 23:12:19 -07001151 return 0;
1152 }
1153 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
1154 int slot = parse_status_slot(k, 13);
Jeff King8b8e8622009-12-12 07:25:24 -05001155 if (slot < 0)
1156 return 0;
Junio C Hamanof766b362009-08-09 23:12:19 -07001157 if (!v)
1158 return config_error_nonbool(k);
1159 color_parse(v, k, s->color_palette[slot]);
1160 return 0;
1161 }
1162 if (!strcmp(k, "status.relativepaths")) {
1163 s->relative_paths = git_config_bool(k, v);
1164 return 0;
1165 }
1166 if (!strcmp(k, "status.showuntrackedfiles")) {
1167 if (!v)
1168 return config_error_nonbool(k);
1169 else if (!strcmp(v, "no"))
1170 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
1171 else if (!strcmp(v, "normal"))
1172 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
1173 else if (!strcmp(v, "all"))
1174 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
1175 else
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001176 return error(_("Invalid untracked files mode '%s'"), v);
Junio C Hamanof766b362009-08-09 23:12:19 -07001177 return 0;
1178 }
1179 return git_diff_ui_config(k, v, NULL);
1180}
1181
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001182int cmd_status(int argc, const char **argv, const char *prefix)
1183{
Junio C Hamanod249b092009-08-09 21:59:30 -07001184 struct wt_status s;
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001185 int fd;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001186 unsigned char sha1[20];
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001187 static struct option builtin_status_options[] = {
René Scharfefd038812010-11-08 18:56:39 +01001188 OPT__VERBOSE(&verbose, "be verbose"),
Jeff Kingdd2be242009-09-05 04:54:14 -04001189 OPT_SET_INT('s', "short", &status_format,
1190 "show status concisely", STATUS_FORMAT_SHORT),
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001191 OPT_BOOLEAN('b', "branch", &status_show_branch,
1192 "show branch information"),
Jeff King6f157872009-09-05 04:55:37 -04001193 OPT_SET_INT(0, "porcelain", &status_format,
Michael J Gruberba9d7fe2011-02-15 14:09:05 +01001194 "machine-readable output",
Jeff King6f157872009-09-05 04:55:37 -04001195 STATUS_FORMAT_PORCELAIN),
Junio C Hamano173e6c82009-08-04 23:55:22 -07001196 OPT_BOOLEAN('z', "null", &null_termination,
1197 "terminate entries with NUL"),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001198 { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
1199 "mode",
1200 "show untracked files, optional modes: all, normal, no. (Default: all)",
1201 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
Junio C Hamano2381e392010-04-10 00:33:17 -07001202 OPT_BOOLEAN(0, "ignored", &show_ignored_in_status,
1203 "show ignored files"),
Jens Lehmann46a958b2010-06-25 16:56:47 +02001204 { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, "when",
1205 "ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)",
1206 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001207 OPT_END(),
1208 };
1209
Nguyễn Thái Ngọc Duy5d3dd912010-10-22 01:45:47 -05001210 if (argc == 2 && !strcmp(argv[1], "-h"))
1211 usage_with_options(builtin_status_usage, builtin_status_options);
1212
Junio C Hamanod249b092009-08-09 21:59:30 -07001213 wt_status_prepare(&s);
Jens Lehmann302ad7a2010-08-06 00:40:48 +02001214 gitmodules_config();
Junio C Hamanod249b092009-08-09 21:59:30 -07001215 git_config(git_status_config, &s);
Jay Soffian37f7a852011-02-19 23:12:29 -05001216 determine_whence(&s);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001217 argc = parse_options(argc, argv, prefix,
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001218 builtin_status_options,
1219 builtin_status_usage, 0);
Junio C Hamano6c929722011-06-06 11:40:08 -07001220
1221 if (null_termination && status_format == STATUS_FORMAT_LONG)
1222 status_format = STATUS_FORMAT_PORCELAIN;
1223
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001224 handle_untracked_files_arg(&s);
Junio C Hamano2381e392010-04-10 00:33:17 -07001225 if (show_ignored_in_status)
1226 s.show_ignored_files = 1;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001227 if (*argv)
1228 s.pathspec = get_pathspec(prefix, argv);
1229
Junio C Hamano149794d2010-02-17 12:30:41 -08001230 read_cache_preload(s.pathspec);
Nguyễn Thái Ngọc Duy688cd6d2010-01-14 22:02:21 +07001231 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec, NULL, NULL);
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001232
1233 fd = hold_locked_index(&index_lock, 0);
Junio C Hamanoccdc4ec2011-03-21 10:16:10 -07001234 if (0 <= fd)
1235 update_index_if_able(&the_index, &index_lock);
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001236
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001237 s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
Jens Lehmann46a958b2010-06-25 16:56:47 +02001238 s.ignore_submodule_arg = ignore_submodule_arg;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001239 wt_status_collect(&s);
1240
Jeff King86617682009-12-07 00:26:25 -05001241 if (s.relative_paths)
1242 s.prefix = prefix;
Markus Heidelberg38920dd2009-01-08 19:53:05 +01001243
Jeff Kingdd2be242009-09-05 04:54:14 -04001244 switch (status_format) {
1245 case STATUS_FORMAT_SHORT:
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001246 wt_shortstatus_print(&s, null_termination, status_show_branch);
Jeff Kingdd2be242009-09-05 04:54:14 -04001247 break;
Jeff King6f157872009-09-05 04:55:37 -04001248 case STATUS_FORMAT_PORCELAIN:
Jeff King4a7cc2f2009-12-07 00:17:15 -05001249 wt_porcelain_print(&s, null_termination);
Jeff King6f157872009-09-05 04:55:37 -04001250 break;
Jeff Kingdd2be242009-09-05 04:54:14 -04001251 case STATUS_FORMAT_LONG:
Junio C Hamano173e6c82009-08-04 23:55:22 -07001252 s.verbose = verbose;
Jens Lehmann46a958b2010-06-25 16:56:47 +02001253 s.ignore_submodule_arg = ignore_submodule_arg;
Junio C Hamano173e6c82009-08-04 23:55:22 -07001254 wt_status_print(&s);
Jeff Kingdd2be242009-09-05 04:54:14 -04001255 break;
Junio C Hamano173e6c82009-08-04 23:55:22 -07001256 }
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001257 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001258}
1259
Junio C Hamano06bb6432011-08-19 11:58:18 -07001260static void print_summary(const char *prefix, const unsigned char *sha1,
1261 int initial_commit)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001262{
1263 struct rev_info rev;
1264 struct commit *commit;
Jeff King49ff9a72010-01-13 12:39:51 -05001265 struct strbuf format = STRBUF_INIT;
Jeff Kingc85db252008-10-01 18:31:25 -04001266 unsigned char junk_sha1[20];
1267 const char *head = resolve_ref("HEAD", junk_sha1, 0, NULL);
Jeff King49ff9a72010-01-13 12:39:51 -05001268 struct pretty_print_context pctx = {0};
1269 struct strbuf author_ident = STRBUF_INIT;
1270 struct strbuf committer_ident = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001271
1272 commit = lookup_commit(sha1);
1273 if (!commit)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001274 die(_("couldn't look up newly created commit"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001275 if (!commit || parse_commit(commit))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001276 die(_("could not parse newly created commit"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001277
Jeff King49ff9a72010-01-13 12:39:51 -05001278 strbuf_addstr(&format, "format:%h] %s");
1279
1280 format_commit_message(commit, "%an <%ae>", &author_ident, &pctx);
1281 format_commit_message(commit, "%cn <%ce>", &committer_ident, &pctx);
1282 if (strbuf_cmp(&author_ident, &committer_ident)) {
1283 strbuf_addstr(&format, "\n Author: ");
1284 strbuf_addbuf_percentquote(&format, &author_ident);
1285 }
Junio C Hamano1a893062010-01-17 13:59:36 -08001286 if (!user_ident_sufficiently_given()) {
Jeff King49ff9a72010-01-13 12:39:51 -05001287 strbuf_addstr(&format, "\n Committer: ");
1288 strbuf_addbuf_percentquote(&format, &committer_ident);
Jeff Kingb706fcf2010-01-13 15:17:08 -05001289 if (advice_implicit_identity) {
1290 strbuf_addch(&format, '\n');
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +00001291 strbuf_addstr(&format, _(implicit_ident_advice));
Jeff Kingb706fcf2010-01-13 15:17:08 -05001292 }
Jeff King49ff9a72010-01-13 12:39:51 -05001293 }
1294 strbuf_release(&author_ident);
1295 strbuf_release(&committer_ident);
1296
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001297 init_revisions(&rev, prefix);
1298 setup_revisions(0, NULL, &rev, NULL);
1299
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001300 rev.diff = 1;
1301 rev.diffopt.output_format =
1302 DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY;
1303
1304 rev.verbose_header = 1;
1305 rev.show_root_diff = 1;
Jeff King49ff9a72010-01-13 12:39:51 -05001306 get_commit_format(format.buf, &rev);
Junio C Hamanobf82a152007-12-10 21:02:26 -08001307 rev.always_show_header = 0;
Junio C Hamano3eb2a152007-12-16 15:05:39 -08001308 rev.diffopt.detect_rename = 1;
Junio C Hamano3eb2a152007-12-16 15:05:39 -08001309 rev.diffopt.break_opt = 0;
Junio C Hamano15964562007-12-16 15:03:58 -08001310 diff_setup_done(&rev.diffopt);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001311
Santi Béjarc5ee71f2009-01-19 23:45:16 +01001312 printf("[%s%s ",
Jeff Kingc85db252008-10-01 18:31:25 -04001313 !prefixcmp(head, "refs/heads/") ?
1314 head + 11 :
1315 !strcmp(head, "HEAD") ?
Ævar Arnfjörð Bjarmason7f5673d2011-02-22 23:41:47 +00001316 _("detached HEAD") :
Jeff Kingc85db252008-10-01 18:31:25 -04001317 head,
Ævar Arnfjörð Bjarmason7f5673d2011-02-22 23:41:47 +00001318 initial_commit ? _(" (root-commit)") : "");
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001319
Junio C Hamanobf82a152007-12-10 21:02:26 -08001320 if (!log_tree_commit(&rev, commit)) {
Tay Ray Chuana45e1a82010-06-12 22:15:39 +08001321 rev.always_show_header = 1;
1322 rev.use_terminator = 1;
1323 log_tree_commit(&rev, commit);
Junio C Hamanobf82a152007-12-10 21:02:26 -08001324 }
Tay Ray Chuana45e1a82010-06-12 22:15:39 +08001325
Junio C Hamanofc6f19f2010-01-17 00:57:51 -08001326 strbuf_release(&format);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001327}
1328
Stephan Beyer186458b2008-07-24 01:09:35 +02001329static int git_commit_config(const char *k, const char *v, void *cb)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001330{
Junio C Hamanod249b092009-08-09 21:59:30 -07001331 struct wt_status *s = cb;
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001332 int status;
Junio C Hamanod249b092009-08-09 21:59:30 -07001333
Brian Hetro984c6e72008-07-05 01:24:40 -04001334 if (!strcmp(k, "commit.template"))
Matthieu Moy395de252009-11-17 18:24:25 +01001335 return git_config_pathname(&template_file, k, v);
James P. Howard, IIbed575e2009-12-07 17:45:27 -05001336 if (!strcmp(k, "commit.status")) {
1337 include_status = git_config_bool(k, v);
1338 return 0;
1339 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001340
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001341 status = git_gpg_config(k, v, NULL);
1342 if (status)
1343 return status;
Junio C Hamanod249b092009-08-09 21:59:30 -07001344 return git_status_config(k, v, s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001345}
1346
Thomas Rast6f6bee32010-03-12 18:04:28 +01001347static const char post_rewrite_hook[] = "hooks/post-rewrite";
1348
1349static int run_rewrite_hook(const unsigned char *oldsha1,
1350 const unsigned char *newsha1)
1351{
1352 /* oldsha1 SP newsha1 LF NUL */
1353 static char buf[2*40 + 3];
1354 struct child_process proc;
1355 const char *argv[3];
1356 int code;
1357 size_t n;
1358
1359 if (access(git_path(post_rewrite_hook), X_OK) < 0)
1360 return 0;
1361
1362 argv[0] = git_path(post_rewrite_hook);
1363 argv[1] = "amend";
1364 argv[2] = NULL;
1365
1366 memset(&proc, 0, sizeof(proc));
1367 proc.argv = argv;
1368 proc.in = -1;
1369 proc.stdout_to_stderr = 1;
1370
1371 code = start_command(&proc);
1372 if (code)
1373 return code;
1374 n = snprintf(buf, sizeof(buf), "%s %s\n",
1375 sha1_to_hex(oldsha1), sha1_to_hex(newsha1));
1376 write_in_full(proc.in, buf, n);
1377 close(proc.in);
1378 return finish_command(&proc);
1379}
1380
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001381int cmd_commit(int argc, const char **argv, const char *prefix)
1382{
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001383 struct strbuf sb = STRBUF_INIT;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -08001384 struct strbuf author_ident = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001385 const char *index_file, *reflog_msg;
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001386 char *nl, *p;
Junio C Hamano06bb6432011-08-19 11:58:18 -07001387 unsigned char sha1[20];
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001388 struct ref_lock *ref_lock;
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001389 struct commit_list *parents = NULL, **pptr = &parents;
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001390 struct stat statbuf;
1391 int allow_fast_forward = 1;
Junio C Hamanod249b092009-08-09 21:59:30 -07001392 struct wt_status s;
Junio C Hamano06bb6432011-08-19 11:58:18 -07001393 struct commit *current_head = NULL;
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001394 struct commit_extra_header *extra = NULL;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001395
Nguyễn Thái Ngọc Duy5d3dd912010-10-22 01:45:47 -05001396 if (argc == 2 && !strcmp(argv[1], "-h"))
1397 usage_with_options(builtin_commit_usage, builtin_commit_options);
1398
Junio C Hamanod249b092009-08-09 21:59:30 -07001399 wt_status_prepare(&s);
1400 git_config(git_commit_config, &s);
Jay Soffian37f7a852011-02-19 23:12:29 -05001401 determine_whence(&s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001402
Junio C Hamano06bb6432011-08-19 11:58:18 -07001403 if (get_sha1("HEAD", sha1))
1404 current_head = NULL;
1405 else {
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +10001406 current_head = lookup_commit_or_die(sha1, "HEAD");
Junio C Hamano06bb6432011-08-19 11:58:18 -07001407 if (!current_head || parse_commit(current_head))
1408 die(_("could not parse HEAD commit"));
1409 }
Junio C Hamanod249b092009-08-09 21:59:30 -07001410 argc = parse_and_validate_options(argc, argv, builtin_commit_usage,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001411 prefix, current_head, &s);
Jeff Kingc9bfb952011-08-17 22:05:35 -07001412 if (dry_run)
Junio C Hamano06bb6432011-08-19 11:58:18 -07001413 return dry_run_commit(argc, argv, prefix, current_head, &s);
Junio C Hamano06bb6432011-08-19 11:58:18 -07001414 index_file = prepare_index(argc, argv, prefix, current_head, 0);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001415
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001416 /* Set up everything for writing the commit object. This includes
1417 running hooks, writing the trees, and interacting with the user. */
Junio C Hamano06bb6432011-08-19 11:58:18 -07001418 if (!prepare_to_commit(index_file, prefix,
1419 current_head, &s, &author_ident)) {
Junio C Hamano28886052007-11-18 01:52:55 -08001420 rollback_index_files();
1421 return 1;
1422 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001423
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001424 /* Determine parents */
Christian Couder643cb5f2010-06-12 18:05:12 +02001425 reflog_msg = getenv("GIT_REFLOG_ACTION");
Junio C Hamano06bb6432011-08-19 11:58:18 -07001426 if (!current_head) {
Christian Couder643cb5f2010-06-12 18:05:12 +02001427 if (!reflog_msg)
1428 reflog_msg = "commit (initial)";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001429 } else if (amend) {
1430 struct commit_list *c;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001431
Christian Couder643cb5f2010-06-12 18:05:12 +02001432 if (!reflog_msg)
1433 reflog_msg = "commit (amend)";
Junio C Hamano06bb6432011-08-19 11:58:18 -07001434 for (c = current_head->parents; c; c = c->next)
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001435 pptr = &commit_list_insert(c->item, pptr)->next;
Jay Soffian37f7a852011-02-19 23:12:29 -05001436 } else if (whence == FROM_MERGE) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001437 struct strbuf m = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001438 FILE *fp;
1439
Christian Couder643cb5f2010-06-12 18:05:12 +02001440 if (!reflog_msg)
1441 reflog_msg = "commit (merge)";
Junio C Hamano06bb6432011-08-19 11:58:18 -07001442 pptr = &commit_list_insert(current_head, pptr)->next;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001443 fp = fopen(git_path("MERGE_HEAD"), "r");
1444 if (fp == NULL)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001445 die_errno(_("could not open '%s' for reading"),
Thomas Rastd824cbb2009-06-27 17:58:46 +02001446 git_path("MERGE_HEAD"));
Linus Torvalds7c3fd252008-01-15 16:12:33 -08001447 while (strbuf_getline(&m, fp, '\n') != EOF) {
Junio C Hamano5231c632011-11-07 16:21:32 -08001448 struct commit *parent;
1449
1450 parent = get_merge_parent(m.buf);
1451 if (!parent)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001452 die(_("Corrupt MERGE_HEAD file (%s)"), m.buf);
Junio C Hamano5231c632011-11-07 16:21:32 -08001453 pptr = &commit_list_insert(parent, pptr)->next;
Linus Torvalds7c3fd252008-01-15 16:12:33 -08001454 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001455 fclose(fp);
1456 strbuf_release(&m);
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001457 if (!stat(git_path("MERGE_MODE"), &statbuf)) {
1458 if (strbuf_read_file(&sb, git_path("MERGE_MODE"), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001459 die_errno(_("could not read MERGE_MODE"));
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001460 if (!strcmp(sb.buf, "no-ff"))
1461 allow_fast_forward = 0;
1462 }
1463 if (allow_fast_forward)
1464 parents = reduce_heads(parents);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001465 } else {
Christian Couder643cb5f2010-06-12 18:05:12 +02001466 if (!reflog_msg)
Jay Soffian37f7a852011-02-19 23:12:29 -05001467 reflog_msg = (whence == FROM_CHERRY_PICK)
1468 ? "commit (cherry-pick)"
1469 : "commit";
Junio C Hamano06bb6432011-08-19 11:58:18 -07001470 pptr = &commit_list_insert(current_head, pptr)->next;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001471 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001472
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001473 /* Finally, get the commit message */
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001474 strbuf_reset(&sb);
Junio C Hamano740001a2007-12-08 23:23:20 -08001475 if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
Thomas Rast0721c312009-06-27 17:58:47 +02001476 int saved_errno = errno;
Junio C Hamano740001a2007-12-08 23:23:20 -08001477 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001478 die(_("could not read commit message: %s"), strerror(saved_errno));
Junio C Hamano740001a2007-12-08 23:23:20 -08001479 }
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001480
1481 /* Truncate the message just before the diff, if any. */
Jeff King0b382272008-11-12 03:25:52 -05001482 if (verbose) {
1483 p = strstr(sb.buf, "\ndiff --git ");
1484 if (p != NULL)
1485 strbuf_setlen(&sb, p - sb.buf + 1);
1486 }
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001487
Alex Riesen5f065732007-12-22 19:46:24 +01001488 if (cleanup_mode != CLEANUP_NONE)
1489 stripspace(&sb, cleanup_mode == CLEANUP_ALL);
Ævar Arnfjörð Bjarmasonc9b5fde2010-04-06 08:40:35 +00001490 if (message_is_empty(&sb) && !allow_empty_message) {
Junio C Hamano28886052007-11-18 01:52:55 -08001491 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001492 fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
Jeff Kingfdc7c812008-07-31 03:36:09 -04001493 exit(1);
Junio C Hamano28886052007-11-18 01:52:55 -08001494 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001495
Junio C Hamano0074d182011-12-20 13:20:56 -08001496 if (amend) {
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001497 const char *exclude_gpgsig[2] = { "gpgsig", NULL };
1498 extra = read_commit_extra_headers(current_head, exclude_gpgsig);
Junio C Hamano0074d182011-12-20 13:20:56 -08001499 } else {
1500 struct commit_extra_header **tail = &extra;
1501 append_merge_tag_headers(parents, &tail);
1502 }
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001503
1504 if (commit_tree_extended(sb.buf, active_cache_tree->sha1, parents, sha1,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001505 author_ident.buf, sign_commit, extra)) {
Junio C Hamano28886052007-11-18 01:52:55 -08001506 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001507 die(_("failed to write commit object"));
Junio C Hamano28886052007-11-18 01:52:55 -08001508 }
Junio C Hamano4c28e4a2010-12-20 17:00:36 -08001509 strbuf_release(&author_ident);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001510 free_commit_extra_headers(extra);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001511
1512 ref_lock = lock_any_ref_for_update("HEAD",
Junio C Hamano06bb6432011-08-19 11:58:18 -07001513 !current_head
1514 ? NULL
1515 : current_head->object.sha1,
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001516 0);
1517
Miklos Vajna6bb6b032008-09-10 22:10:32 +02001518 nl = strchr(sb.buf, '\n');
Johannes Schindelin741707b2007-11-08 12:15:26 +00001519 if (nl)
1520 strbuf_setlen(&sb, nl + 1 - sb.buf);
1521 else
1522 strbuf_addch(&sb, '\n');
Johannes Schindelin741707b2007-11-08 12:15:26 +00001523 strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
1524 strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001525
Junio C Hamano28886052007-11-18 01:52:55 -08001526 if (!ref_lock) {
1527 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001528 die(_("cannot lock HEAD ref"));
Junio C Hamano28886052007-11-18 01:52:55 -08001529 }
Junio C Hamano06bb6432011-08-19 11:58:18 -07001530 if (write_ref_sha1(ref_lock, sha1, sb.buf) < 0) {
Junio C Hamano28886052007-11-18 01:52:55 -08001531 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001532 die(_("cannot update HEAD ref"));
Junio C Hamano28886052007-11-18 01:52:55 -08001533 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001534
Jay Soffiand7e5c0c2011-02-19 23:12:27 -05001535 unlink(git_path("CHERRY_PICK_HEAD"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001536 unlink(git_path("MERGE_HEAD"));
1537 unlink(git_path("MERGE_MSG"));
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001538 unlink(git_path("MERGE_MODE"));
Gerrit Pape5a95b852008-02-08 09:53:58 +00001539 unlink(git_path("SQUASH_MSG"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001540
Brandon Casey5a9dd392008-01-23 11:21:22 -06001541 if (commit_index_files())
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001542 die (_("Repository has been updated, but unable to write\n"
Brandon Casey5a9dd392008-01-23 11:21:22 -06001543 "new_index file. Check that disk is not full or quota is\n"
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001544 "not exceeded, and then \"git reset HEAD\" to recover."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001545
Junio C Hamanocb6020b2009-12-04 00:20:48 -08001546 rerere(0);
Junio C Hamano28886052007-11-18 01:52:55 -08001547 run_hook(get_index_file(), "post-commit", NULL);
Thomas Rast6f6bee32010-03-12 18:04:28 +01001548 if (amend && !no_post_rewrite) {
Thomas Rast6360d342010-03-12 18:04:34 +01001549 struct notes_rewrite_cfg *cfg;
1550 cfg = init_copy_notes_for_rewrite("amend");
1551 if (cfg) {
Junio C Hamano06bb6432011-08-19 11:58:18 -07001552 /* we are amending, so current_head is not NULL */
1553 copy_note_for_rewrite(cfg, current_head->object.sha1, sha1);
Thomas Rast6360d342010-03-12 18:04:34 +01001554 finish_copy_notes_for_rewrite(cfg);
1555 }
Junio C Hamano06bb6432011-08-19 11:58:18 -07001556 run_rewrite_hook(current_head->object.sha1, sha1);
Thomas Rast6f6bee32010-03-12 18:04:28 +01001557 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001558 if (!quiet)
Junio C Hamano06bb6432011-08-19 11:58:18 -07001559 print_summary(prefix, sha1, !current_head);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001560
1561 return 0;
1562}