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