Documentation/git.txt: link to 1.5.5.2 documentation.
[git/git-svn.git] / builtin-commit.c
blob07872c8ea71064c1bcf5813dd66b4a7e5feb9bec
1 /*
2 * Builtin "git commit"
4 * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
6 */
8 #include "cache.h"
9 #include "cache-tree.h"
10 #include "color.h"
11 #include "dir.h"
12 #include "builtin.h"
13 #include "diff.h"
14 #include "diffcore.h"
15 #include "commit.h"
16 #include "revision.h"
17 #include "wt-status.h"
18 #include "run-command.h"
19 #include "refs.h"
20 #include "log-tree.h"
21 #include "strbuf.h"
22 #include "utf8.h"
23 #include "parse-options.h"
24 #include "path-list.h"
25 #include "unpack-trees.h"
27 static const char * const builtin_commit_usage[] = {
28 "git-commit [options] [--] <filepattern>...",
29 NULL
32 static const char * const builtin_status_usage[] = {
33 "git-status [options] [--] <filepattern>...",
34 NULL
37 static unsigned char head_sha1[20], merge_head_sha1[20];
38 static char *use_message_buffer;
39 static const char commit_editmsg[] = "COMMIT_EDITMSG";
40 static struct lock_file index_lock; /* real index */
41 static struct lock_file false_lock; /* used only for partial commits */
42 static enum {
43 COMMIT_AS_IS = 1,
44 COMMIT_NORMAL,
45 COMMIT_PARTIAL,
46 } commit_style;
48 static char *logfile, *force_author, *template_file;
49 static char *edit_message, *use_message;
50 static char *author_name, *author_email, *author_date;
51 static int all, edit_flag, also, interactive, only, amend, signoff;
52 static int quiet, verbose, untracked_files, no_verify, allow_empty;
54 * The default commit message cleanup mode will remove the lines
55 * beginning with # (shell comments) and leading and trailing
56 * whitespaces (empty lines or containing only whitespaces)
57 * if editor is used, and only the whitespaces if the message
58 * is specified explicitly.
60 static enum {
61 CLEANUP_SPACE,
62 CLEANUP_NONE,
63 CLEANUP_ALL,
64 } cleanup_mode;
65 static char *cleanup_arg;
67 static int use_editor = 1, initial_commit, in_merge;
68 const char *only_include_assumed;
69 struct strbuf message;
71 static int opt_parse_m(const struct option *opt, const char *arg, int unset)
73 struct strbuf *buf = opt->value;
74 if (unset)
75 strbuf_setlen(buf, 0);
76 else {
77 strbuf_addstr(buf, arg);
78 strbuf_addch(buf, '\n');
79 strbuf_addch(buf, '\n');
81 return 0;
84 static struct option builtin_commit_options[] = {
85 OPT__QUIET(&quiet),
86 OPT__VERBOSE(&verbose),
87 OPT_GROUP("Commit message options"),
89 OPT_STRING('F', "file", &logfile, "FILE", "read log from file"),
90 OPT_STRING(0, "author", &force_author, "AUTHOR", "override author for commit"),
91 OPT_CALLBACK('m', "message", &message, "MESSAGE", "specify commit message", opt_parse_m),
92 OPT_STRING('c', "reedit-message", &edit_message, "COMMIT", "reuse and edit message from specified commit "),
93 OPT_STRING('C', "reuse-message", &use_message, "COMMIT", "reuse message from specified commit"),
94 OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"),
95 OPT_STRING('t', "template", &template_file, "FILE", "use specified template file"),
96 OPT_BOOLEAN('e', "edit", &edit_flag, "force edit of commit"),
98 OPT_GROUP("Commit contents options"),
99 OPT_BOOLEAN('a', "all", &all, "commit all changed files"),
100 OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"),
101 OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"),
102 OPT_BOOLEAN('o', "only", &only, "commit only specified files"),
103 OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"),
104 OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
105 OPT_BOOLEAN('u', "untracked-files", &untracked_files, "show all untracked files"),
106 OPT_BOOLEAN(0, "allow-empty", &allow_empty, "ok to record an empty change"),
107 OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"),
109 OPT_END()
112 static void rollback_index_files(void)
114 switch (commit_style) {
115 case COMMIT_AS_IS:
116 break; /* nothing to do */
117 case COMMIT_NORMAL:
118 rollback_lock_file(&index_lock);
119 break;
120 case COMMIT_PARTIAL:
121 rollback_lock_file(&index_lock);
122 rollback_lock_file(&false_lock);
123 break;
127 static int commit_index_files(void)
129 int err = 0;
131 switch (commit_style) {
132 case COMMIT_AS_IS:
133 break; /* nothing to do */
134 case COMMIT_NORMAL:
135 err = commit_lock_file(&index_lock);
136 break;
137 case COMMIT_PARTIAL:
138 err = commit_lock_file(&index_lock);
139 rollback_lock_file(&false_lock);
140 break;
143 return err;
147 * Take a union of paths in the index and the named tree (typically, "HEAD"),
148 * and return the paths that match the given pattern in list.
150 static int list_paths(struct path_list *list, const char *with_tree,
151 const char *prefix, const char **pattern)
153 int i;
154 char *m;
156 for (i = 0; pattern[i]; i++)
158 m = xcalloc(1, i);
160 if (with_tree)
161 overlay_tree_on_cache(with_tree, prefix);
163 for (i = 0; i < active_nr; i++) {
164 struct cache_entry *ce = active_cache[i];
165 if (ce->ce_flags & CE_UPDATE)
166 continue;
167 if (!pathspec_match(pattern, m, ce->name, 0))
168 continue;
169 path_list_insert(ce->name, list);
172 return report_path_error(m, pattern, prefix ? strlen(prefix) : 0);
175 static void add_remove_files(struct path_list *list)
177 int i;
178 for (i = 0; i < list->nr; i++) {
179 struct stat st;
180 struct path_list_item *p = &(list->items[i]);
182 if (!lstat(p->path, &st)) {
183 if (add_to_cache(p->path, &st, 0))
184 die("updating files failed");
185 } else
186 remove_file_from_cache(p->path);
190 static void create_base_index(void)
192 struct tree *tree;
193 struct unpack_trees_options opts;
194 struct tree_desc t;
196 if (initial_commit) {
197 discard_cache();
198 return;
201 memset(&opts, 0, sizeof(opts));
202 opts.head_idx = 1;
203 opts.index_only = 1;
204 opts.merge = 1;
205 opts.src_index = &the_index;
206 opts.dst_index = &the_index;
208 opts.fn = oneway_merge;
209 tree = parse_tree_indirect(head_sha1);
210 if (!tree)
211 die("failed to unpack HEAD tree object");
212 parse_tree(tree);
213 init_tree_desc(&t, tree->buffer, tree->size);
214 if (unpack_trees(1, &t, &opts))
215 exit(128); /* We've already reported the error, finish dying */
218 static char *prepare_index(int argc, const char **argv, const char *prefix)
220 int fd;
221 struct path_list partial;
222 const char **pathspec = NULL;
224 if (interactive) {
225 interactive_add(argc, argv, prefix);
226 commit_style = COMMIT_AS_IS;
227 return get_index_file();
230 if (read_cache() < 0)
231 die("index file corrupt");
233 if (*argv)
234 pathspec = get_pathspec(prefix, argv);
237 * Non partial, non as-is commit.
239 * (1) get the real index;
240 * (2) update the_index as necessary;
241 * (3) write the_index out to the real index (still locked);
242 * (4) return the name of the locked index file.
244 * The caller should run hooks on the locked real index, and
245 * (A) if all goes well, commit the real index;
246 * (B) on failure, rollback the real index.
248 if (all || (also && pathspec && *pathspec)) {
249 int fd = hold_locked_index(&index_lock, 1);
250 add_files_to_cache(also ? prefix : NULL, pathspec, 0);
251 refresh_cache(REFRESH_QUIET);
252 if (write_cache(fd, active_cache, active_nr) ||
253 close_lock_file(&index_lock))
254 die("unable to write new_index file");
255 commit_style = COMMIT_NORMAL;
256 return index_lock.filename;
260 * As-is commit.
262 * (1) return the name of the real index file.
264 * The caller should run hooks on the real index, and run
265 * hooks on the real index, and create commit from the_index.
266 * We still need to refresh the index here.
268 if (!pathspec || !*pathspec) {
269 fd = hold_locked_index(&index_lock, 1);
270 refresh_cache(REFRESH_QUIET);
271 if (write_cache(fd, active_cache, active_nr) ||
272 commit_locked_index(&index_lock))
273 die("unable to write new_index file");
274 commit_style = COMMIT_AS_IS;
275 return get_index_file();
279 * A partial commit.
281 * (0) find the set of affected paths;
282 * (1) get lock on the real index file;
283 * (2) update the_index with the given paths;
284 * (3) write the_index out to the real index (still locked);
285 * (4) get lock on the false index file;
286 * (5) reset the_index from HEAD;
287 * (6) update the_index the same way as (2);
288 * (7) write the_index out to the false index file;
289 * (8) return the name of the false index file (still locked);
291 * The caller should run hooks on the locked false index, and
292 * create commit from it. Then
293 * (A) if all goes well, commit the real index;
294 * (B) on failure, rollback the real index;
295 * In either case, rollback the false index.
297 commit_style = COMMIT_PARTIAL;
299 if (file_exists(git_path("MERGE_HEAD")))
300 die("cannot do a partial commit during a merge.");
302 memset(&partial, 0, sizeof(partial));
303 partial.strdup_paths = 1;
304 if (list_paths(&partial, initial_commit ? NULL : "HEAD", prefix, pathspec))
305 exit(1);
307 discard_cache();
308 if (read_cache() < 0)
309 die("cannot read the index");
311 fd = hold_locked_index(&index_lock, 1);
312 add_remove_files(&partial);
313 refresh_cache(REFRESH_QUIET);
314 if (write_cache(fd, active_cache, active_nr) ||
315 close_lock_file(&index_lock))
316 die("unable to write new_index file");
318 fd = hold_lock_file_for_update(&false_lock,
319 git_path("next-index-%d", getpid()), 1);
321 create_base_index();
322 add_remove_files(&partial);
323 refresh_cache(REFRESH_QUIET);
325 if (write_cache(fd, active_cache, active_nr) ||
326 close_lock_file(&false_lock))
327 die("unable to write temporary index file");
329 discard_cache();
330 read_cache_from(false_lock.filename);
332 return false_lock.filename;
335 static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn)
337 struct wt_status s;
339 wt_status_prepare(&s);
340 if (wt_status_relative_paths)
341 s.prefix = prefix;
343 if (amend) {
344 s.amend = 1;
345 s.reference = "HEAD^1";
347 s.verbose = verbose;
348 s.untracked = untracked_files;
349 s.index_file = index_file;
350 s.fp = fp;
351 s.nowarn = nowarn;
353 wt_status_print(&s);
355 return s.commitable;
358 static int run_hook(const char *index_file, const char *name, ...)
360 struct child_process hook;
361 const char *argv[10], *env[2];
362 char index[PATH_MAX];
363 va_list args;
364 int i;
366 va_start(args, name);
367 argv[0] = git_path("hooks/%s", name);
368 i = 0;
369 do {
370 if (++i >= ARRAY_SIZE(argv))
371 die ("run_hook(): too many arguments");
372 argv[i] = va_arg(args, const char *);
373 } while (argv[i]);
374 va_end(args);
376 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
377 env[0] = index;
378 env[1] = NULL;
380 if (access(argv[0], X_OK) < 0)
381 return 0;
383 memset(&hook, 0, sizeof(hook));
384 hook.argv = argv;
385 hook.no_stdin = 1;
386 hook.stdout_to_stderr = 1;
387 hook.env = env;
389 return run_command(&hook);
392 static int is_a_merge(const unsigned char *sha1)
394 struct commit *commit = lookup_commit(sha1);
395 if (!commit || parse_commit(commit))
396 die("could not parse HEAD commit");
397 return !!(commit->parents && commit->parents->next);
400 static const char sign_off_header[] = "Signed-off-by: ";
402 static void determine_author_info(void)
404 char *name, *email, *date;
406 name = getenv("GIT_AUTHOR_NAME");
407 email = getenv("GIT_AUTHOR_EMAIL");
408 date = getenv("GIT_AUTHOR_DATE");
410 if (use_message) {
411 const char *a, *lb, *rb, *eol;
413 a = strstr(use_message_buffer, "\nauthor ");
414 if (!a)
415 die("invalid commit: %s", use_message);
417 lb = strstr(a + 8, " <");
418 rb = strstr(a + 8, "> ");
419 eol = strchr(a + 8, '\n');
420 if (!lb || !rb || !eol)
421 die("invalid commit: %s", use_message);
423 name = xstrndup(a + 8, lb - (a + 8));
424 email = xstrndup(lb + 2, rb - (lb + 2));
425 date = xstrndup(rb + 2, eol - (rb + 2));
428 if (force_author) {
429 const char *lb = strstr(force_author, " <");
430 const char *rb = strchr(force_author, '>');
432 if (!lb || !rb)
433 die("malformed --author parameter");
434 name = xstrndup(force_author, lb - force_author);
435 email = xstrndup(lb + 2, rb - (lb + 2));
438 author_name = name;
439 author_email = email;
440 author_date = date;
443 static int prepare_to_commit(const char *index_file, const char *prefix)
445 struct stat statbuf;
446 int commitable, saved_color_setting;
447 struct strbuf sb;
448 char *buffer;
449 FILE *fp;
450 const char *hook_arg1 = NULL;
451 const char *hook_arg2 = NULL;
452 int ident_shown = 0;
454 if (!no_verify && run_hook(index_file, "pre-commit", NULL))
455 return 0;
457 strbuf_init(&sb, 0);
458 if (message.len) {
459 strbuf_addbuf(&sb, &message);
460 hook_arg1 = "message";
461 } else if (logfile && !strcmp(logfile, "-")) {
462 if (isatty(0))
463 fprintf(stderr, "(reading log message from standard input)\n");
464 if (strbuf_read(&sb, 0, 0) < 0)
465 die("could not read log from standard input");
466 hook_arg1 = "message";
467 } else if (logfile) {
468 if (strbuf_read_file(&sb, logfile, 0) < 0)
469 die("could not read log file '%s': %s",
470 logfile, strerror(errno));
471 hook_arg1 = "message";
472 } else if (use_message) {
473 buffer = strstr(use_message_buffer, "\n\n");
474 if (!buffer || buffer[2] == '\0')
475 die("commit has empty message");
476 strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
477 hook_arg1 = "commit";
478 hook_arg2 = use_message;
479 } else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
480 if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
481 die("could not read MERGE_MSG: %s", strerror(errno));
482 hook_arg1 = "merge";
483 } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
484 if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
485 die("could not read SQUASH_MSG: %s", strerror(errno));
486 hook_arg1 = "squash";
487 } else if (template_file && !stat(template_file, &statbuf)) {
488 if (strbuf_read_file(&sb, template_file, 0) < 0)
489 die("could not read %s: %s",
490 template_file, strerror(errno));
491 hook_arg1 = "template";
495 * This final case does not modify the template message,
496 * it just sets the argument to the prepare-commit-msg hook.
498 else if (in_merge)
499 hook_arg1 = "merge";
501 fp = fopen(git_path(commit_editmsg), "w");
502 if (fp == NULL)
503 die("could not open %s", git_path(commit_editmsg));
505 if (cleanup_mode != CLEANUP_NONE)
506 stripspace(&sb, 0);
508 if (signoff) {
509 struct strbuf sob;
510 int i;
512 strbuf_init(&sob, 0);
513 strbuf_addstr(&sob, sign_off_header);
514 strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"),
515 getenv("GIT_COMMITTER_EMAIL")));
516 strbuf_addch(&sob, '\n');
517 for (i = sb.len - 1; i > 0 && sb.buf[i - 1] != '\n'; i--)
518 ; /* do nothing */
519 if (prefixcmp(sb.buf + i, sob.buf)) {
520 if (prefixcmp(sb.buf + i, sign_off_header))
521 strbuf_addch(&sb, '\n');
522 strbuf_addbuf(&sb, &sob);
524 strbuf_release(&sob);
527 if (fwrite(sb.buf, 1, sb.len, fp) < sb.len)
528 die("could not write commit template: %s", strerror(errno));
530 strbuf_release(&sb);
532 determine_author_info();
534 /* This checks if committer ident is explicitly given */
535 git_committer_info(0);
536 if (use_editor) {
537 char *author_ident;
538 const char *committer_ident;
540 if (in_merge)
541 fprintf(fp,
542 "#\n"
543 "# It looks like you may be committing a MERGE.\n"
544 "# If this is not correct, please remove the file\n"
545 "# %s\n"
546 "# and try again.\n"
547 "#\n",
548 git_path("MERGE_HEAD"));
550 fprintf(fp,
551 "\n"
552 "# Please enter the commit message for your changes.\n"
553 "# (Comment lines starting with '#' will ");
554 if (cleanup_mode == CLEANUP_ALL)
555 fprintf(fp, "not be included)\n");
556 else /* CLEANUP_SPACE, that is. */
557 fprintf(fp, "be kept.\n"
558 "# You can remove them yourself if you want to)\n");
559 if (only_include_assumed)
560 fprintf(fp, "# %s\n", only_include_assumed);
562 author_ident = xstrdup(fmt_name(author_name, author_email));
563 committer_ident = fmt_name(getenv("GIT_COMMITTER_NAME"),
564 getenv("GIT_COMMITTER_EMAIL"));
565 if (strcmp(author_ident, committer_ident))
566 fprintf(fp,
567 "%s"
568 "# Author: %s\n",
569 ident_shown++ ? "" : "#\n",
570 author_ident);
571 free(author_ident);
573 if (!user_ident_explicitly_given)
574 fprintf(fp,
575 "%s"
576 "# Committer: %s\n",
577 ident_shown++ ? "" : "#\n",
578 committer_ident);
580 if (ident_shown)
581 fprintf(fp, "#\n");
583 saved_color_setting = wt_status_use_color;
584 wt_status_use_color = 0;
585 commitable = run_status(fp, index_file, prefix, 1);
586 wt_status_use_color = saved_color_setting;
587 } else {
588 struct rev_info rev;
589 unsigned char sha1[20];
590 const char *parent = "HEAD";
592 if (!active_nr && read_cache() < 0)
593 die("Cannot read index");
595 if (amend)
596 parent = "HEAD^1";
598 if (get_sha1(parent, sha1))
599 commitable = !!active_nr;
600 else {
601 init_revisions(&rev, "");
602 rev.abbrev = 0;
603 setup_revisions(0, NULL, &rev, parent);
604 DIFF_OPT_SET(&rev.diffopt, QUIET);
605 DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
606 run_diff_index(&rev, 1 /* cached */);
608 commitable = !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES);
612 fclose(fp);
614 if (!commitable && !in_merge && !allow_empty &&
615 !(amend && is_a_merge(head_sha1))) {
616 run_status(stdout, index_file, prefix, 0);
617 unlink(commit_editmsg);
618 return 0;
622 * Re-read the index as pre-commit hook could have updated it,
623 * and write it out as a tree. We must do this before we invoke
624 * the editor and after we invoke run_status above.
626 discard_cache();
627 read_cache_from(index_file);
628 if (!active_cache_tree)
629 active_cache_tree = cache_tree();
630 if (cache_tree_update(active_cache_tree,
631 active_cache, active_nr, 0, 0) < 0) {
632 error("Error building trees");
633 return 0;
636 if (run_hook(index_file, "prepare-commit-msg",
637 git_path(commit_editmsg), hook_arg1, hook_arg2, NULL))
638 return 0;
640 if (use_editor) {
641 char index[PATH_MAX];
642 const char *env[2] = { index, NULL };
643 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
644 launch_editor(git_path(commit_editmsg), NULL, env);
647 if (!no_verify &&
648 run_hook(index_file, "commit-msg", git_path(commit_editmsg), NULL)) {
649 return 0;
652 return 1;
656 * Find out if the message starting at position 'start' in the strbuf
657 * contains only whitespace and Signed-off-by lines.
659 static int message_is_empty(struct strbuf *sb, int start)
661 struct strbuf tmpl;
662 const char *nl;
663 int eol, i;
665 if (cleanup_mode == CLEANUP_NONE && sb->len)
666 return 0;
668 /* See if the template is just a prefix of the message. */
669 strbuf_init(&tmpl, 0);
670 if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) {
671 stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
672 if (start + tmpl.len <= sb->len &&
673 memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0)
674 start += tmpl.len;
676 strbuf_release(&tmpl);
678 /* Check if the rest is just whitespace and Signed-of-by's. */
679 for (i = start; i < sb->len; i++) {
680 nl = memchr(sb->buf + i, '\n', sb->len - i);
681 if (nl)
682 eol = nl - sb->buf;
683 else
684 eol = sb->len;
686 if (strlen(sign_off_header) <= eol - i &&
687 !prefixcmp(sb->buf + i, sign_off_header)) {
688 i = eol;
689 continue;
691 while (i < eol)
692 if (!isspace(sb->buf[i++]))
693 return 0;
696 return 1;
699 static int parse_and_validate_options(int argc, const char *argv[],
700 const char * const usage[])
702 int f = 0;
704 argc = parse_options(argc, argv, builtin_commit_options, usage, 0);
706 if (logfile || message.len || use_message)
707 use_editor = 0;
708 if (edit_flag)
709 use_editor = 1;
710 if (!use_editor)
711 setenv("GIT_EDITOR", ":", 1);
713 if (get_sha1("HEAD", head_sha1))
714 initial_commit = 1;
716 if (!get_sha1("MERGE_HEAD", merge_head_sha1))
717 in_merge = 1;
719 /* Sanity check options */
720 if (amend && initial_commit)
721 die("You have nothing to amend.");
722 if (amend && in_merge)
723 die("You are in the middle of a merge -- cannot amend.");
725 if (use_message)
726 f++;
727 if (edit_message)
728 f++;
729 if (logfile)
730 f++;
731 if (f > 1)
732 die("Only one of -c/-C/-F can be used.");
733 if (message.len && f > 0)
734 die("Option -m cannot be combined with -c/-C/-F.");
735 if (edit_message)
736 use_message = edit_message;
737 if (amend && !use_message)
738 use_message = "HEAD";
739 if (use_message) {
740 unsigned char sha1[20];
741 static char utf8[] = "UTF-8";
742 const char *out_enc;
743 char *enc, *end;
744 struct commit *commit;
746 if (get_sha1(use_message, sha1))
747 die("could not lookup commit %s", use_message);
748 commit = lookup_commit_reference(sha1);
749 if (!commit || parse_commit(commit))
750 die("could not parse commit %s", use_message);
752 enc = strstr(commit->buffer, "\nencoding");
753 if (enc) {
754 end = strchr(enc + 10, '\n');
755 enc = xstrndup(enc + 10, end - (enc + 10));
756 } else {
757 enc = utf8;
759 out_enc = git_commit_encoding ? git_commit_encoding : utf8;
761 if (strcmp(out_enc, enc))
762 use_message_buffer =
763 reencode_string(commit->buffer, out_enc, enc);
766 * If we failed to reencode the buffer, just copy it
767 * byte for byte so the user can try to fix it up.
768 * This also handles the case where input and output
769 * encodings are identical.
771 if (use_message_buffer == NULL)
772 use_message_buffer = xstrdup(commit->buffer);
773 if (enc != utf8)
774 free(enc);
777 if (!!also + !!only + !!all + !!interactive > 1)
778 die("Only one of --include/--only/--all/--interactive can be used.");
779 if (argc == 0 && (also || (only && !amend)))
780 die("No paths with --include/--only does not make sense.");
781 if (argc == 0 && only && amend)
782 only_include_assumed = "Clever... amending the last one with dirty index.";
783 if (argc > 0 && !also && !only)
784 only_include_assumed = "Explicit paths specified without -i nor -o; assuming --only paths...";
785 if (!cleanup_arg || !strcmp(cleanup_arg, "default"))
786 cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE;
787 else if (!strcmp(cleanup_arg, "verbatim"))
788 cleanup_mode = CLEANUP_NONE;
789 else if (!strcmp(cleanup_arg, "whitespace"))
790 cleanup_mode = CLEANUP_SPACE;
791 else if (!strcmp(cleanup_arg, "strip"))
792 cleanup_mode = CLEANUP_ALL;
793 else
794 die("Invalid cleanup mode %s", cleanup_arg);
796 if (all && argc > 0)
797 die("Paths with -a does not make sense.");
798 else if (interactive && argc > 0)
799 die("Paths with --interactive does not make sense.");
801 return argc;
804 int cmd_status(int argc, const char **argv, const char *prefix)
806 const char *index_file;
807 int commitable;
809 git_config(git_status_config, NULL);
811 if (wt_status_use_color == -1)
812 wt_status_use_color = git_use_color_default;
814 argc = parse_and_validate_options(argc, argv, builtin_status_usage);
816 index_file = prepare_index(argc, argv, prefix);
818 commitable = run_status(stdout, index_file, prefix, 0);
820 rollback_index_files();
822 return commitable ? 0 : 1;
825 static void print_summary(const char *prefix, const unsigned char *sha1)
827 struct rev_info rev;
828 struct commit *commit;
830 commit = lookup_commit(sha1);
831 if (!commit)
832 die("couldn't look up newly created commit");
833 if (!commit || parse_commit(commit))
834 die("could not parse newly created commit");
836 init_revisions(&rev, prefix);
837 setup_revisions(0, NULL, &rev, NULL);
839 rev.abbrev = 0;
840 rev.diff = 1;
841 rev.diffopt.output_format =
842 DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY;
844 rev.verbose_header = 1;
845 rev.show_root_diff = 1;
846 get_commit_format("format:%h: %s", &rev);
847 rev.always_show_header = 0;
848 rev.diffopt.detect_rename = 1;
849 rev.diffopt.rename_limit = 100;
850 rev.diffopt.break_opt = 0;
851 diff_setup_done(&rev.diffopt);
853 printf("Created %scommit ", initial_commit ? "initial " : "");
855 if (!log_tree_commit(&rev, commit)) {
856 struct strbuf buf = STRBUF_INIT;
857 format_commit_message(commit, "%h: %s", &buf);
858 printf("%s\n", buf.buf);
859 strbuf_release(&buf);
863 int git_commit_config(const char *k, const char *v, void *cb)
865 if (!strcmp(k, "commit.template")) {
866 if (!v)
867 return config_error_nonbool(v);
868 template_file = xstrdup(v);
869 return 0;
872 return git_status_config(k, v, cb);
875 static const char commit_utf8_warn[] =
876 "Warning: commit message does not conform to UTF-8.\n"
877 "You may want to amend it after fixing the message, or set the config\n"
878 "variable i18n.commitencoding to the encoding your project uses.\n";
880 static void add_parent(struct strbuf *sb, const unsigned char *sha1)
882 struct object *obj = parse_object(sha1);
883 const char *parent = sha1_to_hex(sha1);
884 if (!obj)
885 die("Unable to find commit parent %s", parent);
886 if (obj->type != OBJ_COMMIT)
887 die("Parent %s isn't a proper commit", parent);
888 strbuf_addf(sb, "parent %s\n", parent);
891 int cmd_commit(int argc, const char **argv, const char *prefix)
893 int header_len;
894 struct strbuf sb;
895 const char *index_file, *reflog_msg;
896 char *nl, *p;
897 unsigned char commit_sha1[20];
898 struct ref_lock *ref_lock;
900 git_config(git_commit_config, NULL);
902 argc = parse_and_validate_options(argc, argv, builtin_commit_usage);
904 index_file = prepare_index(argc, argv, prefix);
906 /* Set up everything for writing the commit object. This includes
907 running hooks, writing the trees, and interacting with the user. */
908 if (!prepare_to_commit(index_file, prefix)) {
909 rollback_index_files();
910 return 1;
914 * The commit object
916 strbuf_init(&sb, 0);
917 strbuf_addf(&sb, "tree %s\n",
918 sha1_to_hex(active_cache_tree->sha1));
920 /* Determine parents */
921 if (initial_commit) {
922 reflog_msg = "commit (initial)";
923 } else if (amend) {
924 struct commit_list *c;
925 struct commit *commit;
927 reflog_msg = "commit (amend)";
928 commit = lookup_commit(head_sha1);
929 if (!commit || parse_commit(commit))
930 die("could not parse HEAD commit");
932 for (c = commit->parents; c; c = c->next)
933 add_parent(&sb, c->item->object.sha1);
934 } else if (in_merge) {
935 struct strbuf m;
936 FILE *fp;
938 reflog_msg = "commit (merge)";
939 add_parent(&sb, head_sha1);
940 strbuf_init(&m, 0);
941 fp = fopen(git_path("MERGE_HEAD"), "r");
942 if (fp == NULL)
943 die("could not open %s for reading: %s",
944 git_path("MERGE_HEAD"), strerror(errno));
945 while (strbuf_getline(&m, fp, '\n') != EOF) {
946 unsigned char sha1[20];
947 if (get_sha1_hex(m.buf, sha1) < 0)
948 die("Corrupt MERGE_HEAD file (%s)", m.buf);
949 add_parent(&sb, sha1);
951 fclose(fp);
952 strbuf_release(&m);
953 } else {
954 reflog_msg = "commit";
955 strbuf_addf(&sb, "parent %s\n", sha1_to_hex(head_sha1));
958 strbuf_addf(&sb, "author %s\n",
959 fmt_ident(author_name, author_email, author_date, IDENT_ERROR_ON_NO_NAME));
960 strbuf_addf(&sb, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
961 if (!is_encoding_utf8(git_commit_encoding))
962 strbuf_addf(&sb, "encoding %s\n", git_commit_encoding);
963 strbuf_addch(&sb, '\n');
965 /* Finally, get the commit message */
966 header_len = sb.len;
967 if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
968 rollback_index_files();
969 die("could not read commit message");
972 /* Truncate the message just before the diff, if any. */
973 p = strstr(sb.buf, "\ndiff --git a/");
974 if (p != NULL)
975 strbuf_setlen(&sb, p - sb.buf + 1);
977 if (cleanup_mode != CLEANUP_NONE)
978 stripspace(&sb, cleanup_mode == CLEANUP_ALL);
979 if (sb.len < header_len || message_is_empty(&sb, header_len)) {
980 rollback_index_files();
981 die("no commit message? aborting commit.");
983 strbuf_addch(&sb, '\0');
984 if (is_encoding_utf8(git_commit_encoding) && !is_utf8(sb.buf))
985 fprintf(stderr, commit_utf8_warn);
987 if (write_sha1_file(sb.buf, sb.len - 1, commit_type, commit_sha1)) {
988 rollback_index_files();
989 die("failed to write commit object");
992 ref_lock = lock_any_ref_for_update("HEAD",
993 initial_commit ? NULL : head_sha1,
996 nl = strchr(sb.buf + header_len, '\n');
997 if (nl)
998 strbuf_setlen(&sb, nl + 1 - sb.buf);
999 else
1000 strbuf_addch(&sb, '\n');
1001 strbuf_remove(&sb, 0, header_len);
1002 strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
1003 strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
1005 if (!ref_lock) {
1006 rollback_index_files();
1007 die("cannot lock HEAD ref");
1009 if (write_ref_sha1(ref_lock, commit_sha1, sb.buf) < 0) {
1010 rollback_index_files();
1011 die("cannot update HEAD ref");
1014 unlink(git_path("MERGE_HEAD"));
1015 unlink(git_path("MERGE_MSG"));
1016 unlink(git_path("SQUASH_MSG"));
1018 if (commit_index_files())
1019 die ("Repository has been updated, but unable to write\n"
1020 "new_index file. Check that disk is not full or quota is\n"
1021 "not exceeded, and then \"git reset HEAD\" to recover.");
1023 rerere();
1024 run_hook(get_index_file(), "post-commit", NULL);
1025 if (!quiet)
1026 print_summary(prefix, commit_sha1);
1028 return 0;