]>
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" | |
b2141fc1 | 9 | #include "config.h" |
697cc8ef | 10 | #include "lockfile.h" |
f5bbc322 | 11 | #include "cache-tree.h" |
6b2f2d98 | 12 | #include "color.h" |
2888605c | 13 | #include "dir.h" |
f5bbc322 KH |
14 | #include "builtin.h" |
15 | #include "diff.h" | |
16 | #include "diffcore.h" | |
17 | #include "commit.h" | |
18 | #include "revision.h" | |
19 | #include "wt-status.h" | |
20 | #include "run-command.h" | |
21 | #include "refs.h" | |
22 | #include "log-tree.h" | |
23 | #include "strbuf.h" | |
24 | #include "utf8.h" | |
25 | #include "parse-options.h" | |
c455c87c | 26 | #include "string-list.h" |
5b2fd956 | 27 | #include "rerere.h" |
fa9dcf80 | 28 | #include "unpack-trees.h" |
76e2f7ce | 29 | #include "quote.h" |
302ad7a9 | 30 | #include "submodule.h" |
ba3c69a9 | 31 | #include "gpg-interface.h" |
323d0530 | 32 | #include "column.h" |
5ed75e2a | 33 | #include "sequencer.h" |
ea16794e | 34 | #include "mailmap.h" |
f5bbc322 KH |
35 | |
36 | static const char * const builtin_commit_usage[] = { | |
9c9b4f2f | 37 | N_("git commit [<options>] [--] <pathspec>..."), |
f5bbc322 KH |
38 | NULL |
39 | }; | |
40 | ||
2f02b25f | 41 | static const char * const builtin_status_usage[] = { |
9c9b4f2f | 42 | N_("git status [<options>] [--] <pathspec>..."), |
2f02b25f SB |
43 | NULL |
44 | }; | |
45 | ||
f197ed2f | 46 | static const char empty_amend_advice[] = |
fc88e316 | 47 | N_("You asked to amend the most recent commit, but doing so would make\n" |
f197ed2f | 48 | "it empty. You can repeat your command with --allow-empty, or you can\n" |
fc88e316 | 49 | "remove the commit entirely with \"git reset HEAD^\".\n"); |
f197ed2f | 50 | |
37f7a857 | 51 | static const char empty_cherry_pick_advice[] = |
6c80cd29 | 52 | N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n" |
37f7a857 JS |
53 | "If you wish to commit it anyway, use:\n" |
54 | "\n" | |
55 | " git commit --allow-empty\n" | |
c17592a7 JK |
56 | "\n"); |
57 | ||
58 | static const char empty_cherry_pick_advice_single[] = | |
59 | N_("Otherwise, please use 'git reset'\n"); | |
60 | ||
61 | static const char empty_cherry_pick_advice_multi[] = | |
62 | N_("If you wish to skip this commit, use:\n" | |
37f7a857 | 63 | "\n" |
c17592a7 JK |
64 | " git reset\n" |
65 | "\n" | |
66 | "Then \"git cherry-pick --continue\" will resume cherry-picking\n" | |
67 | "the remaining commits.\n"); | |
37f7a857 | 68 | |
e51b0dfc PB |
69 | static GIT_PATH_FUNC(git_path_commit_editmsg, "COMMIT_EDITMSG") |
70 | ||
37f7a857 | 71 | static const char *use_message_buffer; |
2888605c JH |
72 | static struct lock_file index_lock; /* real index */ |
73 | static struct lock_file false_lock; /* used only for partial commits */ | |
74 | static enum { | |
75 | COMMIT_AS_IS = 1, | |
76 | COMMIT_NORMAL, | |
4b05548f | 77 | COMMIT_PARTIAL |
2888605c | 78 | } commit_style; |
f5bbc322 | 79 | |
dbd0f5c7 | 80 | static const char *logfile, *force_author; |
984c6e7e | 81 | static const char *template_file; |
37f7a857 JS |
82 | /* |
83 | * The _message variables are commit names from which to take | |
84 | * the commit message and/or authorship. | |
85 | */ | |
86 | static const char *author_message, *author_message_buffer; | |
f5bbc322 | 87 | static char *edit_message, *use_message; |
89ac1223 | 88 | static char *fixup_message, *squash_message; |
ca1ba201 JH |
89 | static int all, also, interactive, patch_interactive, only, amend, signoff; |
90 | static int edit_flag = -1; /* unspecified */ | |
c51f6cee | 91 | static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship; |
aaab8420 | 92 | static int config_commit_verbose = -1; /* unspecified */ |
c9b5fde7 | 93 | static int no_post_rewrite, allow_empty_message; |
46a958b3 | 94 | static char *untracked_files_arg, *force_date, *ignore_submodule_arg; |
ba3c69a9 JH |
95 | static char *sign_commit; |
96 | ||
5f065737 AR |
97 | /* |
98 | * The default commit message cleanup mode will remove the lines | |
99 | * beginning with # (shell comments) and leading and trailing | |
100 | * whitespaces (empty lines or containing only whitespaces) | |
101 | * if editor is used, and only the whitespaces if the message | |
102 | * is specified explicitly. | |
103 | */ | |
d0aaa46f | 104 | static enum commit_msg_cleanup_mode cleanup_mode; |
51fb3a3d | 105 | static const char *cleanup_arg; |
f5bbc322 | 106 | |
37f7a857 | 107 | static enum commit_whence whence; |
c17592a7 | 108 | static int sequencer_in_use; |
06bb643b | 109 | static int use_editor = 1, include_status = 1; |
25206778 | 110 | static int show_ignored_in_status, have_option_m; |
2c47789d | 111 | static struct strbuf message = STRBUF_INIT; |
f9568530 | 112 | |
be7e795e | 113 | static enum wt_status_format status_format = STATUS_FORMAT_UNSPECIFIED; |
84b4202d | 114 | |
c4f596b9 JH |
115 | static int opt_parse_porcelain(const struct option *opt, const char *arg, int unset) |
116 | { | |
117 | enum wt_status_format *value = (enum wt_status_format *)opt->value; | |
118 | if (unset) | |
119 | *value = STATUS_FORMAT_NONE; | |
120 | else if (!arg) | |
121 | *value = STATUS_FORMAT_PORCELAIN; | |
122 | else if (!strcmp(arg, "v1") || !strcmp(arg, "1")) | |
123 | *value = STATUS_FORMAT_PORCELAIN; | |
1ecdecce JH |
124 | else if (!strcmp(arg, "v2") || !strcmp(arg, "2")) |
125 | *value = STATUS_FORMAT_PORCELAIN_V2; | |
c4f596b9 JH |
126 | else |
127 | die("unsupported porcelain version '%s'", arg); | |
128 | ||
129 | return 0; | |
130 | } | |
7c9f7038 | 131 | |
f9568530 JS |
132 | static int opt_parse_m(const struct option *opt, const char *arg, int unset) |
133 | { | |
134 | struct strbuf *buf = opt->value; | |
25206778 RS |
135 | if (unset) { |
136 | have_option_m = 0; | |
f9568530 | 137 | strbuf_setlen(buf, 0); |
25206778 RS |
138 | } else { |
139 | have_option_m = 1; | |
a24a41ea BC |
140 | if (buf->len) |
141 | strbuf_addch(buf, '\n'); | |
f9568530 | 142 | strbuf_addstr(buf, arg); |
a24a41ea | 143 | strbuf_complete_line(buf); |
f9568530 JS |
144 | } |
145 | return 0; | |
146 | } | |
f5bbc322 | 147 | |
37f7a857 JS |
148 | static void determine_whence(struct wt_status *s) |
149 | { | |
f932729c | 150 | if (file_exists(git_path_merge_head())) |
37f7a857 | 151 | whence = FROM_MERGE; |
f932729c | 152 | else if (file_exists(git_path_cherry_pick_head())) { |
37f7a857 | 153 | whence = FROM_CHERRY_PICK; |
8a2a0f53 | 154 | if (file_exists(git_path_seq_dir())) |
c17592a7 JK |
155 | sequencer_in_use = 1; |
156 | } | |
37f7a857 JS |
157 | else |
158 | whence = FROM_COMMIT; | |
159 | if (s) | |
160 | s->whence = whence; | |
161 | } | |
162 | ||
5c25dfaa MM |
163 | static void status_init_config(struct wt_status *s, config_fn_t fn) |
164 | { | |
165 | wt_status_prepare(s); | |
5c25dfaa MM |
166 | git_config(fn, s); |
167 | determine_whence(s); | |
5404c116 | 168 | init_diff_ui_defaults(); |
6a964f57 | 169 | s->hints = advice_status_hints; /* must come after git_config() */ |
5c25dfaa MM |
170 | } |
171 | ||
2888605c JH |
172 | static void rollback_index_files(void) |
173 | { | |
174 | switch (commit_style) { | |
175 | case COMMIT_AS_IS: | |
176 | break; /* nothing to do */ | |
177 | case COMMIT_NORMAL: | |
178 | rollback_lock_file(&index_lock); | |
179 | break; | |
180 | case COMMIT_PARTIAL: | |
181 | rollback_lock_file(&index_lock); | |
182 | rollback_lock_file(&false_lock); | |
183 | break; | |
184 | } | |
185 | } | |
186 | ||
5a9dd399 | 187 | static int commit_index_files(void) |
2888605c | 188 | { |
5a9dd399 BC |
189 | int err = 0; |
190 | ||
2888605c JH |
191 | switch (commit_style) { |
192 | case COMMIT_AS_IS: | |
193 | break; /* nothing to do */ | |
194 | case COMMIT_NORMAL: | |
5a9dd399 | 195 | err = commit_lock_file(&index_lock); |
2888605c JH |
196 | break; |
197 | case COMMIT_PARTIAL: | |
5a9dd399 | 198 | err = commit_lock_file(&index_lock); |
2888605c JH |
199 | rollback_lock_file(&false_lock); |
200 | break; | |
201 | } | |
5a9dd399 BC |
202 | |
203 | return err; | |
2888605c JH |
204 | } |
205 | ||
206 | /* | |
207 | * Take a union of paths in the index and the named tree (typically, "HEAD"), | |
208 | * and return the paths that match the given pattern in list. | |
209 | */ | |
c455c87c | 210 | static int list_paths(struct string_list *list, const char *with_tree, |
17ddc66e | 211 | const char *prefix, const struct pathspec *pattern) |
2888605c | 212 | { |
5d0b9bf8 | 213 | int i, ret; |
2888605c JH |
214 | char *m; |
215 | ||
17ddc66e | 216 | if (!pattern->nr) |
ea2d4ed3 JK |
217 | return 0; |
218 | ||
17ddc66e | 219 | m = xcalloc(1, pattern->nr); |
2888605c | 220 | |
8894d535 | 221 | if (with_tree) { |
f950eb95 | 222 | char *max_prefix = common_prefix(pattern); |
312c984a BW |
223 | overlay_tree_on_index(&the_index, with_tree, |
224 | max_prefix ? max_prefix : prefix); | |
5879f568 | 225 | free(max_prefix); |
8894d535 | 226 | } |
2888605c JH |
227 | |
228 | for (i = 0; i < active_nr; i++) { | |
9c5e6c80 | 229 | const struct cache_entry *ce = active_cache[i]; |
7fce6e3c NTND |
230 | struct string_list_item *item; |
231 | ||
7a51ed66 | 232 | if (ce->ce_flags & CE_UPDATE) |
e87e22d0 | 233 | continue; |
429bb40a | 234 | if (!ce_path_match(ce, pattern, m)) |
2888605c | 235 | continue; |
78a395d3 | 236 | item = string_list_insert(list, ce->name); |
7fce6e3c NTND |
237 | if (ce_skip_worktree(ce)) |
238 | item->util = item; /* better a valid pointer than a fake one */ | |
2888605c JH |
239 | } |
240 | ||
5d0b9bf8 SB |
241 | ret = report_path_error(m, pattern, prefix); |
242 | free(m); | |
243 | return ret; | |
2888605c JH |
244 | } |
245 | ||
c455c87c | 246 | static void add_remove_files(struct string_list *list) |
2888605c JH |
247 | { |
248 | int i; | |
249 | for (i = 0; i < list->nr; i++) { | |
d177cab0 | 250 | struct stat st; |
c455c87c | 251 | struct string_list_item *p = &(list->items[i]); |
d177cab0 | 252 | |
7fce6e3c NTND |
253 | /* p->util is skip-worktree */ |
254 | if (p->util) | |
b4d1690d | 255 | continue; |
d177cab0 | 256 | |
c455c87c JS |
257 | if (!lstat(p->string, &st)) { |
258 | if (add_to_cache(p->string, &st, 0)) | |
8a6179bc | 259 | die(_("updating files failed")); |
960b8ad1 | 260 | } else |
c455c87c | 261 | remove_file_from_cache(p->string); |
2888605c JH |
262 | } |
263 | } | |
264 | ||
06bb643b | 265 | static void create_base_index(const struct commit *current_head) |
fa9dcf80 LT |
266 | { |
267 | struct tree *tree; | |
268 | struct unpack_trees_options opts; | |
269 | struct tree_desc t; | |
270 | ||
06bb643b | 271 | if (!current_head) { |
fa9dcf80 LT |
272 | discard_cache(); |
273 | return; | |
274 | } | |
275 | ||
276 | memset(&opts, 0, sizeof(opts)); | |
277 | opts.head_idx = 1; | |
278 | opts.index_only = 1; | |
279 | opts.merge = 1; | |
34110cd4 LT |
280 | opts.src_index = &the_index; |
281 | opts.dst_index = &the_index; | |
fa9dcf80 LT |
282 | |
283 | opts.fn = oneway_merge; | |
a9dbc179 | 284 | tree = parse_tree_indirect(¤t_head->object.oid); |
fa9dcf80 | 285 | if (!tree) |
8a6179bc | 286 | die(_("failed to unpack HEAD tree object")); |
fa9dcf80 LT |
287 | parse_tree(tree); |
288 | init_tree_desc(&t, tree->buffer, tree->size); | |
203a2fe1 DB |
289 | if (unpack_trees(1, &t, &opts)) |
290 | exit(128); /* We've already reported the error, finish dying */ | |
fa9dcf80 LT |
291 | } |
292 | ||
d38a30df MM |
293 | static void refresh_cache_or_die(int refresh_flags) |
294 | { | |
295 | /* | |
296 | * refresh_flags contains REFRESH_QUIET, so the only errors | |
297 | * are for unmerged entries. | |
298 | */ | |
299 | if (refresh_cache(refresh_flags | REFRESH_IN_PORCELAIN)) | |
300 | die_resolve_conflict("commit"); | |
301 | } | |
302 | ||
35ff08be MH |
303 | static const char *prepare_index(int argc, const char **argv, const char *prefix, |
304 | const struct commit *current_head, int is_status) | |
f5bbc322 | 305 | { |
dd1055ed | 306 | struct string_list partial = STRING_LIST_INIT_DUP; |
6654c889 | 307 | struct pathspec pathspec; |
50b7e70f | 308 | int refresh_flags = REFRESH_QUIET; |
b4fb09e4 | 309 | const char *ret; |
f5bbc322 | 310 | |
50b7e70f JH |
311 | if (is_status) |
312 | refresh_flags |= REFRESH_UNMERGED; | |
6654c889 NTND |
313 | parse_pathspec(&pathspec, 0, |
314 | PATHSPEC_PREFER_FULL, | |
315 | prefix, argv); | |
f5bbc322 | 316 | |
5ab2a2da | 317 | if (read_cache_preload(&pathspec) < 0) |
8a6179bc | 318 | die(_("index file corrupt")); |
671c9b7e | 319 | |
1020d087 | 320 | if (interactive) { |
e23fd15a | 321 | char *old_index_env = NULL; |
b3e83cc7 | 322 | hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR); |
1020d087 CI |
323 | |
324 | refresh_cache_or_die(refresh_flags); | |
325 | ||
812d6b00 | 326 | if (write_locked_index(&the_index, &index_lock, 0)) |
1020d087 CI |
327 | die(_("unable to create temporary index")); |
328 | ||
329 | old_index_env = getenv(INDEX_ENVIRONMENT); | |
b4fb09e4 | 330 | setenv(INDEX_ENVIRONMENT, get_lock_file_path(&index_lock), 1); |
1020d087 | 331 | |
b4bd4668 | 332 | if (interactive_add(argc, argv, prefix, patch_interactive) != 0) |
1020d087 CI |
333 | die(_("interactive add failed")); |
334 | ||
335 | if (old_index_env && *old_index_env) | |
336 | setenv(INDEX_ENVIRONMENT, old_index_env, 1); | |
337 | else | |
338 | unsetenv(INDEX_ENVIRONMENT); | |
339 | ||
340 | discard_cache(); | |
b4fb09e4 | 341 | read_cache_from(get_lock_file_path(&index_lock)); |
9c4d6c02 | 342 | if (update_main_cache_tree(WRITE_TREE_SILENT) == 0) { |
3fd13cbc | 343 | if (reopen_lock_file(&index_lock) < 0) |
9c4d6c02 | 344 | die(_("unable to write index file")); |
812d6b00 | 345 | if (write_locked_index(&the_index, &index_lock, 0)) |
3fd13cbc | 346 | die(_("unable to update temporary index")); |
9c4d6c02 DT |
347 | } else |
348 | warning(_("Failed to update main cache tree")); | |
1020d087 CI |
349 | |
350 | commit_style = COMMIT_NORMAL; | |
dd1055ed MÅ |
351 | ret = get_lock_file_path(&index_lock); |
352 | goto out; | |
1020d087 CI |
353 | } |
354 | ||
2888605c JH |
355 | /* |
356 | * Non partial, non as-is commit. | |
357 | * | |
358 | * (1) get the real index; | |
359 | * (2) update the_index as necessary; | |
360 | * (3) write the_index out to the real index (still locked); | |
361 | * (4) return the name of the locked index file. | |
362 | * | |
363 | * The caller should run hooks on the locked real index, and | |
364 | * (A) if all goes well, commit the real index; | |
365 | * (B) on failure, rollback the real index. | |
366 | */ | |
6654c889 | 367 | if (all || (also && pathspec.nr)) { |
b3e83cc7 | 368 | hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR); |
610d55af | 369 | add_files_to_cache(also ? prefix : NULL, &pathspec, 0); |
d38a30df | 370 | refresh_cache_or_die(refresh_flags); |
e859c69b | 371 | update_main_cache_tree(WRITE_TREE_SILENT); |
812d6b00 | 372 | if (write_locked_index(&the_index, &index_lock, 0)) |
8a6179bc | 373 | die(_("unable to write new_index file")); |
2888605c | 374 | commit_style = COMMIT_NORMAL; |
dd1055ed MÅ |
375 | ret = get_lock_file_path(&index_lock); |
376 | goto out; | |
f5bbc322 KH |
377 | } |
378 | ||
2888605c JH |
379 | /* |
380 | * As-is commit. | |
381 | * | |
382 | * (1) return the name of the real index file. | |
383 | * | |
73276235 MH |
384 | * The caller should run hooks on the real index, |
385 | * and create commit from the_index. | |
2888605c JH |
386 | * We still need to refresh the index here. |
387 | */ | |
6654c889 | 388 | if (!only && !pathspec.nr) { |
b3e83cc7 | 389 | hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR); |
d38a30df | 390 | refresh_cache_or_die(refresh_flags); |
9c4d6c02 | 391 | if (active_cache_changed |
475a3445 | 392 | || !cache_tree_fully_valid(active_cache_tree)) |
e859c69b | 393 | update_main_cache_tree(WRITE_TREE_SILENT); |
9c4d6c02 | 394 | if (active_cache_changed) { |
03b86647 NTND |
395 | if (write_locked_index(&the_index, &index_lock, |
396 | COMMIT_LOCK)) | |
8a6179bc | 397 | die(_("unable to write new_index file")); |
d5f5d0a9 JH |
398 | } else { |
399 | rollback_lock_file(&index_lock); | |
400 | } | |
2888605c | 401 | commit_style = COMMIT_AS_IS; |
dd1055ed MÅ |
402 | ret = get_index_file(); |
403 | goto out; | |
f5bbc322 KH |
404 | } |
405 | ||
2888605c JH |
406 | /* |
407 | * A partial commit. | |
408 | * | |
409 | * (0) find the set of affected paths; | |
410 | * (1) get lock on the real index file; | |
411 | * (2) update the_index with the given paths; | |
412 | * (3) write the_index out to the real index (still locked); | |
413 | * (4) get lock on the false index file; | |
414 | * (5) reset the_index from HEAD; | |
415 | * (6) update the_index the same way as (2); | |
416 | * (7) write the_index out to the false index file; | |
417 | * (8) return the name of the false index file (still locked); | |
418 | * | |
419 | * The caller should run hooks on the locked false index, and | |
420 | * create commit from it. Then | |
421 | * (A) if all goes well, commit the real index; | |
422 | * (B) on failure, rollback the real index; | |
423 | * In either case, rollback the false index. | |
424 | */ | |
425 | commit_style = COMMIT_PARTIAL; | |
426 | ||
b0cea47e ÆAB |
427 | if (whence != FROM_COMMIT) { |
428 | if (whence == FROM_MERGE) | |
429 | die(_("cannot do a partial commit during a merge.")); | |
430 | else if (whence == FROM_CHERRY_PICK) | |
431 | die(_("cannot do a partial commit during a cherry-pick.")); | |
432 | } | |
2888605c | 433 | |
17ddc66e | 434 | if (list_paths(&partial, !current_head ? NULL : "HEAD", prefix, &pathspec)) |
2888605c JH |
435 | exit(1); |
436 | ||
437 | discard_cache(); | |
438 | if (read_cache() < 0) | |
8a6179bc | 439 | die(_("cannot read the index")); |
2888605c | 440 | |
b3e83cc7 | 441 | hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR); |
2888605c | 442 | add_remove_files(&partial); |
ef12b50d | 443 | refresh_cache(REFRESH_QUIET); |
9c4d6c02 | 444 | update_main_cache_tree(WRITE_TREE_SILENT); |
812d6b00 | 445 | if (write_locked_index(&the_index, &index_lock, 0)) |
8a6179bc | 446 | die(_("unable to write new_index file")); |
f5bbc322 | 447 | |
03b86647 NTND |
448 | hold_lock_file_for_update(&false_lock, |
449 | git_path("next-index-%"PRIuMAX, | |
450 | (uintmax_t) getpid()), | |
451 | LOCK_DIE_ON_ERROR); | |
fa9dcf80 | 452 | |
06bb643b | 453 | create_base_index(current_head); |
2888605c | 454 | add_remove_files(&partial); |
d37d3203 | 455 | refresh_cache(REFRESH_QUIET); |
f5bbc322 | 456 | |
812d6b00 | 457 | if (write_locked_index(&the_index, &false_lock, 0)) |
8a6179bc | 458 | die(_("unable to write temporary index file")); |
959ba670 JK |
459 | |
460 | discard_cache(); | |
b4fb09e4 MH |
461 | ret = get_lock_file_path(&false_lock); |
462 | read_cache_from(ret); | |
dd1055ed MÅ |
463 | out: |
464 | string_list_clear(&partial, 0); | |
465 | clear_pathspec(&pathspec); | |
b4fb09e4 | 466 | return ret; |
f5bbc322 KH |
467 | } |
468 | ||
d249b098 JH |
469 | static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn, |
470 | struct wt_status *s) | |
f5bbc322 | 471 | { |
8066df41 | 472 | struct object_id oid; |
76e2f7ce | 473 | |
d249b098 JH |
474 | if (s->relative_paths) |
475 | s->prefix = prefix; | |
f5bbc322 KH |
476 | |
477 | if (amend) { | |
d249b098 JH |
478 | s->amend = 1; |
479 | s->reference = "HEAD^1"; | |
f5bbc322 | 480 | } |
d249b098 JH |
481 | s->verbose = verbose; |
482 | s->index_file = index_file; | |
483 | s->fp = fp; | |
484 | s->nowarn = nowarn; | |
e82caf38 | 485 | s->is_initial = get_oid(s->reference, &oid) ? 1 : 0; |
d9fc746c | 486 | if (!s->is_initial) |
8066df41 | 487 | hashcpy(s->sha1_commit, oid.hash); |
be7e795e JH |
488 | s->status_format = status_format; |
489 | s->ignore_submodule_arg = ignore_submodule_arg; | |
f5bbc322 | 490 | |
76e2f7ce | 491 | wt_status_collect(s); |
be7e795e | 492 | wt_status_print(s); |
f5bbc322 | 493 | |
d249b098 | 494 | return s->commitable; |
f5bbc322 KH |
495 | } |
496 | ||
06bb643b | 497 | static int is_a_merge(const struct commit *current_head) |
ec84bd00 | 498 | { |
06bb643b | 499 | return !!(current_head->parents && current_head->parents->next); |
ec84bd00 PB |
500 | } |
501 | ||
fac90838 JK |
502 | static void assert_split_ident(struct ident_split *id, const struct strbuf *buf) |
503 | { | |
c83a5099 | 504 | if (split_ident_line(id, buf->buf, buf->len) || !id->date_begin) |
fac90838 JK |
505 | die("BUG: unable to parse our own ident: %s", buf->buf); |
506 | } | |
507 | ||
7dfe8ad6 JH |
508 | static void export_one(const char *var, const char *s, const char *e, int hack) |
509 | { | |
510 | struct strbuf buf = STRBUF_INIT; | |
511 | if (hack) | |
512 | strbuf_addch(&buf, hack); | |
513 | strbuf_addf(&buf, "%.*s", (int)(e - s), s); | |
514 | setenv(var, buf.buf, 1); | |
515 | strbuf_release(&buf); | |
516 | } | |
517 | ||
c33ddc2e | 518 | static int parse_force_date(const char *in, struct strbuf *out) |
14ac2864 | 519 | { |
c33ddc2e | 520 | strbuf_addch(out, '@'); |
14ac2864 | 521 | |
c33ddc2e | 522 | if (parse_date(in, out) < 0) { |
14ac2864 JK |
523 | int errors = 0; |
524 | unsigned long t = approxidate_careful(in, &errors); | |
525 | if (errors) | |
526 | return -1; | |
c33ddc2e | 527 | strbuf_addf(out, "%lu", t); |
14ac2864 JK |
528 | } |
529 | ||
530 | return 0; | |
531 | } | |
532 | ||
f4ef5173 JK |
533 | static void set_ident_var(char **buf, char *val) |
534 | { | |
535 | free(*buf); | |
536 | *buf = val; | |
537 | } | |
538 | ||
4c28e4ad | 539 | static void determine_author_info(struct strbuf *author_ident) |
a45d46ba SB |
540 | { |
541 | char *name, *email, *date; | |
7dfe8ad6 | 542 | struct ident_split author; |
a45d46ba | 543 | |
eaa541eb JK |
544 | name = xstrdup_or_null(getenv("GIT_AUTHOR_NAME")); |
545 | email = xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL")); | |
546 | date = xstrdup_or_null(getenv("GIT_AUTHOR_DATE")); | |
a45d46ba | 547 | |
37f7a857 | 548 | if (author_message) { |
f0f9662a | 549 | struct ident_split ident; |
2c733fb2 | 550 | size_t len; |
f0f9662a | 551 | const char *a; |
a45d46ba | 552 | |
f0f9662a | 553 | a = find_commit_header(author_message_buffer, "author", &len); |
a45d46ba | 554 | if (!a) |
f0f9662a JK |
555 | die(_("commit '%s' lacks author header"), author_message); |
556 | if (split_ident_line(&ident, a, len) < 0) | |
557 | die(_("commit '%s' has malformed author line"), author_message); | |
558 | ||
f4ef5173 JK |
559 | set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin)); |
560 | set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin)); | |
561 | ||
f0f9662a | 562 | if (ident.date_begin) { |
f4ef5173 | 563 | struct strbuf date_buf = STRBUF_INIT; |
f0f9662a JK |
564 | strbuf_addch(&date_buf, '@'); |
565 | strbuf_add(&date_buf, ident.date_begin, ident.date_end - ident.date_begin); | |
566 | strbuf_addch(&date_buf, ' '); | |
567 | strbuf_add(&date_buf, ident.tz_begin, ident.tz_end - ident.tz_begin); | |
f4ef5173 | 568 | set_ident_var(&date, strbuf_detach(&date_buf, NULL)); |
f0f9662a | 569 | } |
a45d46ba SB |
570 | } |
571 | ||
572 | if (force_author) { | |
f0f9662a | 573 | struct ident_split ident; |
a45d46ba | 574 | |
f0f9662a | 575 | if (split_ident_line(&ident, force_author, strlen(force_author)) < 0) |
8a6179bc | 576 | die(_("malformed --author parameter")); |
f4ef5173 JK |
577 | set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin)); |
578 | set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin)); | |
a45d46ba SB |
579 | } |
580 | ||
14ac2864 | 581 | if (force_date) { |
f4ef5173 | 582 | struct strbuf date_buf = STRBUF_INIT; |
c33ddc2e | 583 | if (parse_force_date(force_date, &date_buf)) |
14ac2864 | 584 | die(_("invalid date format: %s"), force_date); |
f4ef5173 | 585 | set_ident_var(&date, strbuf_detach(&date_buf, NULL)); |
14ac2864 JK |
586 | } |
587 | ||
f9bc573f | 588 | strbuf_addstr(author_ident, fmt_ident(name, email, date, IDENT_STRICT)); |
c83a5099 JK |
589 | assert_split_ident(&author, author_ident); |
590 | export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0); | |
591 | export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0); | |
592 | export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@'); | |
f4ef5173 JK |
593 | free(name); |
594 | free(email); | |
595 | free(date); | |
a45d46ba SB |
596 | } |
597 | ||
b7242b8c JK |
598 | static int author_date_is_interesting(void) |
599 | { | |
600 | return author_message || force_date; | |
4c28e4ad JH |
601 | } |
602 | ||
84c9dc2c NTND |
603 | static void adjust_comment_line_char(const struct strbuf *sb) |
604 | { | |
605 | char candidates[] = "#;@!$%^&|:"; | |
606 | char *candidate; | |
607 | const char *p; | |
608 | ||
609 | comment_line_char = candidates[0]; | |
610 | if (!memchr(sb->buf, comment_line_char, sb->len)) | |
611 | return; | |
612 | ||
613 | p = sb->buf; | |
614 | candidate = strchr(candidates, *p); | |
615 | if (candidate) | |
616 | *candidate = ' '; | |
617 | for (p = sb->buf; *p; p++) { | |
618 | if ((p[0] == '\n' || p[0] == '\r') && p[1]) { | |
619 | candidate = strchr(candidates, p[1]); | |
620 | if (candidate) | |
621 | *candidate = ' '; | |
622 | } | |
623 | } | |
624 | ||
625 | for (p = candidates; *p == ' '; p++) | |
626 | ; | |
627 | if (!*p) | |
628 | die(_("unable to select a comment character that is not used\n" | |
629 | "in the current commit message")); | |
630 | comment_line_char = *p; | |
631 | } | |
632 | ||
d249b098 | 633 | static int prepare_to_commit(const char *index_file, const char *prefix, |
06bb643b | 634 | struct commit *current_head, |
4c28e4ad JH |
635 | struct wt_status *s, |
636 | struct strbuf *author_ident) | |
f5bbc322 KH |
637 | { |
638 | struct stat statbuf; | |
4c28e4ad | 639 | struct strbuf committer_ident = STRBUF_INIT; |
e23fd15a | 640 | int commitable; |
f285a2d7 | 641 | struct strbuf sb = STRBUF_INIT; |
8089c85b PB |
642 | const char *hook_arg1 = NULL; |
643 | const char *hook_arg2 = NULL; | |
d0aaa46f | 644 | int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE); |
2556b996 | 645 | int old_display_comment_prefix; |
f5bbc322 | 646 | |
7dfe8ad6 JH |
647 | /* This checks and barfs if author is badly specified */ |
648 | determine_author_info(author_ident); | |
649 | ||
15048f8a | 650 | if (!no_verify && run_commit_hook(use_editor, index_file, "pre-commit", NULL)) |
ec84bd00 | 651 | return 0; |
f5bbc322 | 652 | |
89ac1223 PN |
653 | if (squash_message) { |
654 | /* | |
655 | * Insert the proper subject line before other commit | |
656 | * message options add their content. | |
657 | */ | |
658 | if (use_message && !strcmp(use_message, squash_message)) | |
659 | strbuf_addstr(&sb, "squash! "); | |
660 | else { | |
661 | struct pretty_print_context ctx = {0}; | |
662 | struct commit *c; | |
663 | c = lookup_commit_reference_by_name(squash_message); | |
664 | if (!c) | |
8a6179bc | 665 | die(_("could not lookup commit %s"), squash_message); |
89ac1223 PN |
666 | ctx.output_encoding = get_commit_output_encoding(); |
667 | format_commit_message(c, "squash! %s\n\n", &sb, | |
668 | &ctx); | |
669 | } | |
670 | } | |
671 | ||
27014cbc | 672 | if (have_option_m) { |
f9568530 | 673 | strbuf_addbuf(&sb, &message); |
8089c85b | 674 | hook_arg1 = "message"; |
f5bbc322 KH |
675 | } else if (logfile && !strcmp(logfile, "-")) { |
676 | if (isatty(0)) | |
8a6179bc | 677 | fprintf(stderr, _("(reading log message from standard input)\n")); |
f5bbc322 | 678 | if (strbuf_read(&sb, 0, 0) < 0) |
8a6179bc | 679 | die_errno(_("could not read log from standard input")); |
8089c85b | 680 | hook_arg1 = "message"; |
f5bbc322 KH |
681 | } else if (logfile) { |
682 | if (strbuf_read_file(&sb, logfile, 0) < 0) | |
8a6179bc | 683 | die_errno(_("could not read log file '%s'"), |
d824cbba | 684 | logfile); |
8089c85b | 685 | hook_arg1 = "message"; |
f5bbc322 | 686 | } else if (use_message) { |
e23fd15a | 687 | char *buffer; |
f5bbc322 | 688 | buffer = strstr(use_message_buffer, "\n\n"); |
076cbd63 | 689 | if (buffer) |
84e213a3 | 690 | strbuf_addstr(&sb, skip_blank_lines(buffer + 2)); |
8089c85b PB |
691 | hook_arg1 = "commit"; |
692 | hook_arg2 = use_message; | |
d71b8ba7 PN |
693 | } else if (fixup_message) { |
694 | struct pretty_print_context ctx = {0}; | |
695 | struct commit *commit; | |
696 | commit = lookup_commit_reference_by_name(fixup_message); | |
697 | if (!commit) | |
8a6179bc | 698 | die(_("could not lookup commit %s"), fixup_message); |
d71b8ba7 PN |
699 | ctx.output_encoding = get_commit_output_encoding(); |
700 | format_commit_message(commit, "fixup! %s\n\n", | |
701 | &sb, &ctx); | |
702 | hook_arg1 = "message"; | |
f932729c | 703 | } else if (!stat(git_path_merge_msg(), &statbuf)) { |
b64c1e07 SS |
704 | /* |
705 | * prepend SQUASH_MSG here if it exists and a | |
706 | * "merge --squash" was originally performed | |
707 | */ | |
708 | if (!stat(git_path_squash_msg(), &statbuf)) { | |
709 | if (strbuf_read_file(&sb, git_path_squash_msg(), 0) < 0) | |
710 | die_errno(_("could not read SQUASH_MSG")); | |
711 | hook_arg1 = "squash"; | |
712 | } else | |
713 | hook_arg1 = "merge"; | |
f932729c | 714 | if (strbuf_read_file(&sb, git_path_merge_msg(), 0) < 0) |
8a6179bc | 715 | die_errno(_("could not read MERGE_MSG")); |
f932729c JK |
716 | } else if (!stat(git_path_squash_msg(), &statbuf)) { |
717 | if (strbuf_read_file(&sb, git_path_squash_msg(), 0) < 0) | |
8a6179bc | 718 | die_errno(_("could not read SQUASH_MSG")); |
8089c85b | 719 | hook_arg1 = "squash"; |
2140b140 | 720 | } else if (template_file) { |
f5bbc322 | 721 | if (strbuf_read_file(&sb, template_file, 0) < 0) |
8a6179bc | 722 | die_errno(_("could not read '%s'"), template_file); |
8089c85b | 723 | hook_arg1 = "template"; |
8b1ae678 | 724 | clean_message_contents = 0; |
f5bbc322 KH |
725 | } |
726 | ||
8089c85b | 727 | /* |
37f7a857 JS |
728 | * The remaining cases don't modify the template message, but |
729 | * just set the argument(s) to the prepare-commit-msg hook. | |
8089c85b | 730 | */ |
37f7a857 | 731 | else if (whence == FROM_MERGE) |
8089c85b | 732 | hook_arg1 = "merge"; |
37f7a857 JS |
733 | else if (whence == FROM_CHERRY_PICK) { |
734 | hook_arg1 = "commit"; | |
735 | hook_arg2 = "CHERRY_PICK_HEAD"; | |
736 | } | |
8089c85b | 737 | |
89ac1223 PN |
738 | if (squash_message) { |
739 | /* | |
740 | * If squash_commit was used for the commit subject, | |
741 | * then we're possibly hijacking other commit log options. | |
742 | * Reset the hook args to tell the real story. | |
743 | */ | |
744 | hook_arg1 = "message"; | |
745 | hook_arg2 = ""; | |
746 | } | |
747 | ||
e51b0dfc | 748 | s->fp = fopen_for_writing(git_path_commit_editmsg()); |
37f3012f | 749 | if (s->fp == NULL) |
e51b0dfc | 750 | die_errno(_("could not open '%s'"), git_path_commit_editmsg()); |
f5bbc322 | 751 | |
2556b996 MM |
752 | /* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */ |
753 | old_display_comment_prefix = s->display_comment_prefix; | |
754 | s->display_comment_prefix = 1; | |
755 | ||
ea9882bf MM |
756 | /* |
757 | * Most hints are counter-productive when the commit has | |
758 | * already started. | |
759 | */ | |
760 | s->hints = 0; | |
761 | ||
8b1ae678 | 762 | if (clean_message_contents) |
63af4a84 | 763 | strbuf_stripspace(&sb, 0); |
f5bbc322 | 764 | |
073bd75e | 765 | if (signoff) |
710714aa | 766 | append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0); |
f5bbc322 | 767 | |
37f3012f | 768 | if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len) |
8a6179bc | 769 | die_errno(_("could not write commit template")); |
13208572 | 770 | |
84c9dc2c NTND |
771 | if (auto_comment_line_char) |
772 | adjust_comment_line_char(&sb); | |
f5bbc322 KH |
773 | strbuf_release(&sb); |
774 | ||
bb1ae3f6 | 775 | /* This checks if committer ident is explicitly given */ |
f20f3878 | 776 | strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT)); |
bed575e4 | 777 | if (use_editor && include_status) { |
e23fd15a EP |
778 | int ident_shown = 0; |
779 | int saved_color_setting; | |
47010263 JK |
780 | struct ident_split ci, ai; |
781 | ||
75df1f43 | 782 | if (whence != FROM_COMMIT) { |
d0aaa46f | 783 | if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) |
75df1f43 | 784 | wt_status_add_cut_line(s->fp); |
b926c0d1 | 785 | status_printf_ln(s, GIT_COLOR_NORMAL, |
b0cea47e ÆAB |
786 | whence == FROM_MERGE |
787 | ? _("\n" | |
788 | "It looks like you may be committing a merge.\n" | |
789 | "If this is not correct, please remove the file\n" | |
790 | " %s\n" | |
791 | "and try again.\n") | |
792 | : _("\n" | |
793 | "It looks like you may be committing a cherry-pick.\n" | |
794 | "If this is not correct, please remove the file\n" | |
795 | " %s\n" | |
796 | "and try again.\n"), | |
ca03e067 JK |
797 | whence == FROM_MERGE ? |
798 | git_path_merge_head() : | |
799 | git_path_cherry_pick_head()); | |
75df1f43 | 800 | } |
ec84bd00 | 801 | |
b926c0d1 | 802 | fprintf(s->fp, "\n"); |
d0aaa46f | 803 | if (cleanup_mode == COMMIT_MSG_CLEANUP_ALL) |
4064e665 ÆAB |
804 | status_printf(s, GIT_COLOR_NORMAL, |
805 | _("Please enter the commit message for your changes." | |
eff80a9f JH |
806 | " Lines starting\nwith '%c' will be ignored, and an empty" |
807 | " message aborts the commit.\n"), comment_line_char); | |
d0aaa46f PW |
808 | else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS && |
809 | whence == FROM_COMMIT) | |
75df1f43 | 810 | wt_status_add_cut_line(s->fp); |
d0aaa46f | 811 | else /* COMMIT_MSG_CLEANUP_SPACE, that is. */ |
4064e665 ÆAB |
812 | status_printf(s, GIT_COLOR_NORMAL, |
813 | _("Please enter the commit message for your changes." | |
eff80a9f JH |
814 | " Lines starting\n" |
815 | "with '%c' will be kept; you may remove them" | |
816 | " yourself if you want to.\n" | |
817 | "An empty message aborts the commit.\n"), comment_line_char); | |
ec84bd00 | 818 | |
fac90838 JK |
819 | /* |
820 | * These should never fail because they come from our own | |
821 | * fmt_ident. They may fail the sane_ident test, but we know | |
822 | * that the name and mail pointers will at least be valid, | |
823 | * which is enough for our tests and printing here. | |
824 | */ | |
825 | assert_split_ident(&ai, author_ident); | |
826 | assert_split_ident(&ci, &committer_ident); | |
47010263 JK |
827 | |
828 | if (ident_cmp(&ai, &ci)) | |
b926c0d1 | 829 | status_printf_ln(s, GIT_COLOR_NORMAL, |
fe8165cd | 830 | _("%s" |
47010263 | 831 | "Author: %.*s <%.*s>"), |
b926c0d1 | 832 | ident_shown++ ? "" : "\n", |
47010263 JK |
833 | (int)(ai.name_end - ai.name_begin), ai.name_begin, |
834 | (int)(ai.mail_end - ai.mail_begin), ai.mail_begin); | |
e83dbe80 | 835 | |
b7242b8c JK |
836 | if (author_date_is_interesting()) |
837 | status_printf_ln(s, GIT_COLOR_NORMAL, | |
838 | _("%s" | |
839 | "Date: %s"), | |
840 | ident_shown++ ? "" : "\n", | |
a5481a6c | 841 | show_ident_date(&ai, DATE_MODE(NORMAL))); |
e83dbe80 | 842 | |
d6991cee | 843 | if (!committer_ident_sufficiently_given()) |
b926c0d1 | 844 | status_printf_ln(s, GIT_COLOR_NORMAL, |
fe8165cd | 845 | _("%s" |
47010263 | 846 | "Committer: %.*s <%.*s>"), |
b926c0d1 | 847 | ident_shown++ ? "" : "\n", |
47010263 JK |
848 | (int)(ci.name_end - ci.name_begin), ci.name_begin, |
849 | (int)(ci.mail_end - ci.mail_begin), ci.mail_begin); | |
bb1ae3f6 | 850 | |
b3cf1b77 | 851 | status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); /* Add new line for clarity */ |
bb1ae3f6 | 852 | |
d249b098 JH |
853 | saved_color_setting = s->use_color; |
854 | s->use_color = 0; | |
37f3012f | 855 | commitable = run_status(s->fp, index_file, prefix, 1, s); |
d249b098 | 856 | s->use_color = saved_color_setting; |
ec84bd00 | 857 | } else { |
8066df41 | 858 | struct object_id oid; |
fbcf1184 | 859 | const char *parent = "HEAD"; |
7168624c | 860 | |
7168624c | 861 | if (!active_nr && read_cache() < 0) |
8a6179bc | 862 | die(_("Cannot read index")); |
7168624c | 863 | |
fbcf1184 JH |
864 | if (amend) |
865 | parent = "HEAD^1"; | |
866 | ||
e82caf38 | 867 | if (get_oid(parent, &oid)) { |
2c49f7ff NTND |
868 | int i, ita_nr = 0; |
869 | ||
870 | for (i = 0; i < active_nr; i++) | |
871 | if (ce_intent_to_add(active_cache[i])) | |
872 | ita_nr++; | |
873 | commitable = active_nr - ita_nr > 0; | |
874 | } else { | |
c215d3d2 JL |
875 | /* |
876 | * Unless the user did explicitly request a submodule | |
877 | * ignore mode by passing a command line option we do | |
878 | * not ignore any changed submodule SHA-1s when | |
879 | * comparing index and parent, no matter what is | |
880 | * configured. Otherwise we won't commit any | |
881 | * submodules which were manually staged, which would | |
882 | * be really confusing. | |
883 | */ | |
02f2f56b | 884 | struct diff_flags flags = DIFF_FLAGS_INIT; |
0d1e0e78 | 885 | flags.override_submodule_config = 1; |
c215d3d2 JL |
886 | if (ignore_submodule_arg && |
887 | !strcmp(ignore_submodule_arg, "all")) | |
0d1e0e78 | 888 | flags.ignore_submodules = 1; |
02f2f56b | 889 | commitable = index_differs_from(parent, &flags, 1); |
c215d3d2 | 890 | } |
ec84bd00 | 891 | } |
4c28e4ad | 892 | strbuf_release(&committer_ident); |
d616a239 | 893 | |
37f3012f | 894 | fclose(s->fp); |
7168624c | 895 | |
37f7a857 JS |
896 | /* |
897 | * Reject an attempt to record a non-merge empty commit without | |
898 | * explicit --allow-empty. In the cherry-pick case, it may be | |
899 | * empty due to conflict resolution, which the user should okay. | |
900 | */ | |
901 | if (!commitable && whence != FROM_MERGE && !allow_empty && | |
06bb643b | 902 | !(amend && is_a_merge(current_head))) { |
2556b996 | 903 | s->display_comment_prefix = old_display_comment_prefix; |
d249b098 | 904 | run_status(stdout, index_file, prefix, 0, s); |
f197ed2f | 905 | if (amend) |
fc88e316 | 906 | fputs(_(empty_amend_advice), stderr); |
c17592a7 | 907 | else if (whence == FROM_CHERRY_PICK) { |
6c80cd29 | 908 | fputs(_(empty_cherry_pick_advice), stderr); |
c17592a7 JK |
909 | if (!sequencer_in_use) |
910 | fputs(_(empty_cherry_pick_advice_single), stderr); | |
911 | else | |
912 | fputs(_(empty_cherry_pick_advice_multi), stderr); | |
913 | } | |
ec84bd00 | 914 | return 0; |
7168624c AR |
915 | } |
916 | ||
680ee550 KW |
917 | if (!no_verify && find_hook("pre-commit")) { |
918 | /* | |
919 | * Re-read the index as pre-commit hook could have updated it, | |
920 | * and write it out as a tree. We must do this before we invoke | |
921 | * the editor and after we invoke run_status above. | |
922 | */ | |
923 | discard_cache(); | |
924 | } | |
ec84bd00 | 925 | read_cache_from(index_file); |
680ee550 | 926 | |
996277c5 | 927 | if (update_main_cache_tree(0)) { |
8a6179bc | 928 | error(_("Error building trees")); |
ec84bd00 | 929 | return 0; |
7168624c | 930 | } |
f5bbc322 | 931 | |
15048f8a | 932 | if (run_commit_hook(use_editor, index_file, "prepare-commit-msg", |
e51b0dfc | 933 | git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL)) |
8089c85b | 934 | return 0; |
f5bbc322 | 935 | |
ec84bd00 | 936 | if (use_editor) { |
8d7aa4ba EP |
937 | struct argv_array env = ARGV_ARRAY_INIT; |
938 | ||
939 | argv_array_pushf(&env, "GIT_INDEX_FILE=%s", index_file); | |
940 | if (launch_editor(git_path_commit_editmsg(), NULL, env.argv)) { | |
7198203a | 941 | fprintf(stderr, |
8a6179bc | 942 | _("Please supply the message using either -m or -F option.\n")); |
7198203a SB |
943 | exit(1); |
944 | } | |
8d7aa4ba | 945 | argv_array_clear(&env); |
ec84bd00 | 946 | } |
f5bbc322 | 947 | |
ec84bd00 | 948 | if (!no_verify && |
e51b0dfc | 949 | run_commit_hook(use_editor, index_file, "commit-msg", git_path_commit_editmsg(), NULL)) { |
ec84bd00 PB |
950 | return 0; |
951 | } | |
f5bbc322 | 952 | |
ec84bd00 | 953 | return 1; |
f5bbc322 KH |
954 | } |
955 | ||
146ea068 JH |
956 | static const char *find_author_by_nickname(const char *name) |
957 | { | |
958 | struct rev_info revs; | |
959 | struct commit *commit; | |
960 | struct strbuf buf = STRBUF_INIT; | |
ea16794e | 961 | struct string_list mailmap = STRING_LIST_INIT_NODUP; |
146ea068 JH |
962 | const char *av[20]; |
963 | int ac = 0; | |
964 | ||
965 | init_revisions(&revs, NULL); | |
966 | strbuf_addf(&buf, "--author=%s", name); | |
967 | av[++ac] = "--all"; | |
968 | av[++ac] = "-i"; | |
969 | av[++ac] = buf.buf; | |
970 | av[++ac] = NULL; | |
971 | setup_revisions(ac, av, &revs, NULL); | |
ea16794e AP |
972 | revs.mailmap = &mailmap; |
973 | read_mailmap(revs.mailmap, NULL); | |
974 | ||
81c3ce3c SB |
975 | if (prepare_revision_walk(&revs)) |
976 | die(_("revision walk setup failed")); | |
146ea068 JH |
977 | commit = get_revision(&revs); |
978 | if (commit) { | |
dd2e794a | 979 | struct pretty_print_context ctx = {0}; |
a5481a6c | 980 | ctx.date_mode.type = DATE_NORMAL; |
146ea068 | 981 | strbuf_release(&buf); |
ea16794e AP |
982 | format_commit_message(commit, "%aN <%aE>", &buf, &ctx); |
983 | clear_mailmap(&mailmap); | |
146ea068 JH |
984 | return strbuf_detach(&buf, NULL); |
985 | } | |
1044b1f6 | 986 | die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name); |
146ea068 JH |
987 | } |
988 | ||
76e2f7ce JH |
989 | |
990 | static void handle_untracked_files_arg(struct wt_status *s) | |
991 | { | |
992 | if (!untracked_files_arg) | |
993 | ; /* default already initialized */ | |
994 | else if (!strcmp(untracked_files_arg, "no")) | |
995 | s->show_untracked_files = SHOW_NO_UNTRACKED_FILES; | |
996 | else if (!strcmp(untracked_files_arg, "normal")) | |
997 | s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES; | |
998 | else if (!strcmp(untracked_files_arg, "all")) | |
999 | s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES; | |
1000 | else | |
8a6179bc | 1001 | die(_("Invalid untracked files mode '%s'"), untracked_files_arg); |
76e2f7ce JH |
1002 | } |
1003 | ||
37f7a857 JS |
1004 | static const char *read_commit_message(const char *name) |
1005 | { | |
dd0d388c | 1006 | const char *out_enc; |
37f7a857 JS |
1007 | struct commit *commit; |
1008 | ||
1009 | commit = lookup_commit_reference_by_name(name); | |
1010 | if (!commit) | |
6c80cd29 | 1011 | die(_("could not lookup commit %s"), name); |
37f7a857 | 1012 | out_enc = get_commit_output_encoding(); |
5a10d236 | 1013 | return logmsg_reencode(commit, NULL, out_enc); |
37f7a857 JS |
1014 | } |
1015 | ||
84b4202d JH |
1016 | /* |
1017 | * Enumerate what needs to be propagated when --porcelain | |
1018 | * is not in effect here. | |
1019 | */ | |
1020 | static struct status_deferred_config { | |
be7e795e | 1021 | enum wt_status_format status_format; |
84b4202d JH |
1022 | int show_branch; |
1023 | } status_deferred_config = { | |
1024 | STATUS_FORMAT_UNSPECIFIED, | |
1025 | -1 /* unspecified */ | |
1026 | }; | |
1027 | ||
1028 | static void finalize_deferred_config(struct wt_status *s) | |
1029 | { | |
1030 | int use_deferred_config = (status_format != STATUS_FORMAT_PORCELAIN && | |
1ecdecce | 1031 | status_format != STATUS_FORMAT_PORCELAIN_V2 && |
84b4202d JH |
1032 | !s->null_termination); |
1033 | ||
1034 | if (s->null_termination) { | |
1035 | if (status_format == STATUS_FORMAT_NONE || | |
1036 | status_format == STATUS_FORMAT_UNSPECIFIED) | |
1037 | status_format = STATUS_FORMAT_PORCELAIN; | |
1038 | else if (status_format == STATUS_FORMAT_LONG) | |
1039 | die(_("--long and -z are incompatible")); | |
1040 | } | |
1041 | ||
1042 | if (use_deferred_config && status_format == STATUS_FORMAT_UNSPECIFIED) | |
1043 | status_format = status_deferred_config.status_format; | |
1044 | if (status_format == STATUS_FORMAT_UNSPECIFIED) | |
1045 | status_format = STATUS_FORMAT_NONE; | |
1046 | ||
1047 | if (use_deferred_config && s->show_branch < 0) | |
1048 | s->show_branch = status_deferred_config.show_branch; | |
1049 | if (s->show_branch < 0) | |
1050 | s->show_branch = 0; | |
1051 | } | |
1052 | ||
2f02b25f | 1053 | static int parse_and_validate_options(int argc, const char *argv[], |
036dbbfb | 1054 | const struct option *options, |
dbd0f5c7 | 1055 | const char * const usage[], |
d249b098 | 1056 | const char *prefix, |
06bb643b | 1057 | struct commit *current_head, |
d249b098 | 1058 | struct wt_status *s) |
f5bbc322 KH |
1059 | { |
1060 | int f = 0; | |
1061 | ||
036dbbfb | 1062 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
84b4202d | 1063 | finalize_deferred_config(s); |
f5bbc322 | 1064 | |
146ea068 JH |
1065 | if (force_author && !strchr(force_author, '>')) |
1066 | force_author = find_author_by_nickname(force_author); | |
1067 | ||
c51f6cee | 1068 | if (force_author && renew_authorship) |
8a6179bc | 1069 | die(_("Using both --reset-author and --author does not make sense")); |
c51f6cee | 1070 | |
25206778 | 1071 | if (logfile || have_option_m || use_message || fixup_message) |
4803466f | 1072 | use_editor = 0; |
ca1ba201 JH |
1073 | if (0 <= edit_flag) |
1074 | use_editor = edit_flag; | |
f5bbc322 | 1075 | |
f5bbc322 | 1076 | /* Sanity check options */ |
06bb643b | 1077 | if (amend && !current_head) |
8a6179bc | 1078 | die(_("You have nothing to amend.")); |
b0cea47e ÆAB |
1079 | if (amend && whence != FROM_COMMIT) { |
1080 | if (whence == FROM_MERGE) | |
1081 | die(_("You are in the middle of a merge -- cannot amend.")); | |
1082 | else if (whence == FROM_CHERRY_PICK) | |
1083 | die(_("You are in the middle of a cherry-pick -- cannot amend.")); | |
1084 | } | |
89ac1223 | 1085 | if (fixup_message && squash_message) |
9c227655 | 1086 | die(_("Options --squash and --fixup cannot be used together")); |
f5bbc322 KH |
1087 | if (use_message) |
1088 | f++; | |
1089 | if (edit_message) | |
1090 | f++; | |
d71b8ba7 PN |
1091 | if (fixup_message) |
1092 | f++; | |
f5bbc322 KH |
1093 | if (logfile) |
1094 | f++; | |
1095 | if (f > 1) | |
8a6179bc | 1096 | die(_("Only one of -c/-C/-F/--fixup can be used.")); |
27014cbc | 1097 | if (have_option_m && f > 0) |
8a6179bc | 1098 | die((_("Option -m cannot be combined with -c/-C/-F/--fixup."))); |
27014cbc | 1099 | if (f || have_option_m) |
010c7dbc | 1100 | template_file = NULL; |
f5bbc322 KH |
1101 | if (edit_message) |
1102 | use_message = edit_message; | |
d71b8ba7 | 1103 | if (amend && !use_message && !fixup_message) |
f5bbc322 | 1104 | use_message = "HEAD"; |
37f7a857 | 1105 | if (!use_message && whence != FROM_CHERRY_PICK && renew_authorship) |
8a6179bc | 1106 | die(_("--reset-author can be used only with -C, -c or --amend.")); |
f5bbc322 | 1107 | if (use_message) { |
37f7a857 JS |
1108 | use_message_buffer = read_commit_message(use_message); |
1109 | if (!renew_authorship) { | |
1110 | author_message = use_message; | |
1111 | author_message_buffer = use_message_buffer; | |
1112 | } | |
1113 | } | |
1114 | if (whence == FROM_CHERRY_PICK && !renew_authorship) { | |
1115 | author_message = "CHERRY_PICK_HEAD"; | |
1116 | author_message_buffer = read_commit_message(author_message); | |
f5bbc322 KH |
1117 | } |
1118 | ||
b4bd4668 CI |
1119 | if (patch_interactive) |
1120 | interactive = 1; | |
1121 | ||
05efb7b7 | 1122 | if (also + only + all + interactive > 1) |
b4bd4668 | 1123 | die(_("Only one of --include/--only/--all/--interactive/--patch can be used.")); |
319d8352 | 1124 | if (argc == 0 && (also || (only && !amend && !allow_empty))) |
8a6179bc | 1125 | die(_("No paths with --include/--only does not make sense.")); |
5f065737 | 1126 | if (!cleanup_arg || !strcmp(cleanup_arg, "default")) |
d0aaa46f PW |
1127 | cleanup_mode = use_editor ? COMMIT_MSG_CLEANUP_ALL : |
1128 | COMMIT_MSG_CLEANUP_SPACE; | |
5f065737 | 1129 | else if (!strcmp(cleanup_arg, "verbatim")) |
d0aaa46f | 1130 | cleanup_mode = COMMIT_MSG_CLEANUP_NONE; |
5f065737 | 1131 | else if (!strcmp(cleanup_arg, "whitespace")) |
d0aaa46f | 1132 | cleanup_mode = COMMIT_MSG_CLEANUP_SPACE; |
5f065737 | 1133 | else if (!strcmp(cleanup_arg, "strip")) |
d0aaa46f | 1134 | cleanup_mode = COMMIT_MSG_CLEANUP_ALL; |
75df1f43 | 1135 | else if (!strcmp(cleanup_arg, "scissors")) |
d0aaa46f PW |
1136 | cleanup_mode = use_editor ? COMMIT_MSG_CLEANUP_SCISSORS : |
1137 | COMMIT_MSG_CLEANUP_SPACE; | |
5f065737 | 1138 | else |
8a6179bc | 1139 | die(_("Invalid cleanup mode %s"), cleanup_arg); |
f5bbc322 | 1140 | |
76e2f7ce | 1141 | handle_untracked_files_arg(s); |
4bfee30a | 1142 | |
f5bbc322 | 1143 | if (all && argc > 0) |
8a6179bc | 1144 | die(_("Paths with -a does not make sense.")); |
f5bbc322 | 1145 | |
f3f47a1e | 1146 | if (status_format != STATUS_FORMAT_NONE) |
7c9f7038 JK |
1147 | dry_run = 1; |
1148 | ||
f5bbc322 KH |
1149 | return argc; |
1150 | } | |
1151 | ||
d249b098 | 1152 | static int dry_run_commit(int argc, const char **argv, const char *prefix, |
06bb643b | 1153 | const struct commit *current_head, struct wt_status *s) |
f5bbc322 | 1154 | { |
f5bbc322 | 1155 | int commitable; |
3a5d13a3 | 1156 | const char *index_file; |
f5bbc322 | 1157 | |
06bb643b | 1158 | index_file = prepare_index(argc, argv, prefix, current_head, 1); |
d249b098 | 1159 | commitable = run_status(stdout, index_file, prefix, 0, s); |
3a5d13a3 | 1160 | rollback_index_files(); |
f5bbc322 | 1161 | |
3a5d13a3 JH |
1162 | return commitable ? 0 : 1; |
1163 | } | |
1164 | ||
8852117a | 1165 | static int parse_status_slot(const char *slot) |
f766b367 | 1166 | { |
8852117a | 1167 | if (!strcasecmp(slot, "header")) |
f766b367 | 1168 | return WT_STATUS_HEADER; |
8852117a | 1169 | if (!strcasecmp(slot, "branch")) |
1d282327 | 1170 | return WT_STATUS_ONBRANCH; |
8852117a | 1171 | if (!strcasecmp(slot, "updated") || !strcasecmp(slot, "added")) |
f766b367 | 1172 | return WT_STATUS_UPDATED; |
8852117a | 1173 | if (!strcasecmp(slot, "changed")) |
f766b367 | 1174 | return WT_STATUS_CHANGED; |
8852117a | 1175 | if (!strcasecmp(slot, "untracked")) |
f766b367 | 1176 | return WT_STATUS_UNTRACKED; |
8852117a | 1177 | if (!strcasecmp(slot, "nobranch")) |
f766b367 | 1178 | return WT_STATUS_NOBRANCH; |
8852117a | 1179 | if (!strcasecmp(slot, "unmerged")) |
f766b367 | 1180 | return WT_STATUS_UNMERGED; |
93fdf301 SK |
1181 | if (!strcasecmp(slot, "localBranch")) |
1182 | return WT_STATUS_LOCAL_BRANCH; | |
1183 | if (!strcasecmp(slot, "remoteBranch")) | |
1184 | return WT_STATUS_REMOTE_BRANCH; | |
8b8e8624 | 1185 | return -1; |
f766b367 JH |
1186 | } |
1187 | ||
1188 | static int git_status_config(const char *k, const char *v, void *cb) | |
1189 | { | |
1190 | struct wt_status *s = cb; | |
e3f1da98 | 1191 | const char *slot_name; |
f766b367 | 1192 | |
59556548 | 1193 | if (starts_with(k, "column.")) |
4d2292e9 | 1194 | return git_column_config(k, v, "status", &s->colopts); |
f766b367 JH |
1195 | if (!strcmp(k, "status.submodulesummary")) { |
1196 | int is_bool; | |
1197 | s->submodule_summary = git_config_bool_or_int(k, v, &is_bool); | |
1198 | if (is_bool && s->submodule_summary) | |
1199 | s->submodule_summary = -1; | |
1200 | return 0; | |
1201 | } | |
4fb5166a JJGG |
1202 | if (!strcmp(k, "status.short")) { |
1203 | if (git_config_bool(k, v)) | |
84b4202d | 1204 | status_deferred_config.status_format = STATUS_FORMAT_SHORT; |
4fb5166a | 1205 | else |
84b4202d | 1206 | status_deferred_config.status_format = STATUS_FORMAT_NONE; |
4fb5166a JJGG |
1207 | return 0; |
1208 | } | |
ec85d070 | 1209 | if (!strcmp(k, "status.branch")) { |
84b4202d | 1210 | status_deferred_config.show_branch = git_config_bool(k, v); |
ec85d070 JJGG |
1211 | return 0; |
1212 | } | |
c1b5d019 LB |
1213 | if (!strcmp(k, "status.showstash")) { |
1214 | s->show_stash = git_config_bool(k, v); | |
1215 | return 0; | |
1216 | } | |
f766b367 | 1217 | if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) { |
e269eb79 | 1218 | s->use_color = git_config_colorbool(k, v); |
f766b367 JH |
1219 | return 0; |
1220 | } | |
2556b996 MM |
1221 | if (!strcmp(k, "status.displaycommentprefix")) { |
1222 | s->display_comment_prefix = git_config_bool(k, v); | |
1223 | return 0; | |
1224 | } | |
e3f1da98 RS |
1225 | if (skip_prefix(k, "status.color.", &slot_name) || |
1226 | skip_prefix(k, "color.status.", &slot_name)) { | |
b9465768 | 1227 | int slot = parse_status_slot(slot_name); |
8b8e8624 JK |
1228 | if (slot < 0) |
1229 | return 0; | |
f766b367 JH |
1230 | if (!v) |
1231 | return config_error_nonbool(k); | |
f6c5a296 | 1232 | return color_parse(v, s->color_palette[slot]); |
f766b367 JH |
1233 | } |
1234 | if (!strcmp(k, "status.relativepaths")) { | |
1235 | s->relative_paths = git_config_bool(k, v); | |
1236 | return 0; | |
1237 | } | |
1238 | if (!strcmp(k, "status.showuntrackedfiles")) { | |
1239 | if (!v) | |
1240 | return config_error_nonbool(k); | |
1241 | else if (!strcmp(v, "no")) | |
1242 | s->show_untracked_files = SHOW_NO_UNTRACKED_FILES; | |
1243 | else if (!strcmp(v, "normal")) | |
1244 | s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES; | |
1245 | else if (!strcmp(v, "all")) | |
1246 | s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES; | |
1247 | else | |
8a6179bc | 1248 | return error(_("Invalid untracked files mode '%s'"), v); |
f766b367 JH |
1249 | return 0; |
1250 | } | |
1251 | return git_diff_ui_config(k, v, NULL); | |
1252 | } | |
1253 | ||
3a5d13a3 JH |
1254 | int cmd_status(int argc, const char **argv, const char *prefix) |
1255 | { | |
036dbbfb | 1256 | static struct wt_status s; |
4bb6644d | 1257 | int fd; |
8066df41 | 1258 | struct object_id oid; |
9e4b7ab6 | 1259 | static struct option builtin_status_options[] = { |
f2276316 | 1260 | OPT__VERBOSE(&verbose, N_("be verbose")), |
dd2be243 | 1261 | OPT_SET_INT('s', "short", &status_format, |
f2276316 | 1262 | N_("show status concisely"), STATUS_FORMAT_SHORT), |
84b4202d JH |
1263 | OPT_BOOL('b', "branch", &s.show_branch, |
1264 | N_("show branch information")), | |
c1b5d019 LB |
1265 | OPT_BOOL(0, "show-stash", &s.show_stash, |
1266 | N_("show stash information")), | |
c4f596b9 JH |
1267 | { OPTION_CALLBACK, 0, "porcelain", &status_format, |
1268 | N_("version"), N_("machine-readable output"), | |
1269 | PARSE_OPT_OPTARG, opt_parse_porcelain }, | |
f3f47a1e JK |
1270 | OPT_SET_INT(0, "long", &status_format, |
1271 | N_("show status in long format (default)"), | |
1272 | STATUS_FORMAT_LONG), | |
d5d09d47 SB |
1273 | OPT_BOOL('z', "null", &s.null_termination, |
1274 | N_("terminate entries with NUL")), | |
76e2f7ce | 1275 | { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, |
f2276316 NTND |
1276 | N_("mode"), |
1277 | N_("show untracked files, optional modes: all, normal, no. (Default: all)"), | |
76e2f7ce | 1278 | PARSE_OPT_OPTARG, NULL, (intptr_t)"all" }, |
d5d09d47 SB |
1279 | OPT_BOOL(0, "ignored", &show_ignored_in_status, |
1280 | N_("show ignored files")), | |
f2276316 NTND |
1281 | { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, N_("when"), |
1282 | N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"), | |
46a958b3 | 1283 | PARSE_OPT_OPTARG, NULL, (intptr_t)"all" }, |
9c23f4c5 | 1284 | OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")), |
76e2f7ce JH |
1285 | OPT_END(), |
1286 | }; | |
1287 | ||
5d3dd915 NTND |
1288 | if (argc == 2 && !strcmp(argv[1], "-h")) |
1289 | usage_with_options(builtin_status_usage, builtin_status_options); | |
1290 | ||
5c25dfaa | 1291 | status_init_config(&s, git_status_config); |
76e2f7ce | 1292 | argc = parse_options(argc, argv, prefix, |
9e4b7ab6 JH |
1293 | builtin_status_options, |
1294 | builtin_status_usage, 0); | |
4d2292e9 | 1295 | finalize_colopts(&s.colopts, -1); |
84b4202d | 1296 | finalize_deferred_config(&s); |
000f97bd | 1297 | |
76e2f7ce | 1298 | handle_untracked_files_arg(&s); |
2381e39e JH |
1299 | if (show_ignored_in_status) |
1300 | s.show_ignored_files = 1; | |
15b55ae0 NTND |
1301 | parse_pathspec(&s.pathspec, 0, |
1302 | PATHSPEC_PREFER_FULL, | |
1303 | prefix, argv); | |
76e2f7ce | 1304 | |
5ab2a2da | 1305 | read_cache_preload(&s.pathspec); |
9b2d6149 | 1306 | refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, &s.pathspec, NULL, NULL); |
4bb6644d | 1307 | |
27344d6a JK |
1308 | if (use_optional_locks()) |
1309 | fd = hold_locked_index(&index_lock, 0); | |
1310 | else | |
1311 | fd = -1; | |
4bb6644d | 1312 | |
e82caf38 | 1313 | s.is_initial = get_oid(s.reference, &oid) ? 1 : 0; |
d9fc746c | 1314 | if (!s.is_initial) |
8066df41 | 1315 | hashcpy(s.sha1_commit, oid.hash); |
d9fc746c | 1316 | |
46a958b3 | 1317 | s.ignore_submodule_arg = ignore_submodule_arg; |
be7e795e JH |
1318 | s.status_format = status_format; |
1319 | s.verbose = verbose; | |
1320 | ||
76e2f7ce JH |
1321 | wt_status_collect(&s); |
1322 | ||
226c051a NTND |
1323 | if (0 <= fd) |
1324 | update_index_if_able(&the_index, &index_lock); | |
1325 | ||
8661768f JK |
1326 | if (s.relative_paths) |
1327 | s.prefix = prefix; | |
38920dd6 | 1328 | |
be7e795e | 1329 | wt_status_print(&s); |
76e2f7ce | 1330 | return 0; |
f5bbc322 KH |
1331 | } |
1332 | ||
186458b1 | 1333 | static int git_commit_config(const char *k, const char *v, void *cb) |
f5bbc322 | 1334 | { |
d249b098 | 1335 | struct wt_status *s = cb; |
ba3c69a9 | 1336 | int status; |
d249b098 | 1337 | |
984c6e7e | 1338 | if (!strcmp(k, "commit.template")) |
395de250 | 1339 | return git_config_pathname(&template_file, k, v); |
bed575e4 JHI |
1340 | if (!strcmp(k, "commit.status")) { |
1341 | include_status = git_config_bool(k, v); | |
1342 | return 0; | |
1343 | } | |
51fb3a3d RT |
1344 | if (!strcmp(k, "commit.cleanup")) |
1345 | return git_config_string(&cleanup_arg, k, v); | |
d95bfb12 NV |
1346 | if (!strcmp(k, "commit.gpgsign")) { |
1347 | sign_commit = git_config_bool(k, v) ? "" : NULL; | |
1348 | return 0; | |
1349 | } | |
aaab8420 PB |
1350 | if (!strcmp(k, "commit.verbose")) { |
1351 | int is_bool; | |
1352 | config_commit_verbose = git_config_bool_or_int(k, v, &is_bool); | |
1353 | return 0; | |
1354 | } | |
f5bbc322 | 1355 | |
ba3c69a9 JH |
1356 | status = git_gpg_config(k, v, NULL); |
1357 | if (status) | |
1358 | return status; | |
d249b098 | 1359 | return git_status_config(k, v, s); |
f5bbc322 KH |
1360 | } |
1361 | ||
15048f8a BP |
1362 | int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...) |
1363 | { | |
8d7aa4ba | 1364 | struct argv_array hook_env = ARGV_ARRAY_INIT; |
15048f8a BP |
1365 | va_list args; |
1366 | int ret; | |
1367 | ||
8d7aa4ba | 1368 | argv_array_pushf(&hook_env, "GIT_INDEX_FILE=%s", index_file); |
15048f8a BP |
1369 | |
1370 | /* | |
1371 | * Let the hook know that no editor will be launched. | |
1372 | */ | |
1373 | if (!editor_is_used) | |
8d7aa4ba | 1374 | argv_array_push(&hook_env, "GIT_EDITOR=:"); |
15048f8a BP |
1375 | |
1376 | va_start(args, name); | |
8d7aa4ba | 1377 | ret = run_hook_ve(hook_env.argv,name, args); |
15048f8a | 1378 | va_end(args); |
8d7aa4ba | 1379 | argv_array_clear(&hook_env); |
15048f8a BP |
1380 | |
1381 | return ret; | |
1382 | } | |
1383 | ||
f5bbc322 KH |
1384 | int cmd_commit(int argc, const char **argv, const char *prefix) |
1385 | { | |
036dbbfb JK |
1386 | static struct wt_status s; |
1387 | static struct option builtin_commit_options[] = { | |
9c23f4c5 NTND |
1388 | OPT__QUIET(&quiet, N_("suppress summary after successful commit")), |
1389 | OPT__VERBOSE(&verbose, N_("show diff in commit message template")), | |
1390 | ||
1391 | OPT_GROUP(N_("Commit message options")), | |
1392 | OPT_FILENAME('F', "file", &logfile, N_("read message from file")), | |
1393 | OPT_STRING(0, "author", &force_author, N_("author"), N_("override author for commit")), | |
1394 | OPT_STRING(0, "date", &force_date, N_("date"), N_("override date for commit")), | |
1395 | OPT_CALLBACK('m', "message", &message, N_("message"), N_("commit message"), opt_parse_m), | |
1396 | OPT_STRING('c', "reedit-message", &edit_message, N_("commit"), N_("reuse and edit message from specified commit")), | |
1397 | OPT_STRING('C', "reuse-message", &use_message, N_("commit"), N_("reuse message from specified commit")), | |
1398 | OPT_STRING(0, "fixup", &fixup_message, N_("commit"), N_("use autosquash formatted message to fixup specified commit")), | |
1399 | OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")), | |
d5d09d47 SB |
1400 | OPT_BOOL(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")), |
1401 | OPT_BOOL('s', "signoff", &signoff, N_("add Signed-off-by:")), | |
9c23f4c5 NTND |
1402 | OPT_FILENAME('t', "template", &template_file, N_("use specified template file")), |
1403 | OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")), | |
1404 | OPT_STRING(0, "cleanup", &cleanup_arg, N_("default"), N_("how to strip spaces and #comments from message")), | |
d5d09d47 | 1405 | OPT_BOOL(0, "status", &include_status, N_("include status in commit message template")), |
e703d711 | 1406 | { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"), |
9c23f4c5 | 1407 | N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, |
036dbbfb JK |
1408 | /* end commit message options */ |
1409 | ||
9c23f4c5 | 1410 | OPT_GROUP(N_("Commit contents options")), |
d5d09d47 SB |
1411 | OPT_BOOL('a', "all", &all, N_("commit all changed files")), |
1412 | OPT_BOOL('i', "include", &also, N_("add specified files to index for commit")), | |
1413 | OPT_BOOL(0, "interactive", &interactive, N_("interactively add files")), | |
1414 | OPT_BOOL('p', "patch", &patch_interactive, N_("interactively add changes")), | |
1415 | OPT_BOOL('o', "only", &only, N_("commit only specified files")), | |
def480fe | 1416 | OPT_BOOL('n', "no-verify", &no_verify, N_("bypass pre-commit and commit-msg hooks")), |
d5d09d47 | 1417 | OPT_BOOL(0, "dry-run", &dry_run, N_("show what would be committed")), |
9c23f4c5 | 1418 | OPT_SET_INT(0, "short", &status_format, N_("show status concisely"), |
036dbbfb | 1419 | STATUS_FORMAT_SHORT), |
84b4202d | 1420 | OPT_BOOL(0, "branch", &s.show_branch, N_("show branch information")), |
036dbbfb | 1421 | OPT_SET_INT(0, "porcelain", &status_format, |
9c23f4c5 | 1422 | N_("machine-readable output"), STATUS_FORMAT_PORCELAIN), |
f3f47a1e JK |
1423 | OPT_SET_INT(0, "long", &status_format, |
1424 | N_("show status in long format (default)"), | |
1425 | STATUS_FORMAT_LONG), | |
d5d09d47 SB |
1426 | OPT_BOOL('z', "null", &s.null_termination, |
1427 | N_("terminate entries with NUL")), | |
1428 | OPT_BOOL(0, "amend", &amend, N_("amend previous commit")), | |
1429 | OPT_BOOL(0, "no-post-rewrite", &no_post_rewrite, N_("bypass post-rewrite hook")), | |
9c23f4c5 | 1430 | { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, N_("mode"), N_("show untracked files, optional modes: all, normal, no. (Default: all)"), PARSE_OPT_OPTARG, NULL, (intptr_t)"all" }, |
036dbbfb JK |
1431 | /* end commit contents options */ |
1432 | ||
4741edd5 SB |
1433 | OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty, |
1434 | N_("ok to record an empty change")), | |
1435 | OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message, | |
1436 | N_("ok to record a change with an empty message")), | |
036dbbfb JK |
1437 | |
1438 | OPT_END() | |
1439 | }; | |
1440 | ||
f285a2d7 | 1441 | struct strbuf sb = STRBUF_INIT; |
4c28e4ad | 1442 | struct strbuf author_ident = STRBUF_INIT; |
f5bbc322 | 1443 | const char *index_file, *reflog_msg; |
8066df41 | 1444 | struct object_id oid; |
de9f7fa3 | 1445 | struct commit_list *parents = NULL; |
cf10f9fd | 1446 | struct stat statbuf; |
06bb643b | 1447 | struct commit *current_head = NULL; |
ed7a42a0 | 1448 | struct commit_extra_header *extra = NULL; |
c0fe1ed0 | 1449 | struct strbuf err = STRBUF_INIT; |
f5bbc322 | 1450 | |
5d3dd915 NTND |
1451 | if (argc == 2 && !strcmp(argv[1], "-h")) |
1452 | usage_with_options(builtin_commit_usage, builtin_commit_options); | |
1453 | ||
5c25dfaa | 1454 | status_init_config(&s, git_commit_config); |
4ddb1354 | 1455 | s.commit_template = 1; |
f0915cba | 1456 | status_format = STATUS_FORMAT_NONE; /* Ignore status.short */ |
4d2292e9 | 1457 | s.colopts = 0; |
f5bbc322 | 1458 | |
e82caf38 | 1459 | if (get_oid("HEAD", &oid)) |
06bb643b JH |
1460 | current_head = NULL; |
1461 | else { | |
bc83266a | 1462 | current_head = lookup_commit_or_die(&oid, "HEAD"); |
5e7d4d3e | 1463 | if (parse_commit(current_head)) |
06bb643b JH |
1464 | die(_("could not parse HEAD commit")); |
1465 | } | |
aaab8420 | 1466 | verbose = -1; /* unspecified */ |
036dbbfb JK |
1467 | argc = parse_and_validate_options(argc, argv, builtin_commit_options, |
1468 | builtin_commit_usage, | |
06bb643b | 1469 | prefix, current_head, &s); |
aaab8420 PB |
1470 | if (verbose == -1) |
1471 | verbose = (config_commit_verbose < 0) ? 0 : config_commit_verbose; | |
1472 | ||
c9bfb953 | 1473 | if (dry_run) |
06bb643b | 1474 | return dry_run_commit(argc, argv, prefix, current_head, &s); |
06bb643b | 1475 | index_file = prepare_index(argc, argv, prefix, current_head, 0); |
f5bbc322 | 1476 | |
ec84bd00 PB |
1477 | /* Set up everything for writing the commit object. This includes |
1478 | running hooks, writing the trees, and interacting with the user. */ | |
06bb643b JH |
1479 | if (!prepare_to_commit(index_file, prefix, |
1480 | current_head, &s, &author_ident)) { | |
2888605c | 1481 | rollback_index_files(); |
f5bbc322 KH |
1482 | return 1; |
1483 | } | |
1484 | ||
f5bbc322 | 1485 | /* Determine parents */ |
643cb5f7 | 1486 | reflog_msg = getenv("GIT_REFLOG_ACTION"); |
06bb643b | 1487 | if (!current_head) { |
643cb5f7 CC |
1488 | if (!reflog_msg) |
1489 | reflog_msg = "commit (initial)"; | |
f5bbc322 | 1490 | } else if (amend) { |
643cb5f7 CC |
1491 | if (!reflog_msg) |
1492 | reflog_msg = "commit (amend)"; | |
de9f7fa3 | 1493 | parents = copy_commit_list(current_head->parents); |
37f7a857 | 1494 | } else if (whence == FROM_MERGE) { |
f285a2d7 | 1495 | struct strbuf m = STRBUF_INIT; |
f5bbc322 | 1496 | FILE *fp; |
e23fd15a | 1497 | int allow_fast_forward = 1; |
de9f7fa3 | 1498 | struct commit_list **pptr = &parents; |
f5bbc322 | 1499 | |
643cb5f7 CC |
1500 | if (!reflog_msg) |
1501 | reflog_msg = "commit (merge)"; | |
de9f7fa3 | 1502 | pptr = commit_list_append(current_head, pptr); |
23a9e071 | 1503 | fp = xfopen(git_path_merge_head(), "r"); |
8f309aeb | 1504 | while (strbuf_getline_lf(&m, fp) != EOF) { |
5231c633 JH |
1505 | struct commit *parent; |
1506 | ||
1507 | parent = get_merge_parent(m.buf); | |
1508 | if (!parent) | |
8a6179bc | 1509 | die(_("Corrupt MERGE_HEAD file (%s)"), m.buf); |
de9f7fa3 | 1510 | pptr = commit_list_append(parent, pptr); |
7c3fd25d | 1511 | } |
f5bbc322 KH |
1512 | fclose(fp); |
1513 | strbuf_release(&m); | |
f932729c JK |
1514 | if (!stat(git_path_merge_mode(), &statbuf)) { |
1515 | if (strbuf_read_file(&sb, git_path_merge_mode(), 0) < 0) | |
8a6179bc | 1516 | die_errno(_("could not read MERGE_MODE")); |
cf10f9fd MV |
1517 | if (!strcmp(sb.buf, "no-ff")) |
1518 | allow_fast_forward = 0; | |
1519 | } | |
1520 | if (allow_fast_forward) | |
1521 | parents = reduce_heads(parents); | |
f5bbc322 | 1522 | } else { |
643cb5f7 | 1523 | if (!reflog_msg) |
37f7a857 JS |
1524 | reflog_msg = (whence == FROM_CHERRY_PICK) |
1525 | ? "commit (cherry-pick)" | |
1526 | : "commit"; | |
de9f7fa3 | 1527 | commit_list_insert(current_head, &parents); |
f5bbc322 | 1528 | } |
f5bbc322 | 1529 | |
ec84bd00 | 1530 | /* Finally, get the commit message */ |
cf10f9fd | 1531 | strbuf_reset(&sb); |
e51b0dfc | 1532 | if (strbuf_read_file(&sb, git_path_commit_editmsg(), 0) < 0) { |
0721c314 | 1533 | int saved_errno = errno; |
740001a5 | 1534 | rollback_index_files(); |
8a6179bc | 1535 | die(_("could not read commit message: %s"), strerror(saved_errno)); |
740001a5 | 1536 | } |
99a12694 | 1537 | |
75df1f43 | 1538 | if (verbose || /* Truncate the message just before the diff, if any. */ |
d0aaa46f | 1539 | cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) |
d76650b8 | 1540 | strbuf_setlen(&sb, wt_status_locate_end(sb.buf, sb.len)); |
d0aaa46f PW |
1541 | if (cleanup_mode != COMMIT_MSG_CLEANUP_NONE) |
1542 | strbuf_stripspace(&sb, cleanup_mode == COMMIT_MSG_CLEANUP_ALL); | |
bc17f35f | 1543 | |
d0aaa46f | 1544 | if (message_is_empty(&sb, cleanup_mode) && !allow_empty_message) { |
b2eda9bd | 1545 | rollback_index_files(); |
bc17f35f | 1546 | fprintf(stderr, _("Aborting commit due to empty commit message.\n")); |
b2eda9bd JH |
1547 | exit(1); |
1548 | } | |
d0aaa46f | 1549 | if (template_untouched(&sb, template_file, cleanup_mode) && !allow_empty_message) { |
2888605c | 1550 | rollback_index_files(); |
bc17f35f | 1551 | fprintf(stderr, _("Aborting commit; you did not edit the message.\n")); |
fdc7c811 | 1552 | exit(1); |
2888605c | 1553 | } |
f5bbc322 | 1554 | |
0074d18d | 1555 | if (amend) { |
c871a1d1 JH |
1556 | const char *exclude_gpgsig[2] = { "gpgsig", NULL }; |
1557 | extra = read_commit_extra_headers(current_head, exclude_gpgsig); | |
0074d18d JH |
1558 | } else { |
1559 | struct commit_extra_header **tail = &extra; | |
1560 | append_merge_tag_headers(parents, &tail); | |
1561 | } | |
ed7a42a0 | 1562 | |
e0a92804 | 1563 | if (commit_tree_extended(sb.buf, sb.len, active_cache_tree->oid.hash, |
8066df41 | 1564 | parents, oid.hash, author_ident.buf, sign_commit, extra)) { |
2888605c | 1565 | rollback_index_files(); |
8a6179bc | 1566 | die(_("failed to write commit object")); |
2888605c | 1567 | } |
4c28e4ad | 1568 | strbuf_release(&author_ident); |
ed7a42a0 | 1569 | free_commit_extra_headers(extra); |
f5bbc322 | 1570 | |
0505d604 PW |
1571 | if (update_head_with_reflog(current_head, &oid, reflog_msg, &sb, |
1572 | &err)) { | |
2888605c | 1573 | rollback_index_files(); |
c0fe1ed0 | 1574 | die("%s", err.buf); |
2888605c | 1575 | } |
f5bbc322 | 1576 | |
f932729c JK |
1577 | unlink(git_path_cherry_pick_head()); |
1578 | unlink(git_path_revert_head()); | |
1579 | unlink(git_path_merge_head()); | |
1580 | unlink(git_path_merge_msg()); | |
1581 | unlink(git_path_merge_mode()); | |
1582 | unlink(git_path_squash_msg()); | |
f5bbc322 | 1583 | |
5a9dd399 | 1584 | if (commit_index_files()) |
8a6179bc | 1585 | die (_("Repository has been updated, but unable to write\n" |
ad5fe377 | 1586 | "new_index file. Check that disk is not full and quota is\n" |
8a6179bc | 1587 | "not exceeded, and then \"git reset HEAD\" to recover.")); |
f5bbc322 | 1588 | |
cb6020bb | 1589 | rerere(0); |
15048f8a | 1590 | run_commit_hook(use_editor, get_index_file(), "post-commit", NULL); |
6f6bee3b | 1591 | if (amend && !no_post_rewrite) { |
a87a6f3c | 1592 | commit_post_rewrite(current_head, &oid); |
6f6bee3b | 1593 | } |
e47c6caf PW |
1594 | if (!quiet) { |
1595 | unsigned int flags = 0; | |
1596 | ||
1597 | if (!current_head) | |
1598 | flags |= SUMMARY_INITIAL_COMMIT; | |
1599 | if (author_date_is_interesting()) | |
1600 | flags |= SUMMARY_SHOW_AUTHOR_DATE; | |
1601 | print_commit_summary(prefix, &oid, flags); | |
1602 | } | |
f5bbc322 | 1603 | |
0e5bba53 JK |
1604 | UNLEAK(err); |
1605 | UNLEAK(sb); | |
f5bbc322 KH |
1606 | return 0; |
1607 | } |