4 * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
9 #include "cache-tree.h"
15 #include "wt-status.h"
16 #include "run-command.h"
21 #include "parse-options.h"
23 static const char * const builtin_commit_usage[] = {
24 "git-commit [options] [--] <filepattern>...",
28 static unsigned char head_sha1[20], merge_head_sha1[20];
29 static char *use_message_buffer;
30 static const char commit_editmsg[] = "COMMIT_EDITMSG";
31 static struct lock_file lock_file;
33 static char *logfile, *force_author, *template_file;
34 static char *edit_message, *use_message;
35 static int all, edit_flag, also, interactive, only, amend, signoff;
36 static int quiet, verbose, untracked_files, no_verify;
38 static int no_edit, initial_commit, in_merge;
39 const char *only_include_assumed;
40 struct strbuf message;
42 static int opt_parse_m(const struct option *opt, const char *arg, int unset)
44 struct strbuf *buf = opt->value;
46 strbuf_setlen(buf, 0);
48 strbuf_addstr(buf, arg);
49 strbuf_addch(buf, '\n');
50 strbuf_addch(buf, '\n');
55 static struct option builtin_commit_options[] = {
57 OPT__VERBOSE(&verbose),
58 OPT_GROUP("Commit message options"),
60 OPT_STRING('F', "file", &logfile, "FILE", "read log from file"),
61 OPT_STRING(0, "author", &force_author, "AUTHOR", "override author for commit"),
62 OPT_CALLBACK('m', "message", &message, "MESSAGE", "specify commit message", opt_parse_m),
63 OPT_STRING('c', "reedit-message", &edit_message, "COMMIT", "reuse and edit message from specified commit "),
64 OPT_STRING('C', "reuse-message", &use_message, "COMMIT", "reuse message from specified commit"),
65 OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by: header"),
66 OPT_STRING('t', "template", &template_file, "FILE", "use specified template file"),
67 OPT_BOOLEAN('e', "edit", &edit_flag, "force edit of commit"),
69 OPT_GROUP("Commit contents options"),
70 OPT_BOOLEAN('a', "all", &all, "commit all changed files"),
71 OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"),
72 OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"),
73 OPT_BOOLEAN('o', "only", &only, ""),
74 OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"),
75 OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
76 OPT_BOOLEAN(0, "untracked-files", &untracked_files, "show all untracked files"),
81 static char *prepare_index(const char **files, const char *prefix)
85 struct lock_file *next_index_lock;
89 return get_index_file();
92 fd = hold_locked_index(&lock_file, 1);
94 die("index file corrupt");
97 add_files_to_cache(verbose, also ? prefix : NULL, files);
98 refresh_cache(REFRESH_QUIET);
99 if (write_cache(fd, active_cache, active_nr) || close(fd))
100 die("unable to write new_index file");
101 return lock_file.filename;
104 if (*files == NULL) {
105 /* Commit index as-is. */
106 rollback_lock_file(&lock_file);
107 return get_index_file();
110 /* update the user index file */
111 add_files_to_cache(verbose, prefix, files);
112 if (write_cache(fd, active_cache, active_nr) || close(fd))
113 die("unable to write new_index file");
115 if (!initial_commit) {
116 tree = parse_tree_indirect(head_sha1);
118 die("failed to unpack HEAD tree object");
119 if (read_tree(tree, 0, NULL))
120 die("failed to read HEAD tree object");
123 /* Use a lock file to garbage collect the temporary index file. */
124 next_index_lock = xmalloc(sizeof(*next_index_lock));
125 fd = hold_lock_file_for_update(next_index_lock,
126 git_path("next-index-%d", getpid()), 1);
127 add_files_to_cache(verbose, prefix, files);
128 refresh_cache(REFRESH_QUIET);
129 if (write_cache(fd, active_cache, active_nr) || close(fd))
130 die("unable to write new_index file");
132 return next_index_lock->filename;
135 static int run_status(FILE *fp, const char *index_file, const char *prefix)
139 wt_status_prepare(&s);
144 s.reference = "HEAD^1";
147 s.untracked = untracked_files;
148 s.index_file = index_file;
156 static const char sign_off_header[] = "Signed-off-by: ";
158 static int prepare_log_message(const char *index_file, const char *prefix)
168 strbuf_addbuf(&sb, &message);
169 } else if (logfile && !strcmp(logfile, "-")) {
171 fprintf(stderr, "(reading log message from standard input)\n");
172 if (strbuf_read(&sb, 0, 0) < 0)
173 die("could not read log from standard input");
174 } else if (logfile) {
175 if (strbuf_read_file(&sb, logfile, 0) < 0)
176 die("could not read log file '%s': %s",
177 logfile, strerror(errno));
178 } else if (use_message) {
179 buffer = strstr(use_message_buffer, "\n\n");
180 if (!buffer || buffer[2] == '\0')
181 die("commit has empty message");
182 strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
183 } else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
184 if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
185 die("could not read MERGE_MSG: %s", strerror(errno));
186 } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
187 if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
188 die("could not read SQUASH_MSG: %s", strerror(errno));
189 } else if (template_file && !stat(template_file, &statbuf)) {
190 if (strbuf_read_file(&sb, template_file, 0) < 0)
191 die("could not read %s: %s",
192 template_file, strerror(errno));
195 fp = fopen(git_path(commit_editmsg), "w");
197 die("could not open %s\n", git_path(commit_editmsg));
205 strbuf_init(&sob, 0);
206 strbuf_addstr(&sob, sign_off_header);
207 strbuf_addstr(&sob, fmt_ident(getenv("GIT_COMMITTER_NAME"),
208 getenv("GIT_COMMITTER_EMAIL"),
210 strbuf_addch(&sob, '\n');
212 for (i = sb.len - 1; i > 0 && sb.buf[i - 1] != '\n'; i--)
214 if (prefixcmp(sb.buf + i, sob.buf)) {
215 if (prefixcmp(sb.buf + i, sign_off_header))
216 strbuf_addch(&sb, '\n');
217 strbuf_addbuf(&sb, &sob);
219 strbuf_release(&sob);
222 if (fwrite(sb.buf, 1, sb.len, fp) < sb.len)
223 die("could not write commit template: %s\n",
228 if (in_merge && !no_edit)
231 "# It looks like you may be committing a MERGE.\n"
232 "# If this is not correct, please remove the file\n"
236 git_path("MERGE_HEAD"));
240 "# Please enter the commit message for your changes.\n"
241 "# (Comment lines starting with '#' will not be included)\n");
242 if (only_include_assumed)
243 fprintf(fp, "# %s\n", only_include_assumed);
245 commitable = run_status(fp, index_file, prefix);
253 * Find out if the message starting at position 'start' in the strbuf
254 * contains only whitespace and Signed-off-by lines.
256 static int message_is_empty(struct strbuf *sb, int start)
262 /* See if the template is just a prefix of the message. */
263 strbuf_init(&tmpl, 0);
264 if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) {
265 stripspace(&tmpl, 1);
266 if (start + tmpl.len <= sb->len &&
267 memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0)
270 strbuf_release(&tmpl);
272 /* Check if the rest is just whitespace and Signed-of-by's. */
273 for (i = start; i < sb->len; i++) {
274 nl = memchr(sb->buf + i, '\n', sb->len - i);
280 if (strlen(sign_off_header) <= eol - i &&
281 !prefixcmp(sb->buf + i, sign_off_header)) {
286 if (!isspace(sb->buf[i++]))
293 static void determine_author_info(struct strbuf *sb)
295 char *name, *email, *date;
297 name = getenv("GIT_AUTHOR_NAME");
298 email = getenv("GIT_AUTHOR_EMAIL");
299 date = getenv("GIT_AUTHOR_DATE");
302 const char *a, *lb, *rb, *eol;
304 a = strstr(use_message_buffer, "\nauthor ");
306 die("invalid commit: %s\n", use_message);
308 lb = strstr(a + 8, " <");
309 rb = strstr(a + 8, "> ");
310 eol = strchr(a + 8, '\n');
311 if (!lb || !rb || !eol)
312 die("invalid commit: %s\n", use_message);
314 name = xstrndup(a + 8, lb - (a + 8));
315 email = xstrndup(lb + 2, rb - (lb + 2));
316 date = xstrndup(rb + 2, eol - (rb + 2));
320 const char *lb = strstr(force_author, " <");
321 const char *rb = strchr(force_author, '>');
324 die("malformed --author parameter\n");
325 name = xstrndup(force_author, lb - force_author);
326 email = xstrndup(lb + 2, rb - (lb + 2));
329 strbuf_addf(sb, "author %s\n", fmt_ident(name, email, date, 1));
332 static int parse_and_validate_options(int argc, const char *argv[])
336 argc = parse_options(argc, argv, builtin_commit_options,
337 builtin_commit_usage, 0);
339 if (logfile || message.len || use_message)
344 if (get_sha1("HEAD", head_sha1))
347 if (!get_sha1("MERGE_HEAD", merge_head_sha1))
350 /* Sanity check options */
351 if (amend && initial_commit)
352 die("You have nothing to amend.");
353 if (amend && in_merge)
354 die("You are in the middle of a merger -- cannot amend.");
363 die("Only one of -c/-C/-F can be used.");
364 if (message.len && f > 0)
365 die("Option -m cannot be combined with -c/-C/-F.");
367 use_message = edit_message;
369 use_message = "HEAD";
371 unsigned char sha1[20];
372 static char utf8[] = "UTF-8";
375 struct commit *commit;
377 if (get_sha1(use_message, sha1))
378 die("could not lookup commit %s", use_message);
379 commit = lookup_commit(sha1);
380 if (!commit || parse_commit(commit))
381 die("could not parse commit %s", use_message);
383 enc = strstr(commit->buffer, "\nencoding");
385 end = strchr(enc + 10, '\n');
386 enc = xstrndup(enc + 10, end - (enc + 10));
390 out_enc = git_commit_encoding ? git_commit_encoding : utf8;
392 if (strcmp(out_enc, enc))
394 reencode_string(commit->buffer, out_enc, enc);
397 * If we failed to reencode the buffer, just copy it
398 * byte for byte so the user can try to fix it up.
399 * This also handles the case where input and output
400 * encodings are identical.
402 if (use_message_buffer == NULL)
403 use_message_buffer = xstrdup(commit->buffer);
408 if (!!also + !!only + !!all + !!interactive > 1)
409 die("Only one of --include/--only/--all/--interactive can be used.");
410 if (argc == 0 && (also || (only && !amend)))
411 die("No paths with --include/--only does not make sense.");
412 if (argc == 0 && only && amend)
413 only_include_assumed = "Clever... amending the last one with dirty index.";
414 if (argc > 0 && !also && !only) {
415 only_include_assumed = "Explicit paths specified without -i nor -o; assuming --only paths...";
420 die("Paths with -a does not make sense.");
421 else if (interactive && argc > 0)
422 die("Paths with --interactive does not make sense.");
427 int cmd_status(int argc, const char **argv, const char *prefix)
429 const char *index_file;
432 git_config(git_status_config);
434 argc = parse_and_validate_options(argc, argv);
436 index_file = prepare_index(argv, prefix);
438 commitable = run_status(stdout, index_file, prefix);
440 rollback_lock_file(&lock_file);
442 return commitable ? 0 : 1;
445 static int run_hook(const char *index_file, const char *name, const char *arg)
447 struct child_process hook;
448 const char *argv[3], *env[2];
449 char index[PATH_MAX];
451 argv[0] = git_path("hooks/%s", name);
454 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
458 if (access(argv[0], X_OK) < 0)
461 memset(&hook, 0, sizeof(hook));
464 hook.stdout_to_stderr = 1;
467 return run_command(&hook);
470 static void print_summary(const char *prefix, const unsigned char *sha1)
473 struct commit *commit;
475 commit = lookup_commit(sha1);
477 die("couldn't look up newly created commit\n");
478 if (!commit || parse_commit(commit))
479 die("could not parse newly created commit");
481 init_revisions(&rev, prefix);
482 setup_revisions(0, NULL, &rev, NULL);
486 rev.diffopt.output_format =
487 DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY;
489 rev.verbose_header = 1;
490 rev.show_root_diff = 1;
491 rev.commit_format = get_commit_format("format:%h: %s");
492 rev.always_show_header = 1;
494 printf("Created %scommit ", initial_commit ? "initial " : "");
496 log_tree_commit(&rev, commit);
499 int git_commit_config(const char *k, const char *v)
501 if (!strcmp(k, "commit.template")) {
502 template_file = xstrdup(v);
506 return git_status_config(k, v);
509 static const char commit_utf8_warn[] =
510 "Warning: commit message does not conform to UTF-8.\n"
511 "You may want to amend it after fixing the message, or set the config\n"
512 "variable i18n.commitencoding to the encoding your project uses.\n";
514 int cmd_commit(int argc, const char **argv, const char *prefix)
516 int header_len, parent_count = 0;
518 const char *index_file, *reflog_msg;
520 unsigned char commit_sha1[20];
521 struct ref_lock *ref_lock;
523 git_config(git_commit_config);
525 argc = parse_and_validate_options(argc, argv);
527 index_file = prepare_index(argv, prefix);
529 if (!no_verify && run_hook(index_file, "pre-commit", NULL))
532 if (!prepare_log_message(index_file, prefix) && !in_merge) {
533 run_status(stdout, index_file, prefix);
534 unlink(commit_editmsg);
540 /* Start building up the commit header */
541 read_cache_from(index_file);
542 active_cache_tree = cache_tree();
543 if (cache_tree_update(active_cache_tree,
544 active_cache, active_nr, 0, 0) < 0)
545 die("Error building trees");
546 strbuf_addf(&sb, "tree %s\n",
547 sha1_to_hex(active_cache_tree->sha1));
549 /* Determine parents */
550 if (initial_commit) {
551 reflog_msg = "commit (initial)";
554 struct commit_list *c;
555 struct commit *commit;
557 reflog_msg = "commit (amend)";
558 commit = lookup_commit(head_sha1);
559 if (!commit || parse_commit(commit))
560 die("could not parse HEAD commit");
562 for (c = commit->parents; c; c = c->next)
563 strbuf_addf(&sb, "parent %s\n",
564 sha1_to_hex(c->item->object.sha1));
565 } else if (in_merge) {
569 reflog_msg = "commit (merge)";
570 strbuf_addf(&sb, "parent %s\n", sha1_to_hex(head_sha1));
572 fp = fopen(git_path("MERGE_HEAD"), "r");
574 die("could not open %s for reading: %s",
575 git_path("MERGE_HEAD"), strerror(errno));
576 while (strbuf_getline(&m, fp, '\n') != EOF)
577 strbuf_addf(&sb, "parent %s\n", m.buf);
581 reflog_msg = "commit";
582 strbuf_addf(&sb, "parent %s\n", sha1_to_hex(head_sha1));
585 determine_author_info(&sb);
586 strbuf_addf(&sb, "committer %s\n", git_committer_info(1));
587 if (!is_encoding_utf8(git_commit_encoding))
588 strbuf_addf(&sb, "encoding %s\n", git_commit_encoding);
589 strbuf_addch(&sb, '\n');
591 /* Get the commit message and validate it */
594 fprintf(stderr, "launching editor, log %s\n", logfile);
595 launch_editor(git_path(commit_editmsg), &sb);
596 } else if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0)
597 die("could not read commit message\n");
598 if (run_hook(index_file, "commit-msg", commit_editmsg))
601 if (sb.len < header_len ||
602 message_is_empty(&sb, header_len))
603 die("* no commit message? aborting commit.");
604 strbuf_addch(&sb, '\0');
605 if (is_encoding_utf8(git_commit_encoding) && !is_utf8(sb.buf))
606 fprintf(stderr, commit_utf8_warn);
608 if (write_sha1_file(sb.buf, sb.len - 1, commit_type, commit_sha1))
609 die("failed to write commit object");
611 ref_lock = lock_any_ref_for_update("HEAD",
612 initial_commit ? NULL : head_sha1,
615 nl = strchr(sb.buf + header_len, '\n');
617 strbuf_setlen(&sb, nl + 1 - sb.buf);
619 strbuf_addch(&sb, '\n');
620 strbuf_remove(&sb, 0, header_len);
621 strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
622 strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
625 die("cannot lock HEAD ref");
626 if (write_ref_sha1(ref_lock, commit_sha1, sb.buf) < 0)
627 die("cannot update HEAD ref");
629 unlink(git_path("MERGE_HEAD"));
630 unlink(git_path("MERGE_MSG"));
632 if (lock_file.filename[0] && commit_locked_index(&lock_file))
633 die("failed to write new index");
637 run_hook(index_file, "post-commit", NULL);
640 print_summary(prefix, commit_sha1);