]>
Commit | Line | Data |
---|---|---|
f5bbc322 KH |
1 | /* |
2 | * Builtin "git commit" | |
3 | * | |
4 | * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com> | |
5 | * Based on git-commit.sh by Junio C Hamano and Linus Torvalds | |
6 | */ | |
7 | ||
8 | #include "cache.h" | |
9 | #include "cache-tree.h" | |
6b2f2d98 | 10 | #include "color.h" |
2888605c | 11 | #include "dir.h" |
f5bbc322 KH |
12 | #include "builtin.h" |
13 | #include "diff.h" | |
14 | #include "diffcore.h" | |
15 | #include "commit.h" | |
16 | #include "revision.h" | |
17 | #include "wt-status.h" | |
18 | #include "run-command.h" | |
19 | #include "refs.h" | |
20 | #include "log-tree.h" | |
21 | #include "strbuf.h" | |
22 | #include "utf8.h" | |
23 | #include "parse-options.h" | |
c455c87c | 24 | #include "string-list.h" |
5b2fd956 | 25 | #include "rerere.h" |
fa9dcf80 | 26 | #include "unpack-trees.h" |
76e2f7ce | 27 | #include "quote.h" |
302ad7a9 | 28 | #include "submodule.h" |
f5bbc322 KH |
29 | |
30 | static const char * const builtin_commit_usage[] = { | |
1b1dd23f | 31 | "git commit [options] [--] <filepattern>...", |
f5bbc322 KH |
32 | NULL |
33 | }; | |
34 | ||
2f02b25f | 35 | static const char * const builtin_status_usage[] = { |
1b1dd23f | 36 | "git status [options] [--] <filepattern>...", |
2f02b25f SB |
37 | NULL |
38 | }; | |
39 | ||
49ff9a7a JK |
40 | static const char implicit_ident_advice[] = |
41 | "Your name and email address were configured automatically based\n" | |
42 | "on your username and hostname. Please check that they are accurate.\n" | |
43 | "You can suppress this message by setting them explicitly:\n" | |
44 | "\n" | |
8bb45b25 | 45 | " git config --global user.name \"Your Name\"\n" |
49ff9a7a JK |
46 | " git config --global user.email you@example.com\n" |
47 | "\n" | |
48 | "If the identity used for this commit is wrong, you can fix it with:\n" | |
49 | "\n" | |
50 | " git commit --amend --author='Your Name <you@example.com>'\n"; | |
51 | ||
f197ed2f JK |
52 | static const char empty_amend_advice[] = |
53 | "You asked to amend the most recent commit, but doing so would make\n" | |
54 | "it empty. You can repeat your command with --allow-empty, or you can\n" | |
55 | "remove the commit entirely with \"git reset HEAD^\".\n"; | |
56 | ||
30988301 | 57 | static unsigned char head_sha1[20]; |
49ff9a7a | 58 | |
f5bbc322 KH |
59 | static char *use_message_buffer; |
60 | static const char commit_editmsg[] = "COMMIT_EDITMSG"; | |
2888605c JH |
61 | static struct lock_file index_lock; /* real index */ |
62 | static struct lock_file false_lock; /* used only for partial commits */ | |
63 | static enum { | |
64 | COMMIT_AS_IS = 1, | |
65 | COMMIT_NORMAL, | |
4b05548f | 66 | COMMIT_PARTIAL |
2888605c | 67 | } commit_style; |
f5bbc322 | 68 | |
dbd0f5c7 | 69 | static const char *logfile, *force_author; |
984c6e7e | 70 | static const char *template_file; |
f5bbc322 | 71 | static char *edit_message, *use_message; |
e83dbe80 | 72 | static char *author_name, *author_email, *author_date; |
f5bbc322 | 73 | static int all, edit_flag, also, interactive, only, amend, signoff; |
c51f6cee | 74 | static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship; |
c9b5fde7 | 75 | static int no_post_rewrite, allow_empty_message; |
46a958b3 | 76 | static char *untracked_files_arg, *force_date, *ignore_submodule_arg; |
5f065737 AR |
77 | /* |
78 | * The default commit message cleanup mode will remove the lines | |
79 | * beginning with # (shell comments) and leading and trailing | |
80 | * whitespaces (empty lines or containing only whitespaces) | |
81 | * if editor is used, and only the whitespaces if the message | |
82 | * is specified explicitly. | |
83 | */ | |
84 | static enum { | |
85 | CLEANUP_SPACE, | |
86 | CLEANUP_NONE, | |
4b05548f | 87 | CLEANUP_ALL |
5f065737 AR |
88 | } cleanup_mode; |
89 | static char *cleanup_arg; | |
f5bbc322 | 90 | |
bed575e4 | 91 | static int use_editor = 1, initial_commit, in_merge, include_status = 1; |
2381e39e | 92 | static int show_ignored_in_status; |
3b6aeb3c JS |
93 | static const char *only_include_assumed; |
94 | static struct strbuf message; | |
f9568530 | 95 | |
7c9f7038 JK |
96 | static int null_termination; |
97 | static enum { | |
98 | STATUS_FORMAT_LONG, | |
99 | STATUS_FORMAT_SHORT, | |
4b05548f | 100 | STATUS_FORMAT_PORCELAIN |
7c9f7038 | 101 | } status_format = STATUS_FORMAT_LONG; |
05a59a08 | 102 | static int status_show_branch; |
7c9f7038 | 103 | |
f9568530 JS |
104 | static int opt_parse_m(const struct option *opt, const char *arg, int unset) |
105 | { | |
106 | struct strbuf *buf = opt->value; | |
107 | if (unset) | |
108 | strbuf_setlen(buf, 0); | |
109 | else { | |
110 | strbuf_addstr(buf, arg); | |
3b6aeb3c | 111 | strbuf_addstr(buf, "\n\n"); |
f9568530 JS |
112 | } |
113 | return 0; | |
114 | } | |
f5bbc322 KH |
115 | |
116 | static struct option builtin_commit_options[] = { | |
117 | OPT__QUIET(&quiet), | |
118 | OPT__VERBOSE(&verbose), | |
f5bbc322 | 119 | |
e97ca7f4 | 120 | OPT_GROUP("Commit message options"), |
df217ed6 | 121 | OPT_FILENAME('F', "file", &logfile, "read log from file"), |
f5bbc322 | 122 | OPT_STRING(0, "author", &force_author, "AUTHOR", "override author for commit"), |
02b47cd7 | 123 | OPT_STRING(0, "date", &force_date, "DATE", "override date for commit"), |
f9568530 | 124 | OPT_CALLBACK('m', "message", &message, "MESSAGE", "specify commit message", opt_parse_m), |
c51f6cee | 125 | OPT_STRING('c', "reedit-message", &edit_message, "COMMIT", "reuse and edit message from specified commit"), |
f5bbc322 | 126 | OPT_STRING('C', "reuse-message", &use_message, "COMMIT", "reuse message from specified commit"), |
c51f6cee | 127 | OPT_BOOLEAN(0, "reset-author", &renew_authorship, "the commit is authored by me now (used with -C-c/--amend)"), |
362b0dd5 | 128 | OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"), |
df217ed6 | 129 | OPT_FILENAME('t', "template", &template_file, "use specified template file"), |
f5bbc322 | 130 | OPT_BOOLEAN('e', "edit", &edit_flag, "force edit of commit"), |
e97ca7f4 | 131 | OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"), |
bed575e4 | 132 | OPT_BOOLEAN(0, "status", &include_status, "include status in commit message template"), |
e97ca7f4 | 133 | /* end commit message options */ |
f5bbc322 KH |
134 | |
135 | OPT_GROUP("Commit contents options"), | |
136 | OPT_BOOLEAN('a', "all", &all, "commit all changed files"), | |
137 | OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"), | |
138 | OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"), | |
d4ba07ca | 139 | OPT_BOOLEAN('o', "only", &only, "commit only specified files"), |
f5bbc322 | 140 | OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"), |
3a5d13a3 | 141 | OPT_BOOLEAN(0, "dry-run", &dry_run, "show what would be committed"), |
7c9f7038 JK |
142 | OPT_SET_INT(0, "short", &status_format, "show status concisely", |
143 | STATUS_FORMAT_SHORT), | |
05a59a08 | 144 | OPT_BOOLEAN(0, "branch", &status_show_branch, "show branch information"), |
7c9f7038 JK |
145 | OPT_SET_INT(0, "porcelain", &status_format, |
146 | "show porcelain output format", STATUS_FORMAT_PORCELAIN), | |
147 | OPT_BOOLEAN('z', "null", &null_termination, | |
148 | "terminate entries with NUL"), | |
f5bbc322 | 149 | OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"), |
6f6bee3b | 150 | OPT_BOOLEAN(0, "no-post-rewrite", &no_post_rewrite, "bypass post-rewrite hook"), |
49bb1f2a | 151 | { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, "mode", "show untracked files, optional modes: all, normal, no (Default: all)", PARSE_OPT_OPTARG, NULL, (intptr_t)"all" }, |
e97ca7f4 | 152 | /* end commit contents options */ |
f5bbc322 | 153 | |
c9b5fde7 ÆAB |
154 | { OPTION_BOOLEAN, 0, "allow-empty", &allow_empty, NULL, |
155 | "ok to record an empty change", | |
156 | PARSE_OPT_NOARG | PARSE_OPT_HIDDEN }, | |
157 | { OPTION_BOOLEAN, 0, "allow-empty-message", &allow_empty_message, NULL, | |
158 | "ok to record a change with an empty message", | |
159 | PARSE_OPT_NOARG | PARSE_OPT_HIDDEN }, | |
160 | ||
f5bbc322 KH |
161 | OPT_END() |
162 | }; | |
163 | ||
2888605c JH |
164 | static void rollback_index_files(void) |
165 | { | |
166 | switch (commit_style) { | |
167 | case COMMIT_AS_IS: | |
168 | break; /* nothing to do */ | |
169 | case COMMIT_NORMAL: | |
170 | rollback_lock_file(&index_lock); | |
171 | break; | |
172 | case COMMIT_PARTIAL: | |
173 | rollback_lock_file(&index_lock); | |
174 | rollback_lock_file(&false_lock); | |
175 | break; | |
176 | } | |
177 | } | |
178 | ||
5a9dd399 | 179 | static int commit_index_files(void) |
2888605c | 180 | { |
5a9dd399 BC |
181 | int err = 0; |
182 | ||
2888605c JH |
183 | switch (commit_style) { |
184 | case COMMIT_AS_IS: | |
185 | break; /* nothing to do */ | |
186 | case COMMIT_NORMAL: | |
5a9dd399 | 187 | err = commit_lock_file(&index_lock); |
2888605c JH |
188 | break; |
189 | case COMMIT_PARTIAL: | |
5a9dd399 | 190 | err = commit_lock_file(&index_lock); |
2888605c JH |
191 | rollback_lock_file(&false_lock); |
192 | break; | |
193 | } | |
5a9dd399 BC |
194 | |
195 | return err; | |
2888605c JH |
196 | } |
197 | ||
198 | /* | |
199 | * Take a union of paths in the index and the named tree (typically, "HEAD"), | |
200 | * and return the paths that match the given pattern in list. | |
201 | */ | |
c455c87c | 202 | static int list_paths(struct string_list *list, const char *with_tree, |
2888605c JH |
203 | const char *prefix, const char **pattern) |
204 | { | |
205 | int i; | |
206 | char *m; | |
207 | ||
208 | for (i = 0; pattern[i]; i++) | |
209 | ; | |
210 | m = xcalloc(1, i); | |
211 | ||
212 | if (with_tree) | |
213 | overlay_tree_on_cache(with_tree, prefix); | |
214 | ||
215 | for (i = 0; i < active_nr; i++) { | |
216 | struct cache_entry *ce = active_cache[i]; | |
7fce6e3c NTND |
217 | struct string_list_item *item; |
218 | ||
7a51ed66 | 219 | if (ce->ce_flags & CE_UPDATE) |
e87e22d0 | 220 | continue; |
0b50922a | 221 | if (!match_pathspec(pattern, ce->name, ce_namelen(ce), 0, m)) |
2888605c | 222 | continue; |
78a395d3 | 223 | item = string_list_insert(list, ce->name); |
7fce6e3c NTND |
224 | if (ce_skip_worktree(ce)) |
225 | item->util = item; /* better a valid pointer than a fake one */ | |
2888605c JH |
226 | } |
227 | ||
228 | return report_path_error(m, pattern, prefix ? strlen(prefix) : 0); | |
229 | } | |
230 | ||
c455c87c | 231 | static void add_remove_files(struct string_list *list) |
2888605c JH |
232 | { |
233 | int i; | |
234 | for (i = 0; i < list->nr; i++) { | |
d177cab0 | 235 | struct stat st; |
c455c87c | 236 | struct string_list_item *p = &(list->items[i]); |
d177cab0 | 237 | |
7fce6e3c NTND |
238 | /* p->util is skip-worktree */ |
239 | if (p->util) | |
b4d1690d | 240 | continue; |
d177cab0 | 241 | |
c455c87c JS |
242 | if (!lstat(p->string, &st)) { |
243 | if (add_to_cache(p->string, &st, 0)) | |
960b8ad1 AR |
244 | die("updating files failed"); |
245 | } else | |
c455c87c | 246 | remove_file_from_cache(p->string); |
2888605c JH |
247 | } |
248 | } | |
249 | ||
fa9dcf80 LT |
250 | static void create_base_index(void) |
251 | { | |
252 | struct tree *tree; | |
253 | struct unpack_trees_options opts; | |
254 | struct tree_desc t; | |
255 | ||
256 | if (initial_commit) { | |
257 | discard_cache(); | |
258 | return; | |
259 | } | |
260 | ||
261 | memset(&opts, 0, sizeof(opts)); | |
262 | opts.head_idx = 1; | |
263 | opts.index_only = 1; | |
264 | opts.merge = 1; | |
34110cd4 LT |
265 | opts.src_index = &the_index; |
266 | opts.dst_index = &the_index; | |
fa9dcf80 LT |
267 | |
268 | opts.fn = oneway_merge; | |
269 | tree = parse_tree_indirect(head_sha1); | |
270 | if (!tree) | |
271 | die("failed to unpack HEAD tree object"); | |
272 | parse_tree(tree); | |
273 | init_tree_desc(&t, tree->buffer, tree->size); | |
203a2fe1 DB |
274 | if (unpack_trees(1, &t, &opts)) |
275 | exit(128); /* We've already reported the error, finish dying */ | |
fa9dcf80 LT |
276 | } |
277 | ||
d38a30df MM |
278 | static void refresh_cache_or_die(int refresh_flags) |
279 | { | |
280 | /* | |
281 | * refresh_flags contains REFRESH_QUIET, so the only errors | |
282 | * are for unmerged entries. | |
283 | */ | |
284 | if (refresh_cache(refresh_flags | REFRESH_IN_PORCELAIN)) | |
285 | die_resolve_conflict("commit"); | |
286 | } | |
287 | ||
50b7e70f | 288 | static char *prepare_index(int argc, const char **argv, const char *prefix, int is_status) |
f5bbc322 KH |
289 | { |
290 | int fd; | |
c455c87c | 291 | struct string_list partial; |
2888605c | 292 | const char **pathspec = NULL; |
50b7e70f | 293 | int refresh_flags = REFRESH_QUIET; |
f5bbc322 | 294 | |
50b7e70f JH |
295 | if (is_status) |
296 | refresh_flags |= REFRESH_UNMERGED; | |
f5bbc322 | 297 | if (interactive) { |
4f6a32f8 JK |
298 | if (interactive_add(argc, argv, prefix) != 0) |
299 | die("interactive add failed"); | |
671c9b7e | 300 | if (read_cache_preload(NULL) < 0) |
d5350fd2 | 301 | die("index file corrupt"); |
2888605c | 302 | commit_style = COMMIT_AS_IS; |
f5bbc322 KH |
303 | return get_index_file(); |
304 | } | |
305 | ||
f64fe7b4 JH |
306 | if (*argv) |
307 | pathspec = get_pathspec(prefix, argv); | |
2888605c | 308 | |
671c9b7e LT |
309 | if (read_cache_preload(pathspec) < 0) |
310 | die("index file corrupt"); | |
311 | ||
2888605c JH |
312 | /* |
313 | * Non partial, non as-is commit. | |
314 | * | |
315 | * (1) get the real index; | |
316 | * (2) update the_index as necessary; | |
317 | * (3) write the_index out to the real index (still locked); | |
318 | * (4) return the name of the locked index file. | |
319 | * | |
320 | * The caller should run hooks on the locked real index, and | |
321 | * (A) if all goes well, commit the real index; | |
322 | * (B) on failure, rollback the real index. | |
323 | */ | |
324 | if (all || (also && pathspec && *pathspec)) { | |
1f2362a9 | 325 | fd = hold_locked_index(&index_lock, 1); |
7ae02a30 | 326 | add_files_to_cache(also ? prefix : NULL, pathspec, 0); |
d38a30df | 327 | refresh_cache_or_die(refresh_flags); |
4ed7cd3a BC |
328 | if (write_cache(fd, active_cache, active_nr) || |
329 | close_lock_file(&index_lock)) | |
f5bbc322 | 330 | die("unable to write new_index file"); |
2888605c JH |
331 | commit_style = COMMIT_NORMAL; |
332 | return index_lock.filename; | |
f5bbc322 KH |
333 | } |
334 | ||
2888605c JH |
335 | /* |
336 | * As-is commit. | |
337 | * | |
338 | * (1) return the name of the real index file. | |
339 | * | |
73276235 MH |
340 | * The caller should run hooks on the real index, |
341 | * and create commit from the_index. | |
2888605c JH |
342 | * We still need to refresh the index here. |
343 | */ | |
344 | if (!pathspec || !*pathspec) { | |
345 | fd = hold_locked_index(&index_lock, 1); | |
d38a30df | 346 | refresh_cache_or_die(refresh_flags); |
d5f5d0a9 JH |
347 | if (active_cache_changed) { |
348 | if (write_cache(fd, active_cache, active_nr) || | |
349 | commit_locked_index(&index_lock)) | |
350 | die("unable to write new_index file"); | |
351 | } else { | |
352 | rollback_lock_file(&index_lock); | |
353 | } | |
2888605c | 354 | commit_style = COMMIT_AS_IS; |
f5bbc322 KH |
355 | return get_index_file(); |
356 | } | |
357 | ||
2888605c JH |
358 | /* |
359 | * A partial commit. | |
360 | * | |
361 | * (0) find the set of affected paths; | |
362 | * (1) get lock on the real index file; | |
363 | * (2) update the_index with the given paths; | |
364 | * (3) write the_index out to the real index (still locked); | |
365 | * (4) get lock on the false index file; | |
366 | * (5) reset the_index from HEAD; | |
367 | * (6) update the_index the same way as (2); | |
368 | * (7) write the_index out to the false index file; | |
369 | * (8) return the name of the false index file (still locked); | |
370 | * | |
371 | * The caller should run hooks on the locked false index, and | |
372 | * create commit from it. Then | |
373 | * (A) if all goes well, commit the real index; | |
374 | * (B) on failure, rollback the real index; | |
375 | * In either case, rollback the false index. | |
376 | */ | |
377 | commit_style = COMMIT_PARTIAL; | |
378 | ||
30988301 | 379 | if (in_merge) |
2888605c JH |
380 | die("cannot do a partial commit during a merge."); |
381 | ||
382 | memset(&partial, 0, sizeof(partial)); | |
c455c87c | 383 | partial.strdup_strings = 1; |
2888605c JH |
384 | if (list_paths(&partial, initial_commit ? NULL : "HEAD", prefix, pathspec)) |
385 | exit(1); | |
386 | ||
387 | discard_cache(); | |
388 | if (read_cache() < 0) | |
389 | die("cannot read the index"); | |
390 | ||
391 | fd = hold_locked_index(&index_lock, 1); | |
392 | add_remove_files(&partial); | |
ef12b50d | 393 | refresh_cache(REFRESH_QUIET); |
4ed7cd3a BC |
394 | if (write_cache(fd, active_cache, active_nr) || |
395 | close_lock_file(&index_lock)) | |
f5bbc322 KH |
396 | die("unable to write new_index file"); |
397 | ||
2888605c | 398 | fd = hold_lock_file_for_update(&false_lock, |
a157400c JH |
399 | git_path("next-index-%"PRIuMAX, |
400 | (uintmax_t) getpid()), | |
acd3b9ec | 401 | LOCK_DIE_ON_ERROR); |
fa9dcf80 LT |
402 | |
403 | create_base_index(); | |
2888605c | 404 | add_remove_files(&partial); |
d37d3203 | 405 | refresh_cache(REFRESH_QUIET); |
f5bbc322 | 406 | |
4ed7cd3a BC |
407 | if (write_cache(fd, active_cache, active_nr) || |
408 | close_lock_file(&false_lock)) | |
2888605c | 409 | die("unable to write temporary index file"); |
959ba670 JK |
410 | |
411 | discard_cache(); | |
412 | read_cache_from(false_lock.filename); | |
413 | ||
2888605c | 414 | return false_lock.filename; |
f5bbc322 KH |
415 | } |
416 | ||
d249b098 JH |
417 | static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn, |
418 | struct wt_status *s) | |
f5bbc322 | 419 | { |
76e2f7ce JH |
420 | unsigned char sha1[20]; |
421 | ||
d249b098 JH |
422 | if (s->relative_paths) |
423 | s->prefix = prefix; | |
f5bbc322 KH |
424 | |
425 | if (amend) { | |
d249b098 JH |
426 | s->amend = 1; |
427 | s->reference = "HEAD^1"; | |
f5bbc322 | 428 | } |
d249b098 JH |
429 | s->verbose = verbose; |
430 | s->index_file = index_file; | |
431 | s->fp = fp; | |
432 | s->nowarn = nowarn; | |
76e2f7ce | 433 | s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0; |
f5bbc322 | 434 | |
76e2f7ce | 435 | wt_status_collect(s); |
7c9f7038 JK |
436 | |
437 | switch (status_format) { | |
438 | case STATUS_FORMAT_SHORT: | |
05a59a08 | 439 | wt_shortstatus_print(s, null_termination, status_show_branch); |
7c9f7038 JK |
440 | break; |
441 | case STATUS_FORMAT_PORCELAIN: | |
4a7cc2fd | 442 | wt_porcelain_print(s, null_termination); |
7c9f7038 JK |
443 | break; |
444 | case STATUS_FORMAT_LONG: | |
445 | wt_status_print(s); | |
446 | break; | |
447 | } | |
f5bbc322 | 448 | |
d249b098 | 449 | return s->commitable; |
f5bbc322 KH |
450 | } |
451 | ||
ec84bd00 PB |
452 | static int is_a_merge(const unsigned char *sha1) |
453 | { | |
454 | struct commit *commit = lookup_commit(sha1); | |
455 | if (!commit || parse_commit(commit)) | |
456 | die("could not parse HEAD commit"); | |
457 | return !!(commit->parents && commit->parents->next); | |
458 | } | |
459 | ||
f5bbc322 KH |
460 | static const char sign_off_header[] = "Signed-off-by: "; |
461 | ||
e83dbe80 | 462 | static void determine_author_info(void) |
a45d46ba SB |
463 | { |
464 | char *name, *email, *date; | |
465 | ||
466 | name = getenv("GIT_AUTHOR_NAME"); | |
467 | email = getenv("GIT_AUTHOR_EMAIL"); | |
468 | date = getenv("GIT_AUTHOR_DATE"); | |
469 | ||
c51f6cee | 470 | if (use_message && !renew_authorship) { |
a45d46ba SB |
471 | const char *a, *lb, *rb, *eol; |
472 | ||
473 | a = strstr(use_message_buffer, "\nauthor "); | |
474 | if (!a) | |
475 | die("invalid commit: %s", use_message); | |
476 | ||
fb7749e4 JN |
477 | lb = strchrnul(a + strlen("\nauthor "), '<'); |
478 | rb = strchrnul(lb, '>'); | |
479 | eol = strchrnul(rb, '\n'); | |
480 | if (!*lb || !*rb || !*eol) | |
a45d46ba SB |
481 | die("invalid commit: %s", use_message); |
482 | ||
fb7749e4 JN |
483 | if (lb == a + strlen("\nauthor ")) |
484 | /* \nauthor <foo@example.com> */ | |
485 | name = xcalloc(1, 1); | |
486 | else | |
487 | name = xmemdupz(a + strlen("\nauthor "), | |
488 | (lb - strlen(" ") - | |
489 | (a + strlen("\nauthor ")))); | |
490 | email = xmemdupz(lb + strlen("<"), rb - (lb + strlen("<"))); | |
491 | date = xmemdupz(rb + strlen("> "), eol - (rb + strlen("> "))); | |
a45d46ba SB |
492 | } |
493 | ||
494 | if (force_author) { | |
495 | const char *lb = strstr(force_author, " <"); | |
496 | const char *rb = strchr(force_author, '>'); | |
497 | ||
498 | if (!lb || !rb) | |
499 | die("malformed --author parameter"); | |
500 | name = xstrndup(force_author, lb - force_author); | |
501 | email = xstrndup(lb + 2, rb - (lb + 2)); | |
502 | } | |
503 | ||
02b47cd7 MV |
504 | if (force_date) |
505 | date = force_date; | |
506 | ||
e83dbe80 SB |
507 | author_name = name; |
508 | author_email = email; | |
509 | author_date = date; | |
a45d46ba SB |
510 | } |
511 | ||
c1e01b0c DB |
512 | static int ends_rfc2822_footer(struct strbuf *sb) |
513 | { | |
514 | int ch; | |
515 | int hit = 0; | |
516 | int i, j, k; | |
517 | int len = sb->len; | |
518 | int first = 1; | |
519 | const char *buf = sb->buf; | |
520 | ||
521 | for (i = len - 1; i > 0; i--) { | |
522 | if (hit && buf[i] == '\n') | |
523 | break; | |
524 | hit = (buf[i] == '\n'); | |
525 | } | |
526 | ||
527 | while (i < len - 1 && buf[i] == '\n') | |
528 | i++; | |
529 | ||
530 | for (; i < len; i = k) { | |
531 | for (k = i; k < len && buf[k] != '\n'; k++) | |
532 | ; /* do nothing */ | |
533 | k++; | |
534 | ||
535 | if ((buf[k] == ' ' || buf[k] == '\t') && !first) | |
536 | continue; | |
537 | ||
538 | first = 0; | |
539 | ||
540 | for (j = 0; i + j < len; j++) { | |
541 | ch = buf[i + j]; | |
542 | if (ch == ':') | |
543 | break; | |
544 | if (isalnum(ch) || | |
545 | (ch == '-')) | |
546 | continue; | |
547 | return 0; | |
548 | } | |
549 | } | |
550 | return 1; | |
551 | } | |
552 | ||
d249b098 JH |
553 | static int prepare_to_commit(const char *index_file, const char *prefix, |
554 | struct wt_status *s) | |
f5bbc322 KH |
555 | { |
556 | struct stat statbuf; | |
bc5d248a | 557 | int commitable, saved_color_setting; |
f285a2d7 | 558 | struct strbuf sb = STRBUF_INIT; |
f5bbc322 KH |
559 | char *buffer; |
560 | FILE *fp; | |
8089c85b PB |
561 | const char *hook_arg1 = NULL; |
562 | const char *hook_arg2 = NULL; | |
bb1ae3f6 | 563 | int ident_shown = 0; |
f5bbc322 | 564 | |
ec84bd00 PB |
565 | if (!no_verify && run_hook(index_file, "pre-commit", NULL)) |
566 | return 0; | |
f5bbc322 | 567 | |
f9568530 JS |
568 | if (message.len) { |
569 | strbuf_addbuf(&sb, &message); | |
8089c85b | 570 | hook_arg1 = "message"; |
f5bbc322 KH |
571 | } else if (logfile && !strcmp(logfile, "-")) { |
572 | if (isatty(0)) | |
573 | fprintf(stderr, "(reading log message from standard input)\n"); | |
574 | if (strbuf_read(&sb, 0, 0) < 0) | |
0721c314 | 575 | die_errno("could not read log from standard input"); |
8089c85b | 576 | hook_arg1 = "message"; |
f5bbc322 KH |
577 | } else if (logfile) { |
578 | if (strbuf_read_file(&sb, logfile, 0) < 0) | |
d824cbba TR |
579 | die_errno("could not read log file '%s'", |
580 | logfile); | |
8089c85b | 581 | hook_arg1 = "message"; |
f5bbc322 KH |
582 | } else if (use_message) { |
583 | buffer = strstr(use_message_buffer, "\n\n"); | |
584 | if (!buffer || buffer[2] == '\0') | |
585 | die("commit has empty message"); | |
586 | strbuf_add(&sb, buffer + 2, strlen(buffer + 2)); | |
8089c85b PB |
587 | hook_arg1 = "commit"; |
588 | hook_arg2 = use_message; | |
f5bbc322 KH |
589 | } else if (!stat(git_path("MERGE_MSG"), &statbuf)) { |
590 | if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0) | |
d824cbba | 591 | die_errno("could not read MERGE_MSG"); |
8089c85b | 592 | hook_arg1 = "merge"; |
f5bbc322 KH |
593 | } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) { |
594 | if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0) | |
d824cbba | 595 | die_errno("could not read SQUASH_MSG"); |
8089c85b | 596 | hook_arg1 = "squash"; |
f5bbc322 KH |
597 | } else if (template_file && !stat(template_file, &statbuf)) { |
598 | if (strbuf_read_file(&sb, template_file, 0) < 0) | |
d824cbba | 599 | die_errno("could not read '%s'", template_file); |
8089c85b | 600 | hook_arg1 = "template"; |
f5bbc322 KH |
601 | } |
602 | ||
8089c85b PB |
603 | /* |
604 | * This final case does not modify the template message, | |
605 | * it just sets the argument to the prepare-commit-msg hook. | |
606 | */ | |
607 | else if (in_merge) | |
608 | hook_arg1 = "merge"; | |
609 | ||
f5bbc322 KH |
610 | fp = fopen(git_path(commit_editmsg), "w"); |
611 | if (fp == NULL) | |
d824cbba | 612 | die_errno("could not open '%s'", git_path(commit_editmsg)); |
f5bbc322 | 613 | |
5f065737 AR |
614 | if (cleanup_mode != CLEANUP_NONE) |
615 | stripspace(&sb, 0); | |
f5bbc322 KH |
616 | |
617 | if (signoff) { | |
f285a2d7 | 618 | struct strbuf sob = STRBUF_INIT; |
13208572 JS |
619 | int i; |
620 | ||
13208572 | 621 | strbuf_addstr(&sob, sign_off_header); |
d9ccfe77 JH |
622 | strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"), |
623 | getenv("GIT_COMMITTER_EMAIL"))); | |
13208572 | 624 | strbuf_addch(&sob, '\n'); |
13208572 JS |
625 | for (i = sb.len - 1; i > 0 && sb.buf[i - 1] != '\n'; i--) |
626 | ; /* do nothing */ | |
2150554b | 627 | if (prefixcmp(sb.buf + i, sob.buf)) { |
e5138436 | 628 | if (!i || !ends_rfc2822_footer(&sb)) |
2150554b | 629 | strbuf_addch(&sb, '\n'); |
13208572 | 630 | strbuf_addbuf(&sb, &sob); |
2150554b | 631 | } |
13208572 | 632 | strbuf_release(&sob); |
f5bbc322 KH |
633 | } |
634 | ||
13208572 | 635 | if (fwrite(sb.buf, 1, sb.len, fp) < sb.len) |
d824cbba | 636 | die_errno("could not write commit template"); |
13208572 | 637 | |
f5bbc322 KH |
638 | strbuf_release(&sb); |
639 | ||
e83dbe80 SB |
640 | determine_author_info(); |
641 | ||
bb1ae3f6 SB |
642 | /* This checks if committer ident is explicitly given */ |
643 | git_committer_info(0); | |
bed575e4 | 644 | if (use_editor && include_status) { |
e83dbe80 SB |
645 | char *author_ident; |
646 | const char *committer_ident; | |
647 | ||
ec84bd00 PB |
648 | if (in_merge) |
649 | fprintf(fp, | |
650 | "#\n" | |
651 | "# It looks like you may be committing a MERGE.\n" | |
652 | "# If this is not correct, please remove the file\n" | |
653 | "# %s\n" | |
654 | "# and try again.\n" | |
655 | "#\n", | |
656 | git_path("MERGE_HEAD")); | |
657 | ||
658 | fprintf(fp, | |
659 | "\n" | |
fdc7c811 | 660 | "# Please enter the commit message for your changes."); |
ec84bd00 | 661 | if (cleanup_mode == CLEANUP_ALL) |
fdc7c811 JK |
662 | fprintf(fp, |
663 | " Lines starting\n" | |
664 | "# with '#' will be ignored, and an empty" | |
665 | " message aborts the commit.\n"); | |
ec84bd00 | 666 | else /* CLEANUP_SPACE, that is. */ |
fdc7c811 JK |
667 | fprintf(fp, |
668 | " Lines starting\n" | |
669 | "# with '#' will be kept; you may remove them" | |
670 | " yourself if you want to.\n" | |
671 | "# An empty message aborts the commit.\n"); | |
ec84bd00 PB |
672 | if (only_include_assumed) |
673 | fprintf(fp, "# %s\n", only_include_assumed); | |
674 | ||
e83dbe80 SB |
675 | author_ident = xstrdup(fmt_name(author_name, author_email)); |
676 | committer_ident = fmt_name(getenv("GIT_COMMITTER_NAME"), | |
677 | getenv("GIT_COMMITTER_EMAIL")); | |
678 | if (strcmp(author_ident, committer_ident)) | |
679 | fprintf(fp, | |
bb1ae3f6 SB |
680 | "%s" |
681 | "# Author: %s\n", | |
682 | ident_shown++ ? "" : "#\n", | |
e83dbe80 SB |
683 | author_ident); |
684 | free(author_ident); | |
685 | ||
5aeb3a3a | 686 | if (!user_ident_sufficiently_given()) |
bb1ae3f6 SB |
687 | fprintf(fp, |
688 | "%s" | |
689 | "# Committer: %s\n", | |
690 | ident_shown++ ? "" : "#\n", | |
691 | committer_ident); | |
692 | ||
693 | if (ident_shown) | |
694 | fprintf(fp, "#\n"); | |
695 | ||
d249b098 JH |
696 | saved_color_setting = s->use_color; |
697 | s->use_color = 0; | |
698 | commitable = run_status(fp, index_file, prefix, 1, s); | |
699 | s->use_color = saved_color_setting; | |
ec84bd00 | 700 | } else { |
d616a239 | 701 | unsigned char sha1[20]; |
fbcf1184 | 702 | const char *parent = "HEAD"; |
7168624c | 703 | |
7168624c AR |
704 | if (!active_nr && read_cache() < 0) |
705 | die("Cannot read index"); | |
706 | ||
fbcf1184 JH |
707 | if (amend) |
708 | parent = "HEAD^1"; | |
709 | ||
d616a239 | 710 | if (get_sha1(parent, sha1)) |
ec84bd00 | 711 | commitable = !!active_nr; |
75f3ff2e SB |
712 | else |
713 | commitable = index_differs_from(parent, 0); | |
ec84bd00 | 714 | } |
d616a239 | 715 | |
ec84bd00 | 716 | fclose(fp); |
7168624c | 717 | |
ec84bd00 PB |
718 | if (!commitable && !in_merge && !allow_empty && |
719 | !(amend && is_a_merge(head_sha1))) { | |
d249b098 | 720 | run_status(stdout, index_file, prefix, 0, s); |
f197ed2f JK |
721 | if (amend) |
722 | fputs(empty_amend_advice, stderr); | |
ec84bd00 | 723 | return 0; |
7168624c AR |
724 | } |
725 | ||
ec84bd00 PB |
726 | /* |
727 | * Re-read the index as pre-commit hook could have updated it, | |
728 | * and write it out as a tree. We must do this before we invoke | |
729 | * the editor and after we invoke run_status above. | |
730 | */ | |
731 | discard_cache(); | |
732 | read_cache_from(index_file); | |
733 | if (!active_cache_tree) | |
734 | active_cache_tree = cache_tree(); | |
735 | if (cache_tree_update(active_cache_tree, | |
736 | active_cache, active_nr, 0, 0) < 0) { | |
331fcb59 | 737 | error("Error building trees"); |
ec84bd00 | 738 | return 0; |
7168624c | 739 | } |
f5bbc322 | 740 | |
8089c85b PB |
741 | if (run_hook(index_file, "prepare-commit-msg", |
742 | git_path(commit_editmsg), hook_arg1, hook_arg2, NULL)) | |
743 | return 0; | |
f5bbc322 | 744 | |
ec84bd00 PB |
745 | if (use_editor) { |
746 | char index[PATH_MAX]; | |
66dbfd55 GV |
747 | const char *env[2] = { NULL }; |
748 | env[0] = index; | |
ec84bd00 | 749 | snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file); |
7198203a SB |
750 | if (launch_editor(git_path(commit_editmsg), NULL, env)) { |
751 | fprintf(stderr, | |
752 | "Please supply the message using either -m or -F option.\n"); | |
753 | exit(1); | |
754 | } | |
ec84bd00 | 755 | } |
f5bbc322 | 756 | |
ec84bd00 PB |
757 | if (!no_verify && |
758 | run_hook(index_file, "commit-msg", git_path(commit_editmsg), NULL)) { | |
759 | return 0; | |
760 | } | |
f5bbc322 | 761 | |
ec84bd00 | 762 | return 1; |
f5bbc322 KH |
763 | } |
764 | ||
765 | /* | |
6bb6b034 MV |
766 | * Find out if the message in the strbuf contains only whitespace and |
767 | * Signed-off-by lines. | |
f5bbc322 | 768 | */ |
6bb6b034 | 769 | static int message_is_empty(struct strbuf *sb) |
f5bbc322 | 770 | { |
f285a2d7 | 771 | struct strbuf tmpl = STRBUF_INIT; |
f5bbc322 | 772 | const char *nl; |
6bb6b034 | 773 | int eol, i, start = 0; |
f5bbc322 | 774 | |
5f065737 AR |
775 | if (cleanup_mode == CLEANUP_NONE && sb->len) |
776 | return 0; | |
777 | ||
f5bbc322 | 778 | /* See if the template is just a prefix of the message. */ |
f5bbc322 | 779 | if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) { |
5f065737 | 780 | stripspace(&tmpl, cleanup_mode == CLEANUP_ALL); |
f5bbc322 KH |
781 | if (start + tmpl.len <= sb->len && |
782 | memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0) | |
783 | start += tmpl.len; | |
784 | } | |
785 | strbuf_release(&tmpl); | |
786 | ||
787 | /* Check if the rest is just whitespace and Signed-of-by's. */ | |
788 | for (i = start; i < sb->len; i++) { | |
789 | nl = memchr(sb->buf + i, '\n', sb->len - i); | |
790 | if (nl) | |
791 | eol = nl - sb->buf; | |
792 | else | |
793 | eol = sb->len; | |
794 | ||
795 | if (strlen(sign_off_header) <= eol - i && | |
796 | !prefixcmp(sb->buf + i, sign_off_header)) { | |
797 | i = eol; | |
798 | continue; | |
799 | } | |
800 | while (i < eol) | |
801 | if (!isspace(sb->buf[i++])) | |
802 | return 0; | |
803 | } | |
804 | ||
805 | return 1; | |
806 | } | |
807 | ||
146ea068 JH |
808 | static const char *find_author_by_nickname(const char *name) |
809 | { | |
810 | struct rev_info revs; | |
811 | struct commit *commit; | |
812 | struct strbuf buf = STRBUF_INIT; | |
813 | const char *av[20]; | |
814 | int ac = 0; | |
815 | ||
816 | init_revisions(&revs, NULL); | |
817 | strbuf_addf(&buf, "--author=%s", name); | |
818 | av[++ac] = "--all"; | |
819 | av[++ac] = "-i"; | |
820 | av[++ac] = buf.buf; | |
821 | av[++ac] = NULL; | |
822 | setup_revisions(ac, av, &revs, NULL); | |
823 | prepare_revision_walk(&revs); | |
824 | commit = get_revision(&revs); | |
825 | if (commit) { | |
dd2e794a TR |
826 | struct pretty_print_context ctx = {0}; |
827 | ctx.date_mode = DATE_NORMAL; | |
146ea068 | 828 | strbuf_release(&buf); |
dd2e794a | 829 | format_commit_message(commit, "%an <%ae>", &buf, &ctx); |
146ea068 JH |
830 | return strbuf_detach(&buf, NULL); |
831 | } | |
832 | die("No existing author found with '%s'", name); | |
833 | } | |
834 | ||
76e2f7ce JH |
835 | |
836 | static void handle_untracked_files_arg(struct wt_status *s) | |
837 | { | |
838 | if (!untracked_files_arg) | |
839 | ; /* default already initialized */ | |
840 | else if (!strcmp(untracked_files_arg, "no")) | |
841 | s->show_untracked_files = SHOW_NO_UNTRACKED_FILES; | |
842 | else if (!strcmp(untracked_files_arg, "normal")) | |
843 | s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES; | |
844 | else if (!strcmp(untracked_files_arg, "all")) | |
845 | s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES; | |
846 | else | |
847 | die("Invalid untracked files mode '%s'", untracked_files_arg); | |
848 | } | |
849 | ||
2f02b25f | 850 | static int parse_and_validate_options(int argc, const char *argv[], |
dbd0f5c7 | 851 | const char * const usage[], |
d249b098 JH |
852 | const char *prefix, |
853 | struct wt_status *s) | |
f5bbc322 KH |
854 | { |
855 | int f = 0; | |
856 | ||
37782920 SB |
857 | argc = parse_options(argc, argv, prefix, builtin_commit_options, usage, |
858 | 0); | |
f5bbc322 | 859 | |
146ea068 JH |
860 | if (force_author && !strchr(force_author, '>')) |
861 | force_author = find_author_by_nickname(force_author); | |
862 | ||
c51f6cee EM |
863 | if (force_author && renew_authorship) |
864 | die("Using both --reset-author and --author does not make sense"); | |
865 | ||
f9568530 | 866 | if (logfile || message.len || use_message) |
4803466f | 867 | use_editor = 0; |
f5bbc322 | 868 | if (edit_flag) |
4803466f | 869 | use_editor = 1; |
406400ce PB |
870 | if (!use_editor) |
871 | setenv("GIT_EDITOR", ":", 1); | |
f5bbc322 KH |
872 | |
873 | if (get_sha1("HEAD", head_sha1)) | |
874 | initial_commit = 1; | |
875 | ||
f5bbc322 KH |
876 | /* Sanity check options */ |
877 | if (amend && initial_commit) | |
878 | die("You have nothing to amend."); | |
879 | if (amend && in_merge) | |
b5b644a9 | 880 | die("You are in the middle of a merge -- cannot amend."); |
f5bbc322 KH |
881 | |
882 | if (use_message) | |
883 | f++; | |
884 | if (edit_message) | |
885 | f++; | |
886 | if (logfile) | |
887 | f++; | |
888 | if (f > 1) | |
889 | die("Only one of -c/-C/-F can be used."); | |
f9568530 | 890 | if (message.len && f > 0) |
f5bbc322 KH |
891 | die("Option -m cannot be combined with -c/-C/-F."); |
892 | if (edit_message) | |
893 | use_message = edit_message; | |
1eb1e9ee | 894 | if (amend && !use_message) |
f5bbc322 | 895 | use_message = "HEAD"; |
c51f6cee EM |
896 | if (!use_message && renew_authorship) |
897 | die("--reset-author can be used only with -C, -c or --amend."); | |
f5bbc322 KH |
898 | if (use_message) { |
899 | unsigned char sha1[20]; | |
900 | static char utf8[] = "UTF-8"; | |
901 | const char *out_enc; | |
902 | char *enc, *end; | |
903 | struct commit *commit; | |
904 | ||
905 | if (get_sha1(use_message, sha1)) | |
906 | die("could not lookup commit %s", use_message); | |
8a2f8733 | 907 | commit = lookup_commit_reference(sha1); |
f5bbc322 KH |
908 | if (!commit || parse_commit(commit)) |
909 | die("could not parse commit %s", use_message); | |
910 | ||
911 | enc = strstr(commit->buffer, "\nencoding"); | |
912 | if (enc) { | |
913 | end = strchr(enc + 10, '\n'); | |
914 | enc = xstrndup(enc + 10, end - (enc + 10)); | |
915 | } else { | |
916 | enc = utf8; | |
917 | } | |
918 | out_enc = git_commit_encoding ? git_commit_encoding : utf8; | |
919 | ||
920 | if (strcmp(out_enc, enc)) | |
921 | use_message_buffer = | |
922 | reencode_string(commit->buffer, out_enc, enc); | |
923 | ||
924 | /* | |
925 | * If we failed to reencode the buffer, just copy it | |
926 | * byte for byte so the user can try to fix it up. | |
927 | * This also handles the case where input and output | |
928 | * encodings are identical. | |
929 | */ | |
930 | if (use_message_buffer == NULL) | |
931 | use_message_buffer = xstrdup(commit->buffer); | |
932 | if (enc != utf8) | |
933 | free(enc); | |
934 | } | |
935 | ||
936 | if (!!also + !!only + !!all + !!interactive > 1) | |
937 | die("Only one of --include/--only/--all/--interactive can be used."); | |
938 | if (argc == 0 && (also || (only && !amend))) | |
939 | die("No paths with --include/--only does not make sense."); | |
940 | if (argc == 0 && only && amend) | |
941 | only_include_assumed = "Clever... amending the last one with dirty index."; | |
3c5283f8 | 942 | if (argc > 0 && !also && !only) |
f5bbc322 | 943 | only_include_assumed = "Explicit paths specified without -i nor -o; assuming --only paths..."; |
5f065737 AR |
944 | if (!cleanup_arg || !strcmp(cleanup_arg, "default")) |
945 | cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE; | |
946 | else if (!strcmp(cleanup_arg, "verbatim")) | |
947 | cleanup_mode = CLEANUP_NONE; | |
948 | else if (!strcmp(cleanup_arg, "whitespace")) | |
949 | cleanup_mode = CLEANUP_SPACE; | |
950 | else if (!strcmp(cleanup_arg, "strip")) | |
951 | cleanup_mode = CLEANUP_ALL; | |
952 | else | |
953 | die("Invalid cleanup mode %s", cleanup_arg); | |
f5bbc322 | 954 | |
76e2f7ce | 955 | handle_untracked_files_arg(s); |
4bfee30a | 956 | |
f5bbc322 KH |
957 | if (all && argc > 0) |
958 | die("Paths with -a does not make sense."); | |
959 | else if (interactive && argc > 0) | |
960 | die("Paths with --interactive does not make sense."); | |
961 | ||
7c9f7038 JK |
962 | if (null_termination && status_format == STATUS_FORMAT_LONG) |
963 | status_format = STATUS_FORMAT_PORCELAIN; | |
964 | if (status_format != STATUS_FORMAT_LONG) | |
965 | dry_run = 1; | |
966 | ||
f5bbc322 KH |
967 | return argc; |
968 | } | |
969 | ||
d249b098 JH |
970 | static int dry_run_commit(int argc, const char **argv, const char *prefix, |
971 | struct wt_status *s) | |
f5bbc322 | 972 | { |
f5bbc322 | 973 | int commitable; |
3a5d13a3 | 974 | const char *index_file; |
f5bbc322 | 975 | |
3a5d13a3 | 976 | index_file = prepare_index(argc, argv, prefix, 1); |
d249b098 | 977 | commitable = run_status(stdout, index_file, prefix, 0, s); |
3a5d13a3 | 978 | rollback_index_files(); |
f5bbc322 | 979 | |
3a5d13a3 JH |
980 | return commitable ? 0 : 1; |
981 | } | |
982 | ||
f766b367 JH |
983 | static int parse_status_slot(const char *var, int offset) |
984 | { | |
985 | if (!strcasecmp(var+offset, "header")) | |
986 | return WT_STATUS_HEADER; | |
987 | if (!strcasecmp(var+offset, "updated") | |
988 | || !strcasecmp(var+offset, "added")) | |
989 | return WT_STATUS_UPDATED; | |
990 | if (!strcasecmp(var+offset, "changed")) | |
991 | return WT_STATUS_CHANGED; | |
992 | if (!strcasecmp(var+offset, "untracked")) | |
993 | return WT_STATUS_UNTRACKED; | |
994 | if (!strcasecmp(var+offset, "nobranch")) | |
995 | return WT_STATUS_NOBRANCH; | |
996 | if (!strcasecmp(var+offset, "unmerged")) | |
997 | return WT_STATUS_UNMERGED; | |
8b8e8624 | 998 | return -1; |
f766b367 JH |
999 | } |
1000 | ||
1001 | static int git_status_config(const char *k, const char *v, void *cb) | |
1002 | { | |
1003 | struct wt_status *s = cb; | |
1004 | ||
1005 | if (!strcmp(k, "status.submodulesummary")) { | |
1006 | int is_bool; | |
1007 | s->submodule_summary = git_config_bool_or_int(k, v, &is_bool); | |
1008 | if (is_bool && s->submodule_summary) | |
1009 | s->submodule_summary = -1; | |
1010 | return 0; | |
1011 | } | |
1012 | if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) { | |
1013 | s->use_color = git_config_colorbool(k, v, -1); | |
1014 | return 0; | |
1015 | } | |
1016 | if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) { | |
1017 | int slot = parse_status_slot(k, 13); | |
8b8e8624 JK |
1018 | if (slot < 0) |
1019 | return 0; | |
f766b367 JH |
1020 | if (!v) |
1021 | return config_error_nonbool(k); | |
1022 | color_parse(v, k, s->color_palette[slot]); | |
1023 | return 0; | |
1024 | } | |
1025 | if (!strcmp(k, "status.relativepaths")) { | |
1026 | s->relative_paths = git_config_bool(k, v); | |
1027 | return 0; | |
1028 | } | |
1029 | if (!strcmp(k, "status.showuntrackedfiles")) { | |
1030 | if (!v) | |
1031 | return config_error_nonbool(k); | |
1032 | else if (!strcmp(v, "no")) | |
1033 | s->show_untracked_files = SHOW_NO_UNTRACKED_FILES; | |
1034 | else if (!strcmp(v, "normal")) | |
1035 | s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES; | |
1036 | else if (!strcmp(v, "all")) | |
1037 | s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES; | |
1038 | else | |
1039 | return error("Invalid untracked files mode '%s'", v); | |
1040 | return 0; | |
1041 | } | |
1042 | return git_diff_ui_config(k, v, NULL); | |
1043 | } | |
1044 | ||
3a5d13a3 JH |
1045 | int cmd_status(int argc, const char **argv, const char *prefix) |
1046 | { | |
d249b098 | 1047 | struct wt_status s; |
4bb6644d | 1048 | int fd; |
76e2f7ce | 1049 | unsigned char sha1[20]; |
9e4b7ab6 | 1050 | static struct option builtin_status_options[] = { |
76e2f7ce | 1051 | OPT__VERBOSE(&verbose), |
dd2be243 JK |
1052 | OPT_SET_INT('s', "short", &status_format, |
1053 | "show status concisely", STATUS_FORMAT_SHORT), | |
05a59a08 DKF |
1054 | OPT_BOOLEAN('b', "branch", &status_show_branch, |
1055 | "show branch information"), | |
6f157871 JK |
1056 | OPT_SET_INT(0, "porcelain", &status_format, |
1057 | "show porcelain output format", | |
1058 | STATUS_FORMAT_PORCELAIN), | |
173e6c88 JH |
1059 | OPT_BOOLEAN('z', "null", &null_termination, |
1060 | "terminate entries with NUL"), | |
76e2f7ce JH |
1061 | { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, |
1062 | "mode", | |
1063 | "show untracked files, optional modes: all, normal, no. (Default: all)", | |
1064 | PARSE_OPT_OPTARG, NULL, (intptr_t)"all" }, | |
2381e39e JH |
1065 | OPT_BOOLEAN(0, "ignored", &show_ignored_in_status, |
1066 | "show ignored files"), | |
46a958b3 JL |
1067 | { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, "when", |
1068 | "ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)", | |
1069 | PARSE_OPT_OPTARG, NULL, (intptr_t)"all" }, | |
76e2f7ce JH |
1070 | OPT_END(), |
1071 | }; | |
1072 | ||
dd2be243 | 1073 | if (null_termination && status_format == STATUS_FORMAT_LONG) |
6f157871 | 1074 | status_format = STATUS_FORMAT_PORCELAIN; |
d249b098 JH |
1075 | |
1076 | wt_status_prepare(&s); | |
302ad7a9 | 1077 | gitmodules_config(); |
d249b098 | 1078 | git_config(git_status_config, &s); |
30988301 | 1079 | in_merge = file_exists(git_path("MERGE_HEAD")); |
76e2f7ce | 1080 | argc = parse_options(argc, argv, prefix, |
9e4b7ab6 JH |
1081 | builtin_status_options, |
1082 | builtin_status_usage, 0); | |
76e2f7ce | 1083 | handle_untracked_files_arg(&s); |
2381e39e JH |
1084 | if (show_ignored_in_status) |
1085 | s.show_ignored_files = 1; | |
76e2f7ce JH |
1086 | if (*argv) |
1087 | s.pathspec = get_pathspec(prefix, argv); | |
1088 | ||
149794dd | 1089 | read_cache_preload(s.pathspec); |
688cd6d2 | 1090 | refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec, NULL, NULL); |
4bb6644d MH |
1091 | |
1092 | fd = hold_locked_index(&index_lock, 0); | |
1093 | if (0 <= fd) { | |
d5f5d0a9 JH |
1094 | if (active_cache_changed && |
1095 | !write_cache(fd, active_cache, active_nr)) | |
4bb6644d | 1096 | commit_locked_index(&index_lock); |
d5f5d0a9 JH |
1097 | else |
1098 | rollback_lock_file(&index_lock); | |
4bb6644d MH |
1099 | } |
1100 | ||
76e2f7ce | 1101 | s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0; |
3c588453 | 1102 | s.in_merge = in_merge; |
46a958b3 | 1103 | s.ignore_submodule_arg = ignore_submodule_arg; |
76e2f7ce JH |
1104 | wt_status_collect(&s); |
1105 | ||
8661768f JK |
1106 | if (s.relative_paths) |
1107 | s.prefix = prefix; | |
d249b098 JH |
1108 | if (s.use_color == -1) |
1109 | s.use_color = git_use_color_default; | |
38920dd6 MH |
1110 | if (diff_use_color_default == -1) |
1111 | diff_use_color_default = git_use_color_default; | |
1112 | ||
dd2be243 JK |
1113 | switch (status_format) { |
1114 | case STATUS_FORMAT_SHORT: | |
05a59a08 | 1115 | wt_shortstatus_print(&s, null_termination, status_show_branch); |
dd2be243 | 1116 | break; |
6f157871 | 1117 | case STATUS_FORMAT_PORCELAIN: |
4a7cc2fd | 1118 | wt_porcelain_print(&s, null_termination); |
6f157871 | 1119 | break; |
dd2be243 | 1120 | case STATUS_FORMAT_LONG: |
173e6c88 | 1121 | s.verbose = verbose; |
46a958b3 | 1122 | s.ignore_submodule_arg = ignore_submodule_arg; |
173e6c88 | 1123 | wt_status_print(&s); |
dd2be243 | 1124 | break; |
173e6c88 | 1125 | } |
76e2f7ce | 1126 | return 0; |
f5bbc322 KH |
1127 | } |
1128 | ||
f5bbc322 KH |
1129 | static void print_summary(const char *prefix, const unsigned char *sha1) |
1130 | { | |
1131 | struct rev_info rev; | |
1132 | struct commit *commit; | |
49ff9a7a | 1133 | struct strbuf format = STRBUF_INIT; |
c85db254 JK |
1134 | unsigned char junk_sha1[20]; |
1135 | const char *head = resolve_ref("HEAD", junk_sha1, 0, NULL); | |
49ff9a7a JK |
1136 | struct pretty_print_context pctx = {0}; |
1137 | struct strbuf author_ident = STRBUF_INIT; | |
1138 | struct strbuf committer_ident = STRBUF_INIT; | |
f5bbc322 KH |
1139 | |
1140 | commit = lookup_commit(sha1); | |
1141 | if (!commit) | |
b5b644a9 | 1142 | die("couldn't look up newly created commit"); |
f5bbc322 KH |
1143 | if (!commit || parse_commit(commit)) |
1144 | die("could not parse newly created commit"); | |
1145 | ||
49ff9a7a JK |
1146 | strbuf_addstr(&format, "format:%h] %s"); |
1147 | ||
1148 | format_commit_message(commit, "%an <%ae>", &author_ident, &pctx); | |
1149 | format_commit_message(commit, "%cn <%ce>", &committer_ident, &pctx); | |
1150 | if (strbuf_cmp(&author_ident, &committer_ident)) { | |
1151 | strbuf_addstr(&format, "\n Author: "); | |
1152 | strbuf_addbuf_percentquote(&format, &author_ident); | |
1153 | } | |
1a893064 | 1154 | if (!user_ident_sufficiently_given()) { |
49ff9a7a JK |
1155 | strbuf_addstr(&format, "\n Committer: "); |
1156 | strbuf_addbuf_percentquote(&format, &committer_ident); | |
b706fcfe JK |
1157 | if (advice_implicit_identity) { |
1158 | strbuf_addch(&format, '\n'); | |
1159 | strbuf_addstr(&format, implicit_ident_advice); | |
1160 | } | |
49ff9a7a JK |
1161 | } |
1162 | strbuf_release(&author_ident); | |
1163 | strbuf_release(&committer_ident); | |
1164 | ||
f5bbc322 KH |
1165 | init_revisions(&rev, prefix); |
1166 | setup_revisions(0, NULL, &rev, NULL); | |
1167 | ||
f5bbc322 KH |
1168 | rev.diff = 1; |
1169 | rev.diffopt.output_format = | |
1170 | DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY; | |
1171 | ||
1172 | rev.verbose_header = 1; | |
1173 | rev.show_root_diff = 1; | |
49ff9a7a | 1174 | get_commit_format(format.buf, &rev); |
bf82a150 | 1175 | rev.always_show_header = 0; |
3eb2a15e JH |
1176 | rev.diffopt.detect_rename = 1; |
1177 | rev.diffopt.rename_limit = 100; | |
1178 | rev.diffopt.break_opt = 0; | |
15964563 | 1179 | diff_setup_done(&rev.diffopt); |
f5bbc322 | 1180 | |
c5ee71fd | 1181 | printf("[%s%s ", |
c85db254 JK |
1182 | !prefixcmp(head, "refs/heads/") ? |
1183 | head + 11 : | |
1184 | !strcmp(head, "HEAD") ? | |
1185 | "detached HEAD" : | |
1186 | head, | |
1187 | initial_commit ? " (root-commit)" : ""); | |
f5bbc322 | 1188 | |
bf82a150 | 1189 | if (!log_tree_commit(&rev, commit)) { |
a45e1a87 TRC |
1190 | rev.always_show_header = 1; |
1191 | rev.use_terminator = 1; | |
1192 | log_tree_commit(&rev, commit); | |
bf82a150 | 1193 | } |
a45e1a87 | 1194 | |
fc6f19fe | 1195 | strbuf_release(&format); |
f5bbc322 KH |
1196 | } |
1197 | ||
186458b1 | 1198 | static int git_commit_config(const char *k, const char *v, void *cb) |
f5bbc322 | 1199 | { |
d249b098 JH |
1200 | struct wt_status *s = cb; |
1201 | ||
984c6e7e | 1202 | if (!strcmp(k, "commit.template")) |
395de250 | 1203 | return git_config_pathname(&template_file, k, v); |
bed575e4 JHI |
1204 | if (!strcmp(k, "commit.status")) { |
1205 | include_status = git_config_bool(k, v); | |
1206 | return 0; | |
1207 | } | |
f5bbc322 | 1208 | |
d249b098 | 1209 | return git_status_config(k, v, s); |
f5bbc322 KH |
1210 | } |
1211 | ||
6f6bee3b TR |
1212 | static const char post_rewrite_hook[] = "hooks/post-rewrite"; |
1213 | ||
1214 | static int run_rewrite_hook(const unsigned char *oldsha1, | |
1215 | const unsigned char *newsha1) | |
1216 | { | |
1217 | /* oldsha1 SP newsha1 LF NUL */ | |
1218 | static char buf[2*40 + 3]; | |
1219 | struct child_process proc; | |
1220 | const char *argv[3]; | |
1221 | int code; | |
1222 | size_t n; | |
1223 | ||
1224 | if (access(git_path(post_rewrite_hook), X_OK) < 0) | |
1225 | return 0; | |
1226 | ||
1227 | argv[0] = git_path(post_rewrite_hook); | |
1228 | argv[1] = "amend"; | |
1229 | argv[2] = NULL; | |
1230 | ||
1231 | memset(&proc, 0, sizeof(proc)); | |
1232 | proc.argv = argv; | |
1233 | proc.in = -1; | |
1234 | proc.stdout_to_stderr = 1; | |
1235 | ||
1236 | code = start_command(&proc); | |
1237 | if (code) | |
1238 | return code; | |
1239 | n = snprintf(buf, sizeof(buf), "%s %s\n", | |
1240 | sha1_to_hex(oldsha1), sha1_to_hex(newsha1)); | |
1241 | write_in_full(proc.in, buf, n); | |
1242 | close(proc.in); | |
1243 | return finish_command(&proc); | |
1244 | } | |
1245 | ||
f5bbc322 KH |
1246 | int cmd_commit(int argc, const char **argv, const char *prefix) |
1247 | { | |
f285a2d7 | 1248 | struct strbuf sb = STRBUF_INIT; |
f5bbc322 | 1249 | const char *index_file, *reflog_msg; |
99a12694 | 1250 | char *nl, *p; |
f5bbc322 KH |
1251 | unsigned char commit_sha1[20]; |
1252 | struct ref_lock *ref_lock; | |
6bb6b034 | 1253 | struct commit_list *parents = NULL, **pptr = &parents; |
cf10f9fd MV |
1254 | struct stat statbuf; |
1255 | int allow_fast_forward = 1; | |
d249b098 | 1256 | struct wt_status s; |
f5bbc322 | 1257 | |
d249b098 JH |
1258 | wt_status_prepare(&s); |
1259 | git_config(git_commit_config, &s); | |
30988301 | 1260 | in_merge = file_exists(git_path("MERGE_HEAD")); |
3c588453 | 1261 | s.in_merge = in_merge; |
f5bbc322 | 1262 | |
d249b098 JH |
1263 | if (s.use_color == -1) |
1264 | s.use_color = git_use_color_default; | |
d249b098 JH |
1265 | argc = parse_and_validate_options(argc, argv, builtin_commit_usage, |
1266 | prefix, &s); | |
3fa509df JH |
1267 | if (dry_run) { |
1268 | if (diff_use_color_default == -1) | |
1269 | diff_use_color_default = git_use_color_default; | |
d249b098 | 1270 | return dry_run_commit(argc, argv, prefix, &s); |
3fa509df | 1271 | } |
50b7e70f | 1272 | index_file = prepare_index(argc, argv, prefix, 0); |
f5bbc322 | 1273 | |
ec84bd00 PB |
1274 | /* Set up everything for writing the commit object. This includes |
1275 | running hooks, writing the trees, and interacting with the user. */ | |
d249b098 | 1276 | if (!prepare_to_commit(index_file, prefix, &s)) { |
2888605c | 1277 | rollback_index_files(); |
f5bbc322 KH |
1278 | return 1; |
1279 | } | |
1280 | ||
f5bbc322 | 1281 | /* Determine parents */ |
643cb5f7 | 1282 | reflog_msg = getenv("GIT_REFLOG_ACTION"); |
f5bbc322 | 1283 | if (initial_commit) { |
643cb5f7 CC |
1284 | if (!reflog_msg) |
1285 | reflog_msg = "commit (initial)"; | |
f5bbc322 KH |
1286 | } else if (amend) { |
1287 | struct commit_list *c; | |
1288 | struct commit *commit; | |
1289 | ||
643cb5f7 CC |
1290 | if (!reflog_msg) |
1291 | reflog_msg = "commit (amend)"; | |
f5bbc322 KH |
1292 | commit = lookup_commit(head_sha1); |
1293 | if (!commit || parse_commit(commit)) | |
1294 | die("could not parse HEAD commit"); | |
1295 | ||
1296 | for (c = commit->parents; c; c = c->next) | |
6bb6b034 | 1297 | pptr = &commit_list_insert(c->item, pptr)->next; |
f5bbc322 | 1298 | } else if (in_merge) { |
f285a2d7 | 1299 | struct strbuf m = STRBUF_INIT; |
f5bbc322 KH |
1300 | FILE *fp; |
1301 | ||
643cb5f7 CC |
1302 | if (!reflog_msg) |
1303 | reflog_msg = "commit (merge)"; | |
6bb6b034 | 1304 | pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next; |
f5bbc322 KH |
1305 | fp = fopen(git_path("MERGE_HEAD"), "r"); |
1306 | if (fp == NULL) | |
d824cbba TR |
1307 | die_errno("could not open '%s' for reading", |
1308 | git_path("MERGE_HEAD")); | |
7c3fd25d LT |
1309 | while (strbuf_getline(&m, fp, '\n') != EOF) { |
1310 | unsigned char sha1[20]; | |
1311 | if (get_sha1_hex(m.buf, sha1) < 0) | |
1312 | die("Corrupt MERGE_HEAD file (%s)", m.buf); | |
6bb6b034 | 1313 | pptr = &commit_list_insert(lookup_commit(sha1), pptr)->next; |
7c3fd25d | 1314 | } |
f5bbc322 KH |
1315 | fclose(fp); |
1316 | strbuf_release(&m); | |
cf10f9fd MV |
1317 | if (!stat(git_path("MERGE_MODE"), &statbuf)) { |
1318 | if (strbuf_read_file(&sb, git_path("MERGE_MODE"), 0) < 0) | |
d824cbba | 1319 | die_errno("could not read MERGE_MODE"); |
cf10f9fd MV |
1320 | if (!strcmp(sb.buf, "no-ff")) |
1321 | allow_fast_forward = 0; | |
1322 | } | |
1323 | if (allow_fast_forward) | |
1324 | parents = reduce_heads(parents); | |
f5bbc322 | 1325 | } else { |
643cb5f7 CC |
1326 | if (!reflog_msg) |
1327 | reflog_msg = "commit"; | |
6bb6b034 | 1328 | pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next; |
f5bbc322 | 1329 | } |
f5bbc322 | 1330 | |
ec84bd00 | 1331 | /* Finally, get the commit message */ |
cf10f9fd | 1332 | strbuf_reset(&sb); |
740001a5 | 1333 | if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) { |
0721c314 | 1334 | int saved_errno = errno; |
740001a5 | 1335 | rollback_index_files(); |
0721c314 | 1336 | die("could not read commit message: %s", strerror(saved_errno)); |
740001a5 | 1337 | } |
99a12694 KH |
1338 | |
1339 | /* Truncate the message just before the diff, if any. */ | |
0b38227f JK |
1340 | if (verbose) { |
1341 | p = strstr(sb.buf, "\ndiff --git "); | |
1342 | if (p != NULL) | |
1343 | strbuf_setlen(&sb, p - sb.buf + 1); | |
1344 | } | |
99a12694 | 1345 | |
5f065737 AR |
1346 | if (cleanup_mode != CLEANUP_NONE) |
1347 | stripspace(&sb, cleanup_mode == CLEANUP_ALL); | |
c9b5fde7 | 1348 | if (message_is_empty(&sb) && !allow_empty_message) { |
2888605c | 1349 | rollback_index_files(); |
fdc7c811 JK |
1350 | fprintf(stderr, "Aborting commit due to empty commit message.\n"); |
1351 | exit(1); | |
2888605c | 1352 | } |
f5bbc322 | 1353 | |
6bb6b034 MV |
1354 | if (commit_tree(sb.buf, active_cache_tree->sha1, parents, commit_sha1, |
1355 | fmt_ident(author_name, author_email, author_date, | |
1356 | IDENT_ERROR_ON_NO_NAME))) { | |
2888605c | 1357 | rollback_index_files(); |
f5bbc322 | 1358 | die("failed to write commit object"); |
2888605c | 1359 | } |
f5bbc322 KH |
1360 | |
1361 | ref_lock = lock_any_ref_for_update("HEAD", | |
1362 | initial_commit ? NULL : head_sha1, | |
1363 | 0); | |
1364 | ||
6bb6b034 | 1365 | nl = strchr(sb.buf, '\n'); |
741707b1 JS |
1366 | if (nl) |
1367 | strbuf_setlen(&sb, nl + 1 - sb.buf); | |
1368 | else | |
1369 | strbuf_addch(&sb, '\n'); | |
741707b1 JS |
1370 | strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg)); |
1371 | strbuf_insert(&sb, strlen(reflog_msg), ": ", 2); | |
f5bbc322 | 1372 | |
2888605c JH |
1373 | if (!ref_lock) { |
1374 | rollback_index_files(); | |
f5bbc322 | 1375 | die("cannot lock HEAD ref"); |
2888605c JH |
1376 | } |
1377 | if (write_ref_sha1(ref_lock, commit_sha1, sb.buf) < 0) { | |
1378 | rollback_index_files(); | |
f5bbc322 | 1379 | die("cannot update HEAD ref"); |
2888605c | 1380 | } |
f5bbc322 KH |
1381 | |
1382 | unlink(git_path("MERGE_HEAD")); | |
1383 | unlink(git_path("MERGE_MSG")); | |
cf10f9fd | 1384 | unlink(git_path("MERGE_MODE")); |
5a95b855 | 1385 | unlink(git_path("SQUASH_MSG")); |
f5bbc322 | 1386 | |
5a9dd399 BC |
1387 | if (commit_index_files()) |
1388 | die ("Repository has been updated, but unable to write\n" | |
1389 | "new_index file. Check that disk is not full or quota is\n" | |
1390 | "not exceeded, and then \"git reset HEAD\" to recover."); | |
f5bbc322 | 1391 | |
cb6020bb | 1392 | rerere(0); |
2888605c | 1393 | run_hook(get_index_file(), "post-commit", NULL); |
6f6bee3b | 1394 | if (amend && !no_post_rewrite) { |
6360d343 TR |
1395 | struct notes_rewrite_cfg *cfg; |
1396 | cfg = init_copy_notes_for_rewrite("amend"); | |
1397 | if (cfg) { | |
1398 | copy_note_for_rewrite(cfg, head_sha1, commit_sha1); | |
1399 | finish_copy_notes_for_rewrite(cfg); | |
1400 | } | |
6f6bee3b TR |
1401 | run_rewrite_hook(head_sha1, commit_sha1); |
1402 | } | |
f5bbc322 KH |
1403 | if (!quiet) |
1404 | print_summary(prefix, commit_sha1); | |
1405 | ||
1406 | return 0; | |
1407 | } |