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