]>
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" | |
2888605c | 10 | #include "dir.h" |
f5bbc322 KH |
11 | #include "builtin.h" |
12 | #include "diff.h" | |
13 | #include "diffcore.h" | |
14 | #include "commit.h" | |
15 | #include "revision.h" | |
16 | #include "wt-status.h" | |
17 | #include "run-command.h" | |
18 | #include "refs.h" | |
19 | #include "log-tree.h" | |
20 | #include "strbuf.h" | |
21 | #include "utf8.h" | |
22 | #include "parse-options.h" | |
2888605c | 23 | #include "path-list.h" |
fa9dcf80 | 24 | #include "unpack-trees.h" |
f5bbc322 KH |
25 | |
26 | static const char * const builtin_commit_usage[] = { | |
27 | "git-commit [options] [--] <filepattern>...", | |
28 | NULL | |
29 | }; | |
30 | ||
2f02b25f SB |
31 | static const char * const builtin_status_usage[] = { |
32 | "git-status [options] [--] <filepattern>...", | |
33 | NULL | |
34 | }; | |
35 | ||
f5bbc322 KH |
36 | static unsigned char head_sha1[20], merge_head_sha1[20]; |
37 | static char *use_message_buffer; | |
38 | static const char commit_editmsg[] = "COMMIT_EDITMSG"; | |
2888605c JH |
39 | static struct lock_file index_lock; /* real index */ |
40 | static struct lock_file false_lock; /* used only for partial commits */ | |
41 | static enum { | |
42 | COMMIT_AS_IS = 1, | |
43 | COMMIT_NORMAL, | |
44 | COMMIT_PARTIAL, | |
45 | } commit_style; | |
f5bbc322 | 46 | |
f9568530 | 47 | static char *logfile, *force_author, *template_file; |
f5bbc322 KH |
48 | static char *edit_message, *use_message; |
49 | static int all, edit_flag, also, interactive, only, amend, signoff; | |
5241b6bf | 50 | static int quiet, verbose, untracked_files, no_verify, allow_empty; |
5f065737 AR |
51 | /* |
52 | * The default commit message cleanup mode will remove the lines | |
53 | * beginning with # (shell comments) and leading and trailing | |
54 | * whitespaces (empty lines or containing only whitespaces) | |
55 | * if editor is used, and only the whitespaces if the message | |
56 | * is specified explicitly. | |
57 | */ | |
58 | static enum { | |
59 | CLEANUP_SPACE, | |
60 | CLEANUP_NONE, | |
61 | CLEANUP_ALL, | |
62 | } cleanup_mode; | |
63 | static char *cleanup_arg; | |
f5bbc322 | 64 | |
4803466f | 65 | static int use_editor = 1, initial_commit, in_merge; |
f5bbc322 | 66 | const char *only_include_assumed; |
f9568530 JS |
67 | struct strbuf message; |
68 | ||
69 | static int opt_parse_m(const struct option *opt, const char *arg, int unset) | |
70 | { | |
71 | struct strbuf *buf = opt->value; | |
72 | if (unset) | |
73 | strbuf_setlen(buf, 0); | |
74 | else { | |
75 | strbuf_addstr(buf, arg); | |
76 | strbuf_addch(buf, '\n'); | |
77 | strbuf_addch(buf, '\n'); | |
78 | } | |
79 | return 0; | |
80 | } | |
f5bbc322 KH |
81 | |
82 | static struct option builtin_commit_options[] = { | |
83 | OPT__QUIET(&quiet), | |
84 | OPT__VERBOSE(&verbose), | |
85 | OPT_GROUP("Commit message options"), | |
86 | ||
87 | OPT_STRING('F', "file", &logfile, "FILE", "read log from file"), | |
88 | OPT_STRING(0, "author", &force_author, "AUTHOR", "override author for commit"), | |
f9568530 | 89 | OPT_CALLBACK('m', "message", &message, "MESSAGE", "specify commit message", opt_parse_m), |
f5bbc322 KH |
90 | OPT_STRING('c', "reedit-message", &edit_message, "COMMIT", "reuse and edit message from specified commit "), |
91 | OPT_STRING('C', "reuse-message", &use_message, "COMMIT", "reuse message from specified commit"), | |
92 | OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by: header"), | |
93 | OPT_STRING('t', "template", &template_file, "FILE", "use specified template file"), | |
94 | OPT_BOOLEAN('e', "edit", &edit_flag, "force edit of commit"), | |
95 | ||
96 | OPT_GROUP("Commit contents options"), | |
97 | OPT_BOOLEAN('a', "all", &all, "commit all changed files"), | |
98 | OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"), | |
99 | OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"), | |
100 | OPT_BOOLEAN('o', "only", &only, ""), | |
101 | OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"), | |
102 | OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"), | |
103 | OPT_BOOLEAN(0, "untracked-files", &untracked_files, "show all untracked files"), | |
5241b6bf | 104 | OPT_BOOLEAN(0, "allow-empty", &allow_empty, "ok to record an empty change"), |
5f065737 | 105 | OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"), |
f5bbc322 KH |
106 | |
107 | OPT_END() | |
108 | }; | |
109 | ||
2888605c JH |
110 | static void rollback_index_files(void) |
111 | { | |
112 | switch (commit_style) { | |
113 | case COMMIT_AS_IS: | |
114 | break; /* nothing to do */ | |
115 | case COMMIT_NORMAL: | |
116 | rollback_lock_file(&index_lock); | |
117 | break; | |
118 | case COMMIT_PARTIAL: | |
119 | rollback_lock_file(&index_lock); | |
120 | rollback_lock_file(&false_lock); | |
121 | break; | |
122 | } | |
123 | } | |
124 | ||
125 | static void commit_index_files(void) | |
126 | { | |
127 | switch (commit_style) { | |
128 | case COMMIT_AS_IS: | |
129 | break; /* nothing to do */ | |
130 | case COMMIT_NORMAL: | |
131 | commit_lock_file(&index_lock); | |
132 | break; | |
133 | case COMMIT_PARTIAL: | |
134 | commit_lock_file(&index_lock); | |
135 | rollback_lock_file(&false_lock); | |
136 | break; | |
137 | } | |
138 | } | |
139 | ||
140 | /* | |
141 | * Take a union of paths in the index and the named tree (typically, "HEAD"), | |
142 | * and return the paths that match the given pattern in list. | |
143 | */ | |
144 | static int list_paths(struct path_list *list, const char *with_tree, | |
145 | const char *prefix, const char **pattern) | |
146 | { | |
147 | int i; | |
148 | char *m; | |
149 | ||
150 | for (i = 0; pattern[i]; i++) | |
151 | ; | |
152 | m = xcalloc(1, i); | |
153 | ||
154 | if (with_tree) | |
155 | overlay_tree_on_cache(with_tree, prefix); | |
156 | ||
157 | for (i = 0; i < active_nr; i++) { | |
158 | struct cache_entry *ce = active_cache[i]; | |
e87e22d0 JH |
159 | if (ce->ce_flags & htons(CE_UPDATE)) |
160 | continue; | |
2888605c JH |
161 | if (!pathspec_match(pattern, m, ce->name, 0)) |
162 | continue; | |
163 | path_list_insert(ce->name, list); | |
164 | } | |
165 | ||
166 | return report_path_error(m, pattern, prefix ? strlen(prefix) : 0); | |
167 | } | |
168 | ||
169 | static void add_remove_files(struct path_list *list) | |
170 | { | |
171 | int i; | |
172 | for (i = 0; i < list->nr; i++) { | |
173 | struct path_list_item *p = &(list->items[i]); | |
174 | if (file_exists(p->path)) | |
175 | add_file_to_cache(p->path, 0); | |
176 | else | |
177 | remove_file_from_cache(p->path); | |
178 | } | |
179 | } | |
180 | ||
fa9dcf80 LT |
181 | static void create_base_index(void) |
182 | { | |
183 | struct tree *tree; | |
184 | struct unpack_trees_options opts; | |
185 | struct tree_desc t; | |
186 | ||
187 | if (initial_commit) { | |
188 | discard_cache(); | |
189 | return; | |
190 | } | |
191 | ||
192 | memset(&opts, 0, sizeof(opts)); | |
193 | opts.head_idx = 1; | |
194 | opts.index_only = 1; | |
195 | opts.merge = 1; | |
196 | ||
197 | opts.fn = oneway_merge; | |
198 | tree = parse_tree_indirect(head_sha1); | |
199 | if (!tree) | |
200 | die("failed to unpack HEAD tree object"); | |
201 | parse_tree(tree); | |
202 | init_tree_desc(&t, tree->buffer, tree->size); | |
203 | unpack_trees(1, &t, &opts); | |
204 | } | |
205 | ||
f64fe7b4 | 206 | static char *prepare_index(int argc, const char **argv, const char *prefix) |
f5bbc322 KH |
207 | { |
208 | int fd; | |
2888605c JH |
209 | struct path_list partial; |
210 | const char **pathspec = NULL; | |
f5bbc322 KH |
211 | |
212 | if (interactive) { | |
3f061887 | 213 | interactive_add(argc, argv, prefix); |
2888605c | 214 | commit_style = COMMIT_AS_IS; |
f5bbc322 KH |
215 | return get_index_file(); |
216 | } | |
217 | ||
f5bbc322 KH |
218 | if (read_cache() < 0) |
219 | die("index file corrupt"); | |
220 | ||
f64fe7b4 JH |
221 | if (*argv) |
222 | pathspec = get_pathspec(prefix, argv); | |
2888605c JH |
223 | |
224 | /* | |
225 | * Non partial, non as-is commit. | |
226 | * | |
227 | * (1) get the real index; | |
228 | * (2) update the_index as necessary; | |
229 | * (3) write the_index out to the real index (still locked); | |
230 | * (4) return the name of the locked index file. | |
231 | * | |
232 | * The caller should run hooks on the locked real index, and | |
233 | * (A) if all goes well, commit the real index; | |
234 | * (B) on failure, rollback the real index. | |
235 | */ | |
236 | if (all || (also && pathspec && *pathspec)) { | |
237 | int fd = hold_locked_index(&index_lock, 1); | |
238 | add_files_to_cache(0, also ? prefix : NULL, pathspec); | |
d37d3203 | 239 | refresh_cache(REFRESH_QUIET); |
4439751d | 240 | if (write_cache(fd, active_cache, active_nr)) |
f5bbc322 | 241 | die("unable to write new_index file"); |
2888605c JH |
242 | commit_style = COMMIT_NORMAL; |
243 | return index_lock.filename; | |
f5bbc322 KH |
244 | } |
245 | ||
2888605c JH |
246 | /* |
247 | * As-is commit. | |
248 | * | |
249 | * (1) return the name of the real index file. | |
250 | * | |
251 | * The caller should run hooks on the real index, and run | |
252 | * hooks on the real index, and create commit from the_index. | |
253 | * We still need to refresh the index here. | |
254 | */ | |
255 | if (!pathspec || !*pathspec) { | |
256 | fd = hold_locked_index(&index_lock, 1); | |
257 | refresh_cache(REFRESH_QUIET); | |
258 | if (write_cache(fd, active_cache, active_nr) || | |
4439751d | 259 | commit_locked_index(&index_lock)) |
2888605c JH |
260 | die("unable to write new_index file"); |
261 | commit_style = COMMIT_AS_IS; | |
f5bbc322 KH |
262 | return get_index_file(); |
263 | } | |
264 | ||
2888605c JH |
265 | /* |
266 | * A partial commit. | |
267 | * | |
268 | * (0) find the set of affected paths; | |
269 | * (1) get lock on the real index file; | |
270 | * (2) update the_index with the given paths; | |
271 | * (3) write the_index out to the real index (still locked); | |
272 | * (4) get lock on the false index file; | |
273 | * (5) reset the_index from HEAD; | |
274 | * (6) update the_index the same way as (2); | |
275 | * (7) write the_index out to the false index file; | |
276 | * (8) return the name of the false index file (still locked); | |
277 | * | |
278 | * The caller should run hooks on the locked false index, and | |
279 | * create commit from it. Then | |
280 | * (A) if all goes well, commit the real index; | |
281 | * (B) on failure, rollback the real index; | |
282 | * In either case, rollback the false index. | |
283 | */ | |
284 | commit_style = COMMIT_PARTIAL; | |
285 | ||
286 | if (file_exists(git_path("MERGE_HEAD"))) | |
287 | die("cannot do a partial commit during a merge."); | |
288 | ||
289 | memset(&partial, 0, sizeof(partial)); | |
290 | partial.strdup_paths = 1; | |
291 | if (list_paths(&partial, initial_commit ? NULL : "HEAD", prefix, pathspec)) | |
292 | exit(1); | |
293 | ||
294 | discard_cache(); | |
295 | if (read_cache() < 0) | |
296 | die("cannot read the index"); | |
297 | ||
298 | fd = hold_locked_index(&index_lock, 1); | |
299 | add_remove_files(&partial); | |
ef12b50d | 300 | refresh_cache(REFRESH_QUIET); |
4439751d | 301 | if (write_cache(fd, active_cache, active_nr)) |
f5bbc322 KH |
302 | die("unable to write new_index file"); |
303 | ||
2888605c JH |
304 | fd = hold_lock_file_for_update(&false_lock, |
305 | git_path("next-index-%d", getpid()), 1); | |
fa9dcf80 LT |
306 | |
307 | create_base_index(); | |
2888605c | 308 | add_remove_files(&partial); |
d37d3203 | 309 | refresh_cache(REFRESH_QUIET); |
f5bbc322 | 310 | |
4439751d | 311 | if (write_cache(fd, active_cache, active_nr)) |
2888605c JH |
312 | die("unable to write temporary index file"); |
313 | return false_lock.filename; | |
f5bbc322 KH |
314 | } |
315 | ||
37d07f8f | 316 | static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn) |
f5bbc322 KH |
317 | { |
318 | struct wt_status s; | |
319 | ||
320 | wt_status_prepare(&s); | |
46f721c8 JK |
321 | if (wt_status_relative_paths) |
322 | s.prefix = prefix; | |
f5bbc322 KH |
323 | |
324 | if (amend) { | |
325 | s.amend = 1; | |
326 | s.reference = "HEAD^1"; | |
327 | } | |
328 | s.verbose = verbose; | |
329 | s.untracked = untracked_files; | |
330 | s.index_file = index_file; | |
331 | s.fp = fp; | |
37d07f8f | 332 | s.nowarn = nowarn; |
f5bbc322 KH |
333 | |
334 | wt_status_print(&s); | |
335 | ||
336 | return s.commitable; | |
337 | } | |
338 | ||
339 | static const char sign_off_header[] = "Signed-off-by: "; | |
340 | ||
367c9886 | 341 | static int prepare_log_message(const char *index_file, const char *prefix) |
f5bbc322 KH |
342 | { |
343 | struct stat statbuf; | |
bc5d248a | 344 | int commitable, saved_color_setting; |
f5bbc322 KH |
345 | struct strbuf sb; |
346 | char *buffer; | |
347 | FILE *fp; | |
348 | ||
349 | strbuf_init(&sb, 0); | |
f9568530 JS |
350 | if (message.len) { |
351 | strbuf_addbuf(&sb, &message); | |
f5bbc322 KH |
352 | } else if (logfile && !strcmp(logfile, "-")) { |
353 | if (isatty(0)) | |
354 | fprintf(stderr, "(reading log message from standard input)\n"); | |
355 | if (strbuf_read(&sb, 0, 0) < 0) | |
356 | die("could not read log from standard input"); | |
357 | } else if (logfile) { | |
358 | if (strbuf_read_file(&sb, logfile, 0) < 0) | |
359 | die("could not read log file '%s': %s", | |
360 | logfile, strerror(errno)); | |
361 | } else if (use_message) { | |
362 | buffer = strstr(use_message_buffer, "\n\n"); | |
363 | if (!buffer || buffer[2] == '\0') | |
364 | die("commit has empty message"); | |
365 | strbuf_add(&sb, buffer + 2, strlen(buffer + 2)); | |
366 | } else if (!stat(git_path("MERGE_MSG"), &statbuf)) { | |
367 | if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0) | |
368 | die("could not read MERGE_MSG: %s", strerror(errno)); | |
369 | } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) { | |
370 | if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0) | |
371 | die("could not read SQUASH_MSG: %s", strerror(errno)); | |
372 | } else if (template_file && !stat(template_file, &statbuf)) { | |
373 | if (strbuf_read_file(&sb, template_file, 0) < 0) | |
374 | die("could not read %s: %s", | |
375 | template_file, strerror(errno)); | |
376 | } | |
377 | ||
378 | fp = fopen(git_path(commit_editmsg), "w"); | |
379 | if (fp == NULL) | |
b5b644a9 | 380 | die("could not open %s", git_path(commit_editmsg)); |
f5bbc322 | 381 | |
5f065737 AR |
382 | if (cleanup_mode != CLEANUP_NONE) |
383 | stripspace(&sb, 0); | |
f5bbc322 KH |
384 | |
385 | if (signoff) { | |
13208572 JS |
386 | struct strbuf sob; |
387 | int i; | |
388 | ||
389 | strbuf_init(&sob, 0); | |
390 | strbuf_addstr(&sob, sign_off_header); | |
d9ccfe77 JH |
391 | strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"), |
392 | getenv("GIT_COMMITTER_EMAIL"))); | |
13208572 | 393 | strbuf_addch(&sob, '\n'); |
13208572 JS |
394 | for (i = sb.len - 1; i > 0 && sb.buf[i - 1] != '\n'; i--) |
395 | ; /* do nothing */ | |
2150554b JS |
396 | if (prefixcmp(sb.buf + i, sob.buf)) { |
397 | if (prefixcmp(sb.buf + i, sign_off_header)) | |
398 | strbuf_addch(&sb, '\n'); | |
13208572 | 399 | strbuf_addbuf(&sb, &sob); |
2150554b | 400 | } |
13208572 | 401 | strbuf_release(&sob); |
f5bbc322 KH |
402 | } |
403 | ||
13208572 | 404 | if (fwrite(sb.buf, 1, sb.len, fp) < sb.len) |
b5b644a9 | 405 | die("could not write commit template: %s", strerror(errno)); |
13208572 | 406 | |
f5bbc322 KH |
407 | strbuf_release(&sb); |
408 | ||
4803466f | 409 | if (!use_editor) { |
7168624c | 410 | struct rev_info rev; |
d616a239 | 411 | unsigned char sha1[20]; |
fbcf1184 | 412 | const char *parent = "HEAD"; |
7168624c AR |
413 | |
414 | fclose(fp); | |
415 | ||
416 | if (!active_nr && read_cache() < 0) | |
417 | die("Cannot read index"); | |
418 | ||
fbcf1184 JH |
419 | if (amend) |
420 | parent = "HEAD^1"; | |
421 | ||
d616a239 JH |
422 | if (get_sha1(parent, sha1)) |
423 | return !!active_nr; | |
424 | ||
7168624c AR |
425 | init_revisions(&rev, ""); |
426 | rev.abbrev = 0; | |
fbcf1184 | 427 | setup_revisions(0, NULL, &rev, parent); |
7168624c AR |
428 | DIFF_OPT_SET(&rev.diffopt, QUIET); |
429 | DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS); | |
430 | run_diff_index(&rev, 1 /* cached */); | |
431 | ||
432 | return !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES); | |
433 | } | |
434 | ||
4803466f | 435 | if (in_merge) |
f5bbc322 KH |
436 | fprintf(fp, |
437 | "#\n" | |
438 | "# It looks like you may be committing a MERGE.\n" | |
439 | "# If this is not correct, please remove the file\n" | |
440 | "# %s\n" | |
441 | "# and try again.\n" | |
442 | "#\n", | |
443 | git_path("MERGE_HEAD")); | |
444 | ||
445 | fprintf(fp, | |
446 | "\n" | |
447 | "# Please enter the commit message for your changes.\n" | |
5f065737 AR |
448 | "# (Comment lines starting with '#' will "); |
449 | if (cleanup_mode == CLEANUP_ALL) | |
450 | fprintf(fp, "not be included)\n"); | |
451 | else /* CLEANUP_SPACE, that is. */ | |
452 | fprintf(fp, "be kept.\n" | |
453 | "# You can remove them yourself if you want to)\n"); | |
f5bbc322 KH |
454 | if (only_include_assumed) |
455 | fprintf(fp, "# %s\n", only_include_assumed); | |
456 | ||
bc5d248a JH |
457 | saved_color_setting = wt_status_use_color; |
458 | wt_status_use_color = 0; | |
37d07f8f | 459 | commitable = run_status(fp, index_file, prefix, 1); |
bc5d248a | 460 | wt_status_use_color = saved_color_setting; |
f5bbc322 KH |
461 | |
462 | fclose(fp); | |
463 | ||
464 | return commitable; | |
465 | } | |
466 | ||
467 | /* | |
468 | * Find out if the message starting at position 'start' in the strbuf | |
469 | * contains only whitespace and Signed-off-by lines. | |
470 | */ | |
471 | static int message_is_empty(struct strbuf *sb, int start) | |
472 | { | |
473 | struct strbuf tmpl; | |
474 | const char *nl; | |
475 | int eol, i; | |
476 | ||
5f065737 AR |
477 | if (cleanup_mode == CLEANUP_NONE && sb->len) |
478 | return 0; | |
479 | ||
f5bbc322 KH |
480 | /* See if the template is just a prefix of the message. */ |
481 | strbuf_init(&tmpl, 0); | |
482 | if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) { | |
5f065737 | 483 | stripspace(&tmpl, cleanup_mode == CLEANUP_ALL); |
f5bbc322 KH |
484 | if (start + tmpl.len <= sb->len && |
485 | memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0) | |
486 | start += tmpl.len; | |
487 | } | |
488 | strbuf_release(&tmpl); | |
489 | ||
490 | /* Check if the rest is just whitespace and Signed-of-by's. */ | |
491 | for (i = start; i < sb->len; i++) { | |
492 | nl = memchr(sb->buf + i, '\n', sb->len - i); | |
493 | if (nl) | |
494 | eol = nl - sb->buf; | |
495 | else | |
496 | eol = sb->len; | |
497 | ||
498 | if (strlen(sign_off_header) <= eol - i && | |
499 | !prefixcmp(sb->buf + i, sign_off_header)) { | |
500 | i = eol; | |
501 | continue; | |
502 | } | |
503 | while (i < eol) | |
504 | if (!isspace(sb->buf[i++])) | |
505 | return 0; | |
506 | } | |
507 | ||
508 | return 1; | |
509 | } | |
510 | ||
511 | static void determine_author_info(struct strbuf *sb) | |
512 | { | |
513 | char *name, *email, *date; | |
514 | ||
515 | name = getenv("GIT_AUTHOR_NAME"); | |
516 | email = getenv("GIT_AUTHOR_EMAIL"); | |
517 | date = getenv("GIT_AUTHOR_DATE"); | |
518 | ||
519 | if (use_message) { | |
520 | const char *a, *lb, *rb, *eol; | |
521 | ||
522 | a = strstr(use_message_buffer, "\nauthor "); | |
523 | if (!a) | |
b5b644a9 | 524 | die("invalid commit: %s", use_message); |
f5bbc322 KH |
525 | |
526 | lb = strstr(a + 8, " <"); | |
527 | rb = strstr(a + 8, "> "); | |
528 | eol = strchr(a + 8, '\n'); | |
529 | if (!lb || !rb || !eol) | |
b5b644a9 | 530 | die("invalid commit: %s", use_message); |
f5bbc322 KH |
531 | |
532 | name = xstrndup(a + 8, lb - (a + 8)); | |
533 | email = xstrndup(lb + 2, rb - (lb + 2)); | |
534 | date = xstrndup(rb + 2, eol - (rb + 2)); | |
535 | } | |
536 | ||
537 | if (force_author) { | |
538 | const char *lb = strstr(force_author, " <"); | |
539 | const char *rb = strchr(force_author, '>'); | |
540 | ||
541 | if (!lb || !rb) | |
b5b644a9 | 542 | die("malformed --author parameter"); |
f5bbc322 KH |
543 | name = xstrndup(force_author, lb - force_author); |
544 | email = xstrndup(lb + 2, rb - (lb + 2)); | |
545 | } | |
546 | ||
774751a8 | 547 | strbuf_addf(sb, "author %s\n", fmt_ident(name, email, date, IDENT_ERROR_ON_NO_NAME)); |
f5bbc322 KH |
548 | } |
549 | ||
2f02b25f SB |
550 | static int parse_and_validate_options(int argc, const char *argv[], |
551 | const char * const usage[]) | |
f5bbc322 KH |
552 | { |
553 | int f = 0; | |
554 | ||
2f02b25f | 555 | argc = parse_options(argc, argv, builtin_commit_options, usage, 0); |
f5bbc322 | 556 | |
f9568530 | 557 | if (logfile || message.len || use_message) |
4803466f | 558 | use_editor = 0; |
f5bbc322 | 559 | if (edit_flag) |
4803466f | 560 | use_editor = 1; |
f5bbc322 KH |
561 | |
562 | if (get_sha1("HEAD", head_sha1)) | |
563 | initial_commit = 1; | |
564 | ||
565 | if (!get_sha1("MERGE_HEAD", merge_head_sha1)) | |
566 | in_merge = 1; | |
567 | ||
568 | /* Sanity check options */ | |
569 | if (amend && initial_commit) | |
570 | die("You have nothing to amend."); | |
571 | if (amend && in_merge) | |
b5b644a9 | 572 | die("You are in the middle of a merge -- cannot amend."); |
f5bbc322 KH |
573 | |
574 | if (use_message) | |
575 | f++; | |
576 | if (edit_message) | |
577 | f++; | |
578 | if (logfile) | |
579 | f++; | |
580 | if (f > 1) | |
581 | die("Only one of -c/-C/-F can be used."); | |
f9568530 | 582 | if (message.len && f > 0) |
f5bbc322 KH |
583 | die("Option -m cannot be combined with -c/-C/-F."); |
584 | if (edit_message) | |
585 | use_message = edit_message; | |
1eb1e9ee | 586 | if (amend && !use_message) |
f5bbc322 KH |
587 | use_message = "HEAD"; |
588 | if (use_message) { | |
589 | unsigned char sha1[20]; | |
590 | static char utf8[] = "UTF-8"; | |
591 | const char *out_enc; | |
592 | char *enc, *end; | |
593 | struct commit *commit; | |
594 | ||
595 | if (get_sha1(use_message, sha1)) | |
596 | die("could not lookup commit %s", use_message); | |
597 | commit = lookup_commit(sha1); | |
598 | if (!commit || parse_commit(commit)) | |
599 | die("could not parse commit %s", use_message); | |
600 | ||
601 | enc = strstr(commit->buffer, "\nencoding"); | |
602 | if (enc) { | |
603 | end = strchr(enc + 10, '\n'); | |
604 | enc = xstrndup(enc + 10, end - (enc + 10)); | |
605 | } else { | |
606 | enc = utf8; | |
607 | } | |
608 | out_enc = git_commit_encoding ? git_commit_encoding : utf8; | |
609 | ||
610 | if (strcmp(out_enc, enc)) | |
611 | use_message_buffer = | |
612 | reencode_string(commit->buffer, out_enc, enc); | |
613 | ||
614 | /* | |
615 | * If we failed to reencode the buffer, just copy it | |
616 | * byte for byte so the user can try to fix it up. | |
617 | * This also handles the case where input and output | |
618 | * encodings are identical. | |
619 | */ | |
620 | if (use_message_buffer == NULL) | |
621 | use_message_buffer = xstrdup(commit->buffer); | |
622 | if (enc != utf8) | |
623 | free(enc); | |
624 | } | |
625 | ||
626 | if (!!also + !!only + !!all + !!interactive > 1) | |
627 | die("Only one of --include/--only/--all/--interactive can be used."); | |
628 | if (argc == 0 && (also || (only && !amend))) | |
629 | die("No paths with --include/--only does not make sense."); | |
630 | if (argc == 0 && only && amend) | |
631 | only_include_assumed = "Clever... amending the last one with dirty index."; | |
632 | if (argc > 0 && !also && !only) { | |
633 | only_include_assumed = "Explicit paths specified without -i nor -o; assuming --only paths..."; | |
634 | also = 0; | |
635 | } | |
5f065737 AR |
636 | if (!cleanup_arg || !strcmp(cleanup_arg, "default")) |
637 | cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE; | |
638 | else if (!strcmp(cleanup_arg, "verbatim")) | |
639 | cleanup_mode = CLEANUP_NONE; | |
640 | else if (!strcmp(cleanup_arg, "whitespace")) | |
641 | cleanup_mode = CLEANUP_SPACE; | |
642 | else if (!strcmp(cleanup_arg, "strip")) | |
643 | cleanup_mode = CLEANUP_ALL; | |
644 | else | |
645 | die("Invalid cleanup mode %s", cleanup_arg); | |
f5bbc322 KH |
646 | |
647 | if (all && argc > 0) | |
648 | die("Paths with -a does not make sense."); | |
649 | else if (interactive && argc > 0) | |
650 | die("Paths with --interactive does not make sense."); | |
651 | ||
652 | return argc; | |
653 | } | |
654 | ||
655 | int cmd_status(int argc, const char **argv, const char *prefix) | |
656 | { | |
657 | const char *index_file; | |
658 | int commitable; | |
659 | ||
660 | git_config(git_status_config); | |
661 | ||
2f02b25f | 662 | argc = parse_and_validate_options(argc, argv, builtin_status_usage); |
f5bbc322 | 663 | |
f64fe7b4 | 664 | index_file = prepare_index(argc, argv, prefix); |
f5bbc322 | 665 | |
37d07f8f | 666 | commitable = run_status(stdout, index_file, prefix, 0); |
f5bbc322 | 667 | |
2888605c | 668 | rollback_index_files(); |
f5bbc322 KH |
669 | |
670 | return commitable ? 0 : 1; | |
671 | } | |
672 | ||
673 | static int run_hook(const char *index_file, const char *name, const char *arg) | |
674 | { | |
675 | struct child_process hook; | |
676 | const char *argv[3], *env[2]; | |
677 | char index[PATH_MAX]; | |
678 | ||
679 | argv[0] = git_path("hooks/%s", name); | |
680 | argv[1] = arg; | |
681 | argv[2] = NULL; | |
682 | snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file); | |
683 | env[0] = index; | |
684 | env[1] = NULL; | |
685 | ||
686 | if (access(argv[0], X_OK) < 0) | |
687 | return 0; | |
688 | ||
689 | memset(&hook, 0, sizeof(hook)); | |
690 | hook.argv = argv; | |
691 | hook.no_stdin = 1; | |
692 | hook.stdout_to_stderr = 1; | |
693 | hook.env = env; | |
694 | ||
695 | return run_command(&hook); | |
696 | } | |
697 | ||
698 | static void print_summary(const char *prefix, const unsigned char *sha1) | |
699 | { | |
700 | struct rev_info rev; | |
701 | struct commit *commit; | |
702 | ||
703 | commit = lookup_commit(sha1); | |
704 | if (!commit) | |
b5b644a9 | 705 | die("couldn't look up newly created commit"); |
f5bbc322 KH |
706 | if (!commit || parse_commit(commit)) |
707 | die("could not parse newly created commit"); | |
708 | ||
709 | init_revisions(&rev, prefix); | |
710 | setup_revisions(0, NULL, &rev, NULL); | |
711 | ||
712 | rev.abbrev = 0; | |
713 | rev.diff = 1; | |
714 | rev.diffopt.output_format = | |
715 | DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY; | |
716 | ||
717 | rev.verbose_header = 1; | |
718 | rev.show_root_diff = 1; | |
719 | rev.commit_format = get_commit_format("format:%h: %s"); | |
bf82a150 | 720 | rev.always_show_header = 0; |
3eb2a15e JH |
721 | rev.diffopt.detect_rename = 1; |
722 | rev.diffopt.rename_limit = 100; | |
723 | rev.diffopt.break_opt = 0; | |
15964563 | 724 | diff_setup_done(&rev.diffopt); |
f5bbc322 KH |
725 | |
726 | printf("Created %scommit ", initial_commit ? "initial " : ""); | |
727 | ||
bf82a150 JH |
728 | if (!log_tree_commit(&rev, commit)) { |
729 | struct strbuf buf = STRBUF_INIT; | |
730 | format_commit_message(commit, "%h: %s", &buf); | |
731 | printf("%s\n", buf.buf); | |
732 | strbuf_release(&buf); | |
733 | } | |
f5bbc322 KH |
734 | } |
735 | ||
736 | int git_commit_config(const char *k, const char *v) | |
737 | { | |
738 | if (!strcmp(k, "commit.template")) { | |
739 | template_file = xstrdup(v); | |
740 | return 0; | |
741 | } | |
742 | ||
743 | return git_status_config(k, v); | |
744 | } | |
745 | ||
9663c3bc JH |
746 | static int is_a_merge(const unsigned char *sha1) |
747 | { | |
748 | struct commit *commit = lookup_commit(sha1); | |
749 | if (!commit || parse_commit(commit)) | |
750 | die("could not parse HEAD commit"); | |
751 | return !!(commit->parents && commit->parents->next); | |
752 | } | |
753 | ||
f5bbc322 KH |
754 | static const char commit_utf8_warn[] = |
755 | "Warning: commit message does not conform to UTF-8.\n" | |
756 | "You may want to amend it after fixing the message, or set the config\n" | |
757 | "variable i18n.commitencoding to the encoding your project uses.\n"; | |
758 | ||
7c3fd25d LT |
759 | static void add_parent(struct strbuf *sb, const unsigned char *sha1) |
760 | { | |
761 | struct object *obj = parse_object(sha1); | |
762 | const char *parent = sha1_to_hex(sha1); | |
763 | if (!obj) | |
764 | die("Unable to find commit parent %s", parent); | |
765 | if (obj->type != OBJ_COMMIT) | |
766 | die("Parent %s isn't a proper commit", parent); | |
767 | strbuf_addf(sb, "parent %s\n", parent); | |
768 | } | |
769 | ||
f5bbc322 KH |
770 | int cmd_commit(int argc, const char **argv, const char *prefix) |
771 | { | |
18abc2db | 772 | int header_len; |
f5bbc322 KH |
773 | struct strbuf sb; |
774 | const char *index_file, *reflog_msg; | |
99a12694 | 775 | char *nl, *p; |
f5bbc322 KH |
776 | unsigned char commit_sha1[20]; |
777 | struct ref_lock *ref_lock; | |
778 | ||
779 | git_config(git_commit_config); | |
780 | ||
2f02b25f | 781 | argc = parse_and_validate_options(argc, argv, builtin_commit_usage); |
f5bbc322 | 782 | |
f64fe7b4 | 783 | index_file = prepare_index(argc, argv, prefix); |
f5bbc322 | 784 | |
2888605c JH |
785 | if (!no_verify && run_hook(index_file, "pre-commit", NULL)) { |
786 | rollback_index_files(); | |
787 | return 1; | |
788 | } | |
f5bbc322 | 789 | |
9663c3bc | 790 | if (!prepare_log_message(index_file, prefix) && !in_merge && |
5241b6bf | 791 | !allow_empty && !(amend && is_a_merge(head_sha1))) { |
37d07f8f | 792 | run_status(stdout, index_file, prefix, 0); |
2888605c | 793 | rollback_index_files(); |
f5bbc322 KH |
794 | unlink(commit_editmsg); |
795 | return 1; | |
796 | } | |
797 | ||
2888605c JH |
798 | /* |
799 | * Re-read the index as pre-commit hook could have updated it, | |
800 | * and write it out as a tree. | |
801 | */ | |
802 | discard_cache(); | |
f5bbc322 | 803 | read_cache_from(index_file); |
2888605c JH |
804 | if (!active_cache_tree) |
805 | active_cache_tree = cache_tree(); | |
f5bbc322 | 806 | if (cache_tree_update(active_cache_tree, |
2888605c JH |
807 | active_cache, active_nr, 0, 0) < 0) { |
808 | rollback_index_files(); | |
f5bbc322 | 809 | die("Error building trees"); |
2888605c JH |
810 | } |
811 | ||
812 | /* | |
813 | * The commit object | |
814 | */ | |
815 | strbuf_init(&sb, 0); | |
f5bbc322 KH |
816 | strbuf_addf(&sb, "tree %s\n", |
817 | sha1_to_hex(active_cache_tree->sha1)); | |
818 | ||
819 | /* Determine parents */ | |
820 | if (initial_commit) { | |
821 | reflog_msg = "commit (initial)"; | |
f5bbc322 KH |
822 | } else if (amend) { |
823 | struct commit_list *c; | |
824 | struct commit *commit; | |
825 | ||
826 | reflog_msg = "commit (amend)"; | |
827 | commit = lookup_commit(head_sha1); | |
828 | if (!commit || parse_commit(commit)) | |
829 | die("could not parse HEAD commit"); | |
830 | ||
831 | for (c = commit->parents; c; c = c->next) | |
7c3fd25d | 832 | add_parent(&sb, c->item->object.sha1); |
f5bbc322 KH |
833 | } else if (in_merge) { |
834 | struct strbuf m; | |
835 | FILE *fp; | |
836 | ||
837 | reflog_msg = "commit (merge)"; | |
7c3fd25d | 838 | add_parent(&sb, head_sha1); |
f5bbc322 KH |
839 | strbuf_init(&m, 0); |
840 | fp = fopen(git_path("MERGE_HEAD"), "r"); | |
841 | if (fp == NULL) | |
842 | die("could not open %s for reading: %s", | |
843 | git_path("MERGE_HEAD"), strerror(errno)); | |
7c3fd25d LT |
844 | while (strbuf_getline(&m, fp, '\n') != EOF) { |
845 | unsigned char sha1[20]; | |
846 | if (get_sha1_hex(m.buf, sha1) < 0) | |
847 | die("Corrupt MERGE_HEAD file (%s)", m.buf); | |
848 | add_parent(&sb, sha1); | |
849 | } | |
f5bbc322 KH |
850 | fclose(fp); |
851 | strbuf_release(&m); | |
852 | } else { | |
853 | reflog_msg = "commit"; | |
854 | strbuf_addf(&sb, "parent %s\n", sha1_to_hex(head_sha1)); | |
855 | } | |
856 | ||
857 | determine_author_info(&sb); | |
774751a8 | 858 | strbuf_addf(&sb, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME)); |
f5bbc322 KH |
859 | if (!is_encoding_utf8(git_commit_encoding)) |
860 | strbuf_addf(&sb, "encoding %s\n", git_commit_encoding); | |
861 | strbuf_addch(&sb, '\n'); | |
862 | ||
863 | /* Get the commit message and validate it */ | |
864 | header_len = sb.len; | |
4803466f | 865 | if (use_editor) { |
8babab95 PH |
866 | char index[PATH_MAX]; |
867 | const char *env[2] = { index, NULL }; | |
868 | snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file); | |
740001a5 | 869 | launch_editor(git_path(commit_editmsg), NULL, env); |
2888605c | 870 | } |
6b95655d WC |
871 | if (!no_verify && |
872 | run_hook(index_file, "commit-msg", git_path(commit_editmsg))) { | |
2888605c | 873 | rollback_index_files(); |
f5bbc322 | 874 | exit(1); |
2888605c | 875 | } |
740001a5 JH |
876 | if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) { |
877 | rollback_index_files(); | |
878 | die("could not read commit message"); | |
879 | } | |
99a12694 KH |
880 | |
881 | /* Truncate the message just before the diff, if any. */ | |
882 | p = strstr(sb.buf, "\ndiff --git a/"); | |
883 | if (p != NULL) | |
a98b8191 | 884 | strbuf_setlen(&sb, p - sb.buf + 1); |
99a12694 | 885 | |
5f065737 AR |
886 | if (cleanup_mode != CLEANUP_NONE) |
887 | stripspace(&sb, cleanup_mode == CLEANUP_ALL); | |
2888605c JH |
888 | if (sb.len < header_len || message_is_empty(&sb, header_len)) { |
889 | rollback_index_files(); | |
b5b644a9 | 890 | die("no commit message? aborting commit."); |
2888605c | 891 | } |
f5bbc322 KH |
892 | strbuf_addch(&sb, '\0'); |
893 | if (is_encoding_utf8(git_commit_encoding) && !is_utf8(sb.buf)) | |
894 | fprintf(stderr, commit_utf8_warn); | |
895 | ||
2888605c JH |
896 | if (write_sha1_file(sb.buf, sb.len - 1, commit_type, commit_sha1)) { |
897 | rollback_index_files(); | |
f5bbc322 | 898 | die("failed to write commit object"); |
2888605c | 899 | } |
f5bbc322 KH |
900 | |
901 | ref_lock = lock_any_ref_for_update("HEAD", | |
902 | initial_commit ? NULL : head_sha1, | |
903 | 0); | |
904 | ||
905 | nl = strchr(sb.buf + header_len, '\n'); | |
741707b1 JS |
906 | if (nl) |
907 | strbuf_setlen(&sb, nl + 1 - sb.buf); | |
908 | else | |
909 | strbuf_addch(&sb, '\n'); | |
910 | strbuf_remove(&sb, 0, header_len); | |
911 | strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg)); | |
912 | strbuf_insert(&sb, strlen(reflog_msg), ": ", 2); | |
f5bbc322 | 913 | |
2888605c JH |
914 | if (!ref_lock) { |
915 | rollback_index_files(); | |
f5bbc322 | 916 | die("cannot lock HEAD ref"); |
2888605c JH |
917 | } |
918 | if (write_ref_sha1(ref_lock, commit_sha1, sb.buf) < 0) { | |
919 | rollback_index_files(); | |
f5bbc322 | 920 | die("cannot update HEAD ref"); |
2888605c | 921 | } |
f5bbc322 KH |
922 | |
923 | unlink(git_path("MERGE_HEAD")); | |
924 | unlink(git_path("MERGE_MSG")); | |
925 | ||
2888605c | 926 | commit_index_files(); |
f5bbc322 KH |
927 | |
928 | rerere(); | |
2888605c | 929 | run_hook(get_index_file(), "post-commit", NULL); |
f5bbc322 KH |
930 | if (!quiet) |
931 | print_summary(prefix, commit_sha1); | |
932 | ||
933 | return 0; | |
934 | } |