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