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 "string-list.h"
26 #include "unpack-trees.h"
28 static const char * const builtin_commit_usage
[] = {
29 "git commit [options] [--] <filepattern>...",
33 static const char * const builtin_status_usage
[] = {
34 "git status [options] [--] <filepattern>...",
38 static unsigned char head_sha1
[20], merge_head_sha1
[20];
39 static char *use_message_buffer
;
40 static const char commit_editmsg
[] = "COMMIT_EDITMSG";
41 static struct lock_file index_lock
; /* real index */
42 static struct lock_file false_lock
; /* used only for partial commits */
49 static const char *logfile
, *force_author
;
50 static const char *template_file
;
51 static char *edit_message
, *use_message
;
52 static char *author_name
, *author_email
, *author_date
;
53 static int all
, edit_flag
, also
, interactive
, only
, amend
, signoff
;
54 static int quiet
, verbose
, no_verify
, allow_empty
, dry_run
, renew_authorship
;
55 static char *untracked_files_arg
;
57 * The default commit message cleanup mode will remove the lines
58 * beginning with # (shell comments) and leading and trailing
59 * whitespaces (empty lines or containing only whitespaces)
60 * if editor is used, and only the whitespaces if the message
61 * is specified explicitly.
68 static char *cleanup_arg
;
70 static int use_editor
= 1, initial_commit
, in_merge
;
71 static const char *only_include_assumed
;
72 static struct strbuf message
;
74 static int opt_parse_m(const struct option
*opt
, const char *arg
, int unset
)
76 struct strbuf
*buf
= opt
->value
;
78 strbuf_setlen(buf
, 0);
80 strbuf_addstr(buf
, arg
);
81 strbuf_addstr(buf
, "\n\n");
86 static struct option builtin_commit_options
[] = {
88 OPT__VERBOSE(&verbose
),
89 OPT_GROUP("Commit message options"),
91 OPT_FILENAME('F', "file", &logfile
, "read log from file"),
92 OPT_STRING(0, "author", &force_author
, "AUTHOR", "override author for commit"),
93 OPT_CALLBACK('m', "message", &message
, "MESSAGE", "specify commit message", opt_parse_m
),
94 OPT_STRING('c', "reedit-message", &edit_message
, "COMMIT", "reuse and edit message from specified commit"),
95 OPT_STRING('C', "reuse-message", &use_message
, "COMMIT", "reuse message from specified commit"),
96 OPT_BOOLEAN(0, "reset-author", &renew_authorship
, "the commit is authored by me now (used with -C-c/--amend)"),
97 OPT_BOOLEAN('s', "signoff", &signoff
, "add Signed-off-by:"),
98 OPT_FILENAME('t', "template", &template_file
, "use specified template file"),
99 OPT_BOOLEAN('e', "edit", &edit_flag
, "force edit of commit"),
101 OPT_GROUP("Commit contents options"),
102 OPT_BOOLEAN('a', "all", &all
, "commit all changed files"),
103 OPT_BOOLEAN('i', "include", &also
, "add specified files to index for commit"),
104 OPT_BOOLEAN(0, "interactive", &interactive
, "interactively add files"),
105 OPT_BOOLEAN('o', "only", &only
, "commit only specified files"),
106 OPT_BOOLEAN('n', "no-verify", &no_verify
, "bypass pre-commit hook"),
107 OPT_BOOLEAN(0, "dry-run", &dry_run
, "show what would be committed"),
108 OPT_BOOLEAN(0, "amend", &amend
, "amend previous commit"),
109 { OPTION_STRING
, 'u', "untracked-files", &untracked_files_arg
, "mode", "show untracked files, optional modes: all, normal, no. (Default: all)", PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
110 OPT_BOOLEAN(0, "allow-empty", &allow_empty
, "ok to record an empty change"),
111 OPT_STRING(0, "cleanup", &cleanup_arg
, "default", "how to strip spaces and #comments from message"),
116 static void rollback_index_files(void)
118 switch (commit_style
) {
120 break; /* nothing to do */
122 rollback_lock_file(&index_lock
);
125 rollback_lock_file(&index_lock
);
126 rollback_lock_file(&false_lock
);
131 static int commit_index_files(void)
135 switch (commit_style
) {
137 break; /* nothing to do */
139 err
= commit_lock_file(&index_lock
);
142 err
= commit_lock_file(&index_lock
);
143 rollback_lock_file(&false_lock
);
151 * Take a union of paths in the index and the named tree (typically, "HEAD"),
152 * and return the paths that match the given pattern in list.
154 static int list_paths(struct string_list
*list
, const char *with_tree
,
155 const char *prefix
, const char **pattern
)
160 for (i
= 0; pattern
[i
]; i
++)
165 overlay_tree_on_cache(with_tree
, prefix
);
167 for (i
= 0; i
< active_nr
; i
++) {
168 struct cache_entry
*ce
= active_cache
[i
];
169 if (ce
->ce_flags
& CE_UPDATE
)
171 if (!match_pathspec(pattern
, ce
->name
, ce_namelen(ce
), 0, m
))
173 string_list_insert(ce
->name
, list
);
176 return report_path_error(m
, pattern
, prefix
? strlen(prefix
) : 0);
179 static void add_remove_files(struct string_list
*list
)
182 for (i
= 0; i
< list
->nr
; i
++) {
184 struct string_list_item
*p
= &(list
->items
[i
]);
186 if (!lstat(p
->string
, &st
)) {
187 if (add_to_cache(p
->string
, &st
, 0))
188 die("updating files failed");
190 remove_file_from_cache(p
->string
);
194 static void create_base_index(void)
197 struct unpack_trees_options opts
;
200 if (initial_commit
) {
205 memset(&opts
, 0, sizeof(opts
));
209 opts
.src_index
= &the_index
;
210 opts
.dst_index
= &the_index
;
212 opts
.fn
= oneway_merge
;
213 tree
= parse_tree_indirect(head_sha1
);
215 die("failed to unpack HEAD tree object");
217 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
218 if (unpack_trees(1, &t
, &opts
))
219 exit(128); /* We've already reported the error, finish dying */
222 static char *prepare_index(int argc
, const char **argv
, const char *prefix
, int is_status
)
225 struct string_list partial
;
226 const char **pathspec
= NULL
;
227 int refresh_flags
= REFRESH_QUIET
;
230 refresh_flags
|= REFRESH_UNMERGED
;
232 if (interactive_add(argc
, argv
, prefix
) != 0)
233 die("interactive add failed");
234 if (read_cache_preload(NULL
) < 0)
235 die("index file corrupt");
236 commit_style
= COMMIT_AS_IS
;
237 return get_index_file();
241 pathspec
= get_pathspec(prefix
, argv
);
243 if (read_cache_preload(pathspec
) < 0)
244 die("index file corrupt");
247 * Non partial, non as-is commit.
249 * (1) get the real index;
250 * (2) update the_index as necessary;
251 * (3) write the_index out to the real index (still locked);
252 * (4) return the name of the locked index file.
254 * The caller should run hooks on the locked real index, and
255 * (A) if all goes well, commit the real index;
256 * (B) on failure, rollback the real index.
258 if (all
|| (also
&& pathspec
&& *pathspec
)) {
259 int fd
= hold_locked_index(&index_lock
, 1);
260 add_files_to_cache(also
? prefix
: NULL
, pathspec
, 0);
261 refresh_cache(refresh_flags
);
262 if (write_cache(fd
, active_cache
, active_nr
) ||
263 close_lock_file(&index_lock
))
264 die("unable to write new_index file");
265 commit_style
= COMMIT_NORMAL
;
266 return index_lock
.filename
;
272 * (1) return the name of the real index file.
274 * The caller should run hooks on the real index, and run
275 * hooks on the real index, and create commit from the_index.
276 * We still need to refresh the index here.
278 if (!pathspec
|| !*pathspec
) {
279 fd
= hold_locked_index(&index_lock
, 1);
280 refresh_cache(refresh_flags
);
281 if (write_cache(fd
, active_cache
, active_nr
) ||
282 commit_locked_index(&index_lock
))
283 die("unable to write new_index file");
284 commit_style
= COMMIT_AS_IS
;
285 return get_index_file();
291 * (0) find the set of affected paths;
292 * (1) get lock on the real index file;
293 * (2) update the_index with the given paths;
294 * (3) write the_index out to the real index (still locked);
295 * (4) get lock on the false index file;
296 * (5) reset the_index from HEAD;
297 * (6) update the_index the same way as (2);
298 * (7) write the_index out to the false index file;
299 * (8) return the name of the false index file (still locked);
301 * The caller should run hooks on the locked false index, and
302 * create commit from it. Then
303 * (A) if all goes well, commit the real index;
304 * (B) on failure, rollback the real index;
305 * In either case, rollback the false index.
307 commit_style
= COMMIT_PARTIAL
;
309 if (file_exists(git_path("MERGE_HEAD")))
310 die("cannot do a partial commit during a merge.");
312 memset(&partial
, 0, sizeof(partial
));
313 partial
.strdup_strings
= 1;
314 if (list_paths(&partial
, initial_commit
? NULL
: "HEAD", prefix
, pathspec
))
318 if (read_cache() < 0)
319 die("cannot read the index");
321 fd
= hold_locked_index(&index_lock
, 1);
322 add_remove_files(&partial
);
323 refresh_cache(REFRESH_QUIET
);
324 if (write_cache(fd
, active_cache
, active_nr
) ||
325 close_lock_file(&index_lock
))
326 die("unable to write new_index file");
328 fd
= hold_lock_file_for_update(&false_lock
,
329 git_path("next-index-%"PRIuMAX
,
330 (uintmax_t) getpid()),
334 add_remove_files(&partial
);
335 refresh_cache(REFRESH_QUIET
);
337 if (write_cache(fd
, active_cache
, active_nr
) ||
338 close_lock_file(&false_lock
))
339 die("unable to write temporary index file");
342 read_cache_from(false_lock
.filename
);
344 return false_lock
.filename
;
347 static int run_status(FILE *fp
, const char *index_file
, const char *prefix
, int nowarn
,
350 if (s
->relative_paths
)
355 s
->reference
= "HEAD^1";
357 s
->verbose
= verbose
;
358 s
->index_file
= index_file
;
364 return s
->commitable
;
367 static int is_a_merge(const unsigned char *sha1
)
369 struct commit
*commit
= lookup_commit(sha1
);
370 if (!commit
|| parse_commit(commit
))
371 die("could not parse HEAD commit");
372 return !!(commit
->parents
&& commit
->parents
->next
);
375 static const char sign_off_header
[] = "Signed-off-by: ";
377 static void determine_author_info(void)
379 char *name
, *email
, *date
;
381 name
= getenv("GIT_AUTHOR_NAME");
382 email
= getenv("GIT_AUTHOR_EMAIL");
383 date
= getenv("GIT_AUTHOR_DATE");
385 if (use_message
&& !renew_authorship
) {
386 const char *a
, *lb
, *rb
, *eol
;
388 a
= strstr(use_message_buffer
, "\nauthor ");
390 die("invalid commit: %s", use_message
);
392 lb
= strstr(a
+ 8, " <");
393 rb
= strstr(a
+ 8, "> ");
394 eol
= strchr(a
+ 8, '\n');
395 if (!lb
|| !rb
|| !eol
)
396 die("invalid commit: %s", use_message
);
398 name
= xstrndup(a
+ 8, lb
- (a
+ 8));
399 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
400 date
= xstrndup(rb
+ 2, eol
- (rb
+ 2));
404 const char *lb
= strstr(force_author
, " <");
405 const char *rb
= strchr(force_author
, '>');
408 die("malformed --author parameter");
409 name
= xstrndup(force_author
, lb
- force_author
);
410 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
414 author_email
= email
;
418 static int ends_rfc2822_footer(struct strbuf
*sb
)
425 const char *buf
= sb
->buf
;
427 for (i
= len
- 1; i
> 0; i
--) {
428 if (hit
&& buf
[i
] == '\n')
430 hit
= (buf
[i
] == '\n');
433 while (i
< len
- 1 && buf
[i
] == '\n')
436 for (; i
< len
; i
= k
) {
437 for (k
= i
; k
< len
&& buf
[k
] != '\n'; k
++)
441 if ((buf
[k
] == ' ' || buf
[k
] == '\t') && !first
)
446 for (j
= 0; i
+ j
< len
; j
++) {
459 static int prepare_to_commit(const char *index_file
, const char *prefix
,
463 int commitable
, saved_color_setting
;
464 struct strbuf sb
= STRBUF_INIT
;
467 const char *hook_arg1
= NULL
;
468 const char *hook_arg2
= NULL
;
471 if (!no_verify
&& run_hook(index_file
, "pre-commit", NULL
))
475 strbuf_addbuf(&sb
, &message
);
476 hook_arg1
= "message";
477 } else if (logfile
&& !strcmp(logfile
, "-")) {
479 fprintf(stderr
, "(reading log message from standard input)\n");
480 if (strbuf_read(&sb
, 0, 0) < 0)
481 die_errno("could not read log from standard input");
482 hook_arg1
= "message";
483 } else if (logfile
) {
484 if (strbuf_read_file(&sb
, logfile
, 0) < 0)
485 die_errno("could not read log file '%s'",
487 hook_arg1
= "message";
488 } else if (use_message
) {
489 buffer
= strstr(use_message_buffer
, "\n\n");
490 if (!buffer
|| buffer
[2] == '\0')
491 die("commit has empty message");
492 strbuf_add(&sb
, buffer
+ 2, strlen(buffer
+ 2));
493 hook_arg1
= "commit";
494 hook_arg2
= use_message
;
495 } else if (!stat(git_path("MERGE_MSG"), &statbuf
)) {
496 if (strbuf_read_file(&sb
, git_path("MERGE_MSG"), 0) < 0)
497 die_errno("could not read MERGE_MSG");
499 } else if (!stat(git_path("SQUASH_MSG"), &statbuf
)) {
500 if (strbuf_read_file(&sb
, git_path("SQUASH_MSG"), 0) < 0)
501 die_errno("could not read SQUASH_MSG");
502 hook_arg1
= "squash";
503 } else if (template_file
&& !stat(template_file
, &statbuf
)) {
504 if (strbuf_read_file(&sb
, template_file
, 0) < 0)
505 die_errno("could not read '%s'", template_file
);
506 hook_arg1
= "template";
510 * This final case does not modify the template message,
511 * it just sets the argument to the prepare-commit-msg hook.
516 fp
= fopen(git_path(commit_editmsg
), "w");
518 die_errno("could not open '%s'", git_path(commit_editmsg
));
520 if (cleanup_mode
!= CLEANUP_NONE
)
524 struct strbuf sob
= STRBUF_INIT
;
527 strbuf_addstr(&sob
, sign_off_header
);
528 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
529 getenv("GIT_COMMITTER_EMAIL")));
530 strbuf_addch(&sob
, '\n');
531 for (i
= sb
.len
- 1; i
> 0 && sb
.buf
[i
- 1] != '\n'; i
--)
533 if (prefixcmp(sb
.buf
+ i
, sob
.buf
)) {
534 if (!i
|| !ends_rfc2822_footer(&sb
))
535 strbuf_addch(&sb
, '\n');
536 strbuf_addbuf(&sb
, &sob
);
538 strbuf_release(&sob
);
541 if (fwrite(sb
.buf
, 1, sb
.len
, fp
) < sb
.len
)
542 die_errno("could not write commit template");
546 determine_author_info();
548 /* This checks if committer ident is explicitly given */
549 git_committer_info(0);
552 const char *committer_ident
;
557 "# It looks like you may be committing a MERGE.\n"
558 "# If this is not correct, please remove the file\n"
562 git_path("MERGE_HEAD"));
566 "# Please enter the commit message for your changes.");
567 if (cleanup_mode
== CLEANUP_ALL
)
570 "# with '#' will be ignored, and an empty"
571 " message aborts the commit.\n");
572 else /* CLEANUP_SPACE, that is. */
575 "# with '#' will be kept; you may remove them"
576 " yourself if you want to.\n"
577 "# An empty message aborts the commit.\n");
578 if (only_include_assumed
)
579 fprintf(fp
, "# %s\n", only_include_assumed
);
581 author_ident
= xstrdup(fmt_name(author_name
, author_email
));
582 committer_ident
= fmt_name(getenv("GIT_COMMITTER_NAME"),
583 getenv("GIT_COMMITTER_EMAIL"));
584 if (strcmp(author_ident
, committer_ident
))
588 ident_shown
++ ? "" : "#\n",
592 if (!user_ident_explicitly_given
)
596 ident_shown
++ ? "" : "#\n",
602 saved_color_setting
= s
->use_color
;
604 commitable
= run_status(fp
, index_file
, prefix
, 1, s
);
605 s
->use_color
= saved_color_setting
;
607 unsigned char sha1
[20];
608 const char *parent
= "HEAD";
610 if (!active_nr
&& read_cache() < 0)
611 die("Cannot read index");
616 if (get_sha1(parent
, sha1
))
617 commitable
= !!active_nr
;
619 commitable
= index_differs_from(parent
, 0);
624 if (!commitable
&& !in_merge
&& !allow_empty
&&
625 !(amend
&& is_a_merge(head_sha1
))) {
626 run_status(stdout
, index_file
, prefix
, 0, s
);
631 * Re-read the index as pre-commit hook could have updated it,
632 * and write it out as a tree. We must do this before we invoke
633 * the editor and after we invoke run_status above.
636 read_cache_from(index_file
);
637 if (!active_cache_tree
)
638 active_cache_tree
= cache_tree();
639 if (cache_tree_update(active_cache_tree
,
640 active_cache
, active_nr
, 0, 0) < 0) {
641 error("Error building trees");
645 if (run_hook(index_file
, "prepare-commit-msg",
646 git_path(commit_editmsg
), hook_arg1
, hook_arg2
, NULL
))
650 char index
[PATH_MAX
];
651 const char *env
[2] = { index
, NULL
};
652 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
653 if (launch_editor(git_path(commit_editmsg
), NULL
, env
)) {
655 "Please supply the message using either -m or -F option.\n");
661 run_hook(index_file
, "commit-msg", git_path(commit_editmsg
), NULL
)) {
669 * Find out if the message in the strbuf contains only whitespace and
670 * Signed-off-by lines.
672 static int message_is_empty(struct strbuf
*sb
)
674 struct strbuf tmpl
= STRBUF_INIT
;
676 int eol
, i
, start
= 0;
678 if (cleanup_mode
== CLEANUP_NONE
&& sb
->len
)
681 /* See if the template is just a prefix of the message. */
682 if (template_file
&& strbuf_read_file(&tmpl
, template_file
, 0) > 0) {
683 stripspace(&tmpl
, cleanup_mode
== CLEANUP_ALL
);
684 if (start
+ tmpl
.len
<= sb
->len
&&
685 memcmp(tmpl
.buf
, sb
->buf
+ start
, tmpl
.len
) == 0)
688 strbuf_release(&tmpl
);
690 /* Check if the rest is just whitespace and Signed-of-by's. */
691 for (i
= start
; i
< sb
->len
; i
++) {
692 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
698 if (strlen(sign_off_header
) <= eol
- i
&&
699 !prefixcmp(sb
->buf
+ i
, sign_off_header
)) {
704 if (!isspace(sb
->buf
[i
++]))
711 static const char *find_author_by_nickname(const char *name
)
713 struct rev_info revs
;
714 struct commit
*commit
;
715 struct strbuf buf
= STRBUF_INIT
;
719 init_revisions(&revs
, NULL
);
720 strbuf_addf(&buf
, "--author=%s", name
);
725 setup_revisions(ac
, av
, &revs
, NULL
);
726 prepare_revision_walk(&revs
);
727 commit
= get_revision(&revs
);
729 struct pretty_print_context ctx
= {0};
730 ctx
.date_mode
= DATE_NORMAL
;
731 strbuf_release(&buf
);
732 format_commit_message(commit
, "%an <%ae>", &buf
, &ctx
);
733 return strbuf_detach(&buf
, NULL
);
735 die("No existing author found with '%s'", name
);
738 static int parse_and_validate_options(int argc
, const char *argv
[],
739 const char * const usage
[],
745 argc
= parse_options(argc
, argv
, prefix
, builtin_commit_options
, usage
,
748 if (force_author
&& !strchr(force_author
, '>'))
749 force_author
= find_author_by_nickname(force_author
);
751 if (force_author
&& renew_authorship
)
752 die("Using both --reset-author and --author does not make sense");
754 if (logfile
|| message
.len
|| use_message
)
759 setenv("GIT_EDITOR", ":", 1);
761 if (get_sha1("HEAD", head_sha1
))
764 if (!get_sha1("MERGE_HEAD", merge_head_sha1
))
767 /* Sanity check options */
768 if (amend
&& initial_commit
)
769 die("You have nothing to amend.");
770 if (amend
&& in_merge
)
771 die("You are in the middle of a merge -- cannot amend.");
780 die("Only one of -c/-C/-F can be used.");
781 if (message
.len
&& f
> 0)
782 die("Option -m cannot be combined with -c/-C/-F.");
784 use_message
= edit_message
;
785 if (amend
&& !use_message
)
786 use_message
= "HEAD";
787 if (!use_message
&& renew_authorship
)
788 die("--reset-author can be used only with -C, -c or --amend.");
790 unsigned char sha1
[20];
791 static char utf8
[] = "UTF-8";
794 struct commit
*commit
;
796 if (get_sha1(use_message
, sha1
))
797 die("could not lookup commit %s", use_message
);
798 commit
= lookup_commit_reference(sha1
);
799 if (!commit
|| parse_commit(commit
))
800 die("could not parse commit %s", use_message
);
802 enc
= strstr(commit
->buffer
, "\nencoding");
804 end
= strchr(enc
+ 10, '\n');
805 enc
= xstrndup(enc
+ 10, end
- (enc
+ 10));
809 out_enc
= git_commit_encoding
? git_commit_encoding
: utf8
;
811 if (strcmp(out_enc
, enc
))
813 reencode_string(commit
->buffer
, out_enc
, enc
);
816 * If we failed to reencode the buffer, just copy it
817 * byte for byte so the user can try to fix it up.
818 * This also handles the case where input and output
819 * encodings are identical.
821 if (use_message_buffer
== NULL
)
822 use_message_buffer
= xstrdup(commit
->buffer
);
827 if (!!also
+ !!only
+ !!all
+ !!interactive
> 1)
828 die("Only one of --include/--only/--all/--interactive can be used.");
829 if (argc
== 0 && (also
|| (only
&& !amend
)))
830 die("No paths with --include/--only does not make sense.");
831 if (argc
== 0 && only
&& amend
)
832 only_include_assumed
= "Clever... amending the last one with dirty index.";
833 if (argc
> 0 && !also
&& !only
)
834 only_include_assumed
= "Explicit paths specified without -i nor -o; assuming --only paths...";
835 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "default"))
836 cleanup_mode
= use_editor
? CLEANUP_ALL
: CLEANUP_SPACE
;
837 else if (!strcmp(cleanup_arg
, "verbatim"))
838 cleanup_mode
= CLEANUP_NONE
;
839 else if (!strcmp(cleanup_arg
, "whitespace"))
840 cleanup_mode
= CLEANUP_SPACE
;
841 else if (!strcmp(cleanup_arg
, "strip"))
842 cleanup_mode
= CLEANUP_ALL
;
844 die("Invalid cleanup mode %s", cleanup_arg
);
846 if (!untracked_files_arg
)
847 ; /* default already initialized */
848 else if (!strcmp(untracked_files_arg
, "no"))
849 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
850 else if (!strcmp(untracked_files_arg
, "normal"))
851 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
852 else if (!strcmp(untracked_files_arg
, "all"))
853 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
855 die("Invalid untracked files mode '%s'", untracked_files_arg
);
858 die("Paths with -a does not make sense.");
859 else if (interactive
&& argc
> 0)
860 die("Paths with --interactive does not make sense.");
865 static int dry_run_commit(int argc
, const char **argv
, const char *prefix
,
869 const char *index_file
;
871 index_file
= prepare_index(argc
, argv
, prefix
, 1);
872 commitable
= run_status(stdout
, index_file
, prefix
, 0, s
);
873 rollback_index_files();
875 return commitable
? 0 : 1;
878 static int parse_status_slot(const char *var
, int offset
)
880 if (!strcasecmp(var
+offset
, "header"))
881 return WT_STATUS_HEADER
;
882 if (!strcasecmp(var
+offset
, "updated")
883 || !strcasecmp(var
+offset
, "added"))
884 return WT_STATUS_UPDATED
;
885 if (!strcasecmp(var
+offset
, "changed"))
886 return WT_STATUS_CHANGED
;
887 if (!strcasecmp(var
+offset
, "untracked"))
888 return WT_STATUS_UNTRACKED
;
889 if (!strcasecmp(var
+offset
, "nobranch"))
890 return WT_STATUS_NOBRANCH
;
891 if (!strcasecmp(var
+offset
, "unmerged"))
892 return WT_STATUS_UNMERGED
;
893 die("bad config variable '%s'", var
);
896 static int git_status_config(const char *k
, const char *v
, void *cb
)
898 struct wt_status
*s
= cb
;
900 if (!strcmp(k
, "status.submodulesummary")) {
902 s
->submodule_summary
= git_config_bool_or_int(k
, v
, &is_bool
);
903 if (is_bool
&& s
->submodule_summary
)
904 s
->submodule_summary
= -1;
907 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
908 s
->use_color
= git_config_colorbool(k
, v
, -1);
911 if (!prefixcmp(k
, "status.color.") || !prefixcmp(k
, "color.status.")) {
912 int slot
= parse_status_slot(k
, 13);
914 return config_error_nonbool(k
);
915 color_parse(v
, k
, s
->color_palette
[slot
]);
918 if (!strcmp(k
, "status.relativepaths")) {
919 s
->relative_paths
= git_config_bool(k
, v
);
922 if (!strcmp(k
, "status.showuntrackedfiles")) {
924 return config_error_nonbool(k
);
925 else if (!strcmp(v
, "no"))
926 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
927 else if (!strcmp(v
, "normal"))
928 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
929 else if (!strcmp(v
, "all"))
930 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
932 return error("Invalid untracked files mode '%s'", v
);
935 return git_diff_ui_config(k
, v
, NULL
);
938 int cmd_status(int argc
, const char **argv
, const char *prefix
)
942 wt_status_prepare(&s
);
943 git_config(git_status_config
, &s
);
944 if (s
.use_color
== -1)
945 s
.use_color
= git_use_color_default
;
946 if (diff_use_color_default
== -1)
947 diff_use_color_default
= git_use_color_default
;
949 argc
= parse_and_validate_options(argc
, argv
, builtin_status_usage
,
951 return dry_run_commit(argc
, argv
, prefix
, &s
);
954 static void print_summary(const char *prefix
, const unsigned char *sha1
)
957 struct commit
*commit
;
958 static const char *format
= "format:%h] %s";
959 unsigned char junk_sha1
[20];
960 const char *head
= resolve_ref("HEAD", junk_sha1
, 0, NULL
);
962 commit
= lookup_commit(sha1
);
964 die("couldn't look up newly created commit");
965 if (!commit
|| parse_commit(commit
))
966 die("could not parse newly created commit");
968 init_revisions(&rev
, prefix
);
969 setup_revisions(0, NULL
, &rev
, NULL
);
973 rev
.diffopt
.output_format
=
974 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
976 rev
.verbose_header
= 1;
977 rev
.show_root_diff
= 1;
978 get_commit_format(format
, &rev
);
979 rev
.always_show_header
= 0;
980 rev
.diffopt
.detect_rename
= 1;
981 rev
.diffopt
.rename_limit
= 100;
982 rev
.diffopt
.break_opt
= 0;
983 diff_setup_done(&rev
.diffopt
);
986 !prefixcmp(head
, "refs/heads/") ?
988 !strcmp(head
, "HEAD") ?
991 initial_commit
? " (root-commit)" : "");
993 if (!log_tree_commit(&rev
, commit
)) {
994 struct pretty_print_context ctx
= {0};
995 struct strbuf buf
= STRBUF_INIT
;
996 ctx
.date_mode
= DATE_NORMAL
;
997 format_commit_message(commit
, format
+ 7, &buf
, &ctx
);
998 printf("%s\n", buf
.buf
);
999 strbuf_release(&buf
);
1003 static int git_commit_config(const char *k
, const char *v
, void *cb
)
1005 struct wt_status
*s
= cb
;
1007 if (!strcmp(k
, "commit.template"))
1008 return git_config_pathname(&template_file
, k
, v
);
1010 return git_status_config(k
, v
, s
);
1013 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
1015 struct strbuf sb
= STRBUF_INIT
;
1016 const char *index_file
, *reflog_msg
;
1018 unsigned char commit_sha1
[20];
1019 struct ref_lock
*ref_lock
;
1020 struct commit_list
*parents
= NULL
, **pptr
= &parents
;
1021 struct stat statbuf
;
1022 int allow_fast_forward
= 1;
1025 wt_status_prepare(&s
);
1026 git_config(git_commit_config
, &s
);
1028 if (s
.use_color
== -1)
1029 s
.use_color
= git_use_color_default
;
1031 argc
= parse_and_validate_options(argc
, argv
, builtin_commit_usage
,
1034 if (diff_use_color_default
== -1)
1035 diff_use_color_default
= git_use_color_default
;
1036 return dry_run_commit(argc
, argv
, prefix
, &s
);
1038 index_file
= prepare_index(argc
, argv
, prefix
, 0);
1040 /* Set up everything for writing the commit object. This includes
1041 running hooks, writing the trees, and interacting with the user. */
1042 if (!prepare_to_commit(index_file
, prefix
, &s
)) {
1043 rollback_index_files();
1047 /* Determine parents */
1048 if (initial_commit
) {
1049 reflog_msg
= "commit (initial)";
1051 struct commit_list
*c
;
1052 struct commit
*commit
;
1054 reflog_msg
= "commit (amend)";
1055 commit
= lookup_commit(head_sha1
);
1056 if (!commit
|| parse_commit(commit
))
1057 die("could not parse HEAD commit");
1059 for (c
= commit
->parents
; c
; c
= c
->next
)
1060 pptr
= &commit_list_insert(c
->item
, pptr
)->next
;
1061 } else if (in_merge
) {
1062 struct strbuf m
= STRBUF_INIT
;
1065 reflog_msg
= "commit (merge)";
1066 pptr
= &commit_list_insert(lookup_commit(head_sha1
), pptr
)->next
;
1067 fp
= fopen(git_path("MERGE_HEAD"), "r");
1069 die_errno("could not open '%s' for reading",
1070 git_path("MERGE_HEAD"));
1071 while (strbuf_getline(&m
, fp
, '\n') != EOF
) {
1072 unsigned char sha1
[20];
1073 if (get_sha1_hex(m
.buf
, sha1
) < 0)
1074 die("Corrupt MERGE_HEAD file (%s)", m
.buf
);
1075 pptr
= &commit_list_insert(lookup_commit(sha1
), pptr
)->next
;
1079 if (!stat(git_path("MERGE_MODE"), &statbuf
)) {
1080 if (strbuf_read_file(&sb
, git_path("MERGE_MODE"), 0) < 0)
1081 die_errno("could not read MERGE_MODE");
1082 if (!strcmp(sb
.buf
, "no-ff"))
1083 allow_fast_forward
= 0;
1085 if (allow_fast_forward
)
1086 parents
= reduce_heads(parents
);
1088 reflog_msg
= "commit";
1089 pptr
= &commit_list_insert(lookup_commit(head_sha1
), pptr
)->next
;
1092 /* Finally, get the commit message */
1094 if (strbuf_read_file(&sb
, git_path(commit_editmsg
), 0) < 0) {
1095 int saved_errno
= errno
;
1096 rollback_index_files();
1097 die("could not read commit message: %s", strerror(saved_errno
));
1100 /* Truncate the message just before the diff, if any. */
1102 p
= strstr(sb
.buf
, "\ndiff --git ");
1104 strbuf_setlen(&sb
, p
- sb
.buf
+ 1);
1107 if (cleanup_mode
!= CLEANUP_NONE
)
1108 stripspace(&sb
, cleanup_mode
== CLEANUP_ALL
);
1109 if (message_is_empty(&sb
)) {
1110 rollback_index_files();
1111 fprintf(stderr
, "Aborting commit due to empty commit message.\n");
1115 if (commit_tree(sb
.buf
, active_cache_tree
->sha1
, parents
, commit_sha1
,
1116 fmt_ident(author_name
, author_email
, author_date
,
1117 IDENT_ERROR_ON_NO_NAME
))) {
1118 rollback_index_files();
1119 die("failed to write commit object");
1122 ref_lock
= lock_any_ref_for_update("HEAD",
1123 initial_commit
? NULL
: head_sha1
,
1126 nl
= strchr(sb
.buf
, '\n');
1128 strbuf_setlen(&sb
, nl
+ 1 - sb
.buf
);
1130 strbuf_addch(&sb
, '\n');
1131 strbuf_insert(&sb
, 0, reflog_msg
, strlen(reflog_msg
));
1132 strbuf_insert(&sb
, strlen(reflog_msg
), ": ", 2);
1135 rollback_index_files();
1136 die("cannot lock HEAD ref");
1138 if (write_ref_sha1(ref_lock
, commit_sha1
, sb
.buf
) < 0) {
1139 rollback_index_files();
1140 die("cannot update HEAD ref");
1143 unlink(git_path("MERGE_HEAD"));
1144 unlink(git_path("MERGE_MSG"));
1145 unlink(git_path("MERGE_MODE"));
1146 unlink(git_path("SQUASH_MSG"));
1148 if (commit_index_files())
1149 die ("Repository has been updated, but unable to write\n"
1150 "new_index file. Check that disk is not full or quota is\n"
1151 "not exceeded, and then \"git reset HEAD\" to recover.");
1154 run_hook(get_index_file(), "post-commit", NULL
);
1156 print_summary(prefix
, commit_sha1
);