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"
17 #include "wt-status.h"
18 #include "run-command.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>...",
32 static const char * const builtin_status_usage
[] = {
33 "git-status [options] [--] <filepattern>...",
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 */
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.
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
;
75 strbuf_setlen(buf
, 0);
77 strbuf_addstr(buf
, arg
);
78 strbuf_addch(buf
, '\n');
79 strbuf_addch(buf
, '\n');
84 static struct option builtin_commit_options
[] = {
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"),
112 static void rollback_index_files(void)
114 switch (commit_style
) {
116 break; /* nothing to do */
118 rollback_lock_file(&index_lock
);
121 rollback_lock_file(&index_lock
);
122 rollback_lock_file(&false_lock
);
127 static int commit_index_files(void)
131 switch (commit_style
) {
133 break; /* nothing to do */
135 err
= commit_lock_file(&index_lock
);
138 err
= commit_lock_file(&index_lock
);
139 rollback_lock_file(&false_lock
);
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
)
156 for (i
= 0; pattern
[i
]; i
++)
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
)
167 if (!pathspec_match(pattern
, m
, ce
->name
, 0))
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
)
178 for (i
= 0; i
< list
->nr
; i
++) {
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");
186 remove_file_from_cache(p
->path
);
190 static void create_base_index(void)
193 struct unpack_trees_options opts
;
196 if (initial_commit
) {
201 memset(&opts
, 0, sizeof(opts
));
205 opts
.src_index
= &the_index
;
206 opts
.dst_index
= &the_index
;
208 opts
.fn
= oneway_merge
;
209 tree
= parse_tree_indirect(head_sha1
);
211 die("failed to unpack HEAD tree object");
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
)
221 struct path_list partial
;
222 const char **pathspec
= NULL
;
225 interactive_add(argc
, argv
, prefix
);
226 if (read_cache() < 0)
227 die("index file corrupt");
228 commit_style
= COMMIT_AS_IS
;
229 return get_index_file();
232 if (read_cache() < 0)
233 die("index file corrupt");
236 pathspec
= get_pathspec(prefix
, argv
);
239 * Non partial, non as-is commit.
241 * (1) get the real index;
242 * (2) update the_index as necessary;
243 * (3) write the_index out to the real index (still locked);
244 * (4) return the name of the locked index file.
246 * The caller should run hooks on the locked real index, and
247 * (A) if all goes well, commit the real index;
248 * (B) on failure, rollback the real index.
250 if (all
|| (also
&& pathspec
&& *pathspec
)) {
251 int fd
= hold_locked_index(&index_lock
, 1);
252 add_files_to_cache(also
? prefix
: NULL
, pathspec
, 0);
253 refresh_cache(REFRESH_QUIET
);
254 if (write_cache(fd
, active_cache
, active_nr
) ||
255 close_lock_file(&index_lock
))
256 die("unable to write new_index file");
257 commit_style
= COMMIT_NORMAL
;
258 return index_lock
.filename
;
264 * (1) return the name of the real index file.
266 * The caller should run hooks on the real index, and run
267 * hooks on the real index, and create commit from the_index.
268 * We still need to refresh the index here.
270 if (!pathspec
|| !*pathspec
) {
271 fd
= hold_locked_index(&index_lock
, 1);
272 refresh_cache(REFRESH_QUIET
);
273 if (write_cache(fd
, active_cache
, active_nr
) ||
274 commit_locked_index(&index_lock
))
275 die("unable to write new_index file");
276 commit_style
= COMMIT_AS_IS
;
277 return get_index_file();
283 * (0) find the set of affected paths;
284 * (1) get lock on the real index file;
285 * (2) update the_index with the given paths;
286 * (3) write the_index out to the real index (still locked);
287 * (4) get lock on the false index file;
288 * (5) reset the_index from HEAD;
289 * (6) update the_index the same way as (2);
290 * (7) write the_index out to the false index file;
291 * (8) return the name of the false index file (still locked);
293 * The caller should run hooks on the locked false index, and
294 * create commit from it. Then
295 * (A) if all goes well, commit the real index;
296 * (B) on failure, rollback the real index;
297 * In either case, rollback the false index.
299 commit_style
= COMMIT_PARTIAL
;
301 if (file_exists(git_path("MERGE_HEAD")))
302 die("cannot do a partial commit during a merge.");
304 memset(&partial
, 0, sizeof(partial
));
305 partial
.strdup_paths
= 1;
306 if (list_paths(&partial
, initial_commit
? NULL
: "HEAD", prefix
, pathspec
))
310 if (read_cache() < 0)
311 die("cannot read the index");
313 fd
= hold_locked_index(&index_lock
, 1);
314 add_remove_files(&partial
);
315 refresh_cache(REFRESH_QUIET
);
316 if (write_cache(fd
, active_cache
, active_nr
) ||
317 close_lock_file(&index_lock
))
318 die("unable to write new_index file");
320 fd
= hold_lock_file_for_update(&false_lock
,
321 git_path("next-index-%d", getpid()), 1);
324 add_remove_files(&partial
);
325 refresh_cache(REFRESH_QUIET
);
327 if (write_cache(fd
, active_cache
, active_nr
) ||
328 close_lock_file(&false_lock
))
329 die("unable to write temporary index file");
332 read_cache_from(false_lock
.filename
);
334 return false_lock
.filename
;
337 static int run_status(FILE *fp
, const char *index_file
, const char *prefix
, int nowarn
)
341 wt_status_prepare(&s
);
342 if (wt_status_relative_paths
)
347 s
.reference
= "HEAD^1";
350 s
.untracked
= untracked_files
;
351 s
.index_file
= index_file
;
360 static int run_hook(const char *index_file
, const char *name
, ...)
362 struct child_process hook
;
363 const char *argv
[10], *env
[2];
364 char index
[PATH_MAX
];
368 va_start(args
, name
);
369 argv
[0] = git_path("hooks/%s", name
);
372 if (++i
>= ARRAY_SIZE(argv
))
373 die ("run_hook(): too many arguments");
374 argv
[i
] = va_arg(args
, const char *);
378 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
382 if (access(argv
[0], X_OK
) < 0)
385 memset(&hook
, 0, sizeof(hook
));
388 hook
.stdout_to_stderr
= 1;
391 return run_command(&hook
);
394 static int is_a_merge(const unsigned char *sha1
)
396 struct commit
*commit
= lookup_commit(sha1
);
397 if (!commit
|| parse_commit(commit
))
398 die("could not parse HEAD commit");
399 return !!(commit
->parents
&& commit
->parents
->next
);
402 static const char sign_off_header
[] = "Signed-off-by: ";
404 static void determine_author_info(void)
406 char *name
, *email
, *date
;
408 name
= getenv("GIT_AUTHOR_NAME");
409 email
= getenv("GIT_AUTHOR_EMAIL");
410 date
= getenv("GIT_AUTHOR_DATE");
413 const char *a
, *lb
, *rb
, *eol
;
415 a
= strstr(use_message_buffer
, "\nauthor ");
417 die("invalid commit: %s", use_message
);
419 lb
= strstr(a
+ 8, " <");
420 rb
= strstr(a
+ 8, "> ");
421 eol
= strchr(a
+ 8, '\n');
422 if (!lb
|| !rb
|| !eol
)
423 die("invalid commit: %s", use_message
);
425 name
= xstrndup(a
+ 8, lb
- (a
+ 8));
426 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
427 date
= xstrndup(rb
+ 2, eol
- (rb
+ 2));
431 const char *lb
= strstr(force_author
, " <");
432 const char *rb
= strchr(force_author
, '>');
435 die("malformed --author parameter");
436 name
= xstrndup(force_author
, lb
- force_author
);
437 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
441 author_email
= email
;
445 static int prepare_to_commit(const char *index_file
, const char *prefix
)
448 int commitable
, saved_color_setting
;
452 const char *hook_arg1
= NULL
;
453 const char *hook_arg2
= NULL
;
456 if (!no_verify
&& run_hook(index_file
, "pre-commit", NULL
))
461 strbuf_addbuf(&sb
, &message
);
462 hook_arg1
= "message";
463 } else if (logfile
&& !strcmp(logfile
, "-")) {
465 fprintf(stderr
, "(reading log message from standard input)\n");
466 if (strbuf_read(&sb
, 0, 0) < 0)
467 die("could not read log from standard input");
468 hook_arg1
= "message";
469 } else if (logfile
) {
470 if (strbuf_read_file(&sb
, logfile
, 0) < 0)
471 die("could not read log file '%s': %s",
472 logfile
, strerror(errno
));
473 hook_arg1
= "message";
474 } else if (use_message
) {
475 buffer
= strstr(use_message_buffer
, "\n\n");
476 if (!buffer
|| buffer
[2] == '\0')
477 die("commit has empty message");
478 strbuf_add(&sb
, buffer
+ 2, strlen(buffer
+ 2));
479 hook_arg1
= "commit";
480 hook_arg2
= use_message
;
481 } else if (!stat(git_path("MERGE_MSG"), &statbuf
)) {
482 if (strbuf_read_file(&sb
, git_path("MERGE_MSG"), 0) < 0)
483 die("could not read MERGE_MSG: %s", strerror(errno
));
485 } else if (!stat(git_path("SQUASH_MSG"), &statbuf
)) {
486 if (strbuf_read_file(&sb
, git_path("SQUASH_MSG"), 0) < 0)
487 die("could not read SQUASH_MSG: %s", strerror(errno
));
488 hook_arg1
= "squash";
489 } else if (template_file
&& !stat(template_file
, &statbuf
)) {
490 if (strbuf_read_file(&sb
, template_file
, 0) < 0)
491 die("could not read %s: %s",
492 template_file
, strerror(errno
));
493 hook_arg1
= "template";
497 * This final case does not modify the template message,
498 * it just sets the argument to the prepare-commit-msg hook.
503 fp
= fopen(git_path(commit_editmsg
), "w");
505 die("could not open %s: %s",
506 git_path(commit_editmsg
), strerror(errno
));
508 if (cleanup_mode
!= CLEANUP_NONE
)
515 strbuf_init(&sob
, 0);
516 strbuf_addstr(&sob
, sign_off_header
);
517 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
518 getenv("GIT_COMMITTER_EMAIL")));
519 strbuf_addch(&sob
, '\n');
520 for (i
= sb
.len
- 1; i
> 0 && sb
.buf
[i
- 1] != '\n'; i
--)
522 if (prefixcmp(sb
.buf
+ i
, sob
.buf
)) {
523 if (prefixcmp(sb
.buf
+ i
, sign_off_header
))
524 strbuf_addch(&sb
, '\n');
525 strbuf_addbuf(&sb
, &sob
);
527 strbuf_release(&sob
);
530 if (fwrite(sb
.buf
, 1, sb
.len
, fp
) < sb
.len
)
531 die("could not write commit template: %s", strerror(errno
));
535 determine_author_info();
537 /* This checks if committer ident is explicitly given */
538 git_committer_info(0);
541 const char *committer_ident
;
546 "# It looks like you may be committing a MERGE.\n"
547 "# If this is not correct, please remove the file\n"
551 git_path("MERGE_HEAD"));
555 "# Please enter the commit message for your changes.\n"
556 "# (Comment lines starting with '#' will ");
557 if (cleanup_mode
== CLEANUP_ALL
)
558 fprintf(fp
, "not be included)\n");
559 else /* CLEANUP_SPACE, that is. */
560 fprintf(fp
, "be kept.\n"
561 "# You can remove them yourself if you want to)\n");
562 if (only_include_assumed
)
563 fprintf(fp
, "# %s\n", only_include_assumed
);
565 author_ident
= xstrdup(fmt_name(author_name
, author_email
));
566 committer_ident
= fmt_name(getenv("GIT_COMMITTER_NAME"),
567 getenv("GIT_COMMITTER_EMAIL"));
568 if (strcmp(author_ident
, committer_ident
))
572 ident_shown
++ ? "" : "#\n",
576 if (!user_ident_explicitly_given
)
580 ident_shown
++ ? "" : "#\n",
586 saved_color_setting
= wt_status_use_color
;
587 wt_status_use_color
= 0;
588 commitable
= run_status(fp
, index_file
, prefix
, 1);
589 wt_status_use_color
= saved_color_setting
;
592 unsigned char sha1
[20];
593 const char *parent
= "HEAD";
595 if (!active_nr
&& read_cache() < 0)
596 die("Cannot read index");
601 if (get_sha1(parent
, sha1
))
602 commitable
= !!active_nr
;
604 init_revisions(&rev
, "");
606 setup_revisions(0, NULL
, &rev
, parent
);
607 DIFF_OPT_SET(&rev
.diffopt
, QUIET
);
608 DIFF_OPT_SET(&rev
.diffopt
, EXIT_WITH_STATUS
);
609 run_diff_index(&rev
, 1 /* cached */);
611 commitable
= !!DIFF_OPT_TST(&rev
.diffopt
, HAS_CHANGES
);
617 if (!commitable
&& !in_merge
&& !allow_empty
&&
618 !(amend
&& is_a_merge(head_sha1
))) {
619 run_status(stdout
, index_file
, prefix
, 0);
620 unlink(commit_editmsg
);
625 * Re-read the index as pre-commit hook could have updated it,
626 * and write it out as a tree. We must do this before we invoke
627 * the editor and after we invoke run_status above.
630 read_cache_from(index_file
);
631 if (!active_cache_tree
)
632 active_cache_tree
= cache_tree();
633 if (cache_tree_update(active_cache_tree
,
634 active_cache
, active_nr
, 0, 0) < 0) {
635 error("Error building trees");
639 if (run_hook(index_file
, "prepare-commit-msg",
640 git_path(commit_editmsg
), hook_arg1
, hook_arg2
, NULL
))
644 char index
[PATH_MAX
];
645 const char *env
[2] = { index
, NULL
};
646 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
647 launch_editor(git_path(commit_editmsg
), NULL
, env
);
651 run_hook(index_file
, "commit-msg", git_path(commit_editmsg
), NULL
)) {
659 * Find out if the message starting at position 'start' in the strbuf
660 * contains only whitespace and Signed-off-by lines.
662 static int message_is_empty(struct strbuf
*sb
, int start
)
668 if (cleanup_mode
== CLEANUP_NONE
&& sb
->len
)
671 /* See if the template is just a prefix of the message. */
672 strbuf_init(&tmpl
, 0);
673 if (template_file
&& strbuf_read_file(&tmpl
, template_file
, 0) > 0) {
674 stripspace(&tmpl
, cleanup_mode
== CLEANUP_ALL
);
675 if (start
+ tmpl
.len
<= sb
->len
&&
676 memcmp(tmpl
.buf
, sb
->buf
+ start
, tmpl
.len
) == 0)
679 strbuf_release(&tmpl
);
681 /* Check if the rest is just whitespace and Signed-of-by's. */
682 for (i
= start
; i
< sb
->len
; i
++) {
683 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
689 if (strlen(sign_off_header
) <= eol
- i
&&
690 !prefixcmp(sb
->buf
+ i
, sign_off_header
)) {
695 if (!isspace(sb
->buf
[i
++]))
702 static int parse_and_validate_options(int argc
, const char *argv
[],
703 const char * const usage
[])
707 argc
= parse_options(argc
, argv
, builtin_commit_options
, usage
, 0);
709 if (logfile
|| message
.len
|| use_message
)
714 setenv("GIT_EDITOR", ":", 1);
716 if (get_sha1("HEAD", head_sha1
))
719 if (!get_sha1("MERGE_HEAD", merge_head_sha1
))
722 /* Sanity check options */
723 if (amend
&& initial_commit
)
724 die("You have nothing to amend.");
725 if (amend
&& in_merge
)
726 die("You are in the middle of a merge -- cannot amend.");
735 die("Only one of -c/-C/-F can be used.");
736 if (message
.len
&& f
> 0)
737 die("Option -m cannot be combined with -c/-C/-F.");
739 use_message
= edit_message
;
740 if (amend
&& !use_message
)
741 use_message
= "HEAD";
743 unsigned char sha1
[20];
744 static char utf8
[] = "UTF-8";
747 struct commit
*commit
;
749 if (get_sha1(use_message
, sha1
))
750 die("could not lookup commit %s", use_message
);
751 commit
= lookup_commit_reference(sha1
);
752 if (!commit
|| parse_commit(commit
))
753 die("could not parse commit %s", use_message
);
755 enc
= strstr(commit
->buffer
, "\nencoding");
757 end
= strchr(enc
+ 10, '\n');
758 enc
= xstrndup(enc
+ 10, end
- (enc
+ 10));
762 out_enc
= git_commit_encoding
? git_commit_encoding
: utf8
;
764 if (strcmp(out_enc
, enc
))
766 reencode_string(commit
->buffer
, out_enc
, enc
);
769 * If we failed to reencode the buffer, just copy it
770 * byte for byte so the user can try to fix it up.
771 * This also handles the case where input and output
772 * encodings are identical.
774 if (use_message_buffer
== NULL
)
775 use_message_buffer
= xstrdup(commit
->buffer
);
780 if (!!also
+ !!only
+ !!all
+ !!interactive
> 1)
781 die("Only one of --include/--only/--all/--interactive can be used.");
782 if (argc
== 0 && (also
|| (only
&& !amend
)))
783 die("No paths with --include/--only does not make sense.");
784 if (argc
== 0 && only
&& amend
)
785 only_include_assumed
= "Clever... amending the last one with dirty index.";
786 if (argc
> 0 && !also
&& !only
)
787 only_include_assumed
= "Explicit paths specified without -i nor -o; assuming --only paths...";
788 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "default"))
789 cleanup_mode
= use_editor
? CLEANUP_ALL
: CLEANUP_SPACE
;
790 else if (!strcmp(cleanup_arg
, "verbatim"))
791 cleanup_mode
= CLEANUP_NONE
;
792 else if (!strcmp(cleanup_arg
, "whitespace"))
793 cleanup_mode
= CLEANUP_SPACE
;
794 else if (!strcmp(cleanup_arg
, "strip"))
795 cleanup_mode
= CLEANUP_ALL
;
797 die("Invalid cleanup mode %s", cleanup_arg
);
800 die("Paths with -a does not make sense.");
801 else if (interactive
&& argc
> 0)
802 die("Paths with --interactive does not make sense.");
807 int cmd_status(int argc
, const char **argv
, const char *prefix
)
809 const char *index_file
;
812 git_config(git_status_config
, NULL
);
814 if (wt_status_use_color
== -1)
815 wt_status_use_color
= git_use_color_default
;
817 argc
= parse_and_validate_options(argc
, argv
, builtin_status_usage
);
819 index_file
= prepare_index(argc
, argv
, prefix
);
821 commitable
= run_status(stdout
, index_file
, prefix
, 0);
823 rollback_index_files();
825 return commitable
? 0 : 1;
828 static void print_summary(const char *prefix
, const unsigned char *sha1
)
831 struct commit
*commit
;
833 commit
= lookup_commit(sha1
);
835 die("couldn't look up newly created commit");
836 if (!commit
|| parse_commit(commit
))
837 die("could not parse newly created commit");
839 init_revisions(&rev
, prefix
);
840 setup_revisions(0, NULL
, &rev
, NULL
);
844 rev
.diffopt
.output_format
=
845 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
847 rev
.verbose_header
= 1;
848 rev
.show_root_diff
= 1;
849 get_commit_format("format:%h: %s", &rev
);
850 rev
.always_show_header
= 0;
851 rev
.diffopt
.detect_rename
= 1;
852 rev
.diffopt
.rename_limit
= 100;
853 rev
.diffopt
.break_opt
= 0;
854 diff_setup_done(&rev
.diffopt
);
856 printf("Created %scommit ", initial_commit
? "initial " : "");
858 if (!log_tree_commit(&rev
, commit
)) {
859 struct strbuf buf
= STRBUF_INIT
;
860 format_commit_message(commit
, "%h: %s", &buf
);
861 printf("%s\n", buf
.buf
);
862 strbuf_release(&buf
);
866 int git_commit_config(const char *k
, const char *v
, void *cb
)
868 if (!strcmp(k
, "commit.template")) {
870 return config_error_nonbool(v
);
871 template_file
= xstrdup(v
);
875 return git_status_config(k
, v
, cb
);
878 static const char commit_utf8_warn
[] =
879 "Warning: commit message does not conform to UTF-8.\n"
880 "You may want to amend it after fixing the message, or set the config\n"
881 "variable i18n.commitencoding to the encoding your project uses.\n";
883 static void add_parent(struct strbuf
*sb
, const unsigned char *sha1
)
885 struct object
*obj
= parse_object(sha1
);
886 const char *parent
= sha1_to_hex(sha1
);
890 die("Unable to find commit parent %s", parent
);
891 if (obj
->type
!= OBJ_COMMIT
)
892 die("Parent %s isn't a proper commit", parent
);
894 for (cp
= sb
->buf
; cp
&& (cp
= strstr(cp
, "\nparent ")); cp
+= 8) {
895 if (!memcmp(cp
+ 8, parent
, 40) && cp
[48] == '\n') {
896 error("duplicate parent %s ignored", parent
);
900 strbuf_addf(sb
, "parent %s\n", parent
);
903 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
907 const char *index_file
, *reflog_msg
;
909 unsigned char commit_sha1
[20];
910 struct ref_lock
*ref_lock
;
912 git_config(git_commit_config
, NULL
);
914 argc
= parse_and_validate_options(argc
, argv
, builtin_commit_usage
);
916 index_file
= prepare_index(argc
, argv
, prefix
);
918 /* Set up everything for writing the commit object. This includes
919 running hooks, writing the trees, and interacting with the user. */
920 if (!prepare_to_commit(index_file
, prefix
)) {
921 rollback_index_files();
929 strbuf_addf(&sb
, "tree %s\n",
930 sha1_to_hex(active_cache_tree
->sha1
));
932 /* Determine parents */
933 if (initial_commit
) {
934 reflog_msg
= "commit (initial)";
936 struct commit_list
*c
;
937 struct commit
*commit
;
939 reflog_msg
= "commit (amend)";
940 commit
= lookup_commit(head_sha1
);
941 if (!commit
|| parse_commit(commit
))
942 die("could not parse HEAD commit");
944 for (c
= commit
->parents
; c
; c
= c
->next
)
945 add_parent(&sb
, c
->item
->object
.sha1
);
946 } else if (in_merge
) {
950 reflog_msg
= "commit (merge)";
951 add_parent(&sb
, head_sha1
);
953 fp
= fopen(git_path("MERGE_HEAD"), "r");
955 die("could not open %s for reading: %s",
956 git_path("MERGE_HEAD"), strerror(errno
));
957 while (strbuf_getline(&m
, fp
, '\n') != EOF
) {
958 unsigned char sha1
[20];
959 if (get_sha1_hex(m
.buf
, sha1
) < 0)
960 die("Corrupt MERGE_HEAD file (%s)", m
.buf
);
961 add_parent(&sb
, sha1
);
966 reflog_msg
= "commit";
967 strbuf_addf(&sb
, "parent %s\n", sha1_to_hex(head_sha1
));
970 strbuf_addf(&sb
, "author %s\n",
971 fmt_ident(author_name
, author_email
, author_date
, IDENT_ERROR_ON_NO_NAME
));
972 strbuf_addf(&sb
, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME
));
973 if (!is_encoding_utf8(git_commit_encoding
))
974 strbuf_addf(&sb
, "encoding %s\n", git_commit_encoding
);
975 strbuf_addch(&sb
, '\n');
977 /* Finally, get the commit message */
979 if (strbuf_read_file(&sb
, git_path(commit_editmsg
), 0) < 0) {
980 rollback_index_files();
981 die("could not read commit message");
984 /* Truncate the message just before the diff, if any. */
985 p
= strstr(sb
.buf
, "\ndiff --git a/");
987 strbuf_setlen(&sb
, p
- sb
.buf
+ 1);
989 if (cleanup_mode
!= CLEANUP_NONE
)
990 stripspace(&sb
, cleanup_mode
== CLEANUP_ALL
);
991 if (sb
.len
< header_len
|| message_is_empty(&sb
, header_len
)) {
992 rollback_index_files();
993 die("no commit message? aborting commit.");
995 strbuf_addch(&sb
, '\0');
996 if (is_encoding_utf8(git_commit_encoding
) && !is_utf8(sb
.buf
))
997 fprintf(stderr
, commit_utf8_warn
);
999 if (write_sha1_file(sb
.buf
, sb
.len
- 1, commit_type
, commit_sha1
)) {
1000 rollback_index_files();
1001 die("failed to write commit object");
1004 ref_lock
= lock_any_ref_for_update("HEAD",
1005 initial_commit
? NULL
: head_sha1
,
1008 nl
= strchr(sb
.buf
+ header_len
, '\n');
1010 strbuf_setlen(&sb
, nl
+ 1 - sb
.buf
);
1012 strbuf_addch(&sb
, '\n');
1013 strbuf_remove(&sb
, 0, header_len
);
1014 strbuf_insert(&sb
, 0, reflog_msg
, strlen(reflog_msg
));
1015 strbuf_insert(&sb
, strlen(reflog_msg
), ": ", 2);
1018 rollback_index_files();
1019 die("cannot lock HEAD ref");
1021 if (write_ref_sha1(ref_lock
, commit_sha1
, sb
.buf
) < 0) {
1022 rollback_index_files();
1023 die("cannot update HEAD ref");
1026 unlink(git_path("MERGE_HEAD"));
1027 unlink(git_path("MERGE_MSG"));
1028 unlink(git_path("SQUASH_MSG"));
1030 if (commit_index_files())
1031 die ("Repository has been updated, but unable to write\n"
1032 "new_index file. Check that disk is not full or quota is\n"
1033 "not exceeded, and then \"git reset HEAD\" to recover.");
1036 run_hook(get_index_file(), "post-commit", NULL
);
1038 print_summary(prefix
, commit_sha1
);