]>
Commit | Line | Data |
---|---|---|
f5bbc322 KH |
1 | /* |
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" | |
6b2f2d98 | 10 | #include "color.h" |
2888605c | 11 | #include "dir.h" |
f5bbc322 KH |
12 | #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" | |
c455c87c | 24 | #include "string-list.h" |
5b2fd956 | 25 | #include "rerere.h" |
fa9dcf80 | 26 | #include "unpack-trees.h" |
76e2f7ce | 27 | #include "quote.h" |
302ad7a9 | 28 | #include "submodule.h" |
f5bbc322 KH |
29 | |
30 | static const char * const builtin_commit_usage[] = { | |
1b1dd23f | 31 | "git commit [options] [--] <filepattern>...", |
f5bbc322 KH |
32 | NULL |
33 | }; | |
34 | ||
2f02b25f | 35 | static const char * const builtin_status_usage[] = { |
1b1dd23f | 36 | "git status [options] [--] <filepattern>...", |
2f02b25f SB |
37 | NULL |
38 | }; | |
39 | ||
49ff9a7a JK |
40 | static const char implicit_ident_advice[] = |
41 | "Your name and email address were configured automatically based\n" | |
42 | "on your username and hostname. Please check that they are accurate.\n" | |
43 | "You can suppress this message by setting them explicitly:\n" | |
44 | "\n" | |
8bb45b25 | 45 | " git config --global user.name \"Your Name\"\n" |
49ff9a7a JK |
46 | " git config --global user.email you@example.com\n" |
47 | "\n" | |
3f142468 | 48 | "After doing this, you may fix the identity used for this commit with:\n" |
49ff9a7a | 49 | "\n" |
3f142468 | 50 | " git commit --amend --reset-author\n"; |
49ff9a7a | 51 | |
f197ed2f JK |
52 | static const char empty_amend_advice[] = |
53 | "You asked to amend the most recent commit, but doing so would make\n" | |
54 | "it empty. You can repeat your command with --allow-empty, or you can\n" | |
55 | "remove the commit entirely with \"git reset HEAD^\".\n"; | |
56 | ||
30988301 | 57 | static unsigned char head_sha1[20]; |
49ff9a7a | 58 | |
f5bbc322 KH |
59 | static char *use_message_buffer; |
60 | static const char commit_editmsg[] = "COMMIT_EDITMSG"; | |
2888605c JH |
61 | static struct lock_file index_lock; /* real index */ |
62 | static struct lock_file false_lock; /* used only for partial commits */ | |
63 | static enum { | |
64 | COMMIT_AS_IS = 1, | |
65 | COMMIT_NORMAL, | |
4b05548f | 66 | COMMIT_PARTIAL |
2888605c | 67 | } commit_style; |
f5bbc322 | 68 | |
dbd0f5c7 | 69 | static const char *logfile, *force_author; |
984c6e7e | 70 | static const char *template_file; |
f5bbc322 | 71 | static char *edit_message, *use_message; |
89ac1223 | 72 | static char *fixup_message, *squash_message; |
f5bbc322 | 73 | static int all, edit_flag, also, interactive, only, amend, signoff; |
c51f6cee | 74 | static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship; |
c9b5fde7 | 75 | static int no_post_rewrite, allow_empty_message; |
46a958b3 | 76 | static char *untracked_files_arg, *force_date, *ignore_submodule_arg; |
5f065737 AR |
77 | /* |
78 | * The default commit message cleanup mode will remove the lines | |
79 | * beginning with # (shell comments) and leading and trailing | |
80 | * whitespaces (empty lines or containing only whitespaces) | |
81 | * if editor is used, and only the whitespaces if the message | |
82 | * is specified explicitly. | |
83 | */ | |
84 | static enum { | |
85 | CLEANUP_SPACE, | |
86 | CLEANUP_NONE, | |
4b05548f | 87 | CLEANUP_ALL |
5f065737 AR |
88 | } cleanup_mode; |
89 | static char *cleanup_arg; | |
f5bbc322 | 90 | |
bed575e4 | 91 | static int use_editor = 1, initial_commit, in_merge, include_status = 1; |
2381e39e | 92 | static int show_ignored_in_status; |
3b6aeb3c JS |
93 | static const char *only_include_assumed; |
94 | static struct strbuf message; | |
f9568530 | 95 | |
7c9f7038 JK |
96 | static int null_termination; |
97 | static enum { | |
98 | STATUS_FORMAT_LONG, | |
99 | STATUS_FORMAT_SHORT, | |
4b05548f | 100 | STATUS_FORMAT_PORCELAIN |
7c9f7038 | 101 | } status_format = STATUS_FORMAT_LONG; |
05a59a08 | 102 | static int status_show_branch; |
7c9f7038 | 103 | |
f9568530 JS |
104 | static int opt_parse_m(const struct option *opt, const char *arg, int unset) |
105 | { | |
106 | struct strbuf *buf = opt->value; | |
107 | if (unset) | |
108 | strbuf_setlen(buf, 0); | |
109 | else { | |
110 | strbuf_addstr(buf, arg); | |
3b6aeb3c | 111 | strbuf_addstr(buf, "\n\n"); |
f9568530 JS |
112 | } |
113 | return 0; | |
114 | } | |
f5bbc322 KH |
115 | |
116 | static struct option builtin_commit_options[] = { | |
8c839683 JN |
117 | OPT__QUIET(&quiet, "suppress summary after successful commit"), |
118 | OPT__VERBOSE(&verbose, "show diff in commit message template"), | |
f5bbc322 | 119 | |
e97ca7f4 | 120 | OPT_GROUP("Commit message options"), |
726c4e3d | 121 | OPT_FILENAME('F', "file", &logfile, "read message from file"), |
f5bbc322 | 122 | OPT_STRING(0, "author", &force_author, "AUTHOR", "override author for commit"), |
02b47cd7 | 123 | OPT_STRING(0, "date", &force_date, "DATE", "override date for commit"), |
3f406175 | 124 | OPT_CALLBACK('m', "message", &message, "MESSAGE", "commit message", opt_parse_m), |
c51f6cee | 125 | OPT_STRING('c', "reedit-message", &edit_message, "COMMIT", "reuse and edit message from specified commit"), |
f5bbc322 | 126 | OPT_STRING('C', "reuse-message", &use_message, "COMMIT", "reuse message from specified commit"), |
d71b8ba7 | 127 | OPT_STRING(0, "fixup", &fixup_message, "COMMIT", "use autosquash formatted message to fixup specified commit"), |
89ac1223 | 128 | OPT_STRING(0, "squash", &squash_message, "COMMIT", "use autosquash formatted message to squash specified commit"), |
c51f6cee | 129 | OPT_BOOLEAN(0, "reset-author", &renew_authorship, "the commit is authored by me now (used with -C-c/--amend)"), |
362b0dd5 | 130 | OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"), |
df217ed6 | 131 | OPT_FILENAME('t', "template", &template_file, "use specified template file"), |
f5bbc322 | 132 | OPT_BOOLEAN('e', "edit", &edit_flag, "force edit of commit"), |
e97ca7f4 | 133 | OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"), |
bed575e4 | 134 | OPT_BOOLEAN(0, "status", &include_status, "include status in commit message template"), |
e97ca7f4 | 135 | /* end commit message options */ |
f5bbc322 KH |
136 | |
137 | OPT_GROUP("Commit contents options"), | |
138 | OPT_BOOLEAN('a', "all", &all, "commit all changed files"), | |
139 | OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"), | |
140 | OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"), | |
d4ba07ca | 141 | OPT_BOOLEAN('o', "only", &only, "commit only specified files"), |
f5bbc322 | 142 | OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"), |
3a5d13a3 | 143 | OPT_BOOLEAN(0, "dry-run", &dry_run, "show what would be committed"), |
7c9f7038 JK |
144 | OPT_SET_INT(0, "short", &status_format, "show status concisely", |
145 | STATUS_FORMAT_SHORT), | |
05a59a08 | 146 | OPT_BOOLEAN(0, "branch", &status_show_branch, "show branch information"), |
7c9f7038 | 147 | OPT_SET_INT(0, "porcelain", &status_format, |
ba9d7fe1 | 148 | "machine-readable output", STATUS_FORMAT_PORCELAIN), |
7c9f7038 JK |
149 | OPT_BOOLEAN('z', "null", &null_termination, |
150 | "terminate entries with NUL"), | |
f5bbc322 | 151 | OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"), |
6f6bee3b | 152 | OPT_BOOLEAN(0, "no-post-rewrite", &no_post_rewrite, "bypass post-rewrite hook"), |
8547090c | 153 | { 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" }, |
e97ca7f4 | 154 | /* end commit contents options */ |
f5bbc322 | 155 | |
c9b5fde7 ÆAB |
156 | { OPTION_BOOLEAN, 0, "allow-empty", &allow_empty, NULL, |
157 | "ok to record an empty change", | |
158 | PARSE_OPT_NOARG | PARSE_OPT_HIDDEN }, | |
159 | { OPTION_BOOLEAN, 0, "allow-empty-message", &allow_empty_message, NULL, | |
160 | "ok to record a change with an empty message", | |
161 | PARSE_OPT_NOARG | PARSE_OPT_HIDDEN }, | |
162 | ||
f5bbc322 KH |
163 | OPT_END() |
164 | }; | |
165 | ||
2888605c JH |
166 | static void rollback_index_files(void) |
167 | { | |
168 | switch (commit_style) { | |
169 | case COMMIT_AS_IS: | |
170 | break; /* nothing to do */ | |
171 | case COMMIT_NORMAL: | |
172 | rollback_lock_file(&index_lock); | |
173 | break; | |
174 | case COMMIT_PARTIAL: | |
175 | rollback_lock_file(&index_lock); | |
176 | rollback_lock_file(&false_lock); | |
177 | break; | |
178 | } | |
179 | } | |
180 | ||
5a9dd399 | 181 | static int commit_index_files(void) |
2888605c | 182 | { |
5a9dd399 BC |
183 | int err = 0; |
184 | ||
2888605c JH |
185 | switch (commit_style) { |
186 | case COMMIT_AS_IS: | |
187 | break; /* nothing to do */ | |
188 | case COMMIT_NORMAL: | |
5a9dd399 | 189 | err = commit_lock_file(&index_lock); |
2888605c JH |
190 | break; |
191 | case COMMIT_PARTIAL: | |
5a9dd399 | 192 | err = commit_lock_file(&index_lock); |
2888605c JH |
193 | rollback_lock_file(&false_lock); |
194 | break; | |
195 | } | |
5a9dd399 BC |
196 | |
197 | return err; | |
2888605c JH |
198 | } |
199 | ||
200 | /* | |
201 | * Take a union of paths in the index and the named tree (typically, "HEAD"), | |
202 | * and return the paths that match the given pattern in list. | |
203 | */ | |
c455c87c | 204 | static int list_paths(struct string_list *list, const char *with_tree, |
2888605c JH |
205 | const char *prefix, const char **pattern) |
206 | { | |
207 | int i; | |
208 | char *m; | |
209 | ||
210 | for (i = 0; pattern[i]; i++) | |
211 | ; | |
212 | m = xcalloc(1, i); | |
213 | ||
214 | if (with_tree) | |
215 | overlay_tree_on_cache(with_tree, prefix); | |
216 | ||
217 | for (i = 0; i < active_nr; i++) { | |
218 | struct cache_entry *ce = active_cache[i]; | |
7fce6e3c NTND |
219 | struct string_list_item *item; |
220 | ||
7a51ed66 | 221 | if (ce->ce_flags & CE_UPDATE) |
e87e22d0 | 222 | continue; |
0b50922a | 223 | if (!match_pathspec(pattern, ce->name, ce_namelen(ce), 0, m)) |
2888605c | 224 | continue; |
78a395d3 | 225 | item = string_list_insert(list, ce->name); |
7fce6e3c NTND |
226 | if (ce_skip_worktree(ce)) |
227 | item->util = item; /* better a valid pointer than a fake one */ | |
2888605c JH |
228 | } |
229 | ||
230 | return report_path_error(m, pattern, prefix ? strlen(prefix) : 0); | |
231 | } | |
232 | ||
c455c87c | 233 | static void add_remove_files(struct string_list *list) |
2888605c JH |
234 | { |
235 | int i; | |
236 | for (i = 0; i < list->nr; i++) { | |
d177cab0 | 237 | struct stat st; |
c455c87c | 238 | struct string_list_item *p = &(list->items[i]); |
d177cab0 | 239 | |
7fce6e3c NTND |
240 | /* p->util is skip-worktree */ |
241 | if (p->util) | |
b4d1690d | 242 | continue; |
d177cab0 | 243 | |
c455c87c JS |
244 | if (!lstat(p->string, &st)) { |
245 | if (add_to_cache(p->string, &st, 0)) | |
960b8ad1 AR |
246 | die("updating files failed"); |
247 | } else | |
c455c87c | 248 | remove_file_from_cache(p->string); |
2888605c JH |
249 | } |
250 | } | |
251 | ||
fa9dcf80 LT |
252 | static void create_base_index(void) |
253 | { | |
254 | struct tree *tree; | |
255 | struct unpack_trees_options opts; | |
256 | struct tree_desc t; | |
257 | ||
258 | if (initial_commit) { | |
259 | discard_cache(); | |
260 | return; | |
261 | } | |
262 | ||
263 | memset(&opts, 0, sizeof(opts)); | |
264 | opts.head_idx = 1; | |
265 | opts.index_only = 1; | |
266 | opts.merge = 1; | |
34110cd4 LT |
267 | opts.src_index = &the_index; |
268 | opts.dst_index = &the_index; | |
fa9dcf80 LT |
269 | |
270 | opts.fn = oneway_merge; | |
271 | tree = parse_tree_indirect(head_sha1); | |
272 | if (!tree) | |
273 | die("failed to unpack HEAD tree object"); | |
274 | parse_tree(tree); | |
275 | init_tree_desc(&t, tree->buffer, tree->size); | |
203a2fe1 DB |
276 | if (unpack_trees(1, &t, &opts)) |
277 | exit(128); /* We've already reported the error, finish dying */ | |
fa9dcf80 LT |
278 | } |
279 | ||
d38a30df MM |
280 | static void refresh_cache_or_die(int refresh_flags) |
281 | { | |
282 | /* | |
283 | * refresh_flags contains REFRESH_QUIET, so the only errors | |
284 | * are for unmerged entries. | |
285 | */ | |
286 | if (refresh_cache(refresh_flags | REFRESH_IN_PORCELAIN)) | |
287 | die_resolve_conflict("commit"); | |
288 | } | |
289 | ||
50b7e70f | 290 | static char *prepare_index(int argc, const char **argv, const char *prefix, int is_status) |
f5bbc322 KH |
291 | { |
292 | int fd; | |
c455c87c | 293 | struct string_list partial; |
2888605c | 294 | const char **pathspec = NULL; |
50b7e70f | 295 | int refresh_flags = REFRESH_QUIET; |
f5bbc322 | 296 | |
50b7e70f JH |
297 | if (is_status) |
298 | refresh_flags |= REFRESH_UNMERGED; | |
f5bbc322 | 299 | if (interactive) { |
4f6a32f8 JK |
300 | if (interactive_add(argc, argv, prefix) != 0) |
301 | die("interactive add failed"); | |
671c9b7e | 302 | if (read_cache_preload(NULL) < 0) |
d5350fd2 | 303 | die("index file corrupt"); |
2888605c | 304 | commit_style = COMMIT_AS_IS; |
f5bbc322 KH |
305 | return get_index_file(); |
306 | } | |
307 | ||
f64fe7b4 JH |
308 | if (*argv) |
309 | pathspec = get_pathspec(prefix, argv); | |
2888605c | 310 | |
671c9b7e LT |
311 | if (read_cache_preload(pathspec) < 0) |
312 | die("index file corrupt"); | |
313 | ||
2888605c JH |
314 | /* |
315 | * Non partial, non as-is commit. | |
316 | * | |
317 | * (1) get the real index; | |
318 | * (2) update the_index as necessary; | |
319 | * (3) write the_index out to the real index (still locked); | |
320 | * (4) return the name of the locked index file. | |
321 | * | |
322 | * The caller should run hooks on the locked real index, and | |
323 | * (A) if all goes well, commit the real index; | |
324 | * (B) on failure, rollback the real index. | |
325 | */ | |
326 | if (all || (also && pathspec && *pathspec)) { | |
1f2362a9 | 327 | fd = hold_locked_index(&index_lock, 1); |
7ae02a30 | 328 | add_files_to_cache(also ? prefix : NULL, pathspec, 0); |
d38a30df | 329 | refresh_cache_or_die(refresh_flags); |
4ed7cd3a BC |
330 | if (write_cache(fd, active_cache, active_nr) || |
331 | close_lock_file(&index_lock)) | |
f5bbc322 | 332 | die("unable to write new_index file"); |
2888605c JH |
333 | commit_style = COMMIT_NORMAL; |
334 | return index_lock.filename; | |
f5bbc322 KH |
335 | } |
336 | ||
2888605c JH |
337 | /* |
338 | * As-is commit. | |
339 | * | |
340 | * (1) return the name of the real index file. | |
341 | * | |
73276235 MH |
342 | * The caller should run hooks on the real index, |
343 | * and create commit from the_index. | |
2888605c JH |
344 | * We still need to refresh the index here. |
345 | */ | |
346 | if (!pathspec || !*pathspec) { | |
347 | fd = hold_locked_index(&index_lock, 1); | |
d38a30df | 348 | refresh_cache_or_die(refresh_flags); |
d5f5d0a9 JH |
349 | if (active_cache_changed) { |
350 | if (write_cache(fd, active_cache, active_nr) || | |
351 | commit_locked_index(&index_lock)) | |
352 | die("unable to write new_index file"); | |
353 | } else { | |
354 | rollback_lock_file(&index_lock); | |
355 | } | |
2888605c | 356 | commit_style = COMMIT_AS_IS; |
f5bbc322 KH |
357 | return get_index_file(); |
358 | } | |
359 | ||
2888605c JH |
360 | /* |
361 | * A partial commit. | |
362 | * | |
363 | * (0) find the set of affected paths; | |
364 | * (1) get lock on the real index file; | |
365 | * (2) update the_index with the given paths; | |
366 | * (3) write the_index out to the real index (still locked); | |
367 | * (4) get lock on the false index file; | |
368 | * (5) reset the_index from HEAD; | |
369 | * (6) update the_index the same way as (2); | |
370 | * (7) write the_index out to the false index file; | |
371 | * (8) return the name of the false index file (still locked); | |
372 | * | |
373 | * The caller should run hooks on the locked false index, and | |
374 | * create commit from it. Then | |
375 | * (A) if all goes well, commit the real index; | |
376 | * (B) on failure, rollback the real index; | |
377 | * In either case, rollback the false index. | |
378 | */ | |
379 | commit_style = COMMIT_PARTIAL; | |
380 | ||
30988301 | 381 | if (in_merge) |
2888605c JH |
382 | die("cannot do a partial commit during a merge."); |
383 | ||
384 | memset(&partial, 0, sizeof(partial)); | |
c455c87c | 385 | partial.strdup_strings = 1; |
2888605c JH |
386 | if (list_paths(&partial, initial_commit ? NULL : "HEAD", prefix, pathspec)) |
387 | exit(1); | |
388 | ||
389 | discard_cache(); | |
390 | if (read_cache() < 0) | |
391 | die("cannot read the index"); | |
392 | ||
393 | fd = hold_locked_index(&index_lock, 1); | |
394 | add_remove_files(&partial); | |
ef12b50d | 395 | refresh_cache(REFRESH_QUIET); |
4ed7cd3a BC |
396 | if (write_cache(fd, active_cache, active_nr) || |
397 | close_lock_file(&index_lock)) | |
f5bbc322 KH |
398 | die("unable to write new_index file"); |
399 | ||
2888605c | 400 | fd = hold_lock_file_for_update(&false_lock, |
a157400c JH |
401 | git_path("next-index-%"PRIuMAX, |
402 | (uintmax_t) getpid()), | |
acd3b9ec | 403 | LOCK_DIE_ON_ERROR); |
fa9dcf80 LT |
404 | |
405 | create_base_index(); | |
2888605c | 406 | add_remove_files(&partial); |
d37d3203 | 407 | refresh_cache(REFRESH_QUIET); |
f5bbc322 | 408 | |
4ed7cd3a BC |
409 | if (write_cache(fd, active_cache, active_nr) || |
410 | close_lock_file(&false_lock)) | |
2888605c | 411 | die("unable to write temporary index file"); |
959ba670 JK |
412 | |
413 | discard_cache(); | |
414 | read_cache_from(false_lock.filename); | |
415 | ||
2888605c | 416 | return false_lock.filename; |
f5bbc322 KH |
417 | } |
418 | ||
d249b098 JH |
419 | static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn, |
420 | struct wt_status *s) | |
f5bbc322 | 421 | { |
76e2f7ce JH |
422 | unsigned char sha1[20]; |
423 | ||
d249b098 JH |
424 | if (s->relative_paths) |
425 | s->prefix = prefix; | |
f5bbc322 KH |
426 | |
427 | if (amend) { | |
d249b098 JH |
428 | s->amend = 1; |
429 | s->reference = "HEAD^1"; | |
f5bbc322 | 430 | } |
d249b098 JH |
431 | s->verbose = verbose; |
432 | s->index_file = index_file; | |
433 | s->fp = fp; | |
434 | s->nowarn = nowarn; | |
76e2f7ce | 435 | s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0; |
f5bbc322 | 436 | |
76e2f7ce | 437 | wt_status_collect(s); |
7c9f7038 JK |
438 | |
439 | switch (status_format) { | |
440 | case STATUS_FORMAT_SHORT: | |
05a59a08 | 441 | wt_shortstatus_print(s, null_termination, status_show_branch); |
7c9f7038 JK |
442 | break; |
443 | case STATUS_FORMAT_PORCELAIN: | |
4a7cc2fd | 444 | wt_porcelain_print(s, null_termination); |
7c9f7038 JK |
445 | break; |
446 | case STATUS_FORMAT_LONG: | |
447 | wt_status_print(s); | |
448 | break; | |
449 | } | |
f5bbc322 | 450 | |
d249b098 | 451 | return s->commitable; |
f5bbc322 KH |
452 | } |
453 | ||
ec84bd00 PB |
454 | static int is_a_merge(const unsigned char *sha1) |
455 | { | |
456 | struct commit *commit = lookup_commit(sha1); | |
457 | if (!commit || parse_commit(commit)) | |
458 | die("could not parse HEAD commit"); | |
459 | return !!(commit->parents && commit->parents->next); | |
460 | } | |
461 | ||
f5bbc322 KH |
462 | static const char sign_off_header[] = "Signed-off-by: "; |
463 | ||
4c28e4ad | 464 | static void determine_author_info(struct strbuf *author_ident) |
a45d46ba SB |
465 | { |
466 | char *name, *email, *date; | |
467 | ||
468 | name = getenv("GIT_AUTHOR_NAME"); | |
469 | email = getenv("GIT_AUTHOR_EMAIL"); | |
470 | date = getenv("GIT_AUTHOR_DATE"); | |
471 | ||
c51f6cee | 472 | if (use_message && !renew_authorship) { |
a45d46ba SB |
473 | const char *a, *lb, *rb, *eol; |
474 | ||
475 | a = strstr(use_message_buffer, "\nauthor "); | |
476 | if (!a) | |
477 | die("invalid commit: %s", use_message); | |
478 | ||
fb7749e4 JN |
479 | lb = strchrnul(a + strlen("\nauthor "), '<'); |
480 | rb = strchrnul(lb, '>'); | |
481 | eol = strchrnul(rb, '\n'); | |
482 | if (!*lb || !*rb || !*eol) | |
a45d46ba SB |
483 | die("invalid commit: %s", use_message); |
484 | ||
fb7749e4 JN |
485 | if (lb == a + strlen("\nauthor ")) |
486 | /* \nauthor <foo@example.com> */ | |
487 | name = xcalloc(1, 1); | |
488 | else | |
489 | name = xmemdupz(a + strlen("\nauthor "), | |
490 | (lb - strlen(" ") - | |
491 | (a + strlen("\nauthor ")))); | |
492 | email = xmemdupz(lb + strlen("<"), rb - (lb + strlen("<"))); | |
493 | date = xmemdupz(rb + strlen("> "), eol - (rb + strlen("> "))); | |
a45d46ba SB |
494 | } |
495 | ||
496 | if (force_author) { | |
497 | const char *lb = strstr(force_author, " <"); | |
498 | const char *rb = strchr(force_author, '>'); | |
499 | ||
500 | if (!lb || !rb) | |
501 | die("malformed --author parameter"); | |
502 | name = xstrndup(force_author, lb - force_author); | |
503 | email = xstrndup(lb + 2, rb - (lb + 2)); | |
504 | } | |
505 | ||
02b47cd7 MV |
506 | if (force_date) |
507 | date = force_date; | |
4c28e4ad JH |
508 | strbuf_addstr(author_ident, fmt_ident(name, email, date, |
509 | IDENT_ERROR_ON_NO_NAME)); | |
a45d46ba SB |
510 | } |
511 | ||
c1e01b0c DB |
512 | static int ends_rfc2822_footer(struct strbuf *sb) |
513 | { | |
514 | int ch; | |
515 | int hit = 0; | |
516 | int i, j, k; | |
517 | int len = sb->len; | |
518 | int first = 1; | |
519 | const char *buf = sb->buf; | |
520 | ||
521 | for (i = len - 1; i > 0; i--) { | |
522 | if (hit && buf[i] == '\n') | |
523 | break; | |
524 | hit = (buf[i] == '\n'); | |
525 | } | |
526 | ||
527 | while (i < len - 1 && buf[i] == '\n') | |
528 | i++; | |
529 | ||
530 | for (; i < len; i = k) { | |
531 | for (k = i; k < len && buf[k] != '\n'; k++) | |
532 | ; /* do nothing */ | |
533 | k++; | |
534 | ||
535 | if ((buf[k] == ' ' || buf[k] == '\t') && !first) | |
536 | continue; | |
537 | ||
538 | first = 0; | |
539 | ||
540 | for (j = 0; i + j < len; j++) { | |
541 | ch = buf[i + j]; | |
542 | if (ch == ':') | |
543 | break; | |
544 | if (isalnum(ch) || | |
545 | (ch == '-')) | |
546 | continue; | |
547 | return 0; | |
548 | } | |
549 | } | |
550 | return 1; | |
551 | } | |
552 | ||
4c28e4ad JH |
553 | static char *cut_ident_timestamp_part(char *string) |
554 | { | |
555 | char *ket = strrchr(string, '>'); | |
556 | if (!ket || ket[1] != ' ') | |
557 | die("Malformed ident string: '%s'", string); | |
558 | *++ket = '\0'; | |
559 | return ket; | |
560 | } | |
561 | ||
d249b098 | 562 | static int prepare_to_commit(const char *index_file, const char *prefix, |
4c28e4ad JH |
563 | struct wt_status *s, |
564 | struct strbuf *author_ident) | |
f5bbc322 KH |
565 | { |
566 | struct stat statbuf; | |
4c28e4ad | 567 | struct strbuf committer_ident = STRBUF_INIT; |
bc5d248a | 568 | int commitable, saved_color_setting; |
f285a2d7 | 569 | struct strbuf sb = STRBUF_INIT; |
f5bbc322 | 570 | char *buffer; |
8089c85b PB |
571 | const char *hook_arg1 = NULL; |
572 | const char *hook_arg2 = NULL; | |
bb1ae3f6 | 573 | int ident_shown = 0; |
f5bbc322 | 574 | |
ec84bd00 PB |
575 | if (!no_verify && run_hook(index_file, "pre-commit", NULL)) |
576 | return 0; | |
f5bbc322 | 577 | |
89ac1223 PN |
578 | if (squash_message) { |
579 | /* | |
580 | * Insert the proper subject line before other commit | |
581 | * message options add their content. | |
582 | */ | |
583 | if (use_message && !strcmp(use_message, squash_message)) | |
584 | strbuf_addstr(&sb, "squash! "); | |
585 | else { | |
586 | struct pretty_print_context ctx = {0}; | |
587 | struct commit *c; | |
588 | c = lookup_commit_reference_by_name(squash_message); | |
589 | if (!c) | |
590 | die("could not lookup commit %s", squash_message); | |
591 | ctx.output_encoding = get_commit_output_encoding(); | |
592 | format_commit_message(c, "squash! %s\n\n", &sb, | |
593 | &ctx); | |
594 | } | |
595 | } | |
596 | ||
f9568530 JS |
597 | if (message.len) { |
598 | strbuf_addbuf(&sb, &message); | |
8089c85b | 599 | hook_arg1 = "message"; |
f5bbc322 KH |
600 | } else if (logfile && !strcmp(logfile, "-")) { |
601 | if (isatty(0)) | |
602 | fprintf(stderr, "(reading log message from standard input)\n"); | |
603 | if (strbuf_read(&sb, 0, 0) < 0) | |
0721c314 | 604 | die_errno("could not read log from standard input"); |
8089c85b | 605 | hook_arg1 = "message"; |
f5bbc322 KH |
606 | } else if (logfile) { |
607 | if (strbuf_read_file(&sb, logfile, 0) < 0) | |
d824cbba TR |
608 | die_errno("could not read log file '%s'", |
609 | logfile); | |
8089c85b | 610 | hook_arg1 = "message"; |
f5bbc322 KH |
611 | } else if (use_message) { |
612 | buffer = strstr(use_message_buffer, "\n\n"); | |
613 | if (!buffer || buffer[2] == '\0') | |
614 | die("commit has empty message"); | |
615 | strbuf_add(&sb, buffer + 2, strlen(buffer + 2)); | |
8089c85b PB |
616 | hook_arg1 = "commit"; |
617 | hook_arg2 = use_message; | |
d71b8ba7 PN |
618 | } else if (fixup_message) { |
619 | struct pretty_print_context ctx = {0}; | |
620 | struct commit *commit; | |
621 | commit = lookup_commit_reference_by_name(fixup_message); | |
622 | if (!commit) | |
623 | die("could not lookup commit %s", fixup_message); | |
624 | ctx.output_encoding = get_commit_output_encoding(); | |
625 | format_commit_message(commit, "fixup! %s\n\n", | |
626 | &sb, &ctx); | |
627 | hook_arg1 = "message"; | |
f5bbc322 KH |
628 | } else if (!stat(git_path("MERGE_MSG"), &statbuf)) { |
629 | if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0) | |
d824cbba | 630 | die_errno("could not read MERGE_MSG"); |
8089c85b | 631 | hook_arg1 = "merge"; |
f5bbc322 KH |
632 | } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) { |
633 | if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0) | |
d824cbba | 634 | die_errno("could not read SQUASH_MSG"); |
8089c85b | 635 | hook_arg1 = "squash"; |
f5bbc322 KH |
636 | } else if (template_file && !stat(template_file, &statbuf)) { |
637 | if (strbuf_read_file(&sb, template_file, 0) < 0) | |
d824cbba | 638 | die_errno("could not read '%s'", template_file); |
8089c85b | 639 | hook_arg1 = "template"; |
f5bbc322 KH |
640 | } |
641 | ||
8089c85b PB |
642 | /* |
643 | * This final case does not modify the template message, | |
644 | * it just sets the argument to the prepare-commit-msg hook. | |
645 | */ | |
646 | else if (in_merge) | |
647 | hook_arg1 = "merge"; | |
648 | ||
89ac1223 PN |
649 | if (squash_message) { |
650 | /* | |
651 | * If squash_commit was used for the commit subject, | |
652 | * then we're possibly hijacking other commit log options. | |
653 | * Reset the hook args to tell the real story. | |
654 | */ | |
655 | hook_arg1 = "message"; | |
656 | hook_arg2 = ""; | |
657 | } | |
658 | ||
37f3012f JN |
659 | s->fp = fopen(git_path(commit_editmsg), "w"); |
660 | if (s->fp == NULL) | |
d824cbba | 661 | die_errno("could not open '%s'", git_path(commit_editmsg)); |
f5bbc322 | 662 | |
5f065737 AR |
663 | if (cleanup_mode != CLEANUP_NONE) |
664 | stripspace(&sb, 0); | |
f5bbc322 KH |
665 | |
666 | if (signoff) { | |
f285a2d7 | 667 | struct strbuf sob = STRBUF_INIT; |
13208572 JS |
668 | int i; |
669 | ||
13208572 | 670 | strbuf_addstr(&sob, sign_off_header); |
d9ccfe77 JH |
671 | strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"), |
672 | getenv("GIT_COMMITTER_EMAIL"))); | |
13208572 | 673 | strbuf_addch(&sob, '\n'); |
13208572 JS |
674 | for (i = sb.len - 1; i > 0 && sb.buf[i - 1] != '\n'; i--) |
675 | ; /* do nothing */ | |
2150554b | 676 | if (prefixcmp(sb.buf + i, sob.buf)) { |
e5138436 | 677 | if (!i || !ends_rfc2822_footer(&sb)) |
2150554b | 678 | strbuf_addch(&sb, '\n'); |
13208572 | 679 | strbuf_addbuf(&sb, &sob); |
2150554b | 680 | } |
13208572 | 681 | strbuf_release(&sob); |
f5bbc322 KH |
682 | } |
683 | ||
37f3012f | 684 | if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len) |
d824cbba | 685 | die_errno("could not write commit template"); |
13208572 | 686 | |
f5bbc322 KH |
687 | strbuf_release(&sb); |
688 | ||
4c28e4ad JH |
689 | /* This checks and barfs if author is badly specified */ |
690 | determine_author_info(author_ident); | |
e83dbe80 | 691 | |
bb1ae3f6 | 692 | /* This checks if committer ident is explicitly given */ |
4c28e4ad | 693 | strbuf_addstr(&committer_ident, git_committer_info(0)); |
bed575e4 | 694 | if (use_editor && include_status) { |
4c28e4ad | 695 | char *ai_tmp, *ci_tmp; |
ec84bd00 | 696 | if (in_merge) |
37f3012f | 697 | fprintf(s->fp, |
ec84bd00 PB |
698 | "#\n" |
699 | "# It looks like you may be committing a MERGE.\n" | |
700 | "# If this is not correct, please remove the file\n" | |
701 | "# %s\n" | |
702 | "# and try again.\n" | |
703 | "#\n", | |
704 | git_path("MERGE_HEAD")); | |
705 | ||
37f3012f | 706 | fprintf(s->fp, |
ec84bd00 | 707 | "\n" |
fdc7c811 | 708 | "# Please enter the commit message for your changes."); |
ec84bd00 | 709 | if (cleanup_mode == CLEANUP_ALL) |
37f3012f | 710 | fprintf(s->fp, |
fdc7c811 JK |
711 | " Lines starting\n" |
712 | "# with '#' will be ignored, and an empty" | |
713 | " message aborts the commit.\n"); | |
ec84bd00 | 714 | else /* CLEANUP_SPACE, that is. */ |
37f3012f | 715 | fprintf(s->fp, |
fdc7c811 JK |
716 | " Lines starting\n" |
717 | "# with '#' will be kept; you may remove them" | |
718 | " yourself if you want to.\n" | |
719 | "# An empty message aborts the commit.\n"); | |
ec84bd00 | 720 | if (only_include_assumed) |
37f3012f | 721 | fprintf(s->fp, "# %s\n", only_include_assumed); |
ec84bd00 | 722 | |
4c28e4ad JH |
723 | ai_tmp = cut_ident_timestamp_part(author_ident->buf); |
724 | ci_tmp = cut_ident_timestamp_part(committer_ident.buf); | |
725 | if (strcmp(author_ident->buf, committer_ident.buf)) | |
37f3012f | 726 | fprintf(s->fp, |
bb1ae3f6 SB |
727 | "%s" |
728 | "# Author: %s\n", | |
729 | ident_shown++ ? "" : "#\n", | |
4c28e4ad | 730 | author_ident->buf); |
e83dbe80 | 731 | |
5aeb3a3a | 732 | if (!user_ident_sufficiently_given()) |
37f3012f | 733 | fprintf(s->fp, |
bb1ae3f6 SB |
734 | "%s" |
735 | "# Committer: %s\n", | |
736 | ident_shown++ ? "" : "#\n", | |
4c28e4ad | 737 | committer_ident.buf); |
bb1ae3f6 SB |
738 | |
739 | if (ident_shown) | |
37f3012f | 740 | fprintf(s->fp, "#\n"); |
bb1ae3f6 | 741 | |
d249b098 JH |
742 | saved_color_setting = s->use_color; |
743 | s->use_color = 0; | |
37f3012f | 744 | commitable = run_status(s->fp, index_file, prefix, 1, s); |
d249b098 | 745 | s->use_color = saved_color_setting; |
4c28e4ad JH |
746 | |
747 | *ai_tmp = ' '; | |
748 | *ci_tmp = ' '; | |
ec84bd00 | 749 | } else { |
d616a239 | 750 | unsigned char sha1[20]; |
fbcf1184 | 751 | const char *parent = "HEAD"; |
7168624c | 752 | |
7168624c AR |
753 | if (!active_nr && read_cache() < 0) |
754 | die("Cannot read index"); | |
755 | ||
fbcf1184 JH |
756 | if (amend) |
757 | parent = "HEAD^1"; | |
758 | ||
d616a239 | 759 | if (get_sha1(parent, sha1)) |
ec84bd00 | 760 | commitable = !!active_nr; |
75f3ff2e SB |
761 | else |
762 | commitable = index_differs_from(parent, 0); | |
ec84bd00 | 763 | } |
4c28e4ad | 764 | strbuf_release(&committer_ident); |
d616a239 | 765 | |
37f3012f | 766 | fclose(s->fp); |
7168624c | 767 | |
ec84bd00 PB |
768 | if (!commitable && !in_merge && !allow_empty && |
769 | !(amend && is_a_merge(head_sha1))) { | |
d249b098 | 770 | run_status(stdout, index_file, prefix, 0, s); |
f197ed2f JK |
771 | if (amend) |
772 | fputs(empty_amend_advice, stderr); | |
ec84bd00 | 773 | return 0; |
7168624c AR |
774 | } |
775 | ||
ec84bd00 PB |
776 | /* |
777 | * Re-read the index as pre-commit hook could have updated it, | |
778 | * and write it out as a tree. We must do this before we invoke | |
779 | * the editor and after we invoke run_status above. | |
780 | */ | |
781 | discard_cache(); | |
782 | read_cache_from(index_file); | |
783 | if (!active_cache_tree) | |
784 | active_cache_tree = cache_tree(); | |
785 | if (cache_tree_update(active_cache_tree, | |
786 | active_cache, active_nr, 0, 0) < 0) { | |
331fcb59 | 787 | error("Error building trees"); |
ec84bd00 | 788 | return 0; |
7168624c | 789 | } |
f5bbc322 | 790 | |
8089c85b PB |
791 | if (run_hook(index_file, "prepare-commit-msg", |
792 | git_path(commit_editmsg), hook_arg1, hook_arg2, NULL)) | |
793 | return 0; | |
f5bbc322 | 794 | |
ec84bd00 PB |
795 | if (use_editor) { |
796 | char index[PATH_MAX]; | |
66dbfd55 GV |
797 | const char *env[2] = { NULL }; |
798 | env[0] = index; | |
ec84bd00 | 799 | snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file); |
7198203a SB |
800 | if (launch_editor(git_path(commit_editmsg), NULL, env)) { |
801 | fprintf(stderr, | |
802 | "Please supply the message using either -m or -F option.\n"); | |
803 | exit(1); | |
804 | } | |
ec84bd00 | 805 | } |
f5bbc322 | 806 | |
ec84bd00 PB |
807 | if (!no_verify && |
808 | run_hook(index_file, "commit-msg", git_path(commit_editmsg), NULL)) { | |
809 | return 0; | |
810 | } | |
f5bbc322 | 811 | |
ec84bd00 | 812 | return 1; |
f5bbc322 KH |
813 | } |
814 | ||
815 | /* | |
6bb6b034 MV |
816 | * Find out if the message in the strbuf contains only whitespace and |
817 | * Signed-off-by lines. | |
f5bbc322 | 818 | */ |
6bb6b034 | 819 | static int message_is_empty(struct strbuf *sb) |
f5bbc322 | 820 | { |
f285a2d7 | 821 | struct strbuf tmpl = STRBUF_INIT; |
f5bbc322 | 822 | const char *nl; |
6bb6b034 | 823 | int eol, i, start = 0; |
f5bbc322 | 824 | |
5f065737 AR |
825 | if (cleanup_mode == CLEANUP_NONE && sb->len) |
826 | return 0; | |
827 | ||
f5bbc322 | 828 | /* See if the template is just a prefix of the message. */ |
f5bbc322 | 829 | if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) { |
5f065737 | 830 | stripspace(&tmpl, cleanup_mode == CLEANUP_ALL); |
f5bbc322 KH |
831 | if (start + tmpl.len <= sb->len && |
832 | memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0) | |
833 | start += tmpl.len; | |
834 | } | |
835 | strbuf_release(&tmpl); | |
836 | ||
837 | /* Check if the rest is just whitespace and Signed-of-by's. */ | |
838 | for (i = start; i < sb->len; i++) { | |
839 | nl = memchr(sb->buf + i, '\n', sb->len - i); | |
840 | if (nl) | |
841 | eol = nl - sb->buf; | |
842 | else | |
843 | eol = sb->len; | |
844 | ||
845 | if (strlen(sign_off_header) <= eol - i && | |
846 | !prefixcmp(sb->buf + i, sign_off_header)) { | |
847 | i = eol; | |
848 | continue; | |
849 | } | |
850 | while (i < eol) | |
851 | if (!isspace(sb->buf[i++])) | |
852 | return 0; | |
853 | } | |
854 | ||
855 | return 1; | |
856 | } | |
857 | ||
146ea068 JH |
858 | static const char *find_author_by_nickname(const char *name) |
859 | { | |
860 | struct rev_info revs; | |
861 | struct commit *commit; | |
862 | struct strbuf buf = STRBUF_INIT; | |
863 | const char *av[20]; | |
864 | int ac = 0; | |
865 | ||
866 | init_revisions(&revs, NULL); | |
867 | strbuf_addf(&buf, "--author=%s", name); | |
868 | av[++ac] = "--all"; | |
869 | av[++ac] = "-i"; | |
870 | av[++ac] = buf.buf; | |
871 | av[++ac] = NULL; | |
872 | setup_revisions(ac, av, &revs, NULL); | |
873 | prepare_revision_walk(&revs); | |
874 | commit = get_revision(&revs); | |
875 | if (commit) { | |
dd2e794a TR |
876 | struct pretty_print_context ctx = {0}; |
877 | ctx.date_mode = DATE_NORMAL; | |
146ea068 | 878 | strbuf_release(&buf); |
dd2e794a | 879 | format_commit_message(commit, "%an <%ae>", &buf, &ctx); |
146ea068 JH |
880 | return strbuf_detach(&buf, NULL); |
881 | } | |
882 | die("No existing author found with '%s'", name); | |
883 | } | |
884 | ||
76e2f7ce JH |
885 | |
886 | static void handle_untracked_files_arg(struct wt_status *s) | |
887 | { | |
888 | if (!untracked_files_arg) | |
889 | ; /* default already initialized */ | |
890 | else if (!strcmp(untracked_files_arg, "no")) | |
891 | s->show_untracked_files = SHOW_NO_UNTRACKED_FILES; | |
892 | else if (!strcmp(untracked_files_arg, "normal")) | |
893 | s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES; | |
894 | else if (!strcmp(untracked_files_arg, "all")) | |
895 | s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES; | |
896 | else | |
897 | die("Invalid untracked files mode '%s'", untracked_files_arg); | |
898 | } | |
899 | ||
2f02b25f | 900 | static int parse_and_validate_options(int argc, const char *argv[], |
dbd0f5c7 | 901 | const char * const usage[], |
d249b098 JH |
902 | const char *prefix, |
903 | struct wt_status *s) | |
f5bbc322 KH |
904 | { |
905 | int f = 0; | |
906 | ||
37782920 SB |
907 | argc = parse_options(argc, argv, prefix, builtin_commit_options, usage, |
908 | 0); | |
f5bbc322 | 909 | |
146ea068 JH |
910 | if (force_author && !strchr(force_author, '>')) |
911 | force_author = find_author_by_nickname(force_author); | |
912 | ||
c51f6cee EM |
913 | if (force_author && renew_authorship) |
914 | die("Using both --reset-author and --author does not make sense"); | |
915 | ||
d71b8ba7 | 916 | if (logfile || message.len || use_message || fixup_message) |
4803466f | 917 | use_editor = 0; |
f5bbc322 | 918 | if (edit_flag) |
4803466f | 919 | use_editor = 1; |
406400ce PB |
920 | if (!use_editor) |
921 | setenv("GIT_EDITOR", ":", 1); | |
f5bbc322 KH |
922 | |
923 | if (get_sha1("HEAD", head_sha1)) | |
924 | initial_commit = 1; | |
925 | ||
f5bbc322 KH |
926 | /* Sanity check options */ |
927 | if (amend && initial_commit) | |
928 | die("You have nothing to amend."); | |
929 | if (amend && in_merge) | |
b5b644a9 | 930 | die("You are in the middle of a merge -- cannot amend."); |
89ac1223 PN |
931 | if (fixup_message && squash_message) |
932 | die("Options --squash and --fixup cannot be used together"); | |
f5bbc322 KH |
933 | if (use_message) |
934 | f++; | |
935 | if (edit_message) | |
936 | f++; | |
d71b8ba7 PN |
937 | if (fixup_message) |
938 | f++; | |
f5bbc322 KH |
939 | if (logfile) |
940 | f++; | |
941 | if (f > 1) | |
d71b8ba7 | 942 | die("Only one of -c/-C/-F/--fixup can be used."); |
f9568530 | 943 | if (message.len && f > 0) |
d71b8ba7 | 944 | die("Option -m cannot be combined with -c/-C/-F/--fixup."); |
f5bbc322 KH |
945 | if (edit_message) |
946 | use_message = edit_message; | |
d71b8ba7 | 947 | if (amend && !use_message && !fixup_message) |
f5bbc322 | 948 | use_message = "HEAD"; |
c51f6cee EM |
949 | if (!use_message && renew_authorship) |
950 | die("--reset-author can be used only with -C, -c or --amend."); | |
f5bbc322 | 951 | if (use_message) { |
f5bbc322 | 952 | const char *out_enc; |
f5bbc322 KH |
953 | struct commit *commit; |
954 | ||
a6fa5992 PN |
955 | commit = lookup_commit_reference_by_name(use_message); |
956 | if (!commit) | |
f5bbc322 | 957 | die("could not lookup commit %s", use_message); |
a6fa5992 PN |
958 | out_enc = get_commit_output_encoding(); |
959 | use_message_buffer = logmsg_reencode(commit, out_enc); | |
f5bbc322 KH |
960 | |
961 | /* | |
962 | * If we failed to reencode the buffer, just copy it | |
963 | * byte for byte so the user can try to fix it up. | |
964 | * This also handles the case where input and output | |
965 | * encodings are identical. | |
966 | */ | |
967 | if (use_message_buffer == NULL) | |
968 | use_message_buffer = xstrdup(commit->buffer); | |
f5bbc322 KH |
969 | } |
970 | ||
971 | if (!!also + !!only + !!all + !!interactive > 1) | |
972 | die("Only one of --include/--only/--all/--interactive can be used."); | |
973 | if (argc == 0 && (also || (only && !amend))) | |
974 | die("No paths with --include/--only does not make sense."); | |
975 | if (argc == 0 && only && amend) | |
976 | only_include_assumed = "Clever... amending the last one with dirty index."; | |
3c5283f8 | 977 | if (argc > 0 && !also && !only) |
f5bbc322 | 978 | only_include_assumed = "Explicit paths specified without -i nor -o; assuming --only paths..."; |
5f065737 AR |
979 | if (!cleanup_arg || !strcmp(cleanup_arg, "default")) |
980 | cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE; | |
981 | else if (!strcmp(cleanup_arg, "verbatim")) | |
982 | cleanup_mode = CLEANUP_NONE; | |
983 | else if (!strcmp(cleanup_arg, "whitespace")) | |
984 | cleanup_mode = CLEANUP_SPACE; | |
985 | else if (!strcmp(cleanup_arg, "strip")) | |
986 | cleanup_mode = CLEANUP_ALL; | |
987 | else | |
988 | die("Invalid cleanup mode %s", cleanup_arg); | |
f5bbc322 | 989 | |
76e2f7ce | 990 | handle_untracked_files_arg(s); |
4bfee30a | 991 | |
f5bbc322 KH |
992 | if (all && argc > 0) |
993 | die("Paths with -a does not make sense."); | |
994 | else if (interactive && argc > 0) | |
995 | die("Paths with --interactive does not make sense."); | |
996 | ||
7c9f7038 JK |
997 | if (null_termination && status_format == STATUS_FORMAT_LONG) |
998 | status_format = STATUS_FORMAT_PORCELAIN; | |
999 | if (status_format != STATUS_FORMAT_LONG) | |
1000 | dry_run = 1; | |
1001 | ||
f5bbc322 KH |
1002 | return argc; |
1003 | } | |
1004 | ||
d249b098 JH |
1005 | static int dry_run_commit(int argc, const char **argv, const char *prefix, |
1006 | struct wt_status *s) | |
f5bbc322 | 1007 | { |
f5bbc322 | 1008 | int commitable; |
3a5d13a3 | 1009 | const char *index_file; |
f5bbc322 | 1010 | |
3a5d13a3 | 1011 | index_file = prepare_index(argc, argv, prefix, 1); |
d249b098 | 1012 | commitable = run_status(stdout, index_file, prefix, 0, s); |
3a5d13a3 | 1013 | rollback_index_files(); |
f5bbc322 | 1014 | |
3a5d13a3 JH |
1015 | return commitable ? 0 : 1; |
1016 | } | |
1017 | ||
f766b367 JH |
1018 | static int parse_status_slot(const char *var, int offset) |
1019 | { | |
1020 | if (!strcasecmp(var+offset, "header")) | |
1021 | return WT_STATUS_HEADER; | |
1d282327 AA |
1022 | if (!strcasecmp(var+offset, "branch")) |
1023 | return WT_STATUS_ONBRANCH; | |
f766b367 JH |
1024 | if (!strcasecmp(var+offset, "updated") |
1025 | || !strcasecmp(var+offset, "added")) | |
1026 | return WT_STATUS_UPDATED; | |
1027 | if (!strcasecmp(var+offset, "changed")) | |
1028 | return WT_STATUS_CHANGED; | |
1029 | if (!strcasecmp(var+offset, "untracked")) | |
1030 | return WT_STATUS_UNTRACKED; | |
1031 | if (!strcasecmp(var+offset, "nobranch")) | |
1032 | return WT_STATUS_NOBRANCH; | |
1033 | if (!strcasecmp(var+offset, "unmerged")) | |
1034 | return WT_STATUS_UNMERGED; | |
8b8e8624 | 1035 | return -1; |
f766b367 JH |
1036 | } |
1037 | ||
1038 | static int git_status_config(const char *k, const char *v, void *cb) | |
1039 | { | |
1040 | struct wt_status *s = cb; | |
1041 | ||
1042 | if (!strcmp(k, "status.submodulesummary")) { | |
1043 | int is_bool; | |
1044 | s->submodule_summary = git_config_bool_or_int(k, v, &is_bool); | |
1045 | if (is_bool && s->submodule_summary) | |
1046 | s->submodule_summary = -1; | |
1047 | return 0; | |
1048 | } | |
1049 | if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) { | |
1050 | s->use_color = git_config_colorbool(k, v, -1); | |
1051 | return 0; | |
1052 | } | |
1053 | if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) { | |
1054 | int slot = parse_status_slot(k, 13); | |
8b8e8624 JK |
1055 | if (slot < 0) |
1056 | return 0; | |
f766b367 JH |
1057 | if (!v) |
1058 | return config_error_nonbool(k); | |
1059 | color_parse(v, k, s->color_palette[slot]); | |
1060 | return 0; | |
1061 | } | |
1062 | if (!strcmp(k, "status.relativepaths")) { | |
1063 | s->relative_paths = git_config_bool(k, v); | |
1064 | return 0; | |
1065 | } | |
1066 | if (!strcmp(k, "status.showuntrackedfiles")) { | |
1067 | if (!v) | |
1068 | return config_error_nonbool(k); | |
1069 | else if (!strcmp(v, "no")) | |
1070 | s->show_untracked_files = SHOW_NO_UNTRACKED_FILES; | |
1071 | else if (!strcmp(v, "normal")) | |
1072 | s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES; | |
1073 | else if (!strcmp(v, "all")) | |
1074 | s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES; | |
1075 | else | |
1076 | return error("Invalid untracked files mode '%s'", v); | |
1077 | return 0; | |
1078 | } | |
1079 | return git_diff_ui_config(k, v, NULL); | |
1080 | } | |
1081 | ||
3a5d13a3 JH |
1082 | int cmd_status(int argc, const char **argv, const char *prefix) |
1083 | { | |
d249b098 | 1084 | struct wt_status s; |
4bb6644d | 1085 | int fd; |
76e2f7ce | 1086 | unsigned char sha1[20]; |
9e4b7ab6 | 1087 | static struct option builtin_status_options[] = { |
fd03881a | 1088 | OPT__VERBOSE(&verbose, "be verbose"), |
dd2be243 JK |
1089 | OPT_SET_INT('s', "short", &status_format, |
1090 | "show status concisely", STATUS_FORMAT_SHORT), | |
05a59a08 DKF |
1091 | OPT_BOOLEAN('b', "branch", &status_show_branch, |
1092 | "show branch information"), | |
6f157871 | 1093 | OPT_SET_INT(0, "porcelain", &status_format, |
ba9d7fe1 | 1094 | "machine-readable output", |
6f157871 | 1095 | STATUS_FORMAT_PORCELAIN), |
173e6c88 JH |
1096 | OPT_BOOLEAN('z', "null", &null_termination, |
1097 | "terminate entries with NUL"), | |
76e2f7ce JH |
1098 | { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, |
1099 | "mode", | |
1100 | "show untracked files, optional modes: all, normal, no. (Default: all)", | |
1101 | PARSE_OPT_OPTARG, NULL, (intptr_t)"all" }, | |
2381e39e JH |
1102 | OPT_BOOLEAN(0, "ignored", &show_ignored_in_status, |
1103 | "show ignored files"), | |
46a958b3 JL |
1104 | { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, "when", |
1105 | "ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)", | |
1106 | PARSE_OPT_OPTARG, NULL, (intptr_t)"all" }, | |
76e2f7ce JH |
1107 | OPT_END(), |
1108 | }; | |
1109 | ||
5d3dd915 NTND |
1110 | if (argc == 2 && !strcmp(argv[1], "-h")) |
1111 | usage_with_options(builtin_status_usage, builtin_status_options); | |
1112 | ||
dd2be243 | 1113 | if (null_termination && status_format == STATUS_FORMAT_LONG) |
6f157871 | 1114 | status_format = STATUS_FORMAT_PORCELAIN; |
d249b098 JH |
1115 | |
1116 | wt_status_prepare(&s); | |
302ad7a9 | 1117 | gitmodules_config(); |
d249b098 | 1118 | git_config(git_status_config, &s); |
30988301 | 1119 | in_merge = file_exists(git_path("MERGE_HEAD")); |
76e2f7ce | 1120 | argc = parse_options(argc, argv, prefix, |
9e4b7ab6 JH |
1121 | builtin_status_options, |
1122 | builtin_status_usage, 0); | |
76e2f7ce | 1123 | handle_untracked_files_arg(&s); |
2381e39e JH |
1124 | if (show_ignored_in_status) |
1125 | s.show_ignored_files = 1; | |
76e2f7ce JH |
1126 | if (*argv) |
1127 | s.pathspec = get_pathspec(prefix, argv); | |
1128 | ||
149794dd | 1129 | read_cache_preload(s.pathspec); |
688cd6d2 | 1130 | refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec, NULL, NULL); |
4bb6644d MH |
1131 | |
1132 | fd = hold_locked_index(&index_lock, 0); | |
1133 | if (0 <= fd) { | |
d5f5d0a9 JH |
1134 | if (active_cache_changed && |
1135 | !write_cache(fd, active_cache, active_nr)) | |
4bb6644d | 1136 | commit_locked_index(&index_lock); |
d5f5d0a9 JH |
1137 | else |
1138 | rollback_lock_file(&index_lock); | |
4bb6644d MH |
1139 | } |
1140 | ||
76e2f7ce | 1141 | s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0; |
3c588453 | 1142 | s.in_merge = in_merge; |
46a958b3 | 1143 | s.ignore_submodule_arg = ignore_submodule_arg; |
76e2f7ce JH |
1144 | wt_status_collect(&s); |
1145 | ||
8661768f JK |
1146 | if (s.relative_paths) |
1147 | s.prefix = prefix; | |
d249b098 JH |
1148 | if (s.use_color == -1) |
1149 | s.use_color = git_use_color_default; | |
38920dd6 MH |
1150 | if (diff_use_color_default == -1) |
1151 | diff_use_color_default = git_use_color_default; | |
1152 | ||
dd2be243 JK |
1153 | switch (status_format) { |
1154 | case STATUS_FORMAT_SHORT: | |
05a59a08 | 1155 | wt_shortstatus_print(&s, null_termination, status_show_branch); |
dd2be243 | 1156 | break; |
6f157871 | 1157 | case STATUS_FORMAT_PORCELAIN: |
4a7cc2fd | 1158 | wt_porcelain_print(&s, null_termination); |
6f157871 | 1159 | break; |
dd2be243 | 1160 | case STATUS_FORMAT_LONG: |
173e6c88 | 1161 | s.verbose = verbose; |
46a958b3 | 1162 | s.ignore_submodule_arg = ignore_submodule_arg; |
173e6c88 | 1163 | wt_status_print(&s); |
dd2be243 | 1164 | break; |
173e6c88 | 1165 | } |
76e2f7ce | 1166 | return 0; |
f5bbc322 KH |
1167 | } |
1168 | ||
f5bbc322 KH |
1169 | static void print_summary(const char *prefix, const unsigned char *sha1) |
1170 | { | |
1171 | struct rev_info rev; | |
1172 | struct commit *commit; | |
49ff9a7a | 1173 | struct strbuf format = STRBUF_INIT; |
c85db254 JK |
1174 | unsigned char junk_sha1[20]; |
1175 | const char *head = resolve_ref("HEAD", junk_sha1, 0, NULL); | |
49ff9a7a JK |
1176 | struct pretty_print_context pctx = {0}; |
1177 | struct strbuf author_ident = STRBUF_INIT; | |
1178 | struct strbuf committer_ident = STRBUF_INIT; | |
f5bbc322 KH |
1179 | |
1180 | commit = lookup_commit(sha1); | |
1181 | if (!commit) | |
b5b644a9 | 1182 | die("couldn't look up newly created commit"); |
f5bbc322 KH |
1183 | if (!commit || parse_commit(commit)) |
1184 | die("could not parse newly created commit"); | |
1185 | ||
49ff9a7a JK |
1186 | strbuf_addstr(&format, "format:%h] %s"); |
1187 | ||
1188 | format_commit_message(commit, "%an <%ae>", &author_ident, &pctx); | |
1189 | format_commit_message(commit, "%cn <%ce>", &committer_ident, &pctx); | |
1190 | if (strbuf_cmp(&author_ident, &committer_ident)) { | |
1191 | strbuf_addstr(&format, "\n Author: "); | |
1192 | strbuf_addbuf_percentquote(&format, &author_ident); | |
1193 | } | |
1a893064 | 1194 | if (!user_ident_sufficiently_given()) { |
49ff9a7a JK |
1195 | strbuf_addstr(&format, "\n Committer: "); |
1196 | strbuf_addbuf_percentquote(&format, &committer_ident); | |
b706fcfe JK |
1197 | if (advice_implicit_identity) { |
1198 | strbuf_addch(&format, '\n'); | |
1199 | strbuf_addstr(&format, implicit_ident_advice); | |
1200 | } | |
49ff9a7a JK |
1201 | } |
1202 | strbuf_release(&author_ident); | |
1203 | strbuf_release(&committer_ident); | |
1204 | ||
f5bbc322 KH |
1205 | init_revisions(&rev, prefix); |
1206 | setup_revisions(0, NULL, &rev, NULL); | |
1207 | ||
f5bbc322 KH |
1208 | rev.diff = 1; |
1209 | rev.diffopt.output_format = | |
1210 | DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY; | |
1211 | ||
1212 | rev.verbose_header = 1; | |
1213 | rev.show_root_diff = 1; | |
49ff9a7a | 1214 | get_commit_format(format.buf, &rev); |
bf82a150 | 1215 | rev.always_show_header = 0; |
3eb2a15e JH |
1216 | rev.diffopt.detect_rename = 1; |
1217 | rev.diffopt.rename_limit = 100; | |
1218 | rev.diffopt.break_opt = 0; | |
15964563 | 1219 | diff_setup_done(&rev.diffopt); |
f5bbc322 | 1220 | |
c5ee71fd | 1221 | printf("[%s%s ", |
c85db254 JK |
1222 | !prefixcmp(head, "refs/heads/") ? |
1223 | head + 11 : | |
1224 | !strcmp(head, "HEAD") ? | |
1225 | "detached HEAD" : | |
1226 | head, | |
1227 | initial_commit ? " (root-commit)" : ""); | |
f5bbc322 | 1228 | |
bf82a150 | 1229 | if (!log_tree_commit(&rev, commit)) { |
a45e1a87 TRC |
1230 | rev.always_show_header = 1; |
1231 | rev.use_terminator = 1; | |
1232 | log_tree_commit(&rev, commit); | |
bf82a150 | 1233 | } |
a45e1a87 | 1234 | |
fc6f19fe | 1235 | strbuf_release(&format); |
f5bbc322 KH |
1236 | } |
1237 | ||
186458b1 | 1238 | static int git_commit_config(const char *k, const char *v, void *cb) |
f5bbc322 | 1239 | { |
d249b098 JH |
1240 | struct wt_status *s = cb; |
1241 | ||
984c6e7e | 1242 | if (!strcmp(k, "commit.template")) |
395de250 | 1243 | return git_config_pathname(&template_file, k, v); |
bed575e4 JHI |
1244 | if (!strcmp(k, "commit.status")) { |
1245 | include_status = git_config_bool(k, v); | |
1246 | return 0; | |
1247 | } | |
f5bbc322 | 1248 | |
d249b098 | 1249 | return git_status_config(k, v, s); |
f5bbc322 KH |
1250 | } |
1251 | ||
6f6bee3b TR |
1252 | static const char post_rewrite_hook[] = "hooks/post-rewrite"; |
1253 | ||
1254 | static int run_rewrite_hook(const unsigned char *oldsha1, | |
1255 | const unsigned char *newsha1) | |
1256 | { | |
1257 | /* oldsha1 SP newsha1 LF NUL */ | |
1258 | static char buf[2*40 + 3]; | |
1259 | struct child_process proc; | |
1260 | const char *argv[3]; | |
1261 | int code; | |
1262 | size_t n; | |
1263 | ||
1264 | if (access(git_path(post_rewrite_hook), X_OK) < 0) | |
1265 | return 0; | |
1266 | ||
1267 | argv[0] = git_path(post_rewrite_hook); | |
1268 | argv[1] = "amend"; | |
1269 | argv[2] = NULL; | |
1270 | ||
1271 | memset(&proc, 0, sizeof(proc)); | |
1272 | proc.argv = argv; | |
1273 | proc.in = -1; | |
1274 | proc.stdout_to_stderr = 1; | |
1275 | ||
1276 | code = start_command(&proc); | |
1277 | if (code) | |
1278 | return code; | |
1279 | n = snprintf(buf, sizeof(buf), "%s %s\n", | |
1280 | sha1_to_hex(oldsha1), sha1_to_hex(newsha1)); | |
1281 | write_in_full(proc.in, buf, n); | |
1282 | close(proc.in); | |
1283 | return finish_command(&proc); | |
1284 | } | |
1285 | ||
f5bbc322 KH |
1286 | int cmd_commit(int argc, const char **argv, const char *prefix) |
1287 | { | |
f285a2d7 | 1288 | struct strbuf sb = STRBUF_INIT; |
4c28e4ad | 1289 | struct strbuf author_ident = STRBUF_INIT; |
f5bbc322 | 1290 | const char *index_file, *reflog_msg; |
99a12694 | 1291 | char *nl, *p; |
f5bbc322 KH |
1292 | unsigned char commit_sha1[20]; |
1293 | struct ref_lock *ref_lock; | |
6bb6b034 | 1294 | struct commit_list *parents = NULL, **pptr = &parents; |
cf10f9fd MV |
1295 | struct stat statbuf; |
1296 | int allow_fast_forward = 1; | |
d249b098 | 1297 | struct wt_status s; |
f5bbc322 | 1298 | |
5d3dd915 NTND |
1299 | if (argc == 2 && !strcmp(argv[1], "-h")) |
1300 | usage_with_options(builtin_commit_usage, builtin_commit_options); | |
1301 | ||
d249b098 JH |
1302 | wt_status_prepare(&s); |
1303 | git_config(git_commit_config, &s); | |
30988301 | 1304 | in_merge = file_exists(git_path("MERGE_HEAD")); |
3c588453 | 1305 | s.in_merge = in_merge; |
f5bbc322 | 1306 | |
d249b098 JH |
1307 | if (s.use_color == -1) |
1308 | s.use_color = git_use_color_default; | |
d249b098 JH |
1309 | argc = parse_and_validate_options(argc, argv, builtin_commit_usage, |
1310 | prefix, &s); | |
3fa509df JH |
1311 | if (dry_run) { |
1312 | if (diff_use_color_default == -1) | |
1313 | diff_use_color_default = git_use_color_default; | |
d249b098 | 1314 | return dry_run_commit(argc, argv, prefix, &s); |
3fa509df | 1315 | } |
50b7e70f | 1316 | index_file = prepare_index(argc, argv, prefix, 0); |
f5bbc322 | 1317 | |
ec84bd00 PB |
1318 | /* Set up everything for writing the commit object. This includes |
1319 | running hooks, writing the trees, and interacting with the user. */ | |
4c28e4ad | 1320 | if (!prepare_to_commit(index_file, prefix, &s, &author_ident)) { |
2888605c | 1321 | rollback_index_files(); |
f5bbc322 KH |
1322 | return 1; |
1323 | } | |
1324 | ||
f5bbc322 | 1325 | /* Determine parents */ |
643cb5f7 | 1326 | reflog_msg = getenv("GIT_REFLOG_ACTION"); |
f5bbc322 | 1327 | if (initial_commit) { |
643cb5f7 CC |
1328 | if (!reflog_msg) |
1329 | reflog_msg = "commit (initial)"; | |
f5bbc322 KH |
1330 | } else if (amend) { |
1331 | struct commit_list *c; | |
1332 | struct commit *commit; | |
1333 | ||
643cb5f7 CC |
1334 | if (!reflog_msg) |
1335 | reflog_msg = "commit (amend)"; | |
f5bbc322 KH |
1336 | commit = lookup_commit(head_sha1); |
1337 | if (!commit || parse_commit(commit)) | |
1338 | die("could not parse HEAD commit"); | |
1339 | ||
1340 | for (c = commit->parents; c; c = c->next) | |
6bb6b034 | 1341 | pptr = &commit_list_insert(c->item, pptr)->next; |
f5bbc322 | 1342 | } else if (in_merge) { |
f285a2d7 | 1343 | struct strbuf m = STRBUF_INIT; |
f5bbc322 KH |
1344 | FILE *fp; |
1345 | ||
643cb5f7 CC |
1346 | if (!reflog_msg) |
1347 | reflog_msg = "commit (merge)"; | |
6bb6b034 | 1348 | pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next; |
f5bbc322 KH |
1349 | fp = fopen(git_path("MERGE_HEAD"), "r"); |
1350 | if (fp == NULL) | |
d824cbba TR |
1351 | die_errno("could not open '%s' for reading", |
1352 | git_path("MERGE_HEAD")); | |
7c3fd25d LT |
1353 | while (strbuf_getline(&m, fp, '\n') != EOF) { |
1354 | unsigned char sha1[20]; | |
1355 | if (get_sha1_hex(m.buf, sha1) < 0) | |
1356 | die("Corrupt MERGE_HEAD file (%s)", m.buf); | |
6bb6b034 | 1357 | pptr = &commit_list_insert(lookup_commit(sha1), pptr)->next; |
7c3fd25d | 1358 | } |
f5bbc322 KH |
1359 | fclose(fp); |
1360 | strbuf_release(&m); | |
cf10f9fd MV |
1361 | if (!stat(git_path("MERGE_MODE"), &statbuf)) { |
1362 | if (strbuf_read_file(&sb, git_path("MERGE_MODE"), 0) < 0) | |
d824cbba | 1363 | die_errno("could not read MERGE_MODE"); |
cf10f9fd MV |
1364 | if (!strcmp(sb.buf, "no-ff")) |
1365 | allow_fast_forward = 0; | |
1366 | } | |
1367 | if (allow_fast_forward) | |
1368 | parents = reduce_heads(parents); | |
f5bbc322 | 1369 | } else { |
643cb5f7 CC |
1370 | if (!reflog_msg) |
1371 | reflog_msg = "commit"; | |
6bb6b034 | 1372 | pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next; |
f5bbc322 | 1373 | } |
f5bbc322 | 1374 | |
ec84bd00 | 1375 | /* Finally, get the commit message */ |
cf10f9fd | 1376 | strbuf_reset(&sb); |
740001a5 | 1377 | if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) { |
0721c314 | 1378 | int saved_errno = errno; |
740001a5 | 1379 | rollback_index_files(); |
0721c314 | 1380 | die("could not read commit message: %s", strerror(saved_errno)); |
740001a5 | 1381 | } |
99a12694 KH |
1382 | |
1383 | /* Truncate the message just before the diff, if any. */ | |
0b38227f JK |
1384 | if (verbose) { |
1385 | p = strstr(sb.buf, "\ndiff --git "); | |
1386 | if (p != NULL) | |
1387 | strbuf_setlen(&sb, p - sb.buf + 1); | |
1388 | } | |
99a12694 | 1389 | |
5f065737 AR |
1390 | if (cleanup_mode != CLEANUP_NONE) |
1391 | stripspace(&sb, cleanup_mode == CLEANUP_ALL); | |
c9b5fde7 | 1392 | if (message_is_empty(&sb) && !allow_empty_message) { |
2888605c | 1393 | rollback_index_files(); |
fdc7c811 JK |
1394 | fprintf(stderr, "Aborting commit due to empty commit message.\n"); |
1395 | exit(1); | |
2888605c | 1396 | } |
f5bbc322 | 1397 | |
6bb6b034 | 1398 | if (commit_tree(sb.buf, active_cache_tree->sha1, parents, commit_sha1, |
4c28e4ad | 1399 | author_ident.buf)) { |
2888605c | 1400 | rollback_index_files(); |
f5bbc322 | 1401 | die("failed to write commit object"); |
2888605c | 1402 | } |
4c28e4ad | 1403 | strbuf_release(&author_ident); |
f5bbc322 KH |
1404 | |
1405 | ref_lock = lock_any_ref_for_update("HEAD", | |
1406 | initial_commit ? NULL : head_sha1, | |
1407 | 0); | |
1408 | ||
6bb6b034 | 1409 | nl = strchr(sb.buf, '\n'); |
741707b1 JS |
1410 | if (nl) |
1411 | strbuf_setlen(&sb, nl + 1 - sb.buf); | |
1412 | else | |
1413 | strbuf_addch(&sb, '\n'); | |
741707b1 JS |
1414 | strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg)); |
1415 | strbuf_insert(&sb, strlen(reflog_msg), ": ", 2); | |
f5bbc322 | 1416 | |
2888605c JH |
1417 | if (!ref_lock) { |
1418 | rollback_index_files(); | |
f5bbc322 | 1419 | die("cannot lock HEAD ref"); |
2888605c JH |
1420 | } |
1421 | if (write_ref_sha1(ref_lock, commit_sha1, sb.buf) < 0) { | |
1422 | rollback_index_files(); | |
f5bbc322 | 1423 | die("cannot update HEAD ref"); |
2888605c | 1424 | } |
f5bbc322 KH |
1425 | |
1426 | unlink(git_path("MERGE_HEAD")); | |
1427 | unlink(git_path("MERGE_MSG")); | |
cf10f9fd | 1428 | unlink(git_path("MERGE_MODE")); |
5a95b855 | 1429 | unlink(git_path("SQUASH_MSG")); |
f5bbc322 | 1430 | |
5a9dd399 BC |
1431 | if (commit_index_files()) |
1432 | die ("Repository has been updated, but unable to write\n" | |
1433 | "new_index file. Check that disk is not full or quota is\n" | |
1434 | "not exceeded, and then \"git reset HEAD\" to recover."); | |
f5bbc322 | 1435 | |
cb6020bb | 1436 | rerere(0); |
2888605c | 1437 | run_hook(get_index_file(), "post-commit", NULL); |
6f6bee3b | 1438 | if (amend && !no_post_rewrite) { |
6360d343 TR |
1439 | struct notes_rewrite_cfg *cfg; |
1440 | cfg = init_copy_notes_for_rewrite("amend"); | |
1441 | if (cfg) { | |
1442 | copy_note_for_rewrite(cfg, head_sha1, commit_sha1); | |
1443 | finish_copy_notes_for_rewrite(cfg); | |
1444 | } | |
6f6bee3b TR |
1445 | run_rewrite_hook(head_sha1, commit_sha1); |
1446 | } | |
f5bbc322 KH |
1447 | if (!quiet) |
1448 | print_summary(prefix, commit_sha1); | |
1449 | ||
1450 | return 0; | |
1451 | } |