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