]>
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 | ||
dfd0a893 | 8 | #define USE_THE_INDEX_VARIABLE |
bc5c5ec0 | 9 | #include "builtin.h" |
6c6ddf92 | 10 | #include "advice.h" |
b2141fc1 | 11 | #include "config.h" |
697cc8ef | 12 | #include "lockfile.h" |
f5bbc322 | 13 | #include "cache-tree.h" |
6b2f2d98 | 14 | #include "color.h" |
2888605c | 15 | #include "dir.h" |
4e120823 | 16 | #include "editor.h" |
7ee24e18 | 17 | #include "environment.h" |
f5bbc322 | 18 | #include "diff.h" |
f5bbc322 | 19 | #include "commit.h" |
f394e093 | 20 | #include "gettext.h" |
f5bbc322 KH |
21 | #include "revision.h" |
22 | #include "wt-status.h" | |
23 | #include "run-command.h" | |
f5bbc322 | 24 | #include "strbuf.h" |
dabab1d6 | 25 | #include "object-name.h" |
f5bbc322 | 26 | #include "parse-options.h" |
c339932b | 27 | #include "path.h" |
fbffdfb1 | 28 | #include "preload-index.h" |
08c46a49 | 29 | #include "read-cache.h" |
c455c87c | 30 | #include "string-list.h" |
5b2fd956 | 31 | #include "rerere.h" |
fa9dcf80 | 32 | #include "unpack-trees.h" |
323d0530 | 33 | #include "column.h" |
5ed75e2a | 34 | #include "sequencer.h" |
baf889c2 | 35 | #include "sparse-index.h" |
ea16794e | 36 | #include "mailmap.h" |
3ac68a93 | 37 | #include "help.h" |
64043556 | 38 | #include "commit-reach.h" |
859fdc0c | 39 | #include "commit-graph.h" |
88c7b4c3 | 40 | #include "pretty.h" |
f5bbc322 KH |
41 | |
42 | static const char * const builtin_commit_usage[] = { | |
423be1f8 ÆAB |
43 | N_("git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]\n" |
44 | " [--dry-run] [(-c | -C | --squash) <commit> | --fixup [(amend|reword):]<commit>)]\n" | |
45 | " [-F <file> | -m <msg>] [--reset-author] [--allow-empty]\n" | |
46 | " [--allow-empty-message] [--no-verify] [-e] [--author=<author>]\n" | |
47 | " [--date=<date>] [--cleanup=<mode>] [--[no-]status]\n" | |
48 | " [-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n" | |
49 | " [(--trailer <token>[(=|:)<value>])...] [-S[<keyid>]]\n" | |
50 | " [--] [<pathspec>...]"), | |
f5bbc322 KH |
51 | NULL |
52 | }; | |
53 | ||
2f02b25f | 54 | static const char * const builtin_status_usage[] = { |
463ea0cf | 55 | N_("git status [<options>] [--] [<pathspec>...]"), |
2f02b25f SB |
56 | NULL |
57 | }; | |
58 | ||
f197ed2f | 59 | static const char empty_amend_advice[] = |
fc88e316 | 60 | N_("You asked to amend the most recent commit, but doing so would make\n" |
f197ed2f | 61 | "it empty. You can repeat your command with --allow-empty, or you can\n" |
fc88e316 | 62 | "remove the commit entirely with \"git reset HEAD^\".\n"); |
f197ed2f | 63 | |
37f7a857 | 64 | static const char empty_cherry_pick_advice[] = |
6c80cd29 | 65 | N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n" |
37f7a857 JS |
66 | "If you wish to commit it anyway, use:\n" |
67 | "\n" | |
68 | " git commit --allow-empty\n" | |
c17592a7 JK |
69 | "\n"); |
70 | ||
430b75f7 PW |
71 | static const char empty_rebase_pick_advice[] = |
72 | N_("Otherwise, please use 'git rebase --skip'\n"); | |
73 | ||
c17592a7 | 74 | static const char empty_cherry_pick_advice_single[] = |
dcb500dc | 75 | N_("Otherwise, please use 'git cherry-pick --skip'\n"); |
c17592a7 JK |
76 | |
77 | static const char empty_cherry_pick_advice_multi[] = | |
dcb500dc | 78 | N_("and then use:\n" |
37f7a857 | 79 | "\n" |
dcb500dc | 80 | " git cherry-pick --continue\n" |
c17592a7 | 81 | "\n" |
dcb500dc RA |
82 | "to resume cherry-picking the remaining commits.\n" |
83 | "If you wish to skip this commit, use:\n" | |
84 | "\n" | |
85 | " git cherry-pick --skip\n" | |
86 | "\n"); | |
37f7a857 | 87 | |
a73b3680 NTND |
88 | static const char *color_status_slots[] = { |
89 | [WT_STATUS_HEADER] = "header", | |
90 | [WT_STATUS_UPDATED] = "updated", | |
91 | [WT_STATUS_CHANGED] = "changed", | |
92 | [WT_STATUS_UNTRACKED] = "untracked", | |
93 | [WT_STATUS_NOBRANCH] = "noBranch", | |
94 | [WT_STATUS_UNMERGED] = "unmerged", | |
95 | [WT_STATUS_LOCAL_BRANCH] = "localBranch", | |
96 | [WT_STATUS_REMOTE_BRANCH] = "remoteBranch", | |
97 | [WT_STATUS_ONBRANCH] = "branch", | |
98 | }; | |
99 | ||
37f7a857 | 100 | static const char *use_message_buffer; |
2888605c JH |
101 | static struct lock_file index_lock; /* real index */ |
102 | static struct lock_file false_lock; /* used only for partial commits */ | |
103 | static enum { | |
104 | COMMIT_AS_IS = 1, | |
105 | COMMIT_NORMAL, | |
4b05548f | 106 | COMMIT_PARTIAL |
2888605c | 107 | } commit_style; |
f5bbc322 | 108 | |
dbd0f5c7 | 109 | static const char *logfile, *force_author; |
984c6e7e | 110 | static const char *template_file; |
37f7a857 JS |
111 | /* |
112 | * The _message variables are commit names from which to take | |
113 | * the commit message and/or authorship. | |
114 | */ | |
115 | static const char *author_message, *author_message_buffer; | |
f5bbc322 | 116 | static char *edit_message, *use_message; |
494d314a CM |
117 | static char *fixup_message, *fixup_commit, *squash_message; |
118 | static const char *fixup_prefix; | |
ca1ba201 JH |
119 | static int all, also, interactive, patch_interactive, only, amend, signoff; |
120 | static int edit_flag = -1; /* unspecified */ | |
c51f6cee | 121 | static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship; |
aaab8420 | 122 | static int config_commit_verbose = -1; /* unspecified */ |
e440fc58 | 123 | static int no_post_rewrite, allow_empty_message, pathspec_file_nul; |
eec0f7f2 | 124 | static char *untracked_files_arg, *force_date, *ignore_submodule_arg, *ignored_arg; |
e440fc58 | 125 | static char *sign_commit, *pathspec_from_file; |
2daae3d1 | 126 | static struct strvec trailer_args = STRVEC_INIT; |
ba3c69a9 | 127 | |
5f065737 AR |
128 | /* |
129 | * The default commit message cleanup mode will remove the lines | |
130 | * beginning with # (shell comments) and leading and trailing | |
131 | * whitespaces (empty lines or containing only whitespaces) | |
132 | * if editor is used, and only the whitespaces if the message | |
133 | * is specified explicitly. | |
134 | */ | |
d0aaa46f | 135 | static enum commit_msg_cleanup_mode cleanup_mode; |
51fb3a3d | 136 | static const char *cleanup_arg; |
f5bbc322 | 137 | |
37f7a857 | 138 | static enum commit_whence whence; |
06bb643b | 139 | static int use_editor = 1, include_status = 1; |
eec0f7f2 | 140 | static int have_option_m; |
2c47789d | 141 | static struct strbuf message = STRBUF_INIT; |
f9568530 | 142 | |
be7e795e | 143 | static enum wt_status_format status_format = STATUS_FORMAT_UNSPECIFIED; |
84b4202d | 144 | |
2daae3d1 ZH |
145 | static int opt_pass_trailer(const struct option *opt, const char *arg, int unset) |
146 | { | |
147 | BUG_ON_OPT_NEG(unset); | |
148 | ||
116761ba | 149 | strvec_pushl(opt->value, "--trailer", arg, NULL); |
2daae3d1 ZH |
150 | return 0; |
151 | } | |
152 | ||
c4f596b9 JH |
153 | static int opt_parse_porcelain(const struct option *opt, const char *arg, int unset) |
154 | { | |
155 | enum wt_status_format *value = (enum wt_status_format *)opt->value; | |
156 | if (unset) | |
157 | *value = STATUS_FORMAT_NONE; | |
158 | else if (!arg) | |
159 | *value = STATUS_FORMAT_PORCELAIN; | |
160 | else if (!strcmp(arg, "v1") || !strcmp(arg, "1")) | |
161 | *value = STATUS_FORMAT_PORCELAIN; | |
1ecdecce JH |
162 | else if (!strcmp(arg, "v2") || !strcmp(arg, "2")) |
163 | *value = STATUS_FORMAT_PORCELAIN_V2; | |
c4f596b9 JH |
164 | else |
165 | die("unsupported porcelain version '%s'", arg); | |
166 | ||
167 | return 0; | |
168 | } | |
7c9f7038 | 169 | |
f9568530 JS |
170 | static int opt_parse_m(const struct option *opt, const char *arg, int unset) |
171 | { | |
172 | struct strbuf *buf = opt->value; | |
25206778 RS |
173 | if (unset) { |
174 | have_option_m = 0; | |
f9568530 | 175 | strbuf_setlen(buf, 0); |
25206778 RS |
176 | } else { |
177 | have_option_m = 1; | |
a24a41ea BC |
178 | if (buf->len) |
179 | strbuf_addch(buf, '\n'); | |
f9568530 | 180 | strbuf_addstr(buf, arg); |
a24a41ea | 181 | strbuf_complete_line(buf); |
f9568530 JS |
182 | } |
183 | return 0; | |
184 | } | |
f5bbc322 | 185 | |
e8b2dc2c BP |
186 | static int opt_parse_rename_score(const struct option *opt, const char *arg, int unset) |
187 | { | |
188 | const char **value = opt->value; | |
517fe807 JK |
189 | |
190 | BUG_ON_OPT_NEG(unset); | |
191 | ||
e8b2dc2c BP |
192 | if (arg != NULL && *arg == '=') |
193 | arg = arg + 1; | |
194 | ||
195 | *value = arg; | |
196 | return 0; | |
197 | } | |
198 | ||
37f7a857 JS |
199 | static void determine_whence(struct wt_status *s) |
200 | { | |
102de880 | 201 | if (file_exists(git_path_merge_head(the_repository))) |
37f7a857 | 202 | whence = FROM_MERGE; |
901ba7b1 | 203 | else if (!sequencer_determine_whence(the_repository, &whence)) |
37f7a857 JS |
204 | whence = FROM_COMMIT; |
205 | if (s) | |
206 | s->whence = whence; | |
207 | } | |
208 | ||
5c25dfaa MM |
209 | static void status_init_config(struct wt_status *s, config_fn_t fn) |
210 | { | |
5b02ca38 | 211 | wt_status_prepare(the_repository, s); |
dc6b1d92 | 212 | init_diff_ui_defaults(); |
5c25dfaa MM |
213 | git_config(fn, s); |
214 | determine_whence(s); | |
ed9bff08 | 215 | s->hints = advice_enabled(ADVICE_STATUS_HINTS); /* must come after git_config() */ |
5c25dfaa MM |
216 | } |
217 | ||
2888605c JH |
218 | static void rollback_index_files(void) |
219 | { | |
220 | switch (commit_style) { | |
221 | case COMMIT_AS_IS: | |
222 | break; /* nothing to do */ | |
223 | case COMMIT_NORMAL: | |
224 | rollback_lock_file(&index_lock); | |
225 | break; | |
226 | case COMMIT_PARTIAL: | |
227 | rollback_lock_file(&index_lock); | |
228 | rollback_lock_file(&false_lock); | |
229 | break; | |
230 | } | |
231 | } | |
232 | ||
5a9dd399 | 233 | static int commit_index_files(void) |
2888605c | 234 | { |
5a9dd399 BC |
235 | int err = 0; |
236 | ||
2888605c JH |
237 | switch (commit_style) { |
238 | case COMMIT_AS_IS: | |
239 | break; /* nothing to do */ | |
240 | case COMMIT_NORMAL: | |
5a9dd399 | 241 | err = commit_lock_file(&index_lock); |
2888605c JH |
242 | break; |
243 | case COMMIT_PARTIAL: | |
5a9dd399 | 244 | err = commit_lock_file(&index_lock); |
2888605c JH |
245 | rollback_lock_file(&false_lock); |
246 | break; | |
247 | } | |
5a9dd399 BC |
248 | |
249 | return err; | |
2888605c JH |
250 | } |
251 | ||
252 | /* | |
253 | * Take a union of paths in the index and the named tree (typically, "HEAD"), | |
254 | * and return the paths that match the given pattern in list. | |
255 | */ | |
c455c87c | 256 | static int list_paths(struct string_list *list, const char *with_tree, |
c5c33504 | 257 | const struct pathspec *pattern) |
2888605c | 258 | { |
5d0b9bf8 | 259 | int i, ret; |
2888605c JH |
260 | char *m; |
261 | ||
17ddc66e | 262 | if (!pattern->nr) |
ea2d4ed3 JK |
263 | return 0; |
264 | ||
17ddc66e | 265 | m = xcalloc(1, pattern->nr); |
2888605c | 266 | |
8894d535 | 267 | if (with_tree) { |
f950eb95 | 268 | char *max_prefix = common_prefix(pattern); |
86238e07 | 269 | overlay_tree_on_index(&the_index, with_tree, max_prefix); |
5879f568 | 270 | free(max_prefix); |
8894d535 | 271 | } |
2888605c | 272 | |
cb8388df DS |
273 | /* TODO: audit for interaction with sparse-index. */ |
274 | ensure_full_index(&the_index); | |
dc594180 ÆAB |
275 | for (i = 0; i < the_index.cache_nr; i++) { |
276 | const struct cache_entry *ce = the_index.cache[i]; | |
7fce6e3c NTND |
277 | struct string_list_item *item; |
278 | ||
7a51ed66 | 279 | if (ce->ce_flags & CE_UPDATE) |
e87e22d0 | 280 | continue; |
6d2df284 | 281 | if (!ce_path_match(&the_index, ce, pattern, m)) |
2888605c | 282 | continue; |
78a395d3 | 283 | item = string_list_insert(list, ce->name); |
7fce6e3c NTND |
284 | if (ce_skip_worktree(ce)) |
285 | item->util = item; /* better a valid pointer than a fake one */ | |
2888605c JH |
286 | } |
287 | ||
c5c33504 | 288 | ret = report_path_error(m, pattern); |
5d0b9bf8 SB |
289 | free(m); |
290 | return ret; | |
2888605c JH |
291 | } |
292 | ||
c455c87c | 293 | static void add_remove_files(struct string_list *list) |
2888605c JH |
294 | { |
295 | int i; | |
296 | for (i = 0; i < list->nr; i++) { | |
d177cab0 | 297 | struct stat st; |
c455c87c | 298 | struct string_list_item *p = &(list->items[i]); |
d177cab0 | 299 | |
7fce6e3c NTND |
300 | /* p->util is skip-worktree */ |
301 | if (p->util) | |
b4d1690d | 302 | continue; |
d177cab0 | 303 | |
c455c87c | 304 | if (!lstat(p->string, &st)) { |
fbc1ed62 | 305 | if (add_to_index(&the_index, p->string, &st, 0)) |
8a6179bc | 306 | die(_("updating files failed")); |
960b8ad1 | 307 | } else |
031b2033 | 308 | remove_file_from_index(&the_index, p->string); |
2888605c JH |
309 | } |
310 | } | |
311 | ||
06bb643b | 312 | static void create_base_index(const struct commit *current_head) |
fa9dcf80 LT |
313 | { |
314 | struct tree *tree; | |
315 | struct unpack_trees_options opts; | |
316 | struct tree_desc t; | |
317 | ||
06bb643b | 318 | if (!current_head) { |
07047d68 | 319 | discard_index(&the_index); |
fa9dcf80 LT |
320 | return; |
321 | } | |
322 | ||
323 | memset(&opts, 0, sizeof(opts)); | |
324 | opts.head_idx = 1; | |
325 | opts.index_only = 1; | |
326 | opts.merge = 1; | |
34110cd4 LT |
327 | opts.src_index = &the_index; |
328 | opts.dst_index = &the_index; | |
fa9dcf80 LT |
329 | |
330 | opts.fn = oneway_merge; | |
a9dbc179 | 331 | tree = parse_tree_indirect(¤t_head->object.oid); |
fa9dcf80 | 332 | if (!tree) |
8a6179bc | 333 | die(_("failed to unpack HEAD tree object")); |
aa9f6189 JS |
334 | if (parse_tree(tree) < 0) |
335 | exit(128); | |
efed687e | 336 | init_tree_desc(&t, &tree->object.oid, tree->buffer, tree->size); |
203a2fe1 DB |
337 | if (unpack_trees(1, &t, &opts)) |
338 | exit(128); /* We've already reported the error, finish dying */ | |
fa9dcf80 LT |
339 | } |
340 | ||
d38a30df MM |
341 | static void refresh_cache_or_die(int refresh_flags) |
342 | { | |
343 | /* | |
344 | * refresh_flags contains REFRESH_QUIET, so the only errors | |
345 | * are for unmerged entries. | |
346 | */ | |
07047d68 | 347 | if (refresh_index(&the_index, refresh_flags | REFRESH_IN_PORCELAIN, NULL, NULL, NULL)) |
d38a30df MM |
348 | die_resolve_conflict("commit"); |
349 | } | |
350 | ||
e885a84f | 351 | static const char *prepare_index(const char **argv, const char *prefix, |
35ff08be | 352 | const struct commit *current_head, int is_status) |
f5bbc322 | 353 | { |
dd1055ed | 354 | struct string_list partial = STRING_LIST_INIT_DUP; |
6654c889 | 355 | struct pathspec pathspec; |
50b7e70f | 356 | int refresh_flags = REFRESH_QUIET; |
b4fb09e4 | 357 | const char *ret; |
f5bbc322 | 358 | |
50b7e70f JH |
359 | if (is_status) |
360 | refresh_flags |= REFRESH_UNMERGED; | |
6654c889 NTND |
361 | parse_pathspec(&pathspec, 0, |
362 | PATHSPEC_PREFER_FULL, | |
363 | prefix, argv); | |
f5bbc322 | 364 | |
e440fc58 AM |
365 | if (pathspec_from_file) { |
366 | if (interactive) | |
12909b6b | 367 | die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch"); |
e440fc58 | 368 | |
509efef7 | 369 | if (all) |
12909b6b | 370 | die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "-a"); |
509efef7 | 371 | |
e440fc58 | 372 | if (pathspec.nr) |
246cac85 | 373 | die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file"); |
e440fc58 AM |
374 | |
375 | parse_pathspec_file(&pathspec, 0, | |
376 | PATHSPEC_PREFER_FULL, | |
377 | prefix, pathspec_from_file, pathspec_file_nul); | |
378 | } else if (pathspec_file_nul) { | |
6fa00ee8 | 379 | die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file"); |
e440fc58 AM |
380 | } |
381 | ||
494d314a CM |
382 | if (!pathspec.nr && (also || (only && !allow_empty && |
383 | (!amend || (fixup_message && strcmp(fixup_prefix, "amend")))))) | |
e440fc58 AM |
384 | die(_("No paths with --include/--only does not make sense.")); |
385 | ||
07047d68 | 386 | if (repo_read_index_preload(the_repository, &pathspec, 0) < 0) |
8a6179bc | 387 | die(_("index file corrupt")); |
671c9b7e | 388 | |
1020d087 | 389 | if (interactive) { |
c480eeb5 | 390 | char *old_index_env = NULL, *old_repo_index_file; |
07047d68 ÆAB |
391 | repo_hold_locked_index(the_repository, &index_lock, |
392 | LOCK_DIE_ON_ERROR); | |
1020d087 CI |
393 | |
394 | refresh_cache_or_die(refresh_flags); | |
395 | ||
812d6b00 | 396 | if (write_locked_index(&the_index, &index_lock, 0)) |
1020d087 CI |
397 | die(_("unable to create temporary index")); |
398 | ||
c480eeb5 JS |
399 | old_repo_index_file = the_repository->index_file; |
400 | the_repository->index_file = | |
401 | (char *)get_lock_file_path(&index_lock); | |
406bab38 | 402 | old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT)); |
c480eeb5 | 403 | setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1); |
1020d087 | 404 | |
e885a84f | 405 | if (interactive_add(argv, prefix, patch_interactive) != 0) |
1020d087 CI |
406 | die(_("interactive add failed")); |
407 | ||
c480eeb5 | 408 | the_repository->index_file = old_repo_index_file; |
1020d087 CI |
409 | if (old_index_env && *old_index_env) |
410 | setenv(INDEX_ENVIRONMENT, old_index_env, 1); | |
411 | else | |
412 | unsetenv(INDEX_ENVIRONMENT); | |
406bab38 | 413 | FREE_AND_NULL(old_index_env); |
1020d087 | 414 | |
07047d68 ÆAB |
415 | discard_index(&the_index); |
416 | read_index_from(&the_index, get_lock_file_path(&index_lock), | |
417 | get_git_dir()); | |
fcb864bc | 418 | if (cache_tree_update(&the_index, WRITE_TREE_SILENT) == 0) { |
3fd13cbc | 419 | if (reopen_lock_file(&index_lock) < 0) |
9c4d6c02 | 420 | die(_("unable to write index file")); |
812d6b00 | 421 | if (write_locked_index(&the_index, &index_lock, 0)) |
3fd13cbc | 422 | die(_("unable to update temporary index")); |
9c4d6c02 DT |
423 | } else |
424 | warning(_("Failed to update main cache tree")); | |
1020d087 CI |
425 | |
426 | commit_style = COMMIT_NORMAL; | |
dd1055ed MÅ |
427 | ret = get_lock_file_path(&index_lock); |
428 | goto out; | |
1020d087 CI |
429 | } |
430 | ||
2888605c JH |
431 | /* |
432 | * Non partial, non as-is commit. | |
433 | * | |
434 | * (1) get the real index; | |
435 | * (2) update the_index as necessary; | |
436 | * (3) write the_index out to the real index (still locked); | |
437 | * (4) return the name of the locked index file. | |
438 | * | |
439 | * The caller should run hooks on the locked real index, and | |
440 | * (A) if all goes well, commit the real index; | |
441 | * (B) on failure, rollback the real index. | |
442 | */ | |
6654c889 | 443 | if (all || (also && pathspec.nr)) { |
07047d68 ÆAB |
444 | repo_hold_locked_index(the_repository, &index_lock, |
445 | LOCK_DIE_ON_ERROR); | |
50c37ee8 EN |
446 | add_files_to_cache(the_repository, also ? prefix : NULL, |
447 | &pathspec, 0, 0); | |
d38a30df | 448 | refresh_cache_or_die(refresh_flags); |
fcb864bc | 449 | cache_tree_update(&the_index, WRITE_TREE_SILENT); |
812d6b00 | 450 | if (write_locked_index(&the_index, &index_lock, 0)) |
a060705d | 451 | die(_("unable to write new index file")); |
2888605c | 452 | commit_style = COMMIT_NORMAL; |
dd1055ed MÅ |
453 | ret = get_lock_file_path(&index_lock); |
454 | goto out; | |
f5bbc322 KH |
455 | } |
456 | ||
2888605c JH |
457 | /* |
458 | * As-is commit. | |
459 | * | |
460 | * (1) return the name of the real index file. | |
461 | * | |
73276235 MH |
462 | * The caller should run hooks on the real index, |
463 | * and create commit from the_index. | |
2888605c JH |
464 | * We still need to refresh the index here. |
465 | */ | |
6654c889 | 466 | if (!only && !pathspec.nr) { |
07047d68 ÆAB |
467 | repo_hold_locked_index(the_repository, &index_lock, |
468 | LOCK_DIE_ON_ERROR); | |
d38a30df | 469 | refresh_cache_or_die(refresh_flags); |
dc594180 ÆAB |
470 | if (the_index.cache_changed |
471 | || !cache_tree_fully_valid(the_index.cache_tree)) | |
fcb864bc | 472 | cache_tree_update(&the_index, WRITE_TREE_SILENT); |
61000814 MÅ |
473 | if (write_locked_index(&the_index, &index_lock, |
474 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) | |
a060705d | 475 | die(_("unable to write new index file")); |
2888605c | 476 | commit_style = COMMIT_AS_IS; |
dd1055ed MÅ |
477 | ret = get_index_file(); |
478 | goto out; | |
f5bbc322 KH |
479 | } |
480 | ||
2888605c JH |
481 | /* |
482 | * A partial commit. | |
483 | * | |
484 | * (0) find the set of affected paths; | |
485 | * (1) get lock on the real index file; | |
486 | * (2) update the_index with the given paths; | |
487 | * (3) write the_index out to the real index (still locked); | |
488 | * (4) get lock on the false index file; | |
489 | * (5) reset the_index from HEAD; | |
490 | * (6) update the_index the same way as (2); | |
491 | * (7) write the_index out to the false index file; | |
492 | * (8) return the name of the false index file (still locked); | |
493 | * | |
494 | * The caller should run hooks on the locked false index, and | |
495 | * create commit from it. Then | |
496 | * (A) if all goes well, commit the real index; | |
497 | * (B) on failure, rollback the real index; | |
498 | * In either case, rollback the false index. | |
499 | */ | |
500 | commit_style = COMMIT_PARTIAL; | |
501 | ||
b0cea47e ÆAB |
502 | if (whence != FROM_COMMIT) { |
503 | if (whence == FROM_MERGE) | |
504 | die(_("cannot do a partial commit during a merge.")); | |
8d57f757 | 505 | else if (is_from_cherry_pick(whence)) |
b0cea47e | 506 | die(_("cannot do a partial commit during a cherry-pick.")); |
430b75f7 PW |
507 | else if (is_from_rebase(whence)) |
508 | die(_("cannot do a partial commit during a rebase.")); | |
b0cea47e | 509 | } |
2888605c | 510 | |
c5c33504 | 511 | if (list_paths(&partial, !current_head ? NULL : "HEAD", &pathspec)) |
2888605c JH |
512 | exit(1); |
513 | ||
07047d68 ÆAB |
514 | discard_index(&the_index); |
515 | if (repo_read_index(the_repository) < 0) | |
8a6179bc | 516 | die(_("cannot read the index")); |
2888605c | 517 | |
07047d68 | 518 | repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR); |
2888605c | 519 | add_remove_files(&partial); |
07047d68 | 520 | refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL); |
fcb864bc | 521 | cache_tree_update(&the_index, WRITE_TREE_SILENT); |
812d6b00 | 522 | if (write_locked_index(&the_index, &index_lock, 0)) |
a060705d | 523 | die(_("unable to write new index file")); |
f5bbc322 | 524 | |
03b86647 NTND |
525 | hold_lock_file_for_update(&false_lock, |
526 | git_path("next-index-%"PRIuMAX, | |
527 | (uintmax_t) getpid()), | |
528 | LOCK_DIE_ON_ERROR); | |
fa9dcf80 | 529 | |
06bb643b | 530 | create_base_index(current_head); |
2888605c | 531 | add_remove_files(&partial); |
07047d68 | 532 | refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL); |
f5bbc322 | 533 | |
812d6b00 | 534 | if (write_locked_index(&the_index, &false_lock, 0)) |
8a6179bc | 535 | die(_("unable to write temporary index file")); |
959ba670 | 536 | |
07047d68 | 537 | discard_index(&the_index); |
b4fb09e4 | 538 | ret = get_lock_file_path(&false_lock); |
07047d68 | 539 | read_index_from(&the_index, ret, get_git_dir()); |
dd1055ed MÅ |
540 | out: |
541 | string_list_clear(&partial, 0); | |
542 | clear_pathspec(&pathspec); | |
b4fb09e4 | 543 | return ret; |
f5bbc322 KH |
544 | } |
545 | ||
d249b098 JH |
546 | static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn, |
547 | struct wt_status *s) | |
f5bbc322 | 548 | { |
8066df41 | 549 | struct object_id oid; |
76e2f7ce | 550 | |
d249b098 JH |
551 | if (s->relative_paths) |
552 | s->prefix = prefix; | |
f5bbc322 KH |
553 | |
554 | if (amend) { | |
d249b098 JH |
555 | s->amend = 1; |
556 | s->reference = "HEAD^1"; | |
f5bbc322 | 557 | } |
d249b098 JH |
558 | s->verbose = verbose; |
559 | s->index_file = index_file; | |
560 | s->fp = fp; | |
561 | s->nowarn = nowarn; | |
d850b7a5 | 562 | s->is_initial = repo_get_oid(the_repository, s->reference, &oid) ? 1 : 0; |
d9fc746c | 563 | if (!s->is_initial) |
e0cb7cdb | 564 | oidcpy(&s->oid_commit, &oid); |
be7e795e JH |
565 | s->status_format = status_format; |
566 | s->ignore_submodule_arg = ignore_submodule_arg; | |
f5bbc322 | 567 | |
76e2f7ce | 568 | wt_status_collect(s); |
be7e795e | 569 | wt_status_print(s); |
73ba5d78 | 570 | wt_status_collect_free_buffers(s); |
f5bbc322 | 571 | |
6fa90194 | 572 | return s->committable; |
f5bbc322 KH |
573 | } |
574 | ||
06bb643b | 575 | static int is_a_merge(const struct commit *current_head) |
ec84bd00 | 576 | { |
06bb643b | 577 | return !!(current_head->parents && current_head->parents->next); |
ec84bd00 PB |
578 | } |
579 | ||
fac90838 JK |
580 | static void assert_split_ident(struct ident_split *id, const struct strbuf *buf) |
581 | { | |
c83a5099 | 582 | if (split_ident_line(id, buf->buf, buf->len) || !id->date_begin) |
033abf97 | 583 | BUG("unable to parse our own ident: %s", buf->buf); |
fac90838 JK |
584 | } |
585 | ||
7dfe8ad6 JH |
586 | static void export_one(const char *var, const char *s, const char *e, int hack) |
587 | { | |
588 | struct strbuf buf = STRBUF_INIT; | |
589 | if (hack) | |
590 | strbuf_addch(&buf, hack); | |
147ee355 | 591 | strbuf_add(&buf, s, e - s); |
7dfe8ad6 JH |
592 | setenv(var, buf.buf, 1); |
593 | strbuf_release(&buf); | |
594 | } | |
595 | ||
c33ddc2e | 596 | static int parse_force_date(const char *in, struct strbuf *out) |
14ac2864 | 597 | { |
c33ddc2e | 598 | strbuf_addch(out, '@'); |
14ac2864 | 599 | |
c33ddc2e | 600 | if (parse_date(in, out) < 0) { |
14ac2864 JK |
601 | int errors = 0; |
602 | unsigned long t = approxidate_careful(in, &errors); | |
603 | if (errors) | |
604 | return -1; | |
c33ddc2e | 605 | strbuf_addf(out, "%lu", t); |
14ac2864 JK |
606 | } |
607 | ||
608 | return 0; | |
609 | } | |
610 | ||
f4ef5173 JK |
611 | static void set_ident_var(char **buf, char *val) |
612 | { | |
613 | free(*buf); | |
614 | *buf = val; | |
615 | } | |
616 | ||
4c28e4ad | 617 | static void determine_author_info(struct strbuf *author_ident) |
a45d46ba SB |
618 | { |
619 | char *name, *email, *date; | |
7dfe8ad6 | 620 | struct ident_split author; |
a45d46ba | 621 | |
eaa541eb JK |
622 | name = xstrdup_or_null(getenv("GIT_AUTHOR_NAME")); |
623 | email = xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL")); | |
624 | date = xstrdup_or_null(getenv("GIT_AUTHOR_DATE")); | |
a45d46ba | 625 | |
37f7a857 | 626 | if (author_message) { |
f0f9662a | 627 | struct ident_split ident; |
2c733fb2 | 628 | size_t len; |
f0f9662a | 629 | const char *a; |
a45d46ba | 630 | |
f0f9662a | 631 | a = find_commit_header(author_message_buffer, "author", &len); |
a45d46ba | 632 | if (!a) |
f0f9662a JK |
633 | die(_("commit '%s' lacks author header"), author_message); |
634 | if (split_ident_line(&ident, a, len) < 0) | |
635 | die(_("commit '%s' has malformed author line"), author_message); | |
636 | ||
f4ef5173 JK |
637 | set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin)); |
638 | set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin)); | |
639 | ||
f0f9662a | 640 | if (ident.date_begin) { |
f4ef5173 | 641 | struct strbuf date_buf = STRBUF_INIT; |
f0f9662a JK |
642 | strbuf_addch(&date_buf, '@'); |
643 | strbuf_add(&date_buf, ident.date_begin, ident.date_end - ident.date_begin); | |
644 | strbuf_addch(&date_buf, ' '); | |
645 | strbuf_add(&date_buf, ident.tz_begin, ident.tz_end - ident.tz_begin); | |
f4ef5173 | 646 | set_ident_var(&date, strbuf_detach(&date_buf, NULL)); |
f0f9662a | 647 | } |
a45d46ba SB |
648 | } |
649 | ||
650 | if (force_author) { | |
f0f9662a | 651 | struct ident_split ident; |
a45d46ba | 652 | |
f0f9662a | 653 | if (split_ident_line(&ident, force_author, strlen(force_author)) < 0) |
8a6179bc | 654 | die(_("malformed --author parameter")); |
f4ef5173 JK |
655 | set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin)); |
656 | set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin)); | |
a45d46ba SB |
657 | } |
658 | ||
14ac2864 | 659 | if (force_date) { |
f4ef5173 | 660 | struct strbuf date_buf = STRBUF_INIT; |
c33ddc2e | 661 | if (parse_force_date(force_date, &date_buf)) |
14ac2864 | 662 | die(_("invalid date format: %s"), force_date); |
f4ef5173 | 663 | set_ident_var(&date, strbuf_detach(&date_buf, NULL)); |
14ac2864 JK |
664 | } |
665 | ||
39ab4d09 WH |
666 | strbuf_addstr(author_ident, fmt_ident(name, email, WANT_AUTHOR_IDENT, date, |
667 | IDENT_STRICT)); | |
c83a5099 JK |
668 | assert_split_ident(&author, author_ident); |
669 | export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0); | |
670 | export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0); | |
671 | export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@'); | |
f4ef5173 JK |
672 | free(name); |
673 | free(email); | |
674 | free(date); | |
a45d46ba SB |
675 | } |
676 | ||
b7242b8c JK |
677 | static int author_date_is_interesting(void) |
678 | { | |
679 | return author_message || force_date; | |
4c28e4ad JH |
680 | } |
681 | ||
84c9dc2c NTND |
682 | static void adjust_comment_line_char(const struct strbuf *sb) |
683 | { | |
684 | char candidates[] = "#;@!$%^&|:"; | |
685 | char *candidate; | |
686 | const char *p; | |
687 | ||
688 | comment_line_char = candidates[0]; | |
689 | if (!memchr(sb->buf, comment_line_char, sb->len)) | |
690 | return; | |
691 | ||
692 | p = sb->buf; | |
693 | candidate = strchr(candidates, *p); | |
694 | if (candidate) | |
695 | *candidate = ' '; | |
696 | for (p = sb->buf; *p; p++) { | |
697 | if ((p[0] == '\n' || p[0] == '\r') && p[1]) { | |
698 | candidate = strchr(candidates, p[1]); | |
699 | if (candidate) | |
700 | *candidate = ' '; | |
701 | } | |
702 | } | |
703 | ||
704 | for (p = candidates; *p == ' '; p++) | |
705 | ; | |
706 | if (!*p) | |
707 | die(_("unable to select a comment character that is not used\n" | |
708 | "in the current commit message")); | |
709 | comment_line_char = *p; | |
710 | } | |
711 | ||
494d314a CM |
712 | static void prepare_amend_commit(struct commit *commit, struct strbuf *sb, |
713 | struct pretty_print_context *ctx) | |
714 | { | |
715 | const char *buffer, *subject, *fmt; | |
716 | ||
ecb5091f | 717 | buffer = repo_get_commit_buffer(the_repository, commit, NULL); |
494d314a CM |
718 | find_commit_subject(buffer, &subject); |
719 | /* | |
720 | * If we amend the 'amend!' commit then we don't want to | |
721 | * duplicate the subject line. | |
722 | */ | |
723 | fmt = starts_with(subject, "amend!") ? "%b" : "%B"; | |
bab82164 | 724 | repo_format_commit_message(the_repository, commit, fmt, sb, ctx); |
ecb5091f | 725 | repo_unuse_commit_buffer(the_repository, commit, buffer); |
494d314a CM |
726 | } |
727 | ||
d249b098 | 728 | static int prepare_to_commit(const char *index_file, const char *prefix, |
06bb643b | 729 | struct commit *current_head, |
4c28e4ad JH |
730 | struct wt_status *s, |
731 | struct strbuf *author_ident) | |
f5bbc322 KH |
732 | { |
733 | struct stat statbuf; | |
4c28e4ad | 734 | struct strbuf committer_ident = STRBUF_INIT; |
6fa90194 | 735 | int committable; |
f285a2d7 | 736 | struct strbuf sb = STRBUF_INIT; |
8089c85b PB |
737 | const char *hook_arg1 = NULL; |
738 | const char *hook_arg2 = NULL; | |
d0aaa46f | 739 | int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE); |
2556b996 | 740 | int old_display_comment_prefix; |
a8cc5943 | 741 | int invoked_hook; |
f5bbc322 | 742 | |
7dfe8ad6 JH |
743 | /* This checks and barfs if author is badly specified */ |
744 | determine_author_info(author_ident); | |
745 | ||
a8cc5943 ÆAB |
746 | if (!no_verify && run_commit_hook(use_editor, index_file, &invoked_hook, |
747 | "pre-commit", NULL)) | |
ec84bd00 | 748 | return 0; |
f5bbc322 | 749 | |
89ac1223 PN |
750 | if (squash_message) { |
751 | /* | |
752 | * Insert the proper subject line before other commit | |
753 | * message options add their content. | |
754 | */ | |
755 | if (use_message && !strcmp(use_message, squash_message)) | |
756 | strbuf_addstr(&sb, "squash! "); | |
757 | else { | |
758 | struct pretty_print_context ctx = {0}; | |
759 | struct commit *c; | |
760 | c = lookup_commit_reference_by_name(squash_message); | |
761 | if (!c) | |
e4cf0134 | 762 | die(_("could not lookup commit '%s'"), squash_message); |
89ac1223 | 763 | ctx.output_encoding = get_commit_output_encoding(); |
bab82164 ÆAB |
764 | repo_format_commit_message(the_repository, c, |
765 | "squash! %s\n\n", &sb, | |
766 | &ctx); | |
89ac1223 PN |
767 | } |
768 | } | |
769 | ||
30884c9a | 770 | if (have_option_m && !fixup_message) { |
f9568530 | 771 | strbuf_addbuf(&sb, &message); |
8089c85b | 772 | hook_arg1 = "message"; |
f5bbc322 KH |
773 | } else if (logfile && !strcmp(logfile, "-")) { |
774 | if (isatty(0)) | |
8a6179bc | 775 | fprintf(stderr, _("(reading log message from standard input)\n")); |
f5bbc322 | 776 | if (strbuf_read(&sb, 0, 0) < 0) |
8a6179bc | 777 | die_errno(_("could not read log from standard input")); |
8089c85b | 778 | hook_arg1 = "message"; |
f5bbc322 KH |
779 | } else if (logfile) { |
780 | if (strbuf_read_file(&sb, logfile, 0) < 0) | |
8a6179bc | 781 | die_errno(_("could not read log file '%s'"), |
d824cbba | 782 | logfile); |
8089c85b | 783 | hook_arg1 = "message"; |
f5bbc322 | 784 | } else if (use_message) { |
e23fd15a | 785 | char *buffer; |
f5bbc322 | 786 | buffer = strstr(use_message_buffer, "\n\n"); |
076cbd63 | 787 | if (buffer) |
84e213a3 | 788 | strbuf_addstr(&sb, skip_blank_lines(buffer + 2)); |
8089c85b PB |
789 | hook_arg1 = "commit"; |
790 | hook_arg2 = use_message; | |
d71b8ba7 PN |
791 | } else if (fixup_message) { |
792 | struct pretty_print_context ctx = {0}; | |
793 | struct commit *commit; | |
494d314a CM |
794 | char *fmt; |
795 | commit = lookup_commit_reference_by_name(fixup_commit); | |
d71b8ba7 | 796 | if (!commit) |
e4cf0134 | 797 | die(_("could not lookup commit '%s'"), fixup_commit); |
d71b8ba7 | 798 | ctx.output_encoding = get_commit_output_encoding(); |
494d314a | 799 | fmt = xstrfmt("%s! %%s\n\n", fixup_prefix); |
bab82164 ÆAB |
800 | repo_format_commit_message(the_repository, commit, fmt, &sb, |
801 | &ctx); | |
494d314a | 802 | free(fmt); |
d71b8ba7 | 803 | hook_arg1 = "message"; |
494d314a CM |
804 | |
805 | /* | |
806 | * Only `-m` commit message option is checked here, as | |
807 | * it supports `--fixup` to append the commit message. | |
808 | * | |
809 | * The other commit message options `-c`/`-C`/`-F` are | |
810 | * incompatible with all the forms of `--fixup` and | |
811 | * have already errored out while parsing the `git commit` | |
812 | * options. | |
813 | */ | |
814 | if (have_option_m && !strcmp(fixup_prefix, "fixup")) | |
815 | strbuf_addbuf(&sb, &message); | |
816 | ||
817 | if (!strcmp(fixup_prefix, "amend")) { | |
818 | if (have_option_m) | |
246cac85 | 819 | die(_("options '%s' and '%s:%s' cannot be used together"), "-m", "--fixup", fixup_message); |
494d314a CM |
820 | prepare_amend_commit(commit, &sb, &ctx); |
821 | } | |
102de880 | 822 | } else if (!stat(git_path_merge_msg(the_repository), &statbuf)) { |
1055997e DL |
823 | size_t merge_msg_start; |
824 | ||
b64c1e07 SS |
825 | /* |
826 | * prepend SQUASH_MSG here if it exists and a | |
827 | * "merge --squash" was originally performed | |
828 | */ | |
102de880 SB |
829 | if (!stat(git_path_squash_msg(the_repository), &statbuf)) { |
830 | if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0) | |
b64c1e07 SS |
831 | die_errno(_("could not read SQUASH_MSG")); |
832 | hook_arg1 = "squash"; | |
833 | } else | |
834 | hook_arg1 = "merge"; | |
1055997e DL |
835 | |
836 | merge_msg_start = sb.len; | |
102de880 | 837 | if (strbuf_read_file(&sb, git_path_merge_msg(the_repository), 0) < 0) |
8a6179bc | 838 | die_errno(_("could not read MERGE_MSG")); |
1055997e DL |
839 | |
840 | if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS && | |
841 | wt_status_locate_end(sb.buf + merge_msg_start, | |
842 | sb.len - merge_msg_start) < | |
843 | sb.len - merge_msg_start) | |
e90cc075 | 844 | s->added_cut_line = 1; |
102de880 SB |
845 | } else if (!stat(git_path_squash_msg(the_repository), &statbuf)) { |
846 | if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0) | |
8a6179bc | 847 | die_errno(_("could not read SQUASH_MSG")); |
8089c85b | 848 | hook_arg1 = "squash"; |
2140b140 | 849 | } else if (template_file) { |
f5bbc322 | 850 | if (strbuf_read_file(&sb, template_file, 0) < 0) |
8a6179bc | 851 | die_errno(_("could not read '%s'"), template_file); |
8089c85b | 852 | hook_arg1 = "template"; |
8b1ae678 | 853 | clean_message_contents = 0; |
f5bbc322 KH |
854 | } |
855 | ||
8089c85b | 856 | /* |
37f7a857 JS |
857 | * The remaining cases don't modify the template message, but |
858 | * just set the argument(s) to the prepare-commit-msg hook. | |
8089c85b | 859 | */ |
37f7a857 | 860 | else if (whence == FROM_MERGE) |
8089c85b | 861 | hook_arg1 = "merge"; |
430b75f7 | 862 | else if (is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) { |
37f7a857 JS |
863 | hook_arg1 = "commit"; |
864 | hook_arg2 = "CHERRY_PICK_HEAD"; | |
865 | } | |
8089c85b | 866 | |
89ac1223 PN |
867 | if (squash_message) { |
868 | /* | |
869 | * If squash_commit was used for the commit subject, | |
870 | * then we're possibly hijacking other commit log options. | |
871 | * Reset the hook args to tell the real story. | |
872 | */ | |
873 | hook_arg1 = "message"; | |
874 | hook_arg2 = ""; | |
875 | } | |
876 | ||
e51b0dfc | 877 | s->fp = fopen_for_writing(git_path_commit_editmsg()); |
afe8a907 | 878 | if (!s->fp) |
e51b0dfc | 879 | die_errno(_("could not open '%s'"), git_path_commit_editmsg()); |
f5bbc322 | 880 | |
2556b996 MM |
881 | /* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */ |
882 | old_display_comment_prefix = s->display_comment_prefix; | |
883 | s->display_comment_prefix = 1; | |
884 | ||
ea9882bf MM |
885 | /* |
886 | * Most hints are counter-productive when the commit has | |
887 | * already started. | |
888 | */ | |
889 | s->hints = 0; | |
890 | ||
8b1ae678 | 891 | if (clean_message_contents) |
787cb8a4 | 892 | strbuf_stripspace(&sb, '\0'); |
f5bbc322 | 893 | |
073bd75e | 894 | if (signoff) |
7cb26a17 | 895 | append_signoff(&sb, ignored_log_message_bytes(sb.buf, sb.len), 0); |
f5bbc322 | 896 | |
37f3012f | 897 | if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len) |
8a6179bc | 898 | die_errno(_("could not write commit template")); |
13208572 | 899 | |
84c9dc2c NTND |
900 | if (auto_comment_line_char) |
901 | adjust_comment_line_char(&sb); | |
f5bbc322 KH |
902 | strbuf_release(&sb); |
903 | ||
bb1ae3f6 | 904 | /* This checks if committer ident is explicitly given */ |
f20f3878 | 905 | strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT)); |
bed575e4 | 906 | if (use_editor && include_status) { |
e23fd15a EP |
907 | int ident_shown = 0; |
908 | int saved_color_setting; | |
47010263 | 909 | struct ident_split ci, ai; |
6f70f00b HJ |
910 | const char *hint_cleanup_all = allow_empty_message ? |
911 | _("Please enter the commit message for your changes." | |
912 | " Lines starting\nwith '%c' will be ignored.\n") : | |
913 | _("Please enter the commit message for your changes." | |
914 | " Lines starting\nwith '%c' will be ignored, and an empty" | |
915 | " message aborts the commit.\n"); | |
916 | const char *hint_cleanup_space = allow_empty_message ? | |
917 | _("Please enter the commit message for your changes." | |
918 | " Lines starting\n" | |
919 | "with '%c' will be kept; you may remove them" | |
920 | " yourself if you want to.\n") : | |
921 | _("Please enter the commit message for your changes." | |
922 | " Lines starting\n" | |
923 | "with '%c' will be kept; you may remove them" | |
924 | " yourself if you want to.\n" | |
925 | "An empty message aborts the commit.\n"); | |
75df1f43 | 926 | if (whence != FROM_COMMIT) { |
e90cc075 | 927 | if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) |
688a0a75 | 928 | wt_status_add_cut_line(s); |
b6d2558c HWN |
929 | status_printf_ln( |
930 | s, GIT_COLOR_NORMAL, | |
ca03e067 | 931 | whence == FROM_MERGE ? |
b6d2558c HWN |
932 | _("\n" |
933 | "It looks like you may be committing a merge.\n" | |
934 | "If this is not correct, please run\n" | |
935 | " git update-ref -d MERGE_HEAD\n" | |
936 | "and try again.\n") : | |
937 | _("\n" | |
938 | "It looks like you may be committing a cherry-pick.\n" | |
939 | "If this is not correct, please run\n" | |
940 | " git update-ref -d CHERRY_PICK_HEAD\n" | |
941 | "and try again.\n")); | |
75df1f43 | 942 | } |
ec84bd00 | 943 | |
b926c0d1 | 944 | fprintf(s->fp, "\n"); |
d0aaa46f | 945 | if (cleanup_mode == COMMIT_MSG_CLEANUP_ALL) |
54ba2f18 | 946 | status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_all, comment_line_char); |
1055997e | 947 | else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) { |
e90cc075 | 948 | if (whence == FROM_COMMIT) |
688a0a75 | 949 | wt_status_add_cut_line(s); |
1055997e | 950 | } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */ |
54ba2f18 | 951 | status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_space, comment_line_char); |
ec84bd00 | 952 | |
fac90838 JK |
953 | /* |
954 | * These should never fail because they come from our own | |
955 | * fmt_ident. They may fail the sane_ident test, but we know | |
956 | * that the name and mail pointers will at least be valid, | |
957 | * which is enough for our tests and printing here. | |
958 | */ | |
959 | assert_split_ident(&ai, author_ident); | |
960 | assert_split_ident(&ci, &committer_ident); | |
47010263 JK |
961 | |
962 | if (ident_cmp(&ai, &ci)) | |
b926c0d1 | 963 | status_printf_ln(s, GIT_COLOR_NORMAL, |
fe8165cd | 964 | _("%s" |
47010263 | 965 | "Author: %.*s <%.*s>"), |
b926c0d1 | 966 | ident_shown++ ? "" : "\n", |
47010263 JK |
967 | (int)(ai.name_end - ai.name_begin), ai.name_begin, |
968 | (int)(ai.mail_end - ai.mail_begin), ai.mail_begin); | |
e83dbe80 | 969 | |
b7242b8c JK |
970 | if (author_date_is_interesting()) |
971 | status_printf_ln(s, GIT_COLOR_NORMAL, | |
972 | _("%s" | |
973 | "Date: %s"), | |
974 | ident_shown++ ? "" : "\n", | |
a5481a6c | 975 | show_ident_date(&ai, DATE_MODE(NORMAL))); |
e83dbe80 | 976 | |
d6991cee | 977 | if (!committer_ident_sufficiently_given()) |
b926c0d1 | 978 | status_printf_ln(s, GIT_COLOR_NORMAL, |
fe8165cd | 979 | _("%s" |
47010263 | 980 | "Committer: %.*s <%.*s>"), |
b926c0d1 | 981 | ident_shown++ ? "" : "\n", |
47010263 JK |
982 | (int)(ci.name_end - ci.name_begin), ci.name_begin, |
983 | (int)(ci.mail_end - ci.mail_begin), ci.mail_begin); | |
bb1ae3f6 | 984 | |
b3cf1b77 | 985 | status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); /* Add new line for clarity */ |
bb1ae3f6 | 986 | |
d249b098 JH |
987 | saved_color_setting = s->use_color; |
988 | s->use_color = 0; | |
6fa90194 | 989 | committable = run_status(s->fp, index_file, prefix, 1, s); |
d249b098 | 990 | s->use_color = saved_color_setting; |
3e73cc62 | 991 | string_list_clear(&s->change, 1); |
ec84bd00 | 992 | } else { |
8066df41 | 993 | struct object_id oid; |
fbcf1184 | 994 | const char *parent = "HEAD"; |
7168624c | 995 | |
2ee045ee JS |
996 | if (!the_index.initialized && repo_read_index(the_repository) < 0) |
997 | die(_("Cannot read index")); | |
7168624c | 998 | |
fbcf1184 JH |
999 | if (amend) |
1000 | parent = "HEAD^1"; | |
1001 | ||
d850b7a5 | 1002 | if (repo_get_oid(the_repository, parent, &oid)) { |
2c49f7ff NTND |
1003 | int i, ita_nr = 0; |
1004 | ||
cb8388df DS |
1005 | /* TODO: audit for interaction with sparse-index. */ |
1006 | ensure_full_index(&the_index); | |
dc594180 ÆAB |
1007 | for (i = 0; i < the_index.cache_nr; i++) |
1008 | if (ce_intent_to_add(the_index.cache[i])) | |
2c49f7ff | 1009 | ita_nr++; |
dc594180 | 1010 | committable = the_index.cache_nr - ita_nr > 0; |
2c49f7ff | 1011 | } else { |
c215d3d2 JL |
1012 | /* |
1013 | * Unless the user did explicitly request a submodule | |
1014 | * ignore mode by passing a command line option we do | |
1015 | * not ignore any changed submodule SHA-1s when | |
1016 | * comparing index and parent, no matter what is | |
1017 | * configured. Otherwise we won't commit any | |
1018 | * submodules which were manually staged, which would | |
1019 | * be really confusing. | |
1020 | */ | |
02f2f56b | 1021 | struct diff_flags flags = DIFF_FLAGS_INIT; |
0d1e0e78 | 1022 | flags.override_submodule_config = 1; |
c215d3d2 JL |
1023 | if (ignore_submodule_arg && |
1024 | !strcmp(ignore_submodule_arg, "all")) | |
0d1e0e78 | 1025 | flags.ignore_submodules = 1; |
ffc00a48 NTND |
1026 | committable = index_differs_from(the_repository, |
1027 | parent, &flags, 1); | |
c215d3d2 | 1028 | } |
ec84bd00 | 1029 | } |
4c28e4ad | 1030 | strbuf_release(&committer_ident); |
d616a239 | 1031 | |
37f3012f | 1032 | fclose(s->fp); |
7168624c | 1033 | |
2daae3d1 ZH |
1034 | if (trailer_args.nr) { |
1035 | struct child_process run_trailer = CHILD_PROCESS_INIT; | |
1036 | ||
1037 | strvec_pushl(&run_trailer.args, "interpret-trailers", | |
be3d6543 JK |
1038 | "--in-place", "--no-divider", |
1039 | git_path_commit_editmsg(), NULL); | |
2daae3d1 ZH |
1040 | strvec_pushv(&run_trailer.args, trailer_args.v); |
1041 | run_trailer.git_cmd = 1; | |
1042 | if (run_command(&run_trailer)) | |
1043 | die(_("unable to pass trailers to --trailers")); | |
1044 | strvec_clear(&trailer_args); | |
1045 | } | |
1046 | ||
37f7a857 JS |
1047 | /* |
1048 | * Reject an attempt to record a non-merge empty commit without | |
1049 | * explicit --allow-empty. In the cherry-pick case, it may be | |
1050 | * empty due to conflict resolution, which the user should okay. | |
1051 | */ | |
6fa90194 | 1052 | if (!committable && whence != FROM_MERGE && !allow_empty && |
06bb643b | 1053 | !(amend && is_a_merge(current_head))) { |
ed9bff08 | 1054 | s->hints = advice_enabled(ADVICE_STATUS_HINTS); |
2556b996 | 1055 | s->display_comment_prefix = old_display_comment_prefix; |
d249b098 | 1056 | run_status(stdout, index_file, prefix, 0, s); |
f197ed2f | 1057 | if (amend) |
fc88e316 | 1058 | fputs(_(empty_amend_advice), stderr); |
430b75f7 PW |
1059 | else if (is_from_cherry_pick(whence) || |
1060 | whence == FROM_REBASE_PICK) { | |
6c80cd29 | 1061 | fputs(_(empty_cherry_pick_advice), stderr); |
8d57f757 | 1062 | if (whence == FROM_CHERRY_PICK_SINGLE) |
c17592a7 | 1063 | fputs(_(empty_cherry_pick_advice_single), stderr); |
430b75f7 | 1064 | else if (whence == FROM_CHERRY_PICK_MULTI) |
c17592a7 | 1065 | fputs(_(empty_cherry_pick_advice_multi), stderr); |
430b75f7 PW |
1066 | else |
1067 | fputs(_(empty_rebase_pick_advice), stderr); | |
c17592a7 | 1068 | } |
ec84bd00 | 1069 | return 0; |
7168624c AR |
1070 | } |
1071 | ||
a8cc5943 | 1072 | if (!no_verify && invoked_hook) { |
680ee550 | 1073 | /* |
a8cc5943 ÆAB |
1074 | * Re-read the index as the pre-commit-commit hook was invoked |
1075 | * and could have updated it. We must do this before we invoke | |
680ee550 KW |
1076 | * the editor and after we invoke run_status above. |
1077 | */ | |
07047d68 | 1078 | discard_index(&the_index); |
680ee550 | 1079 | } |
07047d68 | 1080 | read_index_from(&the_index, index_file, get_git_dir()); |
680ee550 | 1081 | |
fcb864bc | 1082 | if (cache_tree_update(&the_index, 0)) { |
8a6179bc | 1083 | error(_("Error building trees")); |
ec84bd00 | 1084 | return 0; |
7168624c | 1085 | } |
f5bbc322 | 1086 | |
a8cc5943 | 1087 | if (run_commit_hook(use_editor, index_file, NULL, "prepare-commit-msg", |
e51b0dfc | 1088 | git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL)) |
8089c85b | 1089 | return 0; |
f5bbc322 | 1090 | |
ec84bd00 | 1091 | if (use_editor) { |
22f9b7f3 | 1092 | struct strvec env = STRVEC_INIT; |
8d7aa4ba | 1093 | |
22f9b7f3 | 1094 | strvec_pushf(&env, "GIT_INDEX_FILE=%s", index_file); |
d70a9eb6 | 1095 | if (launch_editor(git_path_commit_editmsg(), NULL, env.v)) { |
7198203a | 1096 | fprintf(stderr, |
8a6179bc | 1097 | _("Please supply the message using either -m or -F option.\n")); |
7198203a SB |
1098 | exit(1); |
1099 | } | |
22f9b7f3 | 1100 | strvec_clear(&env); |
ec84bd00 | 1101 | } |
f5bbc322 | 1102 | |
ec84bd00 | 1103 | if (!no_verify && |
a8cc5943 ÆAB |
1104 | run_commit_hook(use_editor, index_file, NULL, "commit-msg", |
1105 | git_path_commit_editmsg(), NULL)) { | |
ec84bd00 PB |
1106 | return 0; |
1107 | } | |
f5bbc322 | 1108 | |
ec84bd00 | 1109 | return 1; |
f5bbc322 KH |
1110 | } |
1111 | ||
146ea068 JH |
1112 | static const char *find_author_by_nickname(const char *name) |
1113 | { | |
1114 | struct rev_info revs; | |
1115 | struct commit *commit; | |
1116 | struct strbuf buf = STRBUF_INIT; | |
1117 | const char *av[20]; | |
1118 | int ac = 0; | |
1119 | ||
2abf3503 | 1120 | repo_init_revisions(the_repository, &revs, NULL); |
146ea068 JH |
1121 | strbuf_addf(&buf, "--author=%s", name); |
1122 | av[++ac] = "--all"; | |
1123 | av[++ac] = "-i"; | |
1124 | av[++ac] = buf.buf; | |
1125 | av[++ac] = NULL; | |
1126 | setup_revisions(ac, av, &revs, NULL); | |
a52f07af ÆAB |
1127 | revs.mailmap = xmalloc(sizeof(struct string_list)); |
1128 | string_list_init_nodup(revs.mailmap); | |
4e168333 | 1129 | read_mailmap(revs.mailmap); |
ea16794e | 1130 | |
81c3ce3c SB |
1131 | if (prepare_revision_walk(&revs)) |
1132 | die(_("revision walk setup failed")); | |
146ea068 JH |
1133 | commit = get_revision(&revs); |
1134 | if (commit) { | |
dd2e794a | 1135 | struct pretty_print_context ctx = {0}; |
a5481a6c | 1136 | ctx.date_mode.type = DATE_NORMAL; |
146ea068 | 1137 | strbuf_release(&buf); |
bab82164 ÆAB |
1138 | repo_format_commit_message(the_repository, commit, |
1139 | "%aN <%aE>", &buf, &ctx); | |
2108fe4a | 1140 | release_revisions(&revs); |
146ea068 JH |
1141 | return strbuf_detach(&buf, NULL); |
1142 | } | |
1044b1f6 | 1143 | die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name); |
146ea068 JH |
1144 | } |
1145 | ||
eec0f7f2 JM |
1146 | static void handle_ignored_arg(struct wt_status *s) |
1147 | { | |
1148 | if (!ignored_arg) | |
1149 | ; /* default already initialized */ | |
1150 | else if (!strcmp(ignored_arg, "traditional")) | |
1151 | s->show_ignored_mode = SHOW_TRADITIONAL_IGNORED; | |
1152 | else if (!strcmp(ignored_arg, "no")) | |
1153 | s->show_ignored_mode = SHOW_NO_IGNORED; | |
1154 | else if (!strcmp(ignored_arg, "matching")) | |
1155 | s->show_ignored_mode = SHOW_MATCHING_IGNORED; | |
1156 | else | |
1157 | die(_("Invalid ignored mode '%s'"), ignored_arg); | |
1158 | } | |
76e2f7ce | 1159 | |
63acdc48 | 1160 | static enum untracked_status_type parse_untracked_setting_name(const char *u) |
76e2f7ce | 1161 | { |
5a59a230 NTND |
1162 | /* |
1163 | * Please update $__git_untracked_file_modes in | |
1164 | * git-completion.bash when you add new options | |
1165 | */ | |
f66e1a07 JH |
1166 | switch (git_parse_maybe_bool(u)) { |
1167 | case 0: | |
1168 | u = "no"; | |
1169 | break; | |
1170 | case 1: | |
1171 | u = "normal"; | |
1172 | break; | |
1173 | default: | |
1174 | break; | |
1175 | } | |
1176 | ||
63acdc48 JH |
1177 | if (!strcmp(u, "no")) |
1178 | return SHOW_NO_UNTRACKED_FILES; | |
1179 | else if (!strcmp(u, "normal")) | |
1180 | return SHOW_NORMAL_UNTRACKED_FILES; | |
1181 | else if (!strcmp(u, "all")) | |
1182 | return SHOW_ALL_UNTRACKED_FILES; | |
76e2f7ce | 1183 | else |
63acdc48 JH |
1184 | return SHOW_UNTRACKED_FILES_ERROR; |
1185 | } | |
1186 | ||
1187 | static void handle_untracked_files_arg(struct wt_status *s) | |
1188 | { | |
1189 | enum untracked_status_type u; | |
1190 | ||
1191 | if (!untracked_files_arg) | |
1192 | return; /* default already initialized */ | |
1193 | ||
1194 | u = parse_untracked_setting_name(untracked_files_arg); | |
1195 | if (u == SHOW_UNTRACKED_FILES_ERROR) | |
1196 | die(_("Invalid untracked files mode '%s'"), | |
1197 | untracked_files_arg); | |
1198 | s->show_untracked_files = u; | |
76e2f7ce JH |
1199 | } |
1200 | ||
37f7a857 JS |
1201 | static const char *read_commit_message(const char *name) |
1202 | { | |
dd0d388c | 1203 | const char *out_enc; |
37f7a857 JS |
1204 | struct commit *commit; |
1205 | ||
1206 | commit = lookup_commit_reference_by_name(name); | |
1207 | if (!commit) | |
e4cf0134 | 1208 | die(_("could not lookup commit '%s'"), name); |
37f7a857 | 1209 | out_enc = get_commit_output_encoding(); |
ecb5091f | 1210 | return repo_logmsg_reencode(the_repository, commit, NULL, out_enc); |
37f7a857 JS |
1211 | } |
1212 | ||
84b4202d JH |
1213 | /* |
1214 | * Enumerate what needs to be propagated when --porcelain | |
1215 | * is not in effect here. | |
1216 | */ | |
1217 | static struct status_deferred_config { | |
be7e795e | 1218 | enum wt_status_format status_format; |
84b4202d | 1219 | int show_branch; |
06b324c1 | 1220 | enum ahead_behind_flags ahead_behind; |
84b4202d JH |
1221 | } status_deferred_config = { |
1222 | STATUS_FORMAT_UNSPECIFIED, | |
06b324c1 JH |
1223 | -1, /* unspecified */ |
1224 | AHEAD_BEHIND_UNSPECIFIED, | |
84b4202d JH |
1225 | }; |
1226 | ||
1227 | static void finalize_deferred_config(struct wt_status *s) | |
1228 | { | |
1229 | int use_deferred_config = (status_format != STATUS_FORMAT_PORCELAIN && | |
1ecdecce | 1230 | status_format != STATUS_FORMAT_PORCELAIN_V2 && |
84b4202d JH |
1231 | !s->null_termination); |
1232 | ||
1233 | if (s->null_termination) { | |
1234 | if (status_format == STATUS_FORMAT_NONE || | |
1235 | status_format == STATUS_FORMAT_UNSPECIFIED) | |
1236 | status_format = STATUS_FORMAT_PORCELAIN; | |
1237 | else if (status_format == STATUS_FORMAT_LONG) | |
12909b6b | 1238 | die(_("options '%s' and '%s' cannot be used together"), "--long", "-z"); |
84b4202d JH |
1239 | } |
1240 | ||
1241 | if (use_deferred_config && status_format == STATUS_FORMAT_UNSPECIFIED) | |
1242 | status_format = status_deferred_config.status_format; | |
1243 | if (status_format == STATUS_FORMAT_UNSPECIFIED) | |
1244 | status_format = STATUS_FORMAT_NONE; | |
1245 | ||
1246 | if (use_deferred_config && s->show_branch < 0) | |
1247 | s->show_branch = status_deferred_config.show_branch; | |
1248 | if (s->show_branch < 0) | |
1249 | s->show_branch = 0; | |
fd9b544a | 1250 | |
06b324c1 JH |
1251 | /* |
1252 | * If the user did not give a "--[no]-ahead-behind" command | |
fb4db1a2 JH |
1253 | * line argument *AND* we will print in a human-readable format |
1254 | * (short, long etc.) then we inherit from the status.aheadbehind | |
1255 | * config setting. In all other cases (and porcelain V[12] formats | |
1256 | * in particular), we inherit _FULL for backwards compatibility. | |
06b324c1 | 1257 | */ |
fb4db1a2 JH |
1258 | if (use_deferred_config && |
1259 | s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED) | |
06b324c1 JH |
1260 | s->ahead_behind_flags = status_deferred_config.ahead_behind; |
1261 | ||
fd9b544a JH |
1262 | if (s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED) |
1263 | s->ahead_behind_flags = AHEAD_BEHIND_FULL; | |
84b4202d JH |
1264 | } |
1265 | ||
3270ae82 CM |
1266 | static void check_fixup_reword_options(int argc, const char *argv[]) { |
1267 | if (whence != FROM_COMMIT) { | |
1268 | if (whence == FROM_MERGE) | |
1269 | die(_("You are in the middle of a merge -- cannot reword.")); | |
1270 | else if (is_from_cherry_pick(whence)) | |
1271 | die(_("You are in the middle of a cherry-pick -- cannot reword.")); | |
1272 | } | |
1273 | if (argc) | |
246cac85 | 1274 | die(_("reword option of '%s' and path '%s' cannot be used together"), "--fixup", *argv); |
3270ae82 | 1275 | if (patch_interactive || interactive || all || also || only) |
246cac85 JNA |
1276 | die(_("reword option of '%s' and '%s' cannot be used together"), |
1277 | "--fixup", "--patch/--interactive/--all/--include/--only"); | |
3270ae82 CM |
1278 | } |
1279 | ||
2f02b25f | 1280 | static int parse_and_validate_options(int argc, const char *argv[], |
036dbbfb | 1281 | const struct option *options, |
dbd0f5c7 | 1282 | const char * const usage[], |
d249b098 | 1283 | const char *prefix, |
06bb643b | 1284 | struct commit *current_head, |
d249b098 | 1285 | struct wt_status *s) |
f5bbc322 | 1286 | { |
036dbbfb | 1287 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
84b4202d | 1288 | finalize_deferred_config(s); |
f5bbc322 | 1289 | |
146ea068 JH |
1290 | if (force_author && !strchr(force_author, '>')) |
1291 | force_author = find_author_by_nickname(force_author); | |
1292 | ||
c51f6cee | 1293 | if (force_author && renew_authorship) |
a699367b | 1294 | die(_("options '%s' and '%s' cannot be used together"), "--reset-author", "--author"); |
c51f6cee | 1295 | |
494d314a | 1296 | if (logfile || have_option_m || use_message) |
4803466f | 1297 | use_editor = 0; |
f5bbc322 | 1298 | |
f5bbc322 | 1299 | /* Sanity check options */ |
06bb643b | 1300 | if (amend && !current_head) |
8a6179bc | 1301 | die(_("You have nothing to amend.")); |
b0cea47e ÆAB |
1302 | if (amend && whence != FROM_COMMIT) { |
1303 | if (whence == FROM_MERGE) | |
1304 | die(_("You are in the middle of a merge -- cannot amend.")); | |
8d57f757 | 1305 | else if (is_from_cherry_pick(whence)) |
b0cea47e | 1306 | die(_("You are in the middle of a cherry-pick -- cannot amend.")); |
430b75f7 PW |
1307 | else if (whence == FROM_REBASE_PICK) |
1308 | die(_("You are in the middle of a rebase -- cannot amend.")); | |
b0cea47e | 1309 | } |
89ac1223 | 1310 | if (fixup_message && squash_message) |
a699367b JNA |
1311 | die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup"); |
1312 | die_for_incompatible_opt4(!!use_message, "-C", | |
1313 | !!edit_message, "-c", | |
1314 | !!logfile, "-F", | |
1315 | !!fixup_message, "--fixup"); | |
1316 | die_for_incompatible_opt4(have_option_m, "-m", | |
1317 | !!edit_message, "-c", | |
1318 | !!use_message, "-C", | |
1319 | !!logfile, "-F"); | |
1320 | if (use_message || edit_message || logfile ||fixup_message || have_option_m) | |
010c7dbc | 1321 | template_file = NULL; |
f5bbc322 KH |
1322 | if (edit_message) |
1323 | use_message = edit_message; | |
d71b8ba7 | 1324 | if (amend && !use_message && !fixup_message) |
f5bbc322 | 1325 | use_message = "HEAD"; |
430b75f7 PW |
1326 | if (!use_message && !is_from_cherry_pick(whence) && |
1327 | !is_from_rebase(whence) && renew_authorship) | |
8a6179bc | 1328 | die(_("--reset-author can be used only with -C, -c or --amend.")); |
f5bbc322 | 1329 | if (use_message) { |
37f7a857 JS |
1330 | use_message_buffer = read_commit_message(use_message); |
1331 | if (!renew_authorship) { | |
1332 | author_message = use_message; | |
1333 | author_message_buffer = use_message_buffer; | |
1334 | } | |
1335 | } | |
430b75f7 PW |
1336 | if ((is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) && |
1337 | !renew_authorship) { | |
37f7a857 JS |
1338 | author_message = "CHERRY_PICK_HEAD"; |
1339 | author_message_buffer = read_commit_message(author_message); | |
f5bbc322 KH |
1340 | } |
1341 | ||
b4bd4668 CI |
1342 | if (patch_interactive) |
1343 | interactive = 1; | |
1344 | ||
a699367b JNA |
1345 | die_for_incompatible_opt4(also, "-i/--include", |
1346 | only, "-o/--only", | |
1347 | all, "-a/--all", | |
1348 | interactive, "--interactive/-p/--patch"); | |
494d314a CM |
1349 | if (fixup_message) { |
1350 | /* | |
1351 | * We limit --fixup's suboptions to only alpha characters. | |
1352 | * If the first character after a run of alpha is colon, | |
1353 | * then the part before the colon may be a known suboption | |
3270ae82 CM |
1354 | * name like `amend` or `reword`, or a misspelt suboption |
1355 | * name. In either case, we treat it as | |
1356 | * --fixup=<suboption>:<arg>. | |
494d314a CM |
1357 | * |
1358 | * Otherwise, we are dealing with --fixup=<commit>. | |
1359 | */ | |
1360 | char *p = fixup_message; | |
1361 | while (isalpha(*p)) | |
1362 | p++; | |
1363 | if (p > fixup_message && *p == ':') { | |
1364 | *p = '\0'; | |
1365 | fixup_commit = p + 1; | |
3270ae82 CM |
1366 | if (!strcmp("amend", fixup_message) || |
1367 | !strcmp("reword", fixup_message)) { | |
494d314a CM |
1368 | fixup_prefix = "amend"; |
1369 | allow_empty = 1; | |
3270ae82 CM |
1370 | if (*fixup_message == 'r') { |
1371 | check_fixup_reword_options(argc, argv); | |
1372 | only = 1; | |
1373 | } | |
494d314a CM |
1374 | } else { |
1375 | die(_("unknown option: --fixup=%s:%s"), fixup_message, fixup_commit); | |
1376 | } | |
1377 | } else { | |
1378 | fixup_commit = fixup_message; | |
1379 | fixup_prefix = "fixup"; | |
1380 | use_editor = 0; | |
1381 | } | |
1382 | } | |
1383 | ||
8ef6aad6 JK |
1384 | if (0 <= edit_flag) |
1385 | use_editor = edit_flag; | |
1386 | ||
f29cd862 | 1387 | cleanup_mode = get_cleanup_mode(cleanup_arg, use_editor); |
f5bbc322 | 1388 | |
76e2f7ce | 1389 | handle_untracked_files_arg(s); |
4bfee30a | 1390 | |
f5bbc322 | 1391 | if (all && argc > 0) |
5a1dbd48 NTND |
1392 | die(_("paths '%s ...' with -a does not make sense"), |
1393 | argv[0]); | |
f5bbc322 | 1394 | |
f3f47a1e | 1395 | if (status_format != STATUS_FORMAT_NONE) |
7c9f7038 JK |
1396 | dry_run = 1; |
1397 | ||
f5bbc322 KH |
1398 | return argc; |
1399 | } | |
1400 | ||
e885a84f | 1401 | static int dry_run_commit(const char **argv, const char *prefix, |
06bb643b | 1402 | const struct commit *current_head, struct wt_status *s) |
f5bbc322 | 1403 | { |
6fa90194 | 1404 | int committable; |
3a5d13a3 | 1405 | const char *index_file; |
f5bbc322 | 1406 | |
e885a84f | 1407 | index_file = prepare_index(argv, prefix, current_head, 1); |
6fa90194 | 1408 | committable = run_status(stdout, index_file, prefix, 0, s); |
3a5d13a3 | 1409 | rollback_index_files(); |
f5bbc322 | 1410 | |
6fa90194 | 1411 | return committable ? 0 : 1; |
3a5d13a3 JH |
1412 | } |
1413 | ||
3ac68a93 NTND |
1414 | define_list_config_array_extra(color_status_slots, {"added"}); |
1415 | ||
8852117a | 1416 | static int parse_status_slot(const char *slot) |
f766b367 | 1417 | { |
a73b3680 | 1418 | if (!strcasecmp(slot, "added")) |
f766b367 | 1419 | return WT_STATUS_UPDATED; |
a73b3680 NTND |
1420 | |
1421 | return LOOKUP_CONFIG(color_status_slots, slot); | |
f766b367 JH |
1422 | } |
1423 | ||
a4e7e317 GC |
1424 | static int git_status_config(const char *k, const char *v, |
1425 | const struct config_context *ctx, void *cb) | |
f766b367 JH |
1426 | { |
1427 | struct wt_status *s = cb; | |
e3f1da98 | 1428 | const char *slot_name; |
f766b367 | 1429 | |
59556548 | 1430 | if (starts_with(k, "column.")) |
4d2292e9 | 1431 | return git_column_config(k, v, "status", &s->colopts); |
f766b367 JH |
1432 | if (!strcmp(k, "status.submodulesummary")) { |
1433 | int is_bool; | |
8868b1eb GC |
1434 | s->submodule_summary = git_config_bool_or_int(k, v, ctx->kvi, |
1435 | &is_bool); | |
f766b367 JH |
1436 | if (is_bool && s->submodule_summary) |
1437 | s->submodule_summary = -1; | |
1438 | return 0; | |
1439 | } | |
4fb5166a JJGG |
1440 | if (!strcmp(k, "status.short")) { |
1441 | if (git_config_bool(k, v)) | |
84b4202d | 1442 | status_deferred_config.status_format = STATUS_FORMAT_SHORT; |
4fb5166a | 1443 | else |
84b4202d | 1444 | status_deferred_config.status_format = STATUS_FORMAT_NONE; |
4fb5166a JJGG |
1445 | return 0; |
1446 | } | |
ec85d070 | 1447 | if (!strcmp(k, "status.branch")) { |
84b4202d | 1448 | status_deferred_config.show_branch = git_config_bool(k, v); |
ec85d070 JJGG |
1449 | return 0; |
1450 | } | |
06b324c1 JH |
1451 | if (!strcmp(k, "status.aheadbehind")) { |
1452 | status_deferred_config.ahead_behind = git_config_bool(k, v); | |
1453 | return 0; | |
1454 | } | |
c1b5d019 LB |
1455 | if (!strcmp(k, "status.showstash")) { |
1456 | s->show_stash = git_config_bool(k, v); | |
1457 | return 0; | |
1458 | } | |
f766b367 | 1459 | if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) { |
e269eb79 | 1460 | s->use_color = git_config_colorbool(k, v); |
f766b367 JH |
1461 | return 0; |
1462 | } | |
2556b996 MM |
1463 | if (!strcmp(k, "status.displaycommentprefix")) { |
1464 | s->display_comment_prefix = git_config_bool(k, v); | |
1465 | return 0; | |
1466 | } | |
e3f1da98 RS |
1467 | if (skip_prefix(k, "status.color.", &slot_name) || |
1468 | skip_prefix(k, "color.status.", &slot_name)) { | |
b9465768 | 1469 | int slot = parse_status_slot(slot_name); |
8b8e8624 JK |
1470 | if (slot < 0) |
1471 | return 0; | |
f766b367 JH |
1472 | if (!v) |
1473 | return config_error_nonbool(k); | |
f6c5a296 | 1474 | return color_parse(v, s->color_palette[slot]); |
f766b367 JH |
1475 | } |
1476 | if (!strcmp(k, "status.relativepaths")) { | |
1477 | s->relative_paths = git_config_bool(k, v); | |
1478 | return 0; | |
1479 | } | |
1480 | if (!strcmp(k, "status.showuntrackedfiles")) { | |
63acdc48 JH |
1481 | enum untracked_status_type u; |
1482 | ||
63acdc48 JH |
1483 | u = parse_untracked_setting_name(v); |
1484 | if (u == SHOW_UNTRACKED_FILES_ERROR) | |
8a6179bc | 1485 | return error(_("Invalid untracked files mode '%s'"), v); |
63acdc48 | 1486 | s->show_untracked_files = u; |
f766b367 JH |
1487 | return 0; |
1488 | } | |
e8b2dc2c BP |
1489 | if (!strcmp(k, "diff.renamelimit")) { |
1490 | if (s->rename_limit == -1) | |
8868b1eb | 1491 | s->rename_limit = git_config_int(k, v, ctx->kvi); |
e8b2dc2c BP |
1492 | return 0; |
1493 | } | |
1494 | if (!strcmp(k, "status.renamelimit")) { | |
8868b1eb | 1495 | s->rename_limit = git_config_int(k, v, ctx->kvi); |
e8b2dc2c BP |
1496 | return 0; |
1497 | } | |
1498 | if (!strcmp(k, "diff.renames")) { | |
1499 | if (s->detect_rename == -1) | |
1500 | s->detect_rename = git_config_rename(k, v); | |
1501 | return 0; | |
1502 | } | |
1503 | if (!strcmp(k, "status.renames")) { | |
1504 | s->detect_rename = git_config_rename(k, v); | |
1505 | return 0; | |
1506 | } | |
a4e7e317 | 1507 | return git_diff_ui_config(k, v, ctx, NULL); |
f766b367 JH |
1508 | } |
1509 | ||
3a5d13a3 JH |
1510 | int cmd_status(int argc, const char **argv, const char *prefix) |
1511 | { | |
e8b2dc2c BP |
1512 | static int no_renames = -1; |
1513 | static const char *rename_score_arg = (const char *)-1; | |
036dbbfb | 1514 | static struct wt_status s; |
ae9af122 | 1515 | unsigned int progress_flag = 0; |
4bb6644d | 1516 | int fd; |
8066df41 | 1517 | struct object_id oid; |
9e4b7ab6 | 1518 | static struct option builtin_status_options[] = { |
f2276316 | 1519 | OPT__VERBOSE(&verbose, N_("be verbose")), |
dd2be243 | 1520 | OPT_SET_INT('s', "short", &status_format, |
f2276316 | 1521 | N_("show status concisely"), STATUS_FORMAT_SHORT), |
84b4202d JH |
1522 | OPT_BOOL('b', "branch", &s.show_branch, |
1523 | N_("show branch information")), | |
c1b5d019 LB |
1524 | OPT_BOOL(0, "show-stash", &s.show_stash, |
1525 | N_("show stash information")), | |
fd9b544a JH |
1526 | OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags, |
1527 | N_("compute full ahead/behind values")), | |
203c8533 | 1528 | OPT_CALLBACK_F(0, "porcelain", &status_format, |
c4f596b9 | 1529 | N_("version"), N_("machine-readable output"), |
203c8533 | 1530 | PARSE_OPT_OPTARG, opt_parse_porcelain), |
f3f47a1e JK |
1531 | OPT_SET_INT(0, "long", &status_format, |
1532 | N_("show status in long format (default)"), | |
1533 | STATUS_FORMAT_LONG), | |
d5d09d47 SB |
1534 | OPT_BOOL('z', "null", &s.null_termination, |
1535 | N_("terminate entries with NUL")), | |
76e2f7ce | 1536 | { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, |
f2276316 NTND |
1537 | N_("mode"), |
1538 | N_("show untracked files, optional modes: all, normal, no. (Default: all)"), | |
76e2f7ce | 1539 | PARSE_OPT_OPTARG, NULL, (intptr_t)"all" }, |
eec0f7f2 JM |
1540 | { OPTION_STRING, 0, "ignored", &ignored_arg, |
1541 | N_("mode"), | |
1542 | N_("show ignored files, optional modes: traditional, matching, no. (Default: traditional)"), | |
1543 | PARSE_OPT_OPTARG, NULL, (intptr_t)"traditional" }, | |
f2276316 NTND |
1544 | { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, N_("when"), |
1545 | N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"), | |
46a958b3 | 1546 | PARSE_OPT_OPTARG, NULL, (intptr_t)"all" }, |
9c23f4c5 | 1547 | OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")), |
e8b2dc2c | 1548 | OPT_BOOL(0, "no-renames", &no_renames, N_("do not detect renames")), |
203c8533 | 1549 | OPT_CALLBACK_F('M', "find-renames", &rename_score_arg, |
e8b2dc2c | 1550 | N_("n"), N_("detect renames, optionally set similarity index"), |
203c8533 | 1551 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, opt_parse_rename_score), |
76e2f7ce JH |
1552 | OPT_END(), |
1553 | }; | |
1554 | ||
5d3dd915 NTND |
1555 | if (argc == 2 && !strcmp(argv[1], "-h")) |
1556 | usage_with_options(builtin_status_usage, builtin_status_options); | |
1557 | ||
d76723ee DS |
1558 | prepare_repo_settings(the_repository); |
1559 | the_repository->settings.command_requires_full_index = 0; | |
1560 | ||
5c25dfaa | 1561 | status_init_config(&s, git_status_config); |
76e2f7ce | 1562 | argc = parse_options(argc, argv, prefix, |
9e4b7ab6 JH |
1563 | builtin_status_options, |
1564 | builtin_status_usage, 0); | |
4d2292e9 | 1565 | finalize_colopts(&s.colopts, -1); |
84b4202d | 1566 | finalize_deferred_config(&s); |
000f97bd | 1567 | |
76e2f7ce | 1568 | handle_untracked_files_arg(&s); |
eec0f7f2 JM |
1569 | handle_ignored_arg(&s); |
1570 | ||
1571 | if (s.show_ignored_mode == SHOW_MATCHING_IGNORED && | |
1572 | s.show_untracked_files == SHOW_NO_UNTRACKED_FILES) | |
1573 | die(_("Unsupported combination of ignored and untracked-files arguments")); | |
1574 | ||
15b55ae0 NTND |
1575 | parse_pathspec(&s.pathspec, 0, |
1576 | PATHSPEC_PREFER_FULL, | |
1577 | prefix, argv); | |
76e2f7ce | 1578 | |
ae9af122 NTND |
1579 | if (status_format != STATUS_FORMAT_PORCELAIN && |
1580 | status_format != STATUS_FORMAT_PORCELAIN_V2) | |
1581 | progress_flag = REFRESH_PROGRESS; | |
e1ff0a32 | 1582 | repo_read_index(the_repository); |
ae9af122 NTND |
1583 | refresh_index(&the_index, |
1584 | REFRESH_QUIET|REFRESH_UNMERGED|progress_flag, | |
1585 | &s.pathspec, NULL, NULL); | |
4bb6644d | 1586 | |
27344d6a | 1587 | if (use_optional_locks()) |
07047d68 | 1588 | fd = repo_hold_locked_index(the_repository, &index_lock, 0); |
27344d6a JK |
1589 | else |
1590 | fd = -1; | |
4bb6644d | 1591 | |
d850b7a5 | 1592 | s.is_initial = repo_get_oid(the_repository, s.reference, &oid) ? 1 : 0; |
d9fc746c | 1593 | if (!s.is_initial) |
e0cb7cdb | 1594 | oidcpy(&s.oid_commit, &oid); |
d9fc746c | 1595 | |
46a958b3 | 1596 | s.ignore_submodule_arg = ignore_submodule_arg; |
be7e795e JH |
1597 | s.status_format = status_format; |
1598 | s.verbose = verbose; | |
e8b2dc2c BP |
1599 | if (no_renames != -1) |
1600 | s.detect_rename = !no_renames; | |
1601 | if ((intptr_t)rename_score_arg != -1) { | |
1602 | if (s.detect_rename < DIFF_DETECT_RENAME) | |
1603 | s.detect_rename = DIFF_DETECT_RENAME; | |
1604 | if (rename_score_arg) | |
1605 | s.rename_score = parse_rename_score(&rename_score_arg); | |
1606 | } | |
be7e795e | 1607 | |
76e2f7ce JH |
1608 | wt_status_collect(&s); |
1609 | ||
226c051a | 1610 | if (0 <= fd) |
1b0d968b | 1611 | repo_update_index_if_able(the_repository, &index_lock); |
226c051a | 1612 | |
8661768f JK |
1613 | if (s.relative_paths) |
1614 | s.prefix = prefix; | |
38920dd6 | 1615 | |
be7e795e | 1616 | wt_status_print(&s); |
73ba5d78 SS |
1617 | wt_status_collect_free_buffers(&s); |
1618 | ||
76e2f7ce | 1619 | return 0; |
f5bbc322 KH |
1620 | } |
1621 | ||
a4e7e317 GC |
1622 | static int git_commit_config(const char *k, const char *v, |
1623 | const struct config_context *ctx, void *cb) | |
f5bbc322 | 1624 | { |
d249b098 JH |
1625 | struct wt_status *s = cb; |
1626 | ||
984c6e7e | 1627 | if (!strcmp(k, "commit.template")) |
395de250 | 1628 | return git_config_pathname(&template_file, k, v); |
bed575e4 JHI |
1629 | if (!strcmp(k, "commit.status")) { |
1630 | include_status = git_config_bool(k, v); | |
1631 | return 0; | |
1632 | } | |
51fb3a3d RT |
1633 | if (!strcmp(k, "commit.cleanup")) |
1634 | return git_config_string(&cleanup_arg, k, v); | |
d95bfb12 NV |
1635 | if (!strcmp(k, "commit.gpgsign")) { |
1636 | sign_commit = git_config_bool(k, v) ? "" : NULL; | |
1637 | return 0; | |
1638 | } | |
aaab8420 PB |
1639 | if (!strcmp(k, "commit.verbose")) { |
1640 | int is_bool; | |
8868b1eb GC |
1641 | config_commit_verbose = git_config_bool_or_int(k, v, ctx->kvi, |
1642 | &is_bool); | |
aaab8420 PB |
1643 | return 0; |
1644 | } | |
f5bbc322 | 1645 | |
a4e7e317 | 1646 | return git_status_config(k, v, ctx, s); |
f5bbc322 KH |
1647 | } |
1648 | ||
f5bbc322 KH |
1649 | int cmd_commit(int argc, const char **argv, const char *prefix) |
1650 | { | |
036dbbfb JK |
1651 | static struct wt_status s; |
1652 | static struct option builtin_commit_options[] = { | |
9c23f4c5 NTND |
1653 | OPT__QUIET(&quiet, N_("suppress summary after successful commit")), |
1654 | OPT__VERBOSE(&verbose, N_("show diff in commit message template")), | |
1655 | ||
1656 | OPT_GROUP(N_("Commit message options")), | |
1657 | OPT_FILENAME('F', "file", &logfile, N_("read message from file")), | |
1658 | OPT_STRING(0, "author", &force_author, N_("author"), N_("override author for commit")), | |
1659 | OPT_STRING(0, "date", &force_date, N_("date"), N_("override date for commit")), | |
1660 | OPT_CALLBACK('m', "message", &message, N_("message"), N_("commit message"), opt_parse_m), | |
1661 | OPT_STRING('c', "reedit-message", &edit_message, N_("commit"), N_("reuse and edit message from specified commit")), | |
1662 | OPT_STRING('C', "reuse-message", &use_message, N_("commit"), N_("reuse message from specified commit")), | |
494d314a | 1663 | /* |
3270ae82 CM |
1664 | * TRANSLATORS: Leave "[(amend|reword):]" as-is, |
1665 | * and only translate <commit>. | |
494d314a | 1666 | */ |
3270ae82 | 1667 | OPT_STRING(0, "fixup", &fixup_message, N_("[(amend|reword):]commit"), N_("use autosquash formatted message to fixup or amend/reword specified commit")), |
9c23f4c5 | 1668 | OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")), |
d5d09d47 | 1669 | OPT_BOOL(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")), |
116761ba | 1670 | OPT_CALLBACK_F(0, "trailer", &trailer_args, N_("trailer"), N_("add custom trailer(s)"), PARSE_OPT_NONEG, opt_pass_trailer), |
3abd4a67 | 1671 | OPT_BOOL('s', "signoff", &signoff, N_("add a Signed-off-by trailer")), |
9c23f4c5 NTND |
1672 | OPT_FILENAME('t', "template", &template_file, N_("use specified template file")), |
1673 | OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")), | |
ca04dc96 | 1674 | OPT_CLEANUP(&cleanup_arg), |
d5d09d47 | 1675 | OPT_BOOL(0, "status", &include_status, N_("include status in commit message template")), |
e703d711 | 1676 | { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"), |
9c23f4c5 | 1677 | N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, |
036dbbfb JK |
1678 | /* end commit message options */ |
1679 | ||
9c23f4c5 | 1680 | OPT_GROUP(N_("Commit contents options")), |
d5d09d47 SB |
1681 | OPT_BOOL('a', "all", &all, N_("commit all changed files")), |
1682 | OPT_BOOL('i', "include", &also, N_("add specified files to index for commit")), | |
1683 | OPT_BOOL(0, "interactive", &interactive, N_("interactively add files")), | |
1684 | OPT_BOOL('p', "patch", &patch_interactive, N_("interactively add changes")), | |
1685 | OPT_BOOL('o', "only", &only, N_("commit only specified files")), | |
def480fe | 1686 | OPT_BOOL('n', "no-verify", &no_verify, N_("bypass pre-commit and commit-msg hooks")), |
d5d09d47 | 1687 | OPT_BOOL(0, "dry-run", &dry_run, N_("show what would be committed")), |
9c23f4c5 | 1688 | OPT_SET_INT(0, "short", &status_format, N_("show status concisely"), |
036dbbfb | 1689 | STATUS_FORMAT_SHORT), |
84b4202d | 1690 | OPT_BOOL(0, "branch", &s.show_branch, N_("show branch information")), |
fd9b544a JH |
1691 | OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags, |
1692 | N_("compute full ahead/behind values")), | |
036dbbfb | 1693 | OPT_SET_INT(0, "porcelain", &status_format, |
9c23f4c5 | 1694 | N_("machine-readable output"), STATUS_FORMAT_PORCELAIN), |
f3f47a1e JK |
1695 | OPT_SET_INT(0, "long", &status_format, |
1696 | N_("show status in long format (default)"), | |
1697 | STATUS_FORMAT_LONG), | |
d5d09d47 SB |
1698 | OPT_BOOL('z', "null", &s.null_termination, |
1699 | N_("terminate entries with NUL")), | |
1700 | OPT_BOOL(0, "amend", &amend, N_("amend previous commit")), | |
1701 | OPT_BOOL(0, "no-post-rewrite", &no_post_rewrite, N_("bypass post-rewrite hook")), | |
9c23f4c5 | 1702 | { 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" }, |
e440fc58 AM |
1703 | OPT_PATHSPEC_FROM_FILE(&pathspec_from_file), |
1704 | OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul), | |
036dbbfb JK |
1705 | /* end commit contents options */ |
1706 | ||
4741edd5 SB |
1707 | OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty, |
1708 | N_("ok to record an empty change")), | |
1709 | OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message, | |
1710 | N_("ok to record a change with an empty message")), | |
036dbbfb JK |
1711 | |
1712 | OPT_END() | |
1713 | }; | |
1714 | ||
f285a2d7 | 1715 | struct strbuf sb = STRBUF_INIT; |
4c28e4ad | 1716 | struct strbuf author_ident = STRBUF_INIT; |
f5bbc322 | 1717 | const char *index_file, *reflog_msg; |
8066df41 | 1718 | struct object_id oid; |
de9f7fa3 | 1719 | struct commit_list *parents = NULL; |
cf10f9fd | 1720 | struct stat statbuf; |
06bb643b | 1721 | struct commit *current_head = NULL; |
ed7a42a0 | 1722 | struct commit_extra_header *extra = NULL; |
c0fe1ed0 | 1723 | struct strbuf err = STRBUF_INIT; |
00d8c311 | 1724 | int ret = 0; |
f5bbc322 | 1725 | |
5d3dd915 NTND |
1726 | if (argc == 2 && !strcmp(argv[1], "-h")) |
1727 | usage_with_options(builtin_commit_usage, builtin_commit_options); | |
1728 | ||
daa1acef DS |
1729 | prepare_repo_settings(the_repository); |
1730 | the_repository->settings.command_requires_full_index = 0; | |
1731 | ||
5c25dfaa | 1732 | status_init_config(&s, git_commit_config); |
4ddb1354 | 1733 | s.commit_template = 1; |
f0915cba | 1734 | status_format = STATUS_FORMAT_NONE; /* Ignore status.short */ |
4d2292e9 | 1735 | s.colopts = 0; |
f5bbc322 | 1736 | |
d850b7a5 | 1737 | if (repo_get_oid(the_repository, "HEAD", &oid)) |
06bb643b JH |
1738 | current_head = NULL; |
1739 | else { | |
bc83266a | 1740 | current_head = lookup_commit_or_die(&oid, "HEAD"); |
ecb5091f | 1741 | if (repo_parse_commit(the_repository, current_head)) |
06bb643b JH |
1742 | die(_("could not parse HEAD commit")); |
1743 | } | |
aaab8420 | 1744 | verbose = -1; /* unspecified */ |
036dbbfb JK |
1745 | argc = parse_and_validate_options(argc, argv, builtin_commit_options, |
1746 | builtin_commit_usage, | |
06bb643b | 1747 | prefix, current_head, &s); |
aaab8420 PB |
1748 | if (verbose == -1) |
1749 | verbose = (config_commit_verbose < 0) ? 0 : config_commit_verbose; | |
1750 | ||
c9bfb953 | 1751 | if (dry_run) |
e885a84f JK |
1752 | return dry_run_commit(argv, prefix, current_head, &s); |
1753 | index_file = prepare_index(argv, prefix, current_head, 0); | |
f5bbc322 | 1754 | |
ec84bd00 PB |
1755 | /* Set up everything for writing the commit object. This includes |
1756 | running hooks, writing the trees, and interacting with the user. */ | |
06bb643b JH |
1757 | if (!prepare_to_commit(index_file, prefix, |
1758 | current_head, &s, &author_ident)) { | |
00d8c311 | 1759 | ret = 1; |
2888605c | 1760 | rollback_index_files(); |
00d8c311 | 1761 | goto cleanup; |
f5bbc322 KH |
1762 | } |
1763 | ||
f5bbc322 | 1764 | /* Determine parents */ |
643cb5f7 | 1765 | reflog_msg = getenv("GIT_REFLOG_ACTION"); |
06bb643b | 1766 | if (!current_head) { |
643cb5f7 CC |
1767 | if (!reflog_msg) |
1768 | reflog_msg = "commit (initial)"; | |
f5bbc322 | 1769 | } else if (amend) { |
643cb5f7 CC |
1770 | if (!reflog_msg) |
1771 | reflog_msg = "commit (amend)"; | |
de9f7fa3 | 1772 | parents = copy_commit_list(current_head->parents); |
37f7a857 | 1773 | } else if (whence == FROM_MERGE) { |
f285a2d7 | 1774 | struct strbuf m = STRBUF_INIT; |
f5bbc322 | 1775 | FILE *fp; |
e23fd15a | 1776 | int allow_fast_forward = 1; |
de9f7fa3 | 1777 | struct commit_list **pptr = &parents; |
f5bbc322 | 1778 | |
643cb5f7 CC |
1779 | if (!reflog_msg) |
1780 | reflog_msg = "commit (merge)"; | |
de9f7fa3 | 1781 | pptr = commit_list_append(current_head, pptr); |
102de880 | 1782 | fp = xfopen(git_path_merge_head(the_repository), "r"); |
8f309aeb | 1783 | while (strbuf_getline_lf(&m, fp) != EOF) { |
5231c633 JH |
1784 | struct commit *parent; |
1785 | ||
1786 | parent = get_merge_parent(m.buf); | |
1787 | if (!parent) | |
8a6179bc | 1788 | die(_("Corrupt MERGE_HEAD file (%s)"), m.buf); |
de9f7fa3 | 1789 | pptr = commit_list_append(parent, pptr); |
7c3fd25d | 1790 | } |
f5bbc322 KH |
1791 | fclose(fp); |
1792 | strbuf_release(&m); | |
102de880 SB |
1793 | if (!stat(git_path_merge_mode(the_repository), &statbuf)) { |
1794 | if (strbuf_read_file(&sb, git_path_merge_mode(the_repository), 0) < 0) | |
8a6179bc | 1795 | die_errno(_("could not read MERGE_MODE")); |
cf10f9fd MV |
1796 | if (!strcmp(sb.buf, "no-ff")) |
1797 | allow_fast_forward = 0; | |
1798 | } | |
1799 | if (allow_fast_forward) | |
4da72644 | 1800 | reduce_heads_replace(&parents); |
f5bbc322 | 1801 | } else { |
643cb5f7 | 1802 | if (!reflog_msg) |
8d57f757 | 1803 | reflog_msg = is_from_cherry_pick(whence) |
37f7a857 | 1804 | ? "commit (cherry-pick)" |
430b75f7 PW |
1805 | : is_from_rebase(whence) |
1806 | ? "commit (rebase)" | |
37f7a857 | 1807 | : "commit"; |
de9f7fa3 | 1808 | commit_list_insert(current_head, &parents); |
f5bbc322 | 1809 | } |
f5bbc322 | 1810 | |
ec84bd00 | 1811 | /* Finally, get the commit message */ |
cf10f9fd | 1812 | strbuf_reset(&sb); |
e51b0dfc | 1813 | if (strbuf_read_file(&sb, git_path_commit_editmsg(), 0) < 0) { |
0721c314 | 1814 | int saved_errno = errno; |
740001a5 | 1815 | rollback_index_files(); |
8a6179bc | 1816 | die(_("could not read commit message: %s"), strerror(saved_errno)); |
740001a5 | 1817 | } |
99a12694 | 1818 | |
f29cd862 | 1819 | cleanup_message(&sb, cleanup_mode, verbose); |
bc17f35f | 1820 | |
d0aaa46f | 1821 | if (message_is_empty(&sb, cleanup_mode) && !allow_empty_message) { |
b2eda9bd | 1822 | rollback_index_files(); |
bc17f35f | 1823 | fprintf(stderr, _("Aborting commit due to empty commit message.\n")); |
b2eda9bd JH |
1824 | exit(1); |
1825 | } | |
d0aaa46f | 1826 | if (template_untouched(&sb, template_file, cleanup_mode) && !allow_empty_message) { |
2888605c | 1827 | rollback_index_files(); |
bc17f35f | 1828 | fprintf(stderr, _("Aborting commit; you did not edit the message.\n")); |
fdc7c811 | 1829 | exit(1); |
2888605c | 1830 | } |
f5bbc322 | 1831 | |
494d314a CM |
1832 | if (fixup_message && starts_with(sb.buf, "amend! ") && |
1833 | !allow_empty_message) { | |
1834 | struct strbuf body = STRBUF_INIT; | |
1835 | size_t len = commit_subject_length(sb.buf); | |
1836 | strbuf_addstr(&body, sb.buf + len); | |
1837 | if (message_is_empty(&body, cleanup_mode)) { | |
1838 | rollback_index_files(); | |
1839 | fprintf(stderr, _("Aborting commit due to empty commit message body.\n")); | |
1840 | exit(1); | |
1841 | } | |
1842 | strbuf_release(&body); | |
1843 | } | |
1844 | ||
0074d18d | 1845 | if (amend) { |
42d4e1d1 | 1846 | const char *exclude_gpgsig[3] = { "gpgsig", "gpgsig-sha256", NULL }; |
c871a1d1 | 1847 | extra = read_commit_extra_headers(current_head, exclude_gpgsig); |
0074d18d JH |
1848 | } else { |
1849 | struct commit_extra_header **tail = &extra; | |
1850 | append_merge_tag_headers(parents, &tail); | |
1851 | } | |
ed7a42a0 | 1852 | |
dc594180 | 1853 | if (commit_tree_extended(sb.buf, sb.len, &the_index.cache_tree->oid, |
e8cbe211 PW |
1854 | parents, &oid, author_ident.buf, NULL, |
1855 | sign_commit, extra)) { | |
2888605c | 1856 | rollback_index_files(); |
8a6179bc | 1857 | die(_("failed to write commit object")); |
2888605c | 1858 | } |
ed7a42a0 | 1859 | free_commit_extra_headers(extra); |
f5bbc322 | 1860 | |
0505d604 PW |
1861 | if (update_head_with_reflog(current_head, &oid, reflog_msg, &sb, |
1862 | &err)) { | |
2888605c | 1863 | rollback_index_files(); |
c0fe1ed0 | 1864 | die("%s", err.buf); |
2888605c | 1865 | } |
f5bbc322 | 1866 | |
f496b064 | 1867 | sequencer_post_commit_cleanup(the_repository, 0); |
102de880 SB |
1868 | unlink(git_path_merge_head(the_repository)); |
1869 | unlink(git_path_merge_msg(the_repository)); | |
1870 | unlink(git_path_merge_mode(the_repository)); | |
1871 | unlink(git_path_squash_msg(the_repository)); | |
f5bbc322 | 1872 | |
5a9dd399 | 1873 | if (commit_index_files()) |
1a07e59c | 1874 | die(_("repository has been updated, but unable to write\n" |
a060705d | 1875 | "new index file. Check that disk is not full and quota is\n" |
80f537f7 | 1876 | "not exceeded, and then \"git restore --staged :/\" to recover.")); |
f5bbc322 | 1877 | |
b23ea979 | 1878 | git_test_write_commit_graph_or_die(); |
859fdc0c | 1879 | |
35843b11 | 1880 | repo_rerere(the_repository, 0); |
a95ce124 | 1881 | run_auto_maintenance(quiet); |
a8cc5943 ÆAB |
1882 | run_commit_hook(use_editor, get_index_file(), NULL, "post-commit", |
1883 | NULL); | |
6f6bee3b | 1884 | if (amend && !no_post_rewrite) { |
1d18d758 | 1885 | commit_post_rewrite(the_repository, current_head, &oid); |
6f6bee3b | 1886 | } |
e47c6caf PW |
1887 | if (!quiet) { |
1888 | unsigned int flags = 0; | |
1889 | ||
1890 | if (!current_head) | |
1891 | flags |= SUMMARY_INITIAL_COMMIT; | |
1892 | if (author_date_is_interesting()) | |
1893 | flags |= SUMMARY_SHOW_AUTHOR_DATE; | |
f11c9580 NTND |
1894 | print_commit_summary(the_repository, prefix, |
1895 | &oid, flags); | |
6f6bee3b | 1896 | } |
f5bbc322 | 1897 | |
3f921c75 | 1898 | apply_autostash_ref(the_repository, "MERGE_AUTOSTASH"); |
a03b5553 | 1899 | |
00d8c311 | 1900 | cleanup: |
ac95f5d3 ÆAB |
1901 | strbuf_release(&author_ident); |
1902 | strbuf_release(&err); | |
1903 | strbuf_release(&sb); | |
00d8c311 | 1904 | return ret; |
f5bbc322 | 1905 | } |