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