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
, *message
, *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
;
41 static struct option builtin_commit_options
[] = {
43 OPT__VERBOSE(&verbose
),
44 OPT_GROUP("Commit message options"),
46 OPT_STRING('F', "file", &logfile
, "FILE", "read log from file"),
47 OPT_STRING(0, "author", &force_author
, "AUTHOR", "override author for commit"),
48 OPT_STRING('m', "message", &message
, "MESSAGE", "specify commit message"),
49 OPT_STRING('c', "reedit-message", &edit_message
, "COMMIT", "reuse and edit message from specified commit "),
50 OPT_STRING('C', "reuse-message", &use_message
, "COMMIT", "reuse message from specified commit"),
51 OPT_BOOLEAN('s', "signoff", &signoff
, "add Signed-off-by: header"),
52 OPT_STRING('t', "template", &template_file
, "FILE", "use specified template file"),
53 OPT_BOOLEAN('e', "edit", &edit_flag
, "force edit of commit"),
55 OPT_GROUP("Commit contents options"),
56 OPT_BOOLEAN('a', "all", &all
, "commit all changed files"),
57 OPT_BOOLEAN('i', "include", &also
, "add specified files to index for commit"),
58 OPT_BOOLEAN(0, "interactive", &interactive
, "interactively add files"),
59 OPT_BOOLEAN('o', "only", &only
, ""),
60 OPT_BOOLEAN('n', "no-verify", &no_verify
, "bypass pre-commit hook"),
61 OPT_BOOLEAN(0, "amend", &amend
, "amend previous commit"),
62 OPT_BOOLEAN(0, "untracked-files", &untracked_files
, "show all untracked files"),
67 static char *prepare_index(const char **files
, const char *prefix
)
71 struct lock_file
*next_index_lock
;
75 return get_index_file();
78 fd
= hold_locked_index(&lock_file
, 1);
80 die("index file corrupt");
83 add_files_to_cache(verbose
, also
? prefix
: NULL
, files
);
84 refresh_cache(REFRESH_QUIET
);
85 if (write_cache(fd
, active_cache
, active_nr
) || close(fd
))
86 die("unable to write new_index file");
87 return lock_file
.filename
;
91 /* Commit index as-is. */
92 rollback_lock_file(&lock_file
);
93 return get_index_file();
96 /* update the user index file */
97 add_files_to_cache(verbose
, prefix
, files
);
98 if (write_cache(fd
, active_cache
, active_nr
) || close(fd
))
99 die("unable to write new_index file");
101 if (!initial_commit
) {
102 tree
= parse_tree_indirect(head_sha1
);
104 die("failed to unpack HEAD tree object");
105 if (read_tree(tree
, 0, NULL
))
106 die("failed to read HEAD tree object");
109 /* Use a lock file to garbage collect the temporary index file. */
110 next_index_lock
= xmalloc(sizeof(*next_index_lock
));
111 fd
= hold_lock_file_for_update(next_index_lock
,
112 git_path("next-index-%d", getpid()), 1);
113 add_files_to_cache(verbose
, prefix
, files
);
114 refresh_cache(REFRESH_QUIET
);
115 if (write_cache(fd
, active_cache
, active_nr
) || close(fd
))
116 die("unable to write new_index file");
118 return next_index_lock
->filename
;
121 static int run_status(FILE *fp
, const char *index_file
)
125 wt_status_prepare(&s
);
129 s
.reference
= "HEAD^1";
132 s
.untracked
= untracked_files
;
133 s
.index_file
= index_file
;
141 static const char sign_off_header
[] = "Signed-off-by: ";
143 static int prepare_log_message(const char *index_file
)
153 strbuf_add(&sb
, message
, strlen(message
));
154 } else if (logfile
&& !strcmp(logfile
, "-")) {
156 fprintf(stderr
, "(reading log message from standard input)\n");
157 if (strbuf_read(&sb
, 0, 0) < 0)
158 die("could not read log from standard input");
159 } else if (logfile
) {
160 if (strbuf_read_file(&sb
, logfile
, 0) < 0)
161 die("could not read log file '%s': %s",
162 logfile
, strerror(errno
));
163 } else if (use_message
) {
164 buffer
= strstr(use_message_buffer
, "\n\n");
165 if (!buffer
|| buffer
[2] == '\0')
166 die("commit has empty message");
167 strbuf_add(&sb
, buffer
+ 2, strlen(buffer
+ 2));
168 } else if (!stat(git_path("MERGE_MSG"), &statbuf
)) {
169 if (strbuf_read_file(&sb
, git_path("MERGE_MSG"), 0) < 0)
170 die("could not read MERGE_MSG: %s", strerror(errno
));
171 } else if (!stat(git_path("SQUASH_MSG"), &statbuf
)) {
172 if (strbuf_read_file(&sb
, git_path("SQUASH_MSG"), 0) < 0)
173 die("could not read SQUASH_MSG: %s", strerror(errno
));
174 } else if (template_file
&& !stat(template_file
, &statbuf
)) {
175 if (strbuf_read_file(&sb
, template_file
, 0) < 0)
176 die("could not read %s: %s",
177 template_file
, strerror(errno
));
180 fp
= fopen(git_path(commit_editmsg
), "w");
182 die("could not open %s\n", git_path(commit_editmsg
));
185 if (fwrite(sb
.buf
, 1, sb
.len
, fp
) < sb
.len
)
186 die("could not write commit template: %s\n",
190 const char *info
, *bol
;
192 info
= git_committer_info(1);
193 strbuf_addch(&sb
, '\0');
194 bol
= strrchr(sb
.buf
+ sb
.len
- 1, '\n');
195 if (!bol
|| prefixcmp(bol
, sign_off_header
))
197 fprintf(fp
, "%s%s\n", sign_off_header
, git_committer_info(1));
202 if (in_merge
&& !no_edit
)
205 "# It looks like you may be committing a MERGE.\n"
206 "# If this is not correct, please remove the file\n"
210 git_path("MERGE_HEAD"));
214 "# Please enter the commit message for your changes.\n"
215 "# (Comment lines starting with '#' will not be included)\n");
216 if (only_include_assumed
)
217 fprintf(fp
, "# %s\n", only_include_assumed
);
219 commitable
= run_status(fp
, index_file
);
227 * Find out if the message starting at position 'start' in the strbuf
228 * contains only whitespace and Signed-off-by lines.
230 static int message_is_empty(struct strbuf
*sb
, int start
)
236 /* See if the template is just a prefix of the message. */
237 strbuf_init(&tmpl
, 0);
238 if (template_file
&& strbuf_read_file(&tmpl
, template_file
, 0) > 0) {
239 stripspace(&tmpl
, 1);
240 if (start
+ tmpl
.len
<= sb
->len
&&
241 memcmp(tmpl
.buf
, sb
->buf
+ start
, tmpl
.len
) == 0)
244 strbuf_release(&tmpl
);
246 /* Check if the rest is just whitespace and Signed-of-by's. */
247 for (i
= start
; i
< sb
->len
; i
++) {
248 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
254 if (strlen(sign_off_header
) <= eol
- i
&&
255 !prefixcmp(sb
->buf
+ i
, sign_off_header
)) {
260 if (!isspace(sb
->buf
[i
++]))
267 static void determine_author_info(struct strbuf
*sb
)
269 char *name
, *email
, *date
;
271 name
= getenv("GIT_AUTHOR_NAME");
272 email
= getenv("GIT_AUTHOR_EMAIL");
273 date
= getenv("GIT_AUTHOR_DATE");
276 const char *a
, *lb
, *rb
, *eol
;
278 a
= strstr(use_message_buffer
, "\nauthor ");
280 die("invalid commit: %s\n", use_message
);
282 lb
= strstr(a
+ 8, " <");
283 rb
= strstr(a
+ 8, "> ");
284 eol
= strchr(a
+ 8, '\n');
285 if (!lb
|| !rb
|| !eol
)
286 die("invalid commit: %s\n", use_message
);
288 name
= xstrndup(a
+ 8, lb
- (a
+ 8));
289 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
290 date
= xstrndup(rb
+ 2, eol
- (rb
+ 2));
294 const char *lb
= strstr(force_author
, " <");
295 const char *rb
= strchr(force_author
, '>');
298 die("malformed --author parameter\n");
299 name
= xstrndup(force_author
, lb
- force_author
);
300 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
303 strbuf_addf(sb
, "author %s\n", fmt_ident(name
, email
, date
, 1));
306 static int parse_and_validate_options(int argc
, const char *argv
[])
310 argc
= parse_options(argc
, argv
, builtin_commit_options
,
311 builtin_commit_usage
, 0);
313 if (logfile
|| message
|| use_message
)
318 if (get_sha1("HEAD", head_sha1
))
321 if (!get_sha1("MERGE_HEAD", merge_head_sha1
))
324 /* Sanity check options */
325 if (amend
&& initial_commit
)
326 die("You have nothing to amend.");
327 if (amend
&& in_merge
)
328 die("You are in the middle of a merger -- cannot amend.");
337 die("Only one of -c/-C/-F can be used.");
338 if (message
&& f
> 0)
339 die("Option -m cannot be combined with -c/-C/-F.");
341 use_message
= edit_message
;
343 use_message
= "HEAD";
345 unsigned char sha1
[20];
346 static char utf8
[] = "UTF-8";
349 struct commit
*commit
;
351 if (get_sha1(use_message
, sha1
))
352 die("could not lookup commit %s", use_message
);
353 commit
= lookup_commit(sha1
);
354 if (!commit
|| parse_commit(commit
))
355 die("could not parse commit %s", use_message
);
357 enc
= strstr(commit
->buffer
, "\nencoding");
359 end
= strchr(enc
+ 10, '\n');
360 enc
= xstrndup(enc
+ 10, end
- (enc
+ 10));
364 out_enc
= git_commit_encoding
? git_commit_encoding
: utf8
;
366 if (strcmp(out_enc
, enc
))
368 reencode_string(commit
->buffer
, out_enc
, enc
);
371 * If we failed to reencode the buffer, just copy it
372 * byte for byte so the user can try to fix it up.
373 * This also handles the case where input and output
374 * encodings are identical.
376 if (use_message_buffer
== NULL
)
377 use_message_buffer
= xstrdup(commit
->buffer
);
382 if (!!also
+ !!only
+ !!all
+ !!interactive
> 1)
383 die("Only one of --include/--only/--all/--interactive can be used.");
384 if (argc
== 0 && (also
|| (only
&& !amend
)))
385 die("No paths with --include/--only does not make sense.");
386 if (argc
== 0 && only
&& amend
)
387 only_include_assumed
= "Clever... amending the last one with dirty index.";
388 if (argc
> 0 && !also
&& !only
) {
389 only_include_assumed
= "Explicit paths specified without -i nor -o; assuming --only paths...";
394 die("Paths with -a does not make sense.");
395 else if (interactive
&& argc
> 0)
396 die("Paths with --interactive does not make sense.");
401 int cmd_status(int argc
, const char **argv
, const char *prefix
)
403 const char *index_file
;
406 git_config(git_status_config
);
408 argc
= parse_and_validate_options(argc
, argv
);
410 index_file
= prepare_index(argv
, prefix
);
412 commitable
= run_status(stdout
, index_file
);
414 rollback_lock_file(&lock_file
);
416 return commitable
? 0 : 1;
419 static int run_hook(const char *index_file
, const char *name
, const char *arg
)
421 struct child_process hook
;
422 const char *argv
[3], *env
[2];
423 char index
[PATH_MAX
];
425 argv
[0] = git_path("hooks/%s", name
);
428 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
432 if (access(argv
[0], X_OK
) < 0)
435 memset(&hook
, 0, sizeof(hook
));
438 hook
.stdout_to_stderr
= 1;
441 return run_command(&hook
);
444 static void print_summary(const char *prefix
, const unsigned char *sha1
)
447 struct commit
*commit
;
449 commit
= lookup_commit(sha1
);
451 die("couldn't look up newly created commit\n");
452 if (!commit
|| parse_commit(commit
))
453 die("could not parse newly created commit");
455 init_revisions(&rev
, prefix
);
456 setup_revisions(0, NULL
, &rev
, NULL
);
460 rev
.diffopt
.output_format
=
461 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
463 rev
.verbose_header
= 1;
464 rev
.show_root_diff
= 1;
465 rev
.commit_format
= get_commit_format("format:%h: %s");
466 rev
.always_show_header
= 1;
468 printf("Created %scommit ", initial_commit
? "initial " : "");
470 log_tree_commit(&rev
, commit
);
473 int git_commit_config(const char *k
, const char *v
)
475 if (!strcmp(k
, "commit.template")) {
476 template_file
= xstrdup(v
);
480 return git_status_config(k
, v
);
483 static const char commit_utf8_warn
[] =
484 "Warning: commit message does not conform to UTF-8.\n"
485 "You may want to amend it after fixing the message, or set the config\n"
486 "variable i18n.commitencoding to the encoding your project uses.\n";
488 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
490 int header_len
, parent_count
= 0;
492 const char *index_file
, *reflog_msg
;
494 unsigned char commit_sha1
[20];
495 struct ref_lock
*ref_lock
;
497 git_config(git_commit_config
);
499 argc
= parse_and_validate_options(argc
, argv
);
501 index_file
= prepare_index(argv
, prefix
);
503 if (!no_verify
&& run_hook(index_file
, "pre-commit", NULL
))
506 if (!prepare_log_message(index_file
) && !in_merge
) {
507 run_status(stdout
, index_file
);
508 unlink(commit_editmsg
);
514 /* Start building up the commit header */
515 read_cache_from(index_file
);
516 active_cache_tree
= cache_tree();
517 if (cache_tree_update(active_cache_tree
,
518 active_cache
, active_nr
, 0, 0) < 0)
519 die("Error building trees");
520 strbuf_addf(&sb
, "tree %s\n",
521 sha1_to_hex(active_cache_tree
->sha1
));
523 /* Determine parents */
524 if (initial_commit
) {
525 reflog_msg
= "commit (initial)";
528 struct commit_list
*c
;
529 struct commit
*commit
;
531 reflog_msg
= "commit (amend)";
532 commit
= lookup_commit(head_sha1
);
533 if (!commit
|| parse_commit(commit
))
534 die("could not parse HEAD commit");
536 for (c
= commit
->parents
; c
; c
= c
->next
)
537 strbuf_addf(&sb
, "parent %s\n",
538 sha1_to_hex(c
->item
->object
.sha1
));
539 } else if (in_merge
) {
543 reflog_msg
= "commit (merge)";
544 strbuf_addf(&sb
, "parent %s\n", sha1_to_hex(head_sha1
));
546 fp
= fopen(git_path("MERGE_HEAD"), "r");
548 die("could not open %s for reading: %s",
549 git_path("MERGE_HEAD"), strerror(errno
));
550 while (strbuf_getline(&m
, fp
, '\n') != EOF
)
551 strbuf_addf(&sb
, "parent %s\n", m
.buf
);
555 reflog_msg
= "commit";
556 strbuf_addf(&sb
, "parent %s\n", sha1_to_hex(head_sha1
));
559 determine_author_info(&sb
);
560 strbuf_addf(&sb
, "committer %s\n", git_committer_info(1));
561 if (!is_encoding_utf8(git_commit_encoding
))
562 strbuf_addf(&sb
, "encoding %s\n", git_commit_encoding
);
563 strbuf_addch(&sb
, '\n');
565 /* Get the commit message and validate it */
568 fprintf(stderr
, "launching editor, log %s\n", logfile
);
569 launch_editor(git_path(commit_editmsg
), &sb
);
570 } else if (strbuf_read_file(&sb
, git_path(commit_editmsg
), 0) < 0)
571 die("could not read commit message\n");
572 if (run_hook(index_file
, "commit-msg", commit_editmsg
))
575 if (sb
.len
< header_len
||
576 message_is_empty(&sb
, header_len
))
577 die("* no commit message? aborting commit.");
578 strbuf_addch(&sb
, '\0');
579 if (is_encoding_utf8(git_commit_encoding
) && !is_utf8(sb
.buf
))
580 fprintf(stderr
, commit_utf8_warn
);
582 if (write_sha1_file(sb
.buf
, sb
.len
- 1, commit_type
, commit_sha1
))
583 die("failed to write commit object");
585 ref_lock
= lock_any_ref_for_update("HEAD",
586 initial_commit
? NULL
: head_sha1
,
589 nl
= strchr(sb
.buf
+ header_len
, '\n');
591 strbuf_setlen(&sb
, nl
+ 1 - sb
.buf
);
593 strbuf_addch(&sb
, '\n');
594 strbuf_remove(&sb
, 0, header_len
);
595 strbuf_insert(&sb
, 0, reflog_msg
, strlen(reflog_msg
));
596 strbuf_insert(&sb
, strlen(reflog_msg
), ": ", 2);
599 die("cannot lock HEAD ref");
600 if (write_ref_sha1(ref_lock
, commit_sha1
, sb
.buf
) < 0)
601 die("cannot update HEAD ref");
603 unlink(git_path("MERGE_HEAD"));
604 unlink(git_path("MERGE_MSG"));
606 if (lock_file
.filename
[0] && commit_locked_index(&lock_file
))
607 die("failed to write new index");
611 run_hook(index_file
, "post-commit", NULL
);
614 print_summary(prefix
, commit_sha1
);