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"
24 #include "unpack-trees.h"
26 static const char * const builtin_commit_usage
[] = {
27 "git-commit [options] [--] <filepattern>...",
31 static const char * const builtin_status_usage
[] = {
32 "git-status [options] [--] <filepattern>...",
36 static unsigned char head_sha1
[20], merge_head_sha1
[20];
37 static char *use_message_buffer
;
38 static const char commit_editmsg
[] = "COMMIT_EDITMSG";
39 static struct lock_file index_lock
; /* real index */
40 static struct lock_file false_lock
; /* used only for partial commits */
47 static char *logfile
, *force_author
, *template_file
;
48 static char *edit_message
, *use_message
;
49 static int all
, edit_flag
, also
, interactive
, only
, amend
, signoff
;
50 static int quiet
, verbose
, untracked_files
, no_verify
, allow_empty
;
52 * The default commit message cleanup mode will remove the lines
53 * beginning with # (shell comments) and leading and trailing
54 * whitespaces (empty lines or containing only whitespaces)
55 * if editor is used, and only the whitespaces if the message
56 * is specified explicitly.
63 static char *cleanup_arg
;
65 static int use_editor
= 1, initial_commit
, in_merge
;
66 const char *only_include_assumed
;
67 struct strbuf message
;
69 static int opt_parse_m(const struct option
*opt
, const char *arg
, int unset
)
71 struct strbuf
*buf
= opt
->value
;
73 strbuf_setlen(buf
, 0);
75 strbuf_addstr(buf
, arg
);
76 strbuf_addch(buf
, '\n');
77 strbuf_addch(buf
, '\n');
82 static struct option builtin_commit_options
[] = {
84 OPT__VERBOSE(&verbose
),
85 OPT_GROUP("Commit message options"),
87 OPT_STRING('F', "file", &logfile
, "FILE", "read log from file"),
88 OPT_STRING(0, "author", &force_author
, "AUTHOR", "override author for commit"),
89 OPT_CALLBACK('m', "message", &message
, "MESSAGE", "specify commit message", opt_parse_m
),
90 OPT_STRING('c', "reedit-message", &edit_message
, "COMMIT", "reuse and edit message from specified commit "),
91 OPT_STRING('C', "reuse-message", &use_message
, "COMMIT", "reuse message from specified commit"),
92 OPT_BOOLEAN('s', "signoff", &signoff
, "add Signed-off-by: header"),
93 OPT_STRING('t', "template", &template_file
, "FILE", "use specified template file"),
94 OPT_BOOLEAN('e', "edit", &edit_flag
, "force edit of commit"),
96 OPT_GROUP("Commit contents options"),
97 OPT_BOOLEAN('a', "all", &all
, "commit all changed files"),
98 OPT_BOOLEAN('i', "include", &also
, "add specified files to index for commit"),
99 OPT_BOOLEAN(0, "interactive", &interactive
, "interactively add files"),
100 OPT_BOOLEAN('o', "only", &only
, ""),
101 OPT_BOOLEAN('n', "no-verify", &no_verify
, "bypass pre-commit hook"),
102 OPT_BOOLEAN(0, "amend", &amend
, "amend previous commit"),
103 OPT_BOOLEAN(0, "untracked-files", &untracked_files
, "show all untracked files"),
104 OPT_BOOLEAN(0, "allow-empty", &allow_empty
, "ok to record an empty change"),
105 OPT_STRING(0, "cleanup", &cleanup_arg
, "default", "how to strip spaces and #comments from message"),
110 static void rollback_index_files(void)
112 switch (commit_style
) {
114 break; /* nothing to do */
116 rollback_lock_file(&index_lock
);
119 rollback_lock_file(&index_lock
);
120 rollback_lock_file(&false_lock
);
125 static int commit_index_files(void)
129 switch (commit_style
) {
131 break; /* nothing to do */
133 err
= commit_lock_file(&index_lock
);
136 err
= commit_lock_file(&index_lock
);
137 rollback_lock_file(&false_lock
);
145 * Take a union of paths in the index and the named tree (typically, "HEAD"),
146 * and return the paths that match the given pattern in list.
148 static int list_paths(struct path_list
*list
, const char *with_tree
,
149 const char *prefix
, const char **pattern
)
154 for (i
= 0; pattern
[i
]; i
++)
159 overlay_tree_on_cache(with_tree
, prefix
);
161 for (i
= 0; i
< active_nr
; i
++) {
162 struct cache_entry
*ce
= active_cache
[i
];
163 if (ce
->ce_flags
& htons(CE_UPDATE
))
165 if (!pathspec_match(pattern
, m
, ce
->name
, 0))
167 path_list_insert(ce
->name
, list
);
170 return report_path_error(m
, pattern
, prefix
? strlen(prefix
) : 0);
173 static void add_remove_files(struct path_list
*list
)
176 for (i
= 0; i
< list
->nr
; i
++) {
177 struct path_list_item
*p
= &(list
->items
[i
]);
178 if (file_exists(p
->path
))
179 add_file_to_cache(p
->path
, 0);
181 remove_file_from_cache(p
->path
);
185 static void create_base_index(void)
188 struct unpack_trees_options opts
;
191 if (initial_commit
) {
196 memset(&opts
, 0, sizeof(opts
));
201 opts
.fn
= oneway_merge
;
202 tree
= parse_tree_indirect(head_sha1
);
204 die("failed to unpack HEAD tree object");
206 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
207 unpack_trees(1, &t
, &opts
);
210 static char *prepare_index(int argc
, const char **argv
, const char *prefix
)
213 struct path_list partial
;
214 const char **pathspec
= NULL
;
217 interactive_add(argc
, argv
, prefix
);
218 commit_style
= COMMIT_AS_IS
;
219 return get_index_file();
222 if (read_cache() < 0)
223 die("index file corrupt");
226 pathspec
= get_pathspec(prefix
, argv
);
229 * Non partial, non as-is commit.
231 * (1) get the real index;
232 * (2) update the_index as necessary;
233 * (3) write the_index out to the real index (still locked);
234 * (4) return the name of the locked index file.
236 * The caller should run hooks on the locked real index, and
237 * (A) if all goes well, commit the real index;
238 * (B) on failure, rollback the real index.
240 if (all
|| (also
&& pathspec
&& *pathspec
)) {
241 int fd
= hold_locked_index(&index_lock
, 1);
242 add_files_to_cache(0, also
? prefix
: NULL
, pathspec
);
243 refresh_cache(REFRESH_QUIET
);
244 if (write_cache(fd
, active_cache
, active_nr
) ||
245 close_lock_file(&index_lock
))
246 die("unable to write new_index file");
247 commit_style
= COMMIT_NORMAL
;
248 return index_lock
.filename
;
254 * (1) return the name of the real index file.
256 * The caller should run hooks on the real index, and run
257 * hooks on the real index, and create commit from the_index.
258 * We still need to refresh the index here.
260 if (!pathspec
|| !*pathspec
) {
261 fd
= hold_locked_index(&index_lock
, 1);
262 refresh_cache(REFRESH_QUIET
);
263 if (write_cache(fd
, active_cache
, active_nr
) ||
264 commit_locked_index(&index_lock
))
265 die("unable to write new_index file");
266 commit_style
= COMMIT_AS_IS
;
267 return get_index_file();
273 * (0) find the set of affected paths;
274 * (1) get lock on the real index file;
275 * (2) update the_index with the given paths;
276 * (3) write the_index out to the real index (still locked);
277 * (4) get lock on the false index file;
278 * (5) reset the_index from HEAD;
279 * (6) update the_index the same way as (2);
280 * (7) write the_index out to the false index file;
281 * (8) return the name of the false index file (still locked);
283 * The caller should run hooks on the locked false index, and
284 * create commit from it. Then
285 * (A) if all goes well, commit the real index;
286 * (B) on failure, rollback the real index;
287 * In either case, rollback the false index.
289 commit_style
= COMMIT_PARTIAL
;
291 if (file_exists(git_path("MERGE_HEAD")))
292 die("cannot do a partial commit during a merge.");
294 memset(&partial
, 0, sizeof(partial
));
295 partial
.strdup_paths
= 1;
296 if (list_paths(&partial
, initial_commit
? NULL
: "HEAD", prefix
, pathspec
))
300 if (read_cache() < 0)
301 die("cannot read the index");
303 fd
= hold_locked_index(&index_lock
, 1);
304 add_remove_files(&partial
);
305 refresh_cache(REFRESH_QUIET
);
306 if (write_cache(fd
, active_cache
, active_nr
) ||
307 close_lock_file(&index_lock
))
308 die("unable to write new_index file");
310 fd
= hold_lock_file_for_update(&false_lock
,
311 git_path("next-index-%d", getpid()), 1);
314 add_remove_files(&partial
);
315 refresh_cache(REFRESH_QUIET
);
317 if (write_cache(fd
, active_cache
, active_nr
) ||
318 close_lock_file(&false_lock
))
319 die("unable to write temporary index file");
320 return false_lock
.filename
;
323 static int run_status(FILE *fp
, const char *index_file
, const char *prefix
, int nowarn
)
327 wt_status_prepare(&s
);
328 if (wt_status_relative_paths
)
333 s
.reference
= "HEAD^1";
336 s
.untracked
= untracked_files
;
337 s
.index_file
= index_file
;
346 static int run_hook(const char *index_file
, const char *name
, ...)
348 struct child_process hook
;
349 const char *argv
[10], *env
[2];
350 char index
[PATH_MAX
];
354 va_start(args
, name
);
355 argv
[0] = git_path("hooks/%s", name
);
358 if (++i
>= ARRAY_SIZE(argv
))
359 die ("run_hook(): too many arguments");
360 argv
[i
] = va_arg(args
, const char *);
364 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
368 if (access(argv
[0], X_OK
) < 0)
371 memset(&hook
, 0, sizeof(hook
));
374 hook
.stdout_to_stderr
= 1;
377 return run_command(&hook
);
380 static int is_a_merge(const unsigned char *sha1
)
382 struct commit
*commit
= lookup_commit(sha1
);
383 if (!commit
|| parse_commit(commit
))
384 die("could not parse HEAD commit");
385 return !!(commit
->parents
&& commit
->parents
->next
);
388 static const char sign_off_header
[] = "Signed-off-by: ";
390 static int prepare_to_commit(const char *index_file
, const char *prefix
)
393 int commitable
, saved_color_setting
;
397 const char *hook_arg1
= NULL
;
398 const char *hook_arg2
= NULL
;
400 if (!no_verify
&& run_hook(index_file
, "pre-commit", NULL
))
405 strbuf_addbuf(&sb
, &message
);
406 hook_arg1
= "message";
407 } else if (logfile
&& !strcmp(logfile
, "-")) {
409 fprintf(stderr
, "(reading log message from standard input)\n");
410 if (strbuf_read(&sb
, 0, 0) < 0)
411 die("could not read log from standard input");
412 hook_arg1
= "message";
413 } else if (logfile
) {
414 if (strbuf_read_file(&sb
, logfile
, 0) < 0)
415 die("could not read log file '%s': %s",
416 logfile
, strerror(errno
));
417 hook_arg1
= "message";
418 } else if (use_message
) {
419 buffer
= strstr(use_message_buffer
, "\n\n");
420 if (!buffer
|| buffer
[2] == '\0')
421 die("commit has empty message");
422 strbuf_add(&sb
, buffer
+ 2, strlen(buffer
+ 2));
423 hook_arg1
= "commit";
424 hook_arg2
= use_message
;
425 } else if (!stat(git_path("MERGE_MSG"), &statbuf
)) {
426 if (strbuf_read_file(&sb
, git_path("MERGE_MSG"), 0) < 0)
427 die("could not read MERGE_MSG: %s", strerror(errno
));
429 } else if (!stat(git_path("SQUASH_MSG"), &statbuf
)) {
430 if (strbuf_read_file(&sb
, git_path("SQUASH_MSG"), 0) < 0)
431 die("could not read SQUASH_MSG: %s", strerror(errno
));
432 hook_arg1
= "squash";
433 } else if (template_file
&& !stat(template_file
, &statbuf
)) {
434 if (strbuf_read_file(&sb
, template_file
, 0) < 0)
435 die("could not read %s: %s",
436 template_file
, strerror(errno
));
437 hook_arg1
= "template";
441 * This final case does not modify the template message,
442 * it just sets the argument to the prepare-commit-msg hook.
447 fp
= fopen(git_path(commit_editmsg
), "w");
449 die("could not open %s", git_path(commit_editmsg
));
451 if (cleanup_mode
!= CLEANUP_NONE
)
458 strbuf_init(&sob
, 0);
459 strbuf_addstr(&sob
, sign_off_header
);
460 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
461 getenv("GIT_COMMITTER_EMAIL")));
462 strbuf_addch(&sob
, '\n');
463 for (i
= sb
.len
- 1; i
> 0 && sb
.buf
[i
- 1] != '\n'; i
--)
465 if (prefixcmp(sb
.buf
+ i
, sob
.buf
)) {
466 if (prefixcmp(sb
.buf
+ i
, sign_off_header
))
467 strbuf_addch(&sb
, '\n');
468 strbuf_addbuf(&sb
, &sob
);
470 strbuf_release(&sob
);
473 if (fwrite(sb
.buf
, 1, sb
.len
, fp
) < sb
.len
)
474 die("could not write commit template: %s", strerror(errno
));
482 "# It looks like you may be committing a MERGE.\n"
483 "# If this is not correct, please remove the file\n"
487 git_path("MERGE_HEAD"));
491 "# Please enter the commit message for your changes.\n"
492 "# (Comment lines starting with '#' will ");
493 if (cleanup_mode
== CLEANUP_ALL
)
494 fprintf(fp
, "not be included)\n");
495 else /* CLEANUP_SPACE, that is. */
496 fprintf(fp
, "be kept.\n"
497 "# You can remove them yourself if you want to)\n");
498 if (only_include_assumed
)
499 fprintf(fp
, "# %s\n", only_include_assumed
);
501 saved_color_setting
= wt_status_use_color
;
502 wt_status_use_color
= 0;
503 commitable
= run_status(fp
, index_file
, prefix
, 1);
504 wt_status_use_color
= saved_color_setting
;
507 unsigned char sha1
[20];
508 const char *parent
= "HEAD";
510 if (!active_nr
&& read_cache() < 0)
511 die("Cannot read index");
516 if (get_sha1(parent
, sha1
))
517 commitable
= !!active_nr
;
519 init_revisions(&rev
, "");
521 setup_revisions(0, NULL
, &rev
, parent
);
522 DIFF_OPT_SET(&rev
.diffopt
, QUIET
);
523 DIFF_OPT_SET(&rev
.diffopt
, EXIT_WITH_STATUS
);
524 run_diff_index(&rev
, 1 /* cached */);
526 commitable
= !!DIFF_OPT_TST(&rev
.diffopt
, HAS_CHANGES
);
532 if (!commitable
&& !in_merge
&& !allow_empty
&&
533 !(amend
&& is_a_merge(head_sha1
))) {
534 run_status(stdout
, index_file
, prefix
, 0);
535 unlink(commit_editmsg
);
540 * Re-read the index as pre-commit hook could have updated it,
541 * and write it out as a tree. We must do this before we invoke
542 * the editor and after we invoke run_status above.
545 read_cache_from(index_file
);
546 if (!active_cache_tree
)
547 active_cache_tree
= cache_tree();
548 if (cache_tree_update(active_cache_tree
,
549 active_cache
, active_nr
, 0, 0) < 0) {
550 error("Error building trees");
554 if (run_hook(index_file
, "prepare-commit-msg",
555 git_path(commit_editmsg
), hook_arg1
, hook_arg2
, NULL
))
559 char index
[PATH_MAX
];
560 const char *env
[2] = { index
, NULL
};
561 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
562 launch_editor(git_path(commit_editmsg
), NULL
, env
);
566 run_hook(index_file
, "commit-msg", git_path(commit_editmsg
), NULL
)) {
574 * Find out if the message starting at position 'start' in the strbuf
575 * contains only whitespace and Signed-off-by lines.
577 static int message_is_empty(struct strbuf
*sb
, int start
)
583 if (cleanup_mode
== CLEANUP_NONE
&& sb
->len
)
586 /* See if the template is just a prefix of the message. */
587 strbuf_init(&tmpl
, 0);
588 if (template_file
&& strbuf_read_file(&tmpl
, template_file
, 0) > 0) {
589 stripspace(&tmpl
, cleanup_mode
== CLEANUP_ALL
);
590 if (start
+ tmpl
.len
<= sb
->len
&&
591 memcmp(tmpl
.buf
, sb
->buf
+ start
, tmpl
.len
) == 0)
594 strbuf_release(&tmpl
);
596 /* Check if the rest is just whitespace and Signed-of-by's. */
597 for (i
= start
; i
< sb
->len
; i
++) {
598 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
604 if (strlen(sign_off_header
) <= eol
- i
&&
605 !prefixcmp(sb
->buf
+ i
, sign_off_header
)) {
610 if (!isspace(sb
->buf
[i
++]))
617 static void determine_author_info(struct strbuf
*sb
)
619 char *name
, *email
, *date
;
621 name
= getenv("GIT_AUTHOR_NAME");
622 email
= getenv("GIT_AUTHOR_EMAIL");
623 date
= getenv("GIT_AUTHOR_DATE");
626 const char *a
, *lb
, *rb
, *eol
;
628 a
= strstr(use_message_buffer
, "\nauthor ");
630 die("invalid commit: %s", use_message
);
632 lb
= strstr(a
+ 8, " <");
633 rb
= strstr(a
+ 8, "> ");
634 eol
= strchr(a
+ 8, '\n');
635 if (!lb
|| !rb
|| !eol
)
636 die("invalid commit: %s", use_message
);
638 name
= xstrndup(a
+ 8, lb
- (a
+ 8));
639 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
640 date
= xstrndup(rb
+ 2, eol
- (rb
+ 2));
644 const char *lb
= strstr(force_author
, " <");
645 const char *rb
= strchr(force_author
, '>');
648 die("malformed --author parameter");
649 name
= xstrndup(force_author
, lb
- force_author
);
650 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
653 strbuf_addf(sb
, "author %s\n", fmt_ident(name
, email
, date
, IDENT_ERROR_ON_NO_NAME
));
656 static int parse_and_validate_options(int argc
, const char *argv
[],
657 const char * const usage
[])
661 argc
= parse_options(argc
, argv
, builtin_commit_options
, usage
, 0);
663 if (logfile
|| message
.len
|| use_message
)
668 setenv("GIT_EDITOR", ":", 1);
670 if (get_sha1("HEAD", head_sha1
))
673 if (!get_sha1("MERGE_HEAD", merge_head_sha1
))
676 /* Sanity check options */
677 if (amend
&& initial_commit
)
678 die("You have nothing to amend.");
679 if (amend
&& in_merge
)
680 die("You are in the middle of a merge -- cannot amend.");
689 die("Only one of -c/-C/-F can be used.");
690 if (message
.len
&& f
> 0)
691 die("Option -m cannot be combined with -c/-C/-F.");
693 use_message
= edit_message
;
694 if (amend
&& !use_message
)
695 use_message
= "HEAD";
697 unsigned char sha1
[20];
698 static char utf8
[] = "UTF-8";
701 struct commit
*commit
;
703 if (get_sha1(use_message
, sha1
))
704 die("could not lookup commit %s", use_message
);
705 commit
= lookup_commit_reference(sha1
);
706 if (!commit
|| parse_commit(commit
))
707 die("could not parse commit %s", use_message
);
709 enc
= strstr(commit
->buffer
, "\nencoding");
711 end
= strchr(enc
+ 10, '\n');
712 enc
= xstrndup(enc
+ 10, end
- (enc
+ 10));
716 out_enc
= git_commit_encoding
? git_commit_encoding
: utf8
;
718 if (strcmp(out_enc
, enc
))
720 reencode_string(commit
->buffer
, out_enc
, enc
);
723 * If we failed to reencode the buffer, just copy it
724 * byte for byte so the user can try to fix it up.
725 * This also handles the case where input and output
726 * encodings are identical.
728 if (use_message_buffer
== NULL
)
729 use_message_buffer
= xstrdup(commit
->buffer
);
734 if (!!also
+ !!only
+ !!all
+ !!interactive
> 1)
735 die("Only one of --include/--only/--all/--interactive can be used.");
736 if (argc
== 0 && (also
|| (only
&& !amend
)))
737 die("No paths with --include/--only does not make sense.");
738 if (argc
== 0 && only
&& amend
)
739 only_include_assumed
= "Clever... amending the last one with dirty index.";
740 if (argc
> 0 && !also
&& !only
) {
741 only_include_assumed
= "Explicit paths specified without -i nor -o; assuming --only paths...";
744 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "default"))
745 cleanup_mode
= use_editor
? CLEANUP_ALL
: CLEANUP_SPACE
;
746 else if (!strcmp(cleanup_arg
, "verbatim"))
747 cleanup_mode
= CLEANUP_NONE
;
748 else if (!strcmp(cleanup_arg
, "whitespace"))
749 cleanup_mode
= CLEANUP_SPACE
;
750 else if (!strcmp(cleanup_arg
, "strip"))
751 cleanup_mode
= CLEANUP_ALL
;
753 die("Invalid cleanup mode %s", cleanup_arg
);
756 die("Paths with -a does not make sense.");
757 else if (interactive
&& argc
> 0)
758 die("Paths with --interactive does not make sense.");
763 int cmd_status(int argc
, const char **argv
, const char *prefix
)
765 const char *index_file
;
768 git_config(git_status_config
);
770 argc
= parse_and_validate_options(argc
, argv
, builtin_status_usage
);
772 index_file
= prepare_index(argc
, argv
, prefix
);
774 commitable
= run_status(stdout
, index_file
, prefix
, 0);
776 rollback_index_files();
778 return commitable
? 0 : 1;
781 static void print_summary(const char *prefix
, const unsigned char *sha1
)
784 struct commit
*commit
;
786 commit
= lookup_commit(sha1
);
788 die("couldn't look up newly created commit");
789 if (!commit
|| parse_commit(commit
))
790 die("could not parse newly created commit");
792 init_revisions(&rev
, prefix
);
793 setup_revisions(0, NULL
, &rev
, NULL
);
797 rev
.diffopt
.output_format
=
798 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
800 rev
.verbose_header
= 1;
801 rev
.show_root_diff
= 1;
802 rev
.commit_format
= get_commit_format("format:%h: %s");
803 rev
.always_show_header
= 0;
804 rev
.diffopt
.detect_rename
= 1;
805 rev
.diffopt
.rename_limit
= 100;
806 rev
.diffopt
.break_opt
= 0;
807 diff_setup_done(&rev
.diffopt
);
809 printf("Created %scommit ", initial_commit
? "initial " : "");
811 if (!log_tree_commit(&rev
, commit
)) {
812 struct strbuf buf
= STRBUF_INIT
;
813 format_commit_message(commit
, "%h: %s", &buf
);
814 printf("%s\n", buf
.buf
);
815 strbuf_release(&buf
);
819 int git_commit_config(const char *k
, const char *v
)
821 if (!strcmp(k
, "commit.template")) {
822 template_file
= xstrdup(v
);
826 return git_status_config(k
, v
);
829 static const char commit_utf8_warn
[] =
830 "Warning: commit message does not conform to UTF-8.\n"
831 "You may want to amend it after fixing the message, or set the config\n"
832 "variable i18n.commitencoding to the encoding your project uses.\n";
834 static void add_parent(struct strbuf
*sb
, const unsigned char *sha1
)
836 struct object
*obj
= parse_object(sha1
);
837 const char *parent
= sha1_to_hex(sha1
);
839 die("Unable to find commit parent %s", parent
);
840 if (obj
->type
!= OBJ_COMMIT
)
841 die("Parent %s isn't a proper commit", parent
);
842 strbuf_addf(sb
, "parent %s\n", parent
);
845 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
849 const char *index_file
, *reflog_msg
;
851 unsigned char commit_sha1
[20];
852 struct ref_lock
*ref_lock
;
854 git_config(git_commit_config
);
856 argc
= parse_and_validate_options(argc
, argv
, builtin_commit_usage
);
858 index_file
= prepare_index(argc
, argv
, prefix
);
860 /* Set up everything for writing the commit object. This includes
861 running hooks, writing the trees, and interacting with the user. */
862 if (!prepare_to_commit(index_file
, prefix
)) {
863 rollback_index_files();
871 strbuf_addf(&sb
, "tree %s\n",
872 sha1_to_hex(active_cache_tree
->sha1
));
874 /* Determine parents */
875 if (initial_commit
) {
876 reflog_msg
= "commit (initial)";
878 struct commit_list
*c
;
879 struct commit
*commit
;
881 reflog_msg
= "commit (amend)";
882 commit
= lookup_commit(head_sha1
);
883 if (!commit
|| parse_commit(commit
))
884 die("could not parse HEAD commit");
886 for (c
= commit
->parents
; c
; c
= c
->next
)
887 add_parent(&sb
, c
->item
->object
.sha1
);
888 } else if (in_merge
) {
892 reflog_msg
= "commit (merge)";
893 add_parent(&sb
, head_sha1
);
895 fp
= fopen(git_path("MERGE_HEAD"), "r");
897 die("could not open %s for reading: %s",
898 git_path("MERGE_HEAD"), strerror(errno
));
899 while (strbuf_getline(&m
, fp
, '\n') != EOF
) {
900 unsigned char sha1
[20];
901 if (get_sha1_hex(m
.buf
, sha1
) < 0)
902 die("Corrupt MERGE_HEAD file (%s)", m
.buf
);
903 add_parent(&sb
, sha1
);
908 reflog_msg
= "commit";
909 strbuf_addf(&sb
, "parent %s\n", sha1_to_hex(head_sha1
));
912 determine_author_info(&sb
);
913 strbuf_addf(&sb
, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME
));
914 if (!is_encoding_utf8(git_commit_encoding
))
915 strbuf_addf(&sb
, "encoding %s\n", git_commit_encoding
);
916 strbuf_addch(&sb
, '\n');
918 /* Finally, get the commit message */
920 if (strbuf_read_file(&sb
, git_path(commit_editmsg
), 0) < 0) {
921 rollback_index_files();
922 die("could not read commit message");
925 /* Truncate the message just before the diff, if any. */
926 p
= strstr(sb
.buf
, "\ndiff --git a/");
928 strbuf_setlen(&sb
, p
- sb
.buf
+ 1);
930 if (cleanup_mode
!= CLEANUP_NONE
)
931 stripspace(&sb
, cleanup_mode
== CLEANUP_ALL
);
932 if (sb
.len
< header_len
|| message_is_empty(&sb
, header_len
)) {
933 rollback_index_files();
934 die("no commit message? aborting commit.");
936 strbuf_addch(&sb
, '\0');
937 if (is_encoding_utf8(git_commit_encoding
) && !is_utf8(sb
.buf
))
938 fprintf(stderr
, commit_utf8_warn
);
940 if (write_sha1_file(sb
.buf
, sb
.len
- 1, commit_type
, commit_sha1
)) {
941 rollback_index_files();
942 die("failed to write commit object");
945 ref_lock
= lock_any_ref_for_update("HEAD",
946 initial_commit
? NULL
: head_sha1
,
949 nl
= strchr(sb
.buf
+ header_len
, '\n');
951 strbuf_setlen(&sb
, nl
+ 1 - sb
.buf
);
953 strbuf_addch(&sb
, '\n');
954 strbuf_remove(&sb
, 0, header_len
);
955 strbuf_insert(&sb
, 0, reflog_msg
, strlen(reflog_msg
));
956 strbuf_insert(&sb
, strlen(reflog_msg
), ": ", 2);
959 rollback_index_files();
960 die("cannot lock HEAD ref");
962 if (write_ref_sha1(ref_lock
, commit_sha1
, sb
.buf
) < 0) {
963 rollback_index_files();
964 die("cannot update HEAD ref");
967 unlink(git_path("MERGE_HEAD"));
968 unlink(git_path("MERGE_MSG"));
970 if (commit_index_files())
971 die ("Repository has been updated, but unable to write\n"
972 "new_index file. Check that disk is not full or quota is\n"
973 "not exceeded, and then \"git reset HEAD\" to recover.");
976 run_hook(get_index_file(), "post-commit", NULL
);
978 print_summary(prefix
, commit_sha1
);