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"
29 static const char * const builtin_commit_usage
[] = {
30 "git commit [options] [--] <filepattern>...",
34 static const char * const builtin_status_usage
[] = {
35 "git status [options] [--] <filepattern>...",
39 static const char implicit_ident_advice
[] =
40 "Your name and email address were configured automatically based\n"
41 "on your username and hostname. Please check that they are accurate.\n"
42 "You can suppress this message by setting them explicitly:\n"
44 " git config --global user.name \"Your Name\"\n"
45 " git config --global user.email you@example.com\n"
47 "If the identity used for this commit is wrong, you can fix it with:\n"
49 " git commit --amend --author='Your Name <you@example.com>'\n";
51 static const char empty_amend_advice
[] =
52 "You asked to amend the most recent commit, but doing so would make\n"
53 "it empty. You can repeat your command with --allow-empty, or you can\n"
54 "remove the commit entirely with \"git reset HEAD^\".\n";
56 static unsigned char head_sha1
[20];
58 static char *use_message_buffer
;
59 static const char commit_editmsg
[] = "COMMIT_EDITMSG";
60 static struct lock_file index_lock
; /* real index */
61 static struct lock_file false_lock
; /* used only for partial commits */
68 static const char *logfile
, *force_author
;
69 static const char *template_file
;
70 static char *edit_message
, *use_message
;
71 static char *author_name
, *author_email
, *author_date
;
72 static int all
, edit_flag
, also
, interactive
, only
, amend
, signoff
;
73 static int quiet
, verbose
, no_verify
, allow_empty
, dry_run
, renew_authorship
;
74 static int no_post_rewrite
;
75 static char *untracked_files_arg
, *force_date
;
77 * The default commit message cleanup mode will remove the lines
78 * beginning with # (shell comments) and leading and trailing
79 * whitespaces (empty lines or containing only whitespaces)
80 * if editor is used, and only the whitespaces if the message
81 * is specified explicitly.
88 static char *cleanup_arg
;
90 static int use_editor
= 1, initial_commit
, in_merge
, include_status
= 1;
91 static const char *only_include_assumed
;
92 static struct strbuf message
;
94 static int null_termination
;
98 STATUS_FORMAT_PORCELAIN
,
99 } status_format
= STATUS_FORMAT_LONG
;
101 static int opt_parse_m(const struct option
*opt
, const char *arg
, int unset
)
103 struct strbuf
*buf
= opt
->value
;
105 strbuf_setlen(buf
, 0);
107 strbuf_addstr(buf
, arg
);
108 strbuf_addstr(buf
, "\n\n");
113 static struct option builtin_commit_options
[] = {
115 OPT__VERBOSE(&verbose
),
117 OPT_GROUP("Commit message options"),
118 OPT_FILENAME('F', "file", &logfile
, "read log from file"),
119 OPT_STRING(0, "author", &force_author
, "AUTHOR", "override author for commit"),
120 OPT_STRING(0, "date", &force_date
, "DATE", "override date for commit"),
121 OPT_CALLBACK('m', "message", &message
, "MESSAGE", "specify commit message", opt_parse_m
),
122 OPT_STRING('c', "reedit-message", &edit_message
, "COMMIT", "reuse and edit message from specified commit"),
123 OPT_STRING('C', "reuse-message", &use_message
, "COMMIT", "reuse message from specified commit"),
124 OPT_BOOLEAN(0, "reset-author", &renew_authorship
, "the commit is authored by me now (used with -C-c/--amend)"),
125 OPT_BOOLEAN('s', "signoff", &signoff
, "add Signed-off-by:"),
126 OPT_FILENAME('t', "template", &template_file
, "use specified template file"),
127 OPT_BOOLEAN('e', "edit", &edit_flag
, "force edit of commit"),
128 OPT_STRING(0, "cleanup", &cleanup_arg
, "default", "how to strip spaces and #comments from message"),
129 OPT_BOOLEAN(0, "status", &include_status
, "include status in commit message template"),
130 /* end commit message options */
132 OPT_GROUP("Commit contents options"),
133 OPT_BOOLEAN('a', "all", &all
, "commit all changed files"),
134 OPT_BOOLEAN('i', "include", &also
, "add specified files to index for commit"),
135 OPT_BOOLEAN(0, "interactive", &interactive
, "interactively add files"),
136 OPT_BOOLEAN('o', "only", &only
, "commit only specified files"),
137 OPT_BOOLEAN('n', "no-verify", &no_verify
, "bypass pre-commit hook"),
138 OPT_BOOLEAN(0, "dry-run", &dry_run
, "show what would be committed"),
139 OPT_SET_INT(0, "short", &status_format
, "show status concisely",
140 STATUS_FORMAT_SHORT
),
141 OPT_SET_INT(0, "porcelain", &status_format
,
142 "show porcelain output format", STATUS_FORMAT_PORCELAIN
),
143 OPT_BOOLEAN('z', "null", &null_termination
,
144 "terminate entries with NUL"),
145 OPT_BOOLEAN(0, "amend", &amend
, "amend previous commit"),
146 OPT_BOOLEAN(0, "no-post-rewrite", &no_post_rewrite
, "bypass post-rewrite hook"),
147 { 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" },
148 OPT_BOOLEAN(0, "allow-empty", &allow_empty
, "ok to record an empty change"),
149 /* end commit contents options */
154 static void rollback_index_files(void)
156 switch (commit_style
) {
158 break; /* nothing to do */
160 rollback_lock_file(&index_lock
);
163 rollback_lock_file(&index_lock
);
164 rollback_lock_file(&false_lock
);
169 static int commit_index_files(void)
173 switch (commit_style
) {
175 break; /* nothing to do */
177 err
= commit_lock_file(&index_lock
);
180 err
= commit_lock_file(&index_lock
);
181 rollback_lock_file(&false_lock
);
189 * Take a union of paths in the index and the named tree (typically, "HEAD"),
190 * and return the paths that match the given pattern in list.
192 static int list_paths(struct string_list
*list
, const char *with_tree
,
193 const char *prefix
, const char **pattern
)
198 for (i
= 0; pattern
[i
]; i
++)
203 overlay_tree_on_cache(with_tree
, prefix
);
205 for (i
= 0; i
< active_nr
; i
++) {
206 struct cache_entry
*ce
= active_cache
[i
];
207 struct string_list_item
*item
;
209 if (ce
->ce_flags
& CE_UPDATE
)
211 if (!match_pathspec(pattern
, ce
->name
, ce_namelen(ce
), 0, m
))
213 item
= string_list_insert(ce
->name
, list
);
214 if (ce_skip_worktree(ce
))
215 item
->util
= item
; /* better a valid pointer than a fake one */
218 return report_path_error(m
, pattern
, prefix
? strlen(prefix
) : 0);
221 static void add_remove_files(struct string_list
*list
)
224 for (i
= 0; i
< list
->nr
; i
++) {
226 struct string_list_item
*p
= &(list
->items
[i
]);
228 /* p->util is skip-worktree */
232 if (!lstat(p
->string
, &st
)) {
233 if (add_to_cache(p
->string
, &st
, 0))
234 die("updating files failed");
236 remove_file_from_cache(p
->string
);
240 static void create_base_index(void)
243 struct unpack_trees_options opts
;
246 if (initial_commit
) {
251 memset(&opts
, 0, sizeof(opts
));
255 opts
.src_index
= &the_index
;
256 opts
.dst_index
= &the_index
;
258 opts
.fn
= oneway_merge
;
259 tree
= parse_tree_indirect(head_sha1
);
261 die("failed to unpack HEAD tree object");
263 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
264 if (unpack_trees(1, &t
, &opts
))
265 exit(128); /* We've already reported the error, finish dying */
268 static void refresh_cache_or_die(int refresh_flags
)
271 * refresh_flags contains REFRESH_QUIET, so the only errors
272 * are for unmerged entries.
274 if (refresh_cache(refresh_flags
| REFRESH_IN_PORCELAIN
))
275 die_resolve_conflict("commit");
278 static char *prepare_index(int argc
, const char **argv
, const char *prefix
, int is_status
)
281 struct string_list partial
;
282 const char **pathspec
= NULL
;
283 int refresh_flags
= REFRESH_QUIET
;
286 refresh_flags
|= REFRESH_UNMERGED
;
288 if (interactive_add(argc
, argv
, prefix
) != 0)
289 die("interactive add failed");
290 if (read_cache_preload(NULL
) < 0)
291 die("index file corrupt");
292 commit_style
= COMMIT_AS_IS
;
293 return get_index_file();
297 pathspec
= get_pathspec(prefix
, argv
);
299 if (read_cache_preload(pathspec
) < 0)
300 die("index file corrupt");
303 * Non partial, non as-is commit.
305 * (1) get the real index;
306 * (2) update the_index as necessary;
307 * (3) write the_index out to the real index (still locked);
308 * (4) return the name of the locked index file.
310 * The caller should run hooks on the locked real index, and
311 * (A) if all goes well, commit the real index;
312 * (B) on failure, rollback the real index.
314 if (all
|| (also
&& pathspec
&& *pathspec
)) {
315 fd
= hold_locked_index(&index_lock
, 1);
316 add_files_to_cache(also
? prefix
: NULL
, pathspec
, 0);
317 refresh_cache_or_die(refresh_flags
);
318 if (write_cache(fd
, active_cache
, active_nr
) ||
319 close_lock_file(&index_lock
))
320 die("unable to write new_index file");
321 commit_style
= COMMIT_NORMAL
;
322 return index_lock
.filename
;
328 * (1) return the name of the real index file.
330 * The caller should run hooks on the real index,
331 * and create commit from the_index.
332 * We still need to refresh the index here.
334 if (!pathspec
|| !*pathspec
) {
335 fd
= hold_locked_index(&index_lock
, 1);
336 refresh_cache_or_die(refresh_flags
);
337 if (write_cache(fd
, active_cache
, active_nr
) ||
338 commit_locked_index(&index_lock
))
339 die("unable to write new_index file");
340 commit_style
= COMMIT_AS_IS
;
341 return get_index_file();
347 * (0) find the set of affected paths;
348 * (1) get lock on the real index file;
349 * (2) update the_index with the given paths;
350 * (3) write the_index out to the real index (still locked);
351 * (4) get lock on the false index file;
352 * (5) reset the_index from HEAD;
353 * (6) update the_index the same way as (2);
354 * (7) write the_index out to the false index file;
355 * (8) return the name of the false index file (still locked);
357 * The caller should run hooks on the locked false index, and
358 * create commit from it. Then
359 * (A) if all goes well, commit the real index;
360 * (B) on failure, rollback the real index;
361 * In either case, rollback the false index.
363 commit_style
= COMMIT_PARTIAL
;
366 die("cannot do a partial commit during a merge.");
368 memset(&partial
, 0, sizeof(partial
));
369 partial
.strdup_strings
= 1;
370 if (list_paths(&partial
, initial_commit
? NULL
: "HEAD", prefix
, pathspec
))
374 if (read_cache() < 0)
375 die("cannot read the index");
377 fd
= hold_locked_index(&index_lock
, 1);
378 add_remove_files(&partial
);
379 refresh_cache(REFRESH_QUIET
);
380 if (write_cache(fd
, active_cache
, active_nr
) ||
381 close_lock_file(&index_lock
))
382 die("unable to write new_index file");
384 fd
= hold_lock_file_for_update(&false_lock
,
385 git_path("next-index-%"PRIuMAX
,
386 (uintmax_t) getpid()),
390 add_remove_files(&partial
);
391 refresh_cache(REFRESH_QUIET
);
393 if (write_cache(fd
, active_cache
, active_nr
) ||
394 close_lock_file(&false_lock
))
395 die("unable to write temporary index file");
398 read_cache_from(false_lock
.filename
);
400 return false_lock
.filename
;
403 static int run_status(FILE *fp
, const char *index_file
, const char *prefix
, int nowarn
,
406 unsigned char sha1
[20];
408 if (s
->relative_paths
)
413 s
->reference
= "HEAD^1";
415 s
->verbose
= verbose
;
416 s
->index_file
= index_file
;
419 s
->is_initial
= get_sha1(s
->reference
, sha1
) ? 1 : 0;
421 wt_status_collect(s
);
423 switch (status_format
) {
424 case STATUS_FORMAT_SHORT
:
425 wt_shortstatus_print(s
, null_termination
);
427 case STATUS_FORMAT_PORCELAIN
:
428 wt_porcelain_print(s
, null_termination
);
430 case STATUS_FORMAT_LONG
:
435 return s
->commitable
;
438 static int is_a_merge(const unsigned char *sha1
)
440 struct commit
*commit
= lookup_commit(sha1
);
441 if (!commit
|| parse_commit(commit
))
442 die("could not parse HEAD commit");
443 return !!(commit
->parents
&& commit
->parents
->next
);
446 static const char sign_off_header
[] = "Signed-off-by: ";
448 static void determine_author_info(void)
450 char *name
, *email
, *date
;
452 name
= getenv("GIT_AUTHOR_NAME");
453 email
= getenv("GIT_AUTHOR_EMAIL");
454 date
= getenv("GIT_AUTHOR_DATE");
456 if (use_message
&& !renew_authorship
) {
457 const char *a
, *lb
, *rb
, *eol
;
459 a
= strstr(use_message_buffer
, "\nauthor ");
461 die("invalid commit: %s", use_message
);
463 lb
= strchrnul(a
+ strlen("\nauthor "), '<');
464 rb
= strchrnul(lb
, '>');
465 eol
= strchrnul(rb
, '\n');
466 if (!*lb
|| !*rb
|| !*eol
)
467 die("invalid commit: %s", use_message
);
469 if (lb
== a
+ strlen("\nauthor "))
470 /* \nauthor <foo@example.com> */
471 name
= xcalloc(1, 1);
473 name
= xmemdupz(a
+ strlen("\nauthor "),
475 (a
+ strlen("\nauthor "))));
476 email
= xmemdupz(lb
+ strlen("<"), rb
- (lb
+ strlen("<")));
477 date
= xmemdupz(rb
+ strlen("> "), eol
- (rb
+ strlen("> ")));
481 const char *lb
= strstr(force_author
, " <");
482 const char *rb
= strchr(force_author
, '>');
485 die("malformed --author parameter");
486 name
= xstrndup(force_author
, lb
- force_author
);
487 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
494 author_email
= email
;
498 static int ends_rfc2822_footer(struct strbuf
*sb
)
505 const char *buf
= sb
->buf
;
507 for (i
= len
- 1; i
> 0; i
--) {
508 if (hit
&& buf
[i
] == '\n')
510 hit
= (buf
[i
] == '\n');
513 while (i
< len
- 1 && buf
[i
] == '\n')
516 for (; i
< len
; i
= k
) {
517 for (k
= i
; k
< len
&& buf
[k
] != '\n'; k
++)
521 if ((buf
[k
] == ' ' || buf
[k
] == '\t') && !first
)
526 for (j
= 0; i
+ j
< len
; j
++) {
539 static int prepare_to_commit(const char *index_file
, const char *prefix
,
543 int commitable
, saved_color_setting
;
544 struct strbuf sb
= STRBUF_INIT
;
547 const char *hook_arg1
= NULL
;
548 const char *hook_arg2
= NULL
;
551 if (!no_verify
&& run_hook(index_file
, "pre-commit", NULL
))
555 strbuf_addbuf(&sb
, &message
);
556 hook_arg1
= "message";
557 } else if (logfile
&& !strcmp(logfile
, "-")) {
559 fprintf(stderr
, "(reading log message from standard input)\n");
560 if (strbuf_read(&sb
, 0, 0) < 0)
561 die_errno("could not read log from standard input");
562 hook_arg1
= "message";
563 } else if (logfile
) {
564 if (strbuf_read_file(&sb
, logfile
, 0) < 0)
565 die_errno("could not read log file '%s'",
567 hook_arg1
= "message";
568 } else if (use_message
) {
569 buffer
= strstr(use_message_buffer
, "\n\n");
570 if (!buffer
|| buffer
[2] == '\0')
571 die("commit has empty message");
572 strbuf_add(&sb
, buffer
+ 2, strlen(buffer
+ 2));
573 hook_arg1
= "commit";
574 hook_arg2
= use_message
;
575 } else if (!stat(git_path("MERGE_MSG"), &statbuf
)) {
576 if (strbuf_read_file(&sb
, git_path("MERGE_MSG"), 0) < 0)
577 die_errno("could not read MERGE_MSG");
579 } else if (!stat(git_path("SQUASH_MSG"), &statbuf
)) {
580 if (strbuf_read_file(&sb
, git_path("SQUASH_MSG"), 0) < 0)
581 die_errno("could not read SQUASH_MSG");
582 hook_arg1
= "squash";
583 } else if (template_file
&& !stat(template_file
, &statbuf
)) {
584 if (strbuf_read_file(&sb
, template_file
, 0) < 0)
585 die_errno("could not read '%s'", template_file
);
586 hook_arg1
= "template";
590 * This final case does not modify the template message,
591 * it just sets the argument to the prepare-commit-msg hook.
596 fp
= fopen(git_path(commit_editmsg
), "w");
598 die_errno("could not open '%s'", git_path(commit_editmsg
));
600 if (cleanup_mode
!= CLEANUP_NONE
)
604 struct strbuf sob
= STRBUF_INIT
;
607 strbuf_addstr(&sob
, sign_off_header
);
608 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
609 getenv("GIT_COMMITTER_EMAIL")));
610 strbuf_addch(&sob
, '\n');
611 for (i
= sb
.len
- 1; i
> 0 && sb
.buf
[i
- 1] != '\n'; i
--)
613 if (prefixcmp(sb
.buf
+ i
, sob
.buf
)) {
614 if (!i
|| !ends_rfc2822_footer(&sb
))
615 strbuf_addch(&sb
, '\n');
616 strbuf_addbuf(&sb
, &sob
);
618 strbuf_release(&sob
);
621 if (fwrite(sb
.buf
, 1, sb
.len
, fp
) < sb
.len
)
622 die_errno("could not write commit template");
626 determine_author_info();
628 /* This checks if committer ident is explicitly given */
629 git_committer_info(0);
630 if (use_editor
&& include_status
) {
632 const char *committer_ident
;
637 "# It looks like you may be committing a MERGE.\n"
638 "# If this is not correct, please remove the file\n"
642 git_path("MERGE_HEAD"));
646 "# Please enter the commit message for your changes.");
647 if (cleanup_mode
== CLEANUP_ALL
)
650 "# with '#' will be ignored, and an empty"
651 " message aborts the commit.\n");
652 else /* CLEANUP_SPACE, that is. */
655 "# with '#' will be kept; you may remove them"
656 " yourself if you want to.\n"
657 "# An empty message aborts the commit.\n");
658 if (only_include_assumed
)
659 fprintf(fp
, "# %s\n", only_include_assumed
);
661 author_ident
= xstrdup(fmt_name(author_name
, author_email
));
662 committer_ident
= fmt_name(getenv("GIT_COMMITTER_NAME"),
663 getenv("GIT_COMMITTER_EMAIL"));
664 if (strcmp(author_ident
, committer_ident
))
668 ident_shown
++ ? "" : "#\n",
672 if (!user_ident_sufficiently_given())
676 ident_shown
++ ? "" : "#\n",
682 saved_color_setting
= s
->use_color
;
684 commitable
= run_status(fp
, index_file
, prefix
, 1, s
);
685 s
->use_color
= saved_color_setting
;
687 unsigned char sha1
[20];
688 const char *parent
= "HEAD";
690 if (!active_nr
&& read_cache() < 0)
691 die("Cannot read index");
696 if (get_sha1(parent
, sha1
))
697 commitable
= !!active_nr
;
699 commitable
= index_differs_from(parent
, 0);
704 if (!commitable
&& !in_merge
&& !allow_empty
&&
705 !(amend
&& is_a_merge(head_sha1
))) {
706 run_status(stdout
, index_file
, prefix
, 0, s
);
708 fputs(empty_amend_advice
, stderr
);
713 * Re-read the index as pre-commit hook could have updated it,
714 * and write it out as a tree. We must do this before we invoke
715 * the editor and after we invoke run_status above.
718 read_cache_from(index_file
);
719 if (!active_cache_tree
)
720 active_cache_tree
= cache_tree();
721 if (cache_tree_update(active_cache_tree
,
722 active_cache
, active_nr
, 0, 0) < 0) {
723 error("Error building trees");
727 if (run_hook(index_file
, "prepare-commit-msg",
728 git_path(commit_editmsg
), hook_arg1
, hook_arg2
, NULL
))
732 char index
[PATH_MAX
];
733 const char *env
[2] = { index
, NULL
};
734 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
735 if (launch_editor(git_path(commit_editmsg
), NULL
, env
)) {
737 "Please supply the message using either -m or -F option.\n");
743 run_hook(index_file
, "commit-msg", git_path(commit_editmsg
), NULL
)) {
751 * Find out if the message in the strbuf contains only whitespace and
752 * Signed-off-by lines.
754 static int message_is_empty(struct strbuf
*sb
)
756 struct strbuf tmpl
= STRBUF_INIT
;
758 int eol
, i
, start
= 0;
760 if (cleanup_mode
== CLEANUP_NONE
&& sb
->len
)
763 /* See if the template is just a prefix of the message. */
764 if (template_file
&& strbuf_read_file(&tmpl
, template_file
, 0) > 0) {
765 stripspace(&tmpl
, cleanup_mode
== CLEANUP_ALL
);
766 if (start
+ tmpl
.len
<= sb
->len
&&
767 memcmp(tmpl
.buf
, sb
->buf
+ start
, tmpl
.len
) == 0)
770 strbuf_release(&tmpl
);
772 /* Check if the rest is just whitespace and Signed-of-by's. */
773 for (i
= start
; i
< sb
->len
; i
++) {
774 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
780 if (strlen(sign_off_header
) <= eol
- i
&&
781 !prefixcmp(sb
->buf
+ i
, sign_off_header
)) {
786 if (!isspace(sb
->buf
[i
++]))
793 static const char *find_author_by_nickname(const char *name
)
795 struct rev_info revs
;
796 struct commit
*commit
;
797 struct strbuf buf
= STRBUF_INIT
;
801 init_revisions(&revs
, NULL
);
802 strbuf_addf(&buf
, "--author=%s", name
);
807 setup_revisions(ac
, av
, &revs
, NULL
);
808 prepare_revision_walk(&revs
);
809 commit
= get_revision(&revs
);
811 struct pretty_print_context ctx
= {0};
812 ctx
.date_mode
= DATE_NORMAL
;
813 strbuf_release(&buf
);
814 format_commit_message(commit
, "%an <%ae>", &buf
, &ctx
);
815 return strbuf_detach(&buf
, NULL
);
817 die("No existing author found with '%s'", name
);
821 static void handle_untracked_files_arg(struct wt_status
*s
)
823 if (!untracked_files_arg
)
824 ; /* default already initialized */
825 else if (!strcmp(untracked_files_arg
, "no"))
826 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
827 else if (!strcmp(untracked_files_arg
, "normal"))
828 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
829 else if (!strcmp(untracked_files_arg
, "all"))
830 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
832 die("Invalid untracked files mode '%s'", untracked_files_arg
);
835 static int parse_and_validate_options(int argc
, const char *argv
[],
836 const char * const usage
[],
842 argc
= parse_options(argc
, argv
, prefix
, builtin_commit_options
, usage
,
845 if (force_author
&& !strchr(force_author
, '>'))
846 force_author
= find_author_by_nickname(force_author
);
848 if (force_author
&& renew_authorship
)
849 die("Using both --reset-author and --author does not make sense");
851 if (logfile
|| message
.len
|| use_message
)
856 setenv("GIT_EDITOR", ":", 1);
858 if (get_sha1("HEAD", head_sha1
))
861 /* Sanity check options */
862 if (amend
&& initial_commit
)
863 die("You have nothing to amend.");
864 if (amend
&& in_merge
)
865 die("You are in the middle of a merge -- cannot amend.");
874 die("Only one of -c/-C/-F can be used.");
875 if (message
.len
&& f
> 0)
876 die("Option -m cannot be combined with -c/-C/-F.");
878 use_message
= edit_message
;
879 if (amend
&& !use_message
)
880 use_message
= "HEAD";
881 if (!use_message
&& renew_authorship
)
882 die("--reset-author can be used only with -C, -c or --amend.");
884 unsigned char sha1
[20];
885 static char utf8
[] = "UTF-8";
888 struct commit
*commit
;
890 if (get_sha1(use_message
, sha1
))
891 die("could not lookup commit %s", use_message
);
892 commit
= lookup_commit_reference(sha1
);
893 if (!commit
|| parse_commit(commit
))
894 die("could not parse commit %s", use_message
);
896 enc
= strstr(commit
->buffer
, "\nencoding");
898 end
= strchr(enc
+ 10, '\n');
899 enc
= xstrndup(enc
+ 10, end
- (enc
+ 10));
903 out_enc
= git_commit_encoding
? git_commit_encoding
: utf8
;
905 if (strcmp(out_enc
, enc
))
907 reencode_string(commit
->buffer
, out_enc
, enc
);
910 * If we failed to reencode the buffer, just copy it
911 * byte for byte so the user can try to fix it up.
912 * This also handles the case where input and output
913 * encodings are identical.
915 if (use_message_buffer
== NULL
)
916 use_message_buffer
= xstrdup(commit
->buffer
);
921 if (!!also
+ !!only
+ !!all
+ !!interactive
> 1)
922 die("Only one of --include/--only/--all/--interactive can be used.");
923 if (argc
== 0 && (also
|| (only
&& !amend
)))
924 die("No paths with --include/--only does not make sense.");
925 if (argc
== 0 && only
&& amend
)
926 only_include_assumed
= "Clever... amending the last one with dirty index.";
927 if (argc
> 0 && !also
&& !only
)
928 only_include_assumed
= "Explicit paths specified without -i nor -o; assuming --only paths...";
929 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "default"))
930 cleanup_mode
= use_editor
? CLEANUP_ALL
: CLEANUP_SPACE
;
931 else if (!strcmp(cleanup_arg
, "verbatim"))
932 cleanup_mode
= CLEANUP_NONE
;
933 else if (!strcmp(cleanup_arg
, "whitespace"))
934 cleanup_mode
= CLEANUP_SPACE
;
935 else if (!strcmp(cleanup_arg
, "strip"))
936 cleanup_mode
= CLEANUP_ALL
;
938 die("Invalid cleanup mode %s", cleanup_arg
);
940 handle_untracked_files_arg(s
);
943 die("Paths with -a does not make sense.");
944 else if (interactive
&& argc
> 0)
945 die("Paths with --interactive does not make sense.");
947 if (null_termination
&& status_format
== STATUS_FORMAT_LONG
)
948 status_format
= STATUS_FORMAT_PORCELAIN
;
949 if (status_format
!= STATUS_FORMAT_LONG
)
955 static int dry_run_commit(int argc
, const char **argv
, const char *prefix
,
959 const char *index_file
;
961 index_file
= prepare_index(argc
, argv
, prefix
, 1);
962 commitable
= run_status(stdout
, index_file
, prefix
, 0, s
);
963 rollback_index_files();
965 return commitable
? 0 : 1;
968 static int parse_status_slot(const char *var
, int offset
)
970 if (!strcasecmp(var
+offset
, "header"))
971 return WT_STATUS_HEADER
;
972 if (!strcasecmp(var
+offset
, "updated")
973 || !strcasecmp(var
+offset
, "added"))
974 return WT_STATUS_UPDATED
;
975 if (!strcasecmp(var
+offset
, "changed"))
976 return WT_STATUS_CHANGED
;
977 if (!strcasecmp(var
+offset
, "untracked"))
978 return WT_STATUS_UNTRACKED
;
979 if (!strcasecmp(var
+offset
, "nobranch"))
980 return WT_STATUS_NOBRANCH
;
981 if (!strcasecmp(var
+offset
, "unmerged"))
982 return WT_STATUS_UNMERGED
;
986 static int git_status_config(const char *k
, const char *v
, void *cb
)
988 struct wt_status
*s
= cb
;
990 if (!strcmp(k
, "status.submodulesummary")) {
992 s
->submodule_summary
= git_config_bool_or_int(k
, v
, &is_bool
);
993 if (is_bool
&& s
->submodule_summary
)
994 s
->submodule_summary
= -1;
997 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
998 s
->use_color
= git_config_colorbool(k
, v
, -1);
1001 if (!prefixcmp(k
, "status.color.") || !prefixcmp(k
, "color.status.")) {
1002 int slot
= parse_status_slot(k
, 13);
1006 return config_error_nonbool(k
);
1007 color_parse(v
, k
, s
->color_palette
[slot
]);
1010 if (!strcmp(k
, "status.relativepaths")) {
1011 s
->relative_paths
= git_config_bool(k
, v
);
1014 if (!strcmp(k
, "status.showuntrackedfiles")) {
1016 return config_error_nonbool(k
);
1017 else if (!strcmp(v
, "no"))
1018 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
1019 else if (!strcmp(v
, "normal"))
1020 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
1021 else if (!strcmp(v
, "all"))
1022 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
1024 return error("Invalid untracked files mode '%s'", v
);
1027 return git_diff_ui_config(k
, v
, NULL
);
1030 int cmd_status(int argc
, const char **argv
, const char *prefix
)
1034 unsigned char sha1
[20];
1035 static struct option builtin_status_options
[] = {
1036 OPT__VERBOSE(&verbose
),
1037 OPT_SET_INT('s', "short", &status_format
,
1038 "show status concisely", STATUS_FORMAT_SHORT
),
1039 OPT_SET_INT(0, "porcelain", &status_format
,
1040 "show porcelain output format",
1041 STATUS_FORMAT_PORCELAIN
),
1042 OPT_BOOLEAN('z', "null", &null_termination
,
1043 "terminate entries with NUL"),
1044 { OPTION_STRING
, 'u', "untracked-files", &untracked_files_arg
,
1046 "show untracked files, optional modes: all, normal, no. (Default: all)",
1047 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1051 if (null_termination
&& status_format
== STATUS_FORMAT_LONG
)
1052 status_format
= STATUS_FORMAT_PORCELAIN
;
1054 wt_status_prepare(&s
);
1055 git_config(git_status_config
, &s
);
1056 in_merge
= file_exists(git_path("MERGE_HEAD"));
1057 argc
= parse_options(argc
, argv
, prefix
,
1058 builtin_status_options
,
1059 builtin_status_usage
, 0);
1060 handle_untracked_files_arg(&s
);
1063 s
.pathspec
= get_pathspec(prefix
, argv
);
1065 read_cache_preload(s
.pathspec
);
1066 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, s
.pathspec
, NULL
, NULL
);
1068 fd
= hold_locked_index(&index_lock
, 0);
1070 if (!write_cache(fd
, active_cache
, active_nr
))
1071 commit_locked_index(&index_lock
);
1072 rollback_lock_file(&index_lock
);
1075 s
.is_initial
= get_sha1(s
.reference
, sha1
) ? 1 : 0;
1076 s
.in_merge
= in_merge
;
1077 wt_status_collect(&s
);
1079 if (s
.relative_paths
)
1081 if (s
.use_color
== -1)
1082 s
.use_color
= git_use_color_default
;
1083 if (diff_use_color_default
== -1)
1084 diff_use_color_default
= git_use_color_default
;
1086 switch (status_format
) {
1087 case STATUS_FORMAT_SHORT
:
1088 wt_shortstatus_print(&s
, null_termination
);
1090 case STATUS_FORMAT_PORCELAIN
:
1091 wt_porcelain_print(&s
, null_termination
);
1093 case STATUS_FORMAT_LONG
:
1094 s
.verbose
= verbose
;
1095 wt_status_print(&s
);
1101 static void print_summary(const char *prefix
, const unsigned char *sha1
)
1103 struct rev_info rev
;
1104 struct commit
*commit
;
1105 struct strbuf format
= STRBUF_INIT
;
1106 unsigned char junk_sha1
[20];
1107 const char *head
= resolve_ref("HEAD", junk_sha1
, 0, NULL
);
1108 struct pretty_print_context pctx
= {0};
1109 struct strbuf author_ident
= STRBUF_INIT
;
1110 struct strbuf committer_ident
= STRBUF_INIT
;
1112 commit
= lookup_commit(sha1
);
1114 die("couldn't look up newly created commit");
1115 if (!commit
|| parse_commit(commit
))
1116 die("could not parse newly created commit");
1118 strbuf_addstr(&format
, "format:%h] %s");
1120 format_commit_message(commit
, "%an <%ae>", &author_ident
, &pctx
);
1121 format_commit_message(commit
, "%cn <%ce>", &committer_ident
, &pctx
);
1122 if (strbuf_cmp(&author_ident
, &committer_ident
)) {
1123 strbuf_addstr(&format
, "\n Author: ");
1124 strbuf_addbuf_percentquote(&format
, &author_ident
);
1126 if (!user_ident_sufficiently_given()) {
1127 strbuf_addstr(&format
, "\n Committer: ");
1128 strbuf_addbuf_percentquote(&format
, &committer_ident
);
1129 if (advice_implicit_identity
) {
1130 strbuf_addch(&format
, '\n');
1131 strbuf_addstr(&format
, implicit_ident_advice
);
1134 strbuf_release(&author_ident
);
1135 strbuf_release(&committer_ident
);
1137 init_revisions(&rev
, prefix
);
1138 setup_revisions(0, NULL
, &rev
, NULL
);
1142 rev
.diffopt
.output_format
=
1143 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
1145 rev
.verbose_header
= 1;
1146 rev
.show_root_diff
= 1;
1147 get_commit_format(format
.buf
, &rev
);
1148 rev
.always_show_header
= 0;
1149 rev
.diffopt
.detect_rename
= 1;
1150 rev
.diffopt
.rename_limit
= 100;
1151 rev
.diffopt
.break_opt
= 0;
1152 diff_setup_done(&rev
.diffopt
);
1155 !prefixcmp(head
, "refs/heads/") ?
1157 !strcmp(head
, "HEAD") ?
1160 initial_commit
? " (root-commit)" : "");
1162 if (!log_tree_commit(&rev
, commit
)) {
1163 rev
.always_show_header
= 1;
1164 rev
.use_terminator
= 1;
1165 log_tree_commit(&rev
, commit
);
1168 strbuf_release(&format
);
1171 static int git_commit_config(const char *k
, const char *v
, void *cb
)
1173 struct wt_status
*s
= cb
;
1175 if (!strcmp(k
, "commit.template"))
1176 return git_config_pathname(&template_file
, k
, v
);
1177 if (!strcmp(k
, "commit.status")) {
1178 include_status
= git_config_bool(k
, v
);
1182 return git_status_config(k
, v
, s
);
1185 static const char post_rewrite_hook
[] = "hooks/post-rewrite";
1187 static int run_rewrite_hook(const unsigned char *oldsha1
,
1188 const unsigned char *newsha1
)
1190 /* oldsha1 SP newsha1 LF NUL */
1191 static char buf
[2*40 + 3];
1192 struct child_process proc
;
1193 const char *argv
[3];
1197 if (access(git_path(post_rewrite_hook
), X_OK
) < 0)
1200 argv
[0] = git_path(post_rewrite_hook
);
1204 memset(&proc
, 0, sizeof(proc
));
1207 proc
.stdout_to_stderr
= 1;
1209 code
= start_command(&proc
);
1212 n
= snprintf(buf
, sizeof(buf
), "%s %s\n",
1213 sha1_to_hex(oldsha1
), sha1_to_hex(newsha1
));
1214 write_in_full(proc
.in
, buf
, n
);
1216 return finish_command(&proc
);
1219 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
1221 struct strbuf sb
= STRBUF_INIT
;
1222 const char *index_file
, *reflog_msg
;
1224 unsigned char commit_sha1
[20];
1225 struct ref_lock
*ref_lock
;
1226 struct commit_list
*parents
= NULL
, **pptr
= &parents
;
1227 struct stat statbuf
;
1228 int allow_fast_forward
= 1;
1231 wt_status_prepare(&s
);
1232 git_config(git_commit_config
, &s
);
1233 in_merge
= file_exists(git_path("MERGE_HEAD"));
1234 s
.in_merge
= in_merge
;
1236 if (s
.use_color
== -1)
1237 s
.use_color
= git_use_color_default
;
1238 argc
= parse_and_validate_options(argc
, argv
, builtin_commit_usage
,
1241 if (diff_use_color_default
== -1)
1242 diff_use_color_default
= git_use_color_default
;
1243 return dry_run_commit(argc
, argv
, prefix
, &s
);
1245 index_file
= prepare_index(argc
, argv
, prefix
, 0);
1247 /* Set up everything for writing the commit object. This includes
1248 running hooks, writing the trees, and interacting with the user. */
1249 if (!prepare_to_commit(index_file
, prefix
, &s
)) {
1250 rollback_index_files();
1254 /* Determine parents */
1255 reflog_msg
= getenv("GIT_REFLOG_ACTION");
1256 if (initial_commit
) {
1258 reflog_msg
= "commit (initial)";
1260 struct commit_list
*c
;
1261 struct commit
*commit
;
1264 reflog_msg
= "commit (amend)";
1265 commit
= lookup_commit(head_sha1
);
1266 if (!commit
|| parse_commit(commit
))
1267 die("could not parse HEAD commit");
1269 for (c
= commit
->parents
; c
; c
= c
->next
)
1270 pptr
= &commit_list_insert(c
->item
, pptr
)->next
;
1271 } else if (in_merge
) {
1272 struct strbuf m
= STRBUF_INIT
;
1276 reflog_msg
= "commit (merge)";
1277 pptr
= &commit_list_insert(lookup_commit(head_sha1
), pptr
)->next
;
1278 fp
= fopen(git_path("MERGE_HEAD"), "r");
1280 die_errno("could not open '%s' for reading",
1281 git_path("MERGE_HEAD"));
1282 while (strbuf_getline(&m
, fp
, '\n') != EOF
) {
1283 unsigned char sha1
[20];
1284 if (get_sha1_hex(m
.buf
, sha1
) < 0)
1285 die("Corrupt MERGE_HEAD file (%s)", m
.buf
);
1286 pptr
= &commit_list_insert(lookup_commit(sha1
), pptr
)->next
;
1290 if (!stat(git_path("MERGE_MODE"), &statbuf
)) {
1291 if (strbuf_read_file(&sb
, git_path("MERGE_MODE"), 0) < 0)
1292 die_errno("could not read MERGE_MODE");
1293 if (!strcmp(sb
.buf
, "no-ff"))
1294 allow_fast_forward
= 0;
1296 if (allow_fast_forward
)
1297 parents
= reduce_heads(parents
);
1300 reflog_msg
= "commit";
1301 pptr
= &commit_list_insert(lookup_commit(head_sha1
), pptr
)->next
;
1304 /* Finally, get the commit message */
1306 if (strbuf_read_file(&sb
, git_path(commit_editmsg
), 0) < 0) {
1307 int saved_errno
= errno
;
1308 rollback_index_files();
1309 die("could not read commit message: %s", strerror(saved_errno
));
1312 /* Truncate the message just before the diff, if any. */
1314 p
= strstr(sb
.buf
, "\ndiff --git ");
1316 strbuf_setlen(&sb
, p
- sb
.buf
+ 1);
1319 if (cleanup_mode
!= CLEANUP_NONE
)
1320 stripspace(&sb
, cleanup_mode
== CLEANUP_ALL
);
1321 if (message_is_empty(&sb
)) {
1322 rollback_index_files();
1323 fprintf(stderr
, "Aborting commit due to empty commit message.\n");
1327 if (commit_tree(sb
.buf
, active_cache_tree
->sha1
, parents
, commit_sha1
,
1328 fmt_ident(author_name
, author_email
, author_date
,
1329 IDENT_ERROR_ON_NO_NAME
))) {
1330 rollback_index_files();
1331 die("failed to write commit object");
1334 ref_lock
= lock_any_ref_for_update("HEAD",
1335 initial_commit
? NULL
: head_sha1
,
1338 nl
= strchr(sb
.buf
, '\n');
1340 strbuf_setlen(&sb
, nl
+ 1 - sb
.buf
);
1342 strbuf_addch(&sb
, '\n');
1343 strbuf_insert(&sb
, 0, reflog_msg
, strlen(reflog_msg
));
1344 strbuf_insert(&sb
, strlen(reflog_msg
), ": ", 2);
1347 rollback_index_files();
1348 die("cannot lock HEAD ref");
1350 if (write_ref_sha1(ref_lock
, commit_sha1
, sb
.buf
) < 0) {
1351 rollback_index_files();
1352 die("cannot update HEAD ref");
1355 unlink(git_path("MERGE_HEAD"));
1356 unlink(git_path("MERGE_MSG"));
1357 unlink(git_path("MERGE_MODE"));
1358 unlink(git_path("SQUASH_MSG"));
1360 if (commit_index_files())
1361 die ("Repository has been updated, but unable to write\n"
1362 "new_index file. Check that disk is not full or quota is\n"
1363 "not exceeded, and then \"git reset HEAD\" to recover.");
1366 run_hook(get_index_file(), "post-commit", NULL
);
1367 if (amend
&& !no_post_rewrite
) {
1368 struct notes_rewrite_cfg
*cfg
;
1369 cfg
= init_copy_notes_for_rewrite("amend");
1371 copy_note_for_rewrite(cfg
, head_sha1
, commit_sha1
);
1372 finish_copy_notes_for_rewrite(cfg
);
1374 run_rewrite_hook(head_sha1
, commit_sha1
);
1377 print_summary(prefix
, commit_sha1
);