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