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