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