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"
16 #include "wt-status.h"
17 #include "run-command.h"
22 #include "parse-options.h"
23 #include "path-list.h"
25 static const char * const builtin_commit_usage
[] = {
26 "git-commit [options] [--] <filepattern>...",
30 static unsigned char head_sha1
[20], merge_head_sha1
[20];
31 static char *use_message_buffer
;
32 static const char commit_editmsg
[] = "COMMIT_EDITMSG";
33 static struct lock_file index_lock
; /* real index */
34 static struct lock_file false_lock
; /* used only for partial commits */
41 static char *logfile
, *force_author
, *template_file
;
42 static char *edit_message
, *use_message
;
43 static int all
, edit_flag
, also
, interactive
, only
, amend
, signoff
;
44 static int quiet
, verbose
, untracked_files
, no_verify
;
46 static int no_edit
, initial_commit
, in_merge
;
47 const char *only_include_assumed
;
48 struct strbuf message
;
50 static int opt_parse_m(const struct option
*opt
, const char *arg
, int unset
)
52 struct strbuf
*buf
= opt
->value
;
54 strbuf_setlen(buf
, 0);
56 strbuf_addstr(buf
, arg
);
57 strbuf_addch(buf
, '\n');
58 strbuf_addch(buf
, '\n');
63 static struct option builtin_commit_options
[] = {
65 OPT__VERBOSE(&verbose
),
66 OPT_GROUP("Commit message options"),
68 OPT_STRING('F', "file", &logfile
, "FILE", "read log from file"),
69 OPT_STRING(0, "author", &force_author
, "AUTHOR", "override author for commit"),
70 OPT_CALLBACK('m', "message", &message
, "MESSAGE", "specify commit message", opt_parse_m
),
71 OPT_STRING('c', "reedit-message", &edit_message
, "COMMIT", "reuse and edit message from specified commit "),
72 OPT_STRING('C', "reuse-message", &use_message
, "COMMIT", "reuse message from specified commit"),
73 OPT_BOOLEAN('s', "signoff", &signoff
, "add Signed-off-by: header"),
74 OPT_STRING('t', "template", &template_file
, "FILE", "use specified template file"),
75 OPT_BOOLEAN('e', "edit", &edit_flag
, "force edit of commit"),
77 OPT_GROUP("Commit contents options"),
78 OPT_BOOLEAN('a', "all", &all
, "commit all changed files"),
79 OPT_BOOLEAN('i', "include", &also
, "add specified files to index for commit"),
80 OPT_BOOLEAN(0, "interactive", &interactive
, "interactively add files"),
81 OPT_BOOLEAN('o', "only", &only
, ""),
82 OPT_BOOLEAN('n', "no-verify", &no_verify
, "bypass pre-commit hook"),
83 OPT_BOOLEAN(0, "amend", &amend
, "amend previous commit"),
84 OPT_BOOLEAN(0, "untracked-files", &untracked_files
, "show all untracked files"),
89 static void rollback_index_files(void)
91 switch (commit_style
) {
93 break; /* nothing to do */
95 rollback_lock_file(&index_lock
);
98 rollback_lock_file(&index_lock
);
99 rollback_lock_file(&false_lock
);
104 static void commit_index_files(void)
106 switch (commit_style
) {
108 break; /* nothing to do */
110 commit_lock_file(&index_lock
);
113 commit_lock_file(&index_lock
);
114 rollback_lock_file(&false_lock
);
120 * Take a union of paths in the index and the named tree (typically, "HEAD"),
121 * and return the paths that match the given pattern in list.
123 static int list_paths(struct path_list
*list
, const char *with_tree
,
124 const char *prefix
, const char **pattern
)
129 for (i
= 0; pattern
[i
]; i
++)
134 overlay_tree_on_cache(with_tree
, prefix
);
136 for (i
= 0; i
< active_nr
; i
++) {
137 struct cache_entry
*ce
= active_cache
[i
];
138 if (ce
->ce_flags
& htons(CE_UPDATE
))
140 if (!pathspec_match(pattern
, m
, ce
->name
, 0))
142 path_list_insert(ce
->name
, list
);
145 return report_path_error(m
, pattern
, prefix
? strlen(prefix
) : 0);
148 static void add_remove_files(struct path_list
*list
)
151 for (i
= 0; i
< list
->nr
; i
++) {
152 struct path_list_item
*p
= &(list
->items
[i
]);
153 if (file_exists(p
->path
))
154 add_file_to_cache(p
->path
, 0);
156 remove_file_from_cache(p
->path
);
160 static char *prepare_index(int argc
, const char **argv
, const char *prefix
)
164 struct path_list partial
;
165 const char **pathspec
= NULL
;
168 interactive_add(argc
, argv
, prefix
);
169 commit_style
= COMMIT_AS_IS
;
170 return get_index_file();
173 if (read_cache() < 0)
174 die("index file corrupt");
177 pathspec
= get_pathspec(prefix
, argv
);
180 * Non partial, non as-is commit.
182 * (1) get the real index;
183 * (2) update the_index as necessary;
184 * (3) write the_index out to the real index (still locked);
185 * (4) return the name of the locked index file.
187 * The caller should run hooks on the locked real index, and
188 * (A) if all goes well, commit the real index;
189 * (B) on failure, rollback the real index.
191 if (all
|| (also
&& pathspec
&& *pathspec
)) {
192 int fd
= hold_locked_index(&index_lock
, 1);
193 add_files_to_cache(0, also
? prefix
: NULL
, pathspec
);
194 refresh_cache(REFRESH_QUIET
);
195 if (write_cache(fd
, active_cache
, active_nr
) || close(fd
))
196 die("unable to write new_index file");
197 commit_style
= COMMIT_NORMAL
;
198 return index_lock
.filename
;
204 * (1) return the name of the real index file.
206 * The caller should run hooks on the real index, and run
207 * hooks on the real index, and create commit from the_index.
208 * We still need to refresh the index here.
210 if (!pathspec
|| !*pathspec
) {
211 fd
= hold_locked_index(&index_lock
, 1);
212 refresh_cache(REFRESH_QUIET
);
213 if (write_cache(fd
, active_cache
, active_nr
) ||
214 close(fd
) || commit_locked_index(&index_lock
))
215 die("unable to write new_index file");
216 commit_style
= COMMIT_AS_IS
;
217 return get_index_file();
223 * (0) find the set of affected paths;
224 * (1) get lock on the real index file;
225 * (2) update the_index with the given paths;
226 * (3) write the_index out to the real index (still locked);
227 * (4) get lock on the false index file;
228 * (5) reset the_index from HEAD;
229 * (6) update the_index the same way as (2);
230 * (7) write the_index out to the false index file;
231 * (8) return the name of the false index file (still locked);
233 * The caller should run hooks on the locked false index, and
234 * create commit from it. Then
235 * (A) if all goes well, commit the real index;
236 * (B) on failure, rollback the real index;
237 * In either case, rollback the false index.
239 commit_style
= COMMIT_PARTIAL
;
241 if (file_exists(git_path("MERGE_HEAD")))
242 die("cannot do a partial commit during a merge.");
244 memset(&partial
, 0, sizeof(partial
));
245 partial
.strdup_paths
= 1;
246 if (list_paths(&partial
, initial_commit
? NULL
: "HEAD", prefix
, pathspec
))
250 if (read_cache() < 0)
251 die("cannot read the index");
253 fd
= hold_locked_index(&index_lock
, 1);
254 add_remove_files(&partial
);
255 refresh_cache(REFRESH_QUIET
);
256 if (write_cache(fd
, active_cache
, active_nr
) || close(fd
))
257 die("unable to write new_index file");
259 fd
= hold_lock_file_for_update(&false_lock
,
260 git_path("next-index-%d", getpid()), 1);
262 if (!initial_commit
) {
263 tree
= parse_tree_indirect(head_sha1
);
265 die("failed to unpack HEAD tree object");
266 if (read_tree(tree
, 0, NULL
))
267 die("failed to read HEAD tree object");
269 add_remove_files(&partial
);
270 refresh_cache(REFRESH_QUIET
);
272 if (write_cache(fd
, active_cache
, active_nr
) || close(fd
))
273 die("unable to write temporary index file");
274 return false_lock
.filename
;
277 static int run_status(FILE *fp
, const char *index_file
, const char *prefix
)
281 wt_status_prepare(&s
);
286 s
.reference
= "HEAD^1";
289 s
.untracked
= untracked_files
;
290 s
.index_file
= index_file
;
298 static const char sign_off_header
[] = "Signed-off-by: ";
300 static int prepare_log_message(const char *index_file
, const char *prefix
)
303 int commitable
, saved_color_setting
;
310 strbuf_addbuf(&sb
, &message
);
311 } else if (logfile
&& !strcmp(logfile
, "-")) {
313 fprintf(stderr
, "(reading log message from standard input)\n");
314 if (strbuf_read(&sb
, 0, 0) < 0)
315 die("could not read log from standard input");
316 } else if (logfile
) {
317 if (strbuf_read_file(&sb
, logfile
, 0) < 0)
318 die("could not read log file '%s': %s",
319 logfile
, strerror(errno
));
320 } else if (use_message
) {
321 buffer
= strstr(use_message_buffer
, "\n\n");
322 if (!buffer
|| buffer
[2] == '\0')
323 die("commit has empty message");
324 strbuf_add(&sb
, buffer
+ 2, strlen(buffer
+ 2));
325 } else if (!stat(git_path("MERGE_MSG"), &statbuf
)) {
326 if (strbuf_read_file(&sb
, git_path("MERGE_MSG"), 0) < 0)
327 die("could not read MERGE_MSG: %s", strerror(errno
));
328 } else if (!stat(git_path("SQUASH_MSG"), &statbuf
)) {
329 if (strbuf_read_file(&sb
, git_path("SQUASH_MSG"), 0) < 0)
330 die("could not read SQUASH_MSG: %s", strerror(errno
));
331 } else if (template_file
&& !stat(template_file
, &statbuf
)) {
332 if (strbuf_read_file(&sb
, template_file
, 0) < 0)
333 die("could not read %s: %s",
334 template_file
, strerror(errno
));
337 fp
= fopen(git_path(commit_editmsg
), "w");
339 die("could not open %s\n", git_path(commit_editmsg
));
347 strbuf_init(&sob
, 0);
348 strbuf_addstr(&sob
, sign_off_header
);
349 strbuf_addstr(&sob
, fmt_ident(getenv("GIT_COMMITTER_NAME"),
350 getenv("GIT_COMMITTER_EMAIL"),
352 strbuf_addch(&sob
, '\n');
354 for (i
= sb
.len
- 1; i
> 0 && sb
.buf
[i
- 1] != '\n'; i
--)
356 if (prefixcmp(sb
.buf
+ i
, sob
.buf
)) {
357 if (prefixcmp(sb
.buf
+ i
, sign_off_header
))
358 strbuf_addch(&sb
, '\n');
359 strbuf_addbuf(&sb
, &sob
);
361 strbuf_release(&sob
);
364 if (fwrite(sb
.buf
, 1, sb
.len
, fp
) < sb
.len
)
365 die("could not write commit template: %s\n",
370 if (in_merge
&& !no_edit
)
373 "# It looks like you may be committing a MERGE.\n"
374 "# If this is not correct, please remove the file\n"
378 git_path("MERGE_HEAD"));
382 "# Please enter the commit message for your changes.\n"
383 "# (Comment lines starting with '#' will not be included)\n");
384 if (only_include_assumed
)
385 fprintf(fp
, "# %s\n", only_include_assumed
);
387 saved_color_setting
= wt_status_use_color
;
388 wt_status_use_color
= 0;
389 commitable
= run_status(fp
, index_file
, prefix
);
390 wt_status_use_color
= saved_color_setting
;
398 * Find out if the message starting at position 'start' in the strbuf
399 * contains only whitespace and Signed-off-by lines.
401 static int message_is_empty(struct strbuf
*sb
, int start
)
407 /* See if the template is just a prefix of the message. */
408 strbuf_init(&tmpl
, 0);
409 if (template_file
&& strbuf_read_file(&tmpl
, template_file
, 0) > 0) {
410 stripspace(&tmpl
, 1);
411 if (start
+ tmpl
.len
<= sb
->len
&&
412 memcmp(tmpl
.buf
, sb
->buf
+ start
, tmpl
.len
) == 0)
415 strbuf_release(&tmpl
);
417 /* Check if the rest is just whitespace and Signed-of-by's. */
418 for (i
= start
; i
< sb
->len
; i
++) {
419 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
425 if (strlen(sign_off_header
) <= eol
- i
&&
426 !prefixcmp(sb
->buf
+ i
, sign_off_header
)) {
431 if (!isspace(sb
->buf
[i
++]))
438 static void determine_author_info(struct strbuf
*sb
)
440 char *name
, *email
, *date
;
442 name
= getenv("GIT_AUTHOR_NAME");
443 email
= getenv("GIT_AUTHOR_EMAIL");
444 date
= getenv("GIT_AUTHOR_DATE");
447 const char *a
, *lb
, *rb
, *eol
;
449 a
= strstr(use_message_buffer
, "\nauthor ");
451 die("invalid commit: %s\n", use_message
);
453 lb
= strstr(a
+ 8, " <");
454 rb
= strstr(a
+ 8, "> ");
455 eol
= strchr(a
+ 8, '\n');
456 if (!lb
|| !rb
|| !eol
)
457 die("invalid commit: %s\n", use_message
);
459 name
= xstrndup(a
+ 8, lb
- (a
+ 8));
460 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
461 date
= xstrndup(rb
+ 2, eol
- (rb
+ 2));
465 const char *lb
= strstr(force_author
, " <");
466 const char *rb
= strchr(force_author
, '>');
469 die("malformed --author parameter\n");
470 name
= xstrndup(force_author
, lb
- force_author
);
471 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
474 strbuf_addf(sb
, "author %s\n", fmt_ident(name
, email
, date
, 1));
477 static int parse_and_validate_options(int argc
, const char *argv
[])
481 argc
= parse_options(argc
, argv
, builtin_commit_options
,
482 builtin_commit_usage
, 0);
484 if (logfile
|| message
.len
|| use_message
)
489 if (get_sha1("HEAD", head_sha1
))
492 if (!get_sha1("MERGE_HEAD", merge_head_sha1
))
495 /* Sanity check options */
496 if (amend
&& initial_commit
)
497 die("You have nothing to amend.");
498 if (amend
&& in_merge
)
499 die("You are in the middle of a merger -- cannot amend.");
508 die("Only one of -c/-C/-F can be used.");
509 if (message
.len
&& f
> 0)
510 die("Option -m cannot be combined with -c/-C/-F.");
512 use_message
= edit_message
;
514 use_message
= "HEAD";
516 unsigned char sha1
[20];
517 static char utf8
[] = "UTF-8";
520 struct commit
*commit
;
522 if (get_sha1(use_message
, sha1
))
523 die("could not lookup commit %s", use_message
);
524 commit
= lookup_commit(sha1
);
525 if (!commit
|| parse_commit(commit
))
526 die("could not parse commit %s", use_message
);
528 enc
= strstr(commit
->buffer
, "\nencoding");
530 end
= strchr(enc
+ 10, '\n');
531 enc
= xstrndup(enc
+ 10, end
- (enc
+ 10));
535 out_enc
= git_commit_encoding
? git_commit_encoding
: utf8
;
537 if (strcmp(out_enc
, enc
))
539 reencode_string(commit
->buffer
, out_enc
, enc
);
542 * If we failed to reencode the buffer, just copy it
543 * byte for byte so the user can try to fix it up.
544 * This also handles the case where input and output
545 * encodings are identical.
547 if (use_message_buffer
== NULL
)
548 use_message_buffer
= xstrdup(commit
->buffer
);
553 if (!!also
+ !!only
+ !!all
+ !!interactive
> 1)
554 die("Only one of --include/--only/--all/--interactive can be used.");
555 if (argc
== 0 && (also
|| (only
&& !amend
)))
556 die("No paths with --include/--only does not make sense.");
557 if (argc
== 0 && only
&& amend
)
558 only_include_assumed
= "Clever... amending the last one with dirty index.";
559 if (argc
> 0 && !also
&& !only
) {
560 only_include_assumed
= "Explicit paths specified without -i nor -o; assuming --only paths...";
565 die("Paths with -a does not make sense.");
566 else if (interactive
&& argc
> 0)
567 die("Paths with --interactive does not make sense.");
572 int cmd_status(int argc
, const char **argv
, const char *prefix
)
574 const char *index_file
;
577 git_config(git_status_config
);
579 argc
= parse_and_validate_options(argc
, argv
);
581 index_file
= prepare_index(argc
, argv
, prefix
);
583 commitable
= run_status(stdout
, index_file
, prefix
);
585 rollback_index_files();
587 return commitable
? 0 : 1;
590 static int run_hook(const char *index_file
, const char *name
, const char *arg
)
592 struct child_process hook
;
593 const char *argv
[3], *env
[2];
594 char index
[PATH_MAX
];
596 argv
[0] = git_path("hooks/%s", name
);
599 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
603 if (access(argv
[0], X_OK
) < 0)
606 memset(&hook
, 0, sizeof(hook
));
609 hook
.stdout_to_stderr
= 1;
612 return run_command(&hook
);
615 static void print_summary(const char *prefix
, const unsigned char *sha1
)
618 struct commit
*commit
;
620 commit
= lookup_commit(sha1
);
622 die("couldn't look up newly created commit\n");
623 if (!commit
|| parse_commit(commit
))
624 die("could not parse newly created commit");
626 init_revisions(&rev
, prefix
);
627 setup_revisions(0, NULL
, &rev
, NULL
);
631 rev
.diffopt
.output_format
=
632 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
634 rev
.verbose_header
= 1;
635 rev
.show_root_diff
= 1;
636 rev
.commit_format
= get_commit_format("format:%h: %s");
637 rev
.always_show_header
= 1;
639 printf("Created %scommit ", initial_commit
? "initial " : "");
641 log_tree_commit(&rev
, commit
);
645 int git_commit_config(const char *k
, const char *v
)
647 if (!strcmp(k
, "commit.template")) {
648 template_file
= xstrdup(v
);
652 return git_status_config(k
, v
);
655 static const char commit_utf8_warn
[] =
656 "Warning: commit message does not conform to UTF-8.\n"
657 "You may want to amend it after fixing the message, or set the config\n"
658 "variable i18n.commitencoding to the encoding your project uses.\n";
660 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
664 const char *index_file
, *reflog_msg
;
666 unsigned char commit_sha1
[20];
667 struct ref_lock
*ref_lock
;
669 git_config(git_commit_config
);
671 argc
= parse_and_validate_options(argc
, argv
);
673 index_file
= prepare_index(argc
, argv
, prefix
);
675 if (!no_verify
&& run_hook(index_file
, "pre-commit", NULL
)) {
676 rollback_index_files();
680 if (!prepare_log_message(index_file
, prefix
) && !in_merge
) {
681 run_status(stdout
, index_file
, prefix
);
682 rollback_index_files();
683 unlink(commit_editmsg
);
688 * Re-read the index as pre-commit hook could have updated it,
689 * and write it out as a tree.
692 read_cache_from(index_file
);
693 if (!active_cache_tree
)
694 active_cache_tree
= cache_tree();
695 if (cache_tree_update(active_cache_tree
,
696 active_cache
, active_nr
, 0, 0) < 0) {
697 rollback_index_files();
698 die("Error building trees");
705 strbuf_addf(&sb
, "tree %s\n",
706 sha1_to_hex(active_cache_tree
->sha1
));
708 /* Determine parents */
709 if (initial_commit
) {
710 reflog_msg
= "commit (initial)";
712 struct commit_list
*c
;
713 struct commit
*commit
;
715 reflog_msg
= "commit (amend)";
716 commit
= lookup_commit(head_sha1
);
717 if (!commit
|| parse_commit(commit
))
718 die("could not parse HEAD commit");
720 for (c
= commit
->parents
; c
; c
= c
->next
)
721 strbuf_addf(&sb
, "parent %s\n",
722 sha1_to_hex(c
->item
->object
.sha1
));
723 } else if (in_merge
) {
727 reflog_msg
= "commit (merge)";
728 strbuf_addf(&sb
, "parent %s\n", sha1_to_hex(head_sha1
));
730 fp
= fopen(git_path("MERGE_HEAD"), "r");
732 die("could not open %s for reading: %s",
733 git_path("MERGE_HEAD"), strerror(errno
));
734 while (strbuf_getline(&m
, fp
, '\n') != EOF
)
735 strbuf_addf(&sb
, "parent %s\n", m
.buf
);
739 reflog_msg
= "commit";
740 strbuf_addf(&sb
, "parent %s\n", sha1_to_hex(head_sha1
));
743 determine_author_info(&sb
);
744 strbuf_addf(&sb
, "committer %s\n", git_committer_info(1));
745 if (!is_encoding_utf8(git_commit_encoding
))
746 strbuf_addf(&sb
, "encoding %s\n", git_commit_encoding
);
747 strbuf_addch(&sb
, '\n');
749 /* Get the commit message and validate it */
752 launch_editor(git_path(commit_editmsg
), &sb
);
753 else if (strbuf_read_file(&sb
, git_path(commit_editmsg
), 0) < 0) {
754 rollback_index_files();
755 die("could not read commit message\n");
757 if (run_hook(index_file
, "commit-msg", git_path(commit_editmsg
))) {
758 rollback_index_files();
762 /* Truncate the message just before the diff, if any. */
763 p
= strstr(sb
.buf
, "\ndiff --git a/");
765 strbuf_setlen(&sb
, p
- sb
.buf
);
768 if (sb
.len
< header_len
|| message_is_empty(&sb
, header_len
)) {
769 rollback_index_files();
770 die("* no commit message? aborting commit.");
772 strbuf_addch(&sb
, '\0');
773 if (is_encoding_utf8(git_commit_encoding
) && !is_utf8(sb
.buf
))
774 fprintf(stderr
, commit_utf8_warn
);
776 if (write_sha1_file(sb
.buf
, sb
.len
- 1, commit_type
, commit_sha1
)) {
777 rollback_index_files();
778 die("failed to write commit object");
781 ref_lock
= lock_any_ref_for_update("HEAD",
782 initial_commit
? NULL
: head_sha1
,
785 nl
= strchr(sb
.buf
+ header_len
, '\n');
787 strbuf_setlen(&sb
, nl
+ 1 - sb
.buf
);
789 strbuf_addch(&sb
, '\n');
790 strbuf_remove(&sb
, 0, header_len
);
791 strbuf_insert(&sb
, 0, reflog_msg
, strlen(reflog_msg
));
792 strbuf_insert(&sb
, strlen(reflog_msg
), ": ", 2);
795 rollback_index_files();
796 die("cannot lock HEAD ref");
798 if (write_ref_sha1(ref_lock
, commit_sha1
, sb
.buf
) < 0) {
799 rollback_index_files();
800 die("cannot update HEAD ref");
803 unlink(git_path("MERGE_HEAD"));
804 unlink(git_path("MERGE_MSG"));
806 commit_index_files();
809 run_hook(get_index_file(), "post-commit", NULL
);
811 print_summary(prefix
, commit_sha1
);