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 #include "submodule.h"
30 static const char * const builtin_commit_usage
[] = {
31 "git commit [options] [--] <filepattern>...",
35 static const char * const builtin_status_usage
[] = {
36 "git status [options] [--] <filepattern>...",
40 static const char implicit_ident_advice
[] =
41 "Your name and email address were configured automatically based\n"
42 "on your username and hostname. Please check that they are accurate.\n"
43 "You can suppress this message by setting them explicitly:\n"
45 " git config --global user.name \"Your Name\"\n"
46 " git config --global user.email you@example.com\n"
48 "After doing this, you may fix the identity used for this commit with:\n"
50 " git commit --amend --reset-author\n";
52 static const char empty_amend_advice
[] =
53 "You asked to amend the most recent commit, but doing so would make\n"
54 "it empty. You can repeat your command with --allow-empty, or you can\n"
55 "remove the commit entirely with \"git reset HEAD^\".\n";
57 static unsigned char head_sha1
[20];
59 static char *use_message_buffer
;
60 static const char commit_editmsg
[] = "COMMIT_EDITMSG";
61 static struct lock_file index_lock
; /* real index */
62 static struct lock_file false_lock
; /* used only for partial commits */
69 static const char *logfile
, *force_author
;
70 static const char *template_file
;
71 static char *edit_message
, *use_message
;
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
, allow_empty_message
;
75 static char *untracked_files_arg
, *force_date
, *ignore_submodule_arg
;
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 int show_ignored_in_status
;
92 static const char *only_include_assumed
;
93 static struct strbuf message
;
95 static int null_termination
;
99 STATUS_FORMAT_PORCELAIN
100 } status_format
= STATUS_FORMAT_LONG
;
101 static int status_show_branch
;
103 static int opt_parse_m(const struct option
*opt
, const char *arg
, int unset
)
105 struct strbuf
*buf
= opt
->value
;
107 strbuf_setlen(buf
, 0);
109 strbuf_addstr(buf
, arg
);
110 strbuf_addstr(buf
, "\n\n");
115 static struct option builtin_commit_options
[] = {
117 OPT__VERBOSE(&verbose
),
119 OPT_GROUP("Commit message options"),
120 OPT_FILENAME('F', "file", &logfile
, "read log from file"),
121 OPT_STRING(0, "author", &force_author
, "AUTHOR", "override author for commit"),
122 OPT_STRING(0, "date", &force_date
, "DATE", "override date for commit"),
123 OPT_CALLBACK('m', "message", &message
, "MESSAGE", "specify commit message", opt_parse_m
),
124 OPT_STRING('c', "reedit-message", &edit_message
, "COMMIT", "reuse and edit message from specified commit"),
125 OPT_STRING('C', "reuse-message", &use_message
, "COMMIT", "reuse message from specified commit"),
126 OPT_BOOLEAN(0, "reset-author", &renew_authorship
, "the commit is authored by me now (used with -C-c/--amend)"),
127 OPT_BOOLEAN('s', "signoff", &signoff
, "add Signed-off-by:"),
128 OPT_FILENAME('t', "template", &template_file
, "use specified template file"),
129 OPT_BOOLEAN('e', "edit", &edit_flag
, "force edit of commit"),
130 OPT_STRING(0, "cleanup", &cleanup_arg
, "default", "how to strip spaces and #comments from message"),
131 OPT_BOOLEAN(0, "status", &include_status
, "include status in commit message template"),
132 /* end commit message options */
134 OPT_GROUP("Commit contents options"),
135 OPT_BOOLEAN('a', "all", &all
, "commit all changed files"),
136 OPT_BOOLEAN('i', "include", &also
, "add specified files to index for commit"),
137 OPT_BOOLEAN(0, "interactive", &interactive
, "interactively add files"),
138 OPT_BOOLEAN('o', "only", &only
, "commit only specified files"),
139 OPT_BOOLEAN('n', "no-verify", &no_verify
, "bypass pre-commit hook"),
140 OPT_BOOLEAN(0, "dry-run", &dry_run
, "show what would be committed"),
141 OPT_SET_INT(0, "short", &status_format
, "show status concisely",
142 STATUS_FORMAT_SHORT
),
143 OPT_BOOLEAN(0, "branch", &status_show_branch
, "show branch information"),
144 OPT_SET_INT(0, "porcelain", &status_format
,
145 "show porcelain output format", STATUS_FORMAT_PORCELAIN
),
146 OPT_BOOLEAN('z', "null", &null_termination
,
147 "terminate entries with NUL"),
148 OPT_BOOLEAN(0, "amend", &amend
, "amend previous commit"),
149 OPT_BOOLEAN(0, "no-post-rewrite", &no_post_rewrite
, "bypass post-rewrite hook"),
150 { 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" },
151 /* end commit contents options */
153 { OPTION_BOOLEAN
, 0, "allow-empty", &allow_empty
, NULL
,
154 "ok to record an empty change",
155 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
},
156 { OPTION_BOOLEAN
, 0, "allow-empty-message", &allow_empty_message
, NULL
,
157 "ok to record a change with an empty message",
158 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
},
163 static void rollback_index_files(void)
165 switch (commit_style
) {
167 break; /* nothing to do */
169 rollback_lock_file(&index_lock
);
172 rollback_lock_file(&index_lock
);
173 rollback_lock_file(&false_lock
);
178 static int commit_index_files(void)
182 switch (commit_style
) {
184 break; /* nothing to do */
186 err
= commit_lock_file(&index_lock
);
189 err
= commit_lock_file(&index_lock
);
190 rollback_lock_file(&false_lock
);
198 * Take a union of paths in the index and the named tree (typically, "HEAD"),
199 * and return the paths that match the given pattern in list.
201 static int list_paths(struct string_list
*list
, const char *with_tree
,
202 const char *prefix
, const char **pattern
)
207 for (i
= 0; pattern
[i
]; i
++)
212 overlay_tree_on_cache(with_tree
, prefix
);
214 for (i
= 0; i
< active_nr
; i
++) {
215 struct cache_entry
*ce
= active_cache
[i
];
216 struct string_list_item
*item
;
218 if (ce
->ce_flags
& CE_UPDATE
)
220 if (!match_pathspec(pattern
, ce
->name
, ce_namelen(ce
), 0, m
))
222 item
= string_list_insert(list
, ce
->name
);
223 if (ce_skip_worktree(ce
))
224 item
->util
= item
; /* better a valid pointer than a fake one */
227 return report_path_error(m
, pattern
, prefix
? strlen(prefix
) : 0);
230 static void add_remove_files(struct string_list
*list
)
233 for (i
= 0; i
< list
->nr
; i
++) {
235 struct string_list_item
*p
= &(list
->items
[i
]);
237 /* p->util is skip-worktree */
241 if (!lstat(p
->string
, &st
)) {
242 if (add_to_cache(p
->string
, &st
, 0))
243 die("updating files failed");
245 remove_file_from_cache(p
->string
);
249 static void create_base_index(void)
252 struct unpack_trees_options opts
;
255 if (initial_commit
) {
260 memset(&opts
, 0, sizeof(opts
));
264 opts
.src_index
= &the_index
;
265 opts
.dst_index
= &the_index
;
267 opts
.fn
= oneway_merge
;
268 tree
= parse_tree_indirect(head_sha1
);
270 die("failed to unpack HEAD tree object");
272 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
273 if (unpack_trees(1, &t
, &opts
))
274 exit(128); /* We've already reported the error, finish dying */
277 static void refresh_cache_or_die(int refresh_flags
)
280 * refresh_flags contains REFRESH_QUIET, so the only errors
281 * are for unmerged entries.
283 if (refresh_cache(refresh_flags
| REFRESH_IN_PORCELAIN
))
284 die_resolve_conflict("commit");
287 static char *prepare_index(int argc
, const char **argv
, const char *prefix
, int is_status
)
290 struct string_list partial
;
291 const char **pathspec
= NULL
;
292 int refresh_flags
= REFRESH_QUIET
;
295 refresh_flags
|= REFRESH_UNMERGED
;
297 if (interactive_add(argc
, argv
, prefix
) != 0)
298 die("interactive add failed");
299 if (read_cache_preload(NULL
) < 0)
300 die("index file corrupt");
301 commit_style
= COMMIT_AS_IS
;
302 return get_index_file();
306 pathspec
= get_pathspec(prefix
, argv
);
308 if (read_cache_preload(pathspec
) < 0)
309 die("index file corrupt");
312 * Non partial, non as-is commit.
314 * (1) get the real index;
315 * (2) update the_index as necessary;
316 * (3) write the_index out to the real index (still locked);
317 * (4) return the name of the locked index file.
319 * The caller should run hooks on the locked real index, and
320 * (A) if all goes well, commit the real index;
321 * (B) on failure, rollback the real index.
323 if (all
|| (also
&& pathspec
&& *pathspec
)) {
324 fd
= hold_locked_index(&index_lock
, 1);
325 add_files_to_cache(also
? prefix
: NULL
, pathspec
, 0);
326 refresh_cache_or_die(refresh_flags
);
327 if (write_cache(fd
, active_cache
, active_nr
) ||
328 close_lock_file(&index_lock
))
329 die("unable to write new_index file");
330 commit_style
= COMMIT_NORMAL
;
331 return index_lock
.filename
;
337 * (1) return the name of the real index file.
339 * The caller should run hooks on the real index,
340 * and create commit from the_index.
341 * We still need to refresh the index here.
343 if (!pathspec
|| !*pathspec
) {
344 fd
= hold_locked_index(&index_lock
, 1);
345 refresh_cache_or_die(refresh_flags
);
346 if (active_cache_changed
) {
347 if (write_cache(fd
, active_cache
, active_nr
) ||
348 commit_locked_index(&index_lock
))
349 die("unable to write new_index file");
351 rollback_lock_file(&index_lock
);
353 commit_style
= COMMIT_AS_IS
;
354 return get_index_file();
360 * (0) find the set of affected paths;
361 * (1) get lock on the real index file;
362 * (2) update the_index with the given paths;
363 * (3) write the_index out to the real index (still locked);
364 * (4) get lock on the false index file;
365 * (5) reset the_index from HEAD;
366 * (6) update the_index the same way as (2);
367 * (7) write the_index out to the false index file;
368 * (8) return the name of the false index file (still locked);
370 * The caller should run hooks on the locked false index, and
371 * create commit from it. Then
372 * (A) if all goes well, commit the real index;
373 * (B) on failure, rollback the real index;
374 * In either case, rollback the false index.
376 commit_style
= COMMIT_PARTIAL
;
379 die("cannot do a partial commit during a merge.");
381 memset(&partial
, 0, sizeof(partial
));
382 partial
.strdup_strings
= 1;
383 if (list_paths(&partial
, initial_commit
? NULL
: "HEAD", prefix
, pathspec
))
387 if (read_cache() < 0)
388 die("cannot read the index");
390 fd
= hold_locked_index(&index_lock
, 1);
391 add_remove_files(&partial
);
392 refresh_cache(REFRESH_QUIET
);
393 if (write_cache(fd
, active_cache
, active_nr
) ||
394 close_lock_file(&index_lock
))
395 die("unable to write new_index file");
397 fd
= hold_lock_file_for_update(&false_lock
,
398 git_path("next-index-%"PRIuMAX
,
399 (uintmax_t) getpid()),
403 add_remove_files(&partial
);
404 refresh_cache(REFRESH_QUIET
);
406 if (write_cache(fd
, active_cache
, active_nr
) ||
407 close_lock_file(&false_lock
))
408 die("unable to write temporary index file");
411 read_cache_from(false_lock
.filename
);
413 return false_lock
.filename
;
416 static int run_status(FILE *fp
, const char *index_file
, const char *prefix
, int nowarn
,
419 unsigned char sha1
[20];
421 if (s
->relative_paths
)
426 s
->reference
= "HEAD^1";
428 s
->verbose
= verbose
;
429 s
->index_file
= index_file
;
432 s
->is_initial
= get_sha1(s
->reference
, sha1
) ? 1 : 0;
434 wt_status_collect(s
);
436 switch (status_format
) {
437 case STATUS_FORMAT_SHORT
:
438 wt_shortstatus_print(s
, null_termination
, status_show_branch
);
440 case STATUS_FORMAT_PORCELAIN
:
441 wt_porcelain_print(s
, null_termination
);
443 case STATUS_FORMAT_LONG
:
448 return s
->commitable
;
451 static int is_a_merge(const unsigned char *sha1
)
453 struct commit
*commit
= lookup_commit(sha1
);
454 if (!commit
|| parse_commit(commit
))
455 die("could not parse HEAD commit");
456 return !!(commit
->parents
&& commit
->parents
->next
);
459 static const char sign_off_header
[] = "Signed-off-by: ";
461 static void determine_author_info(struct strbuf
*author_ident
)
463 char *name
, *email
, *date
;
465 name
= getenv("GIT_AUTHOR_NAME");
466 email
= getenv("GIT_AUTHOR_EMAIL");
467 date
= getenv("GIT_AUTHOR_DATE");
469 if (use_message
&& !renew_authorship
) {
470 const char *a
, *lb
, *rb
, *eol
;
472 a
= strstr(use_message_buffer
, "\nauthor ");
474 die("invalid commit: %s", use_message
);
476 lb
= strchrnul(a
+ strlen("\nauthor "), '<');
477 rb
= strchrnul(lb
, '>');
478 eol
= strchrnul(rb
, '\n');
479 if (!*lb
|| !*rb
|| !*eol
)
480 die("invalid commit: %s", use_message
);
482 if (lb
== a
+ strlen("\nauthor "))
483 /* \nauthor <foo@example.com> */
484 name
= xcalloc(1, 1);
486 name
= xmemdupz(a
+ strlen("\nauthor "),
488 (a
+ strlen("\nauthor "))));
489 email
= xmemdupz(lb
+ strlen("<"), rb
- (lb
+ strlen("<")));
490 date
= xmemdupz(rb
+ strlen("> "), eol
- (rb
+ strlen("> ")));
494 const char *lb
= strstr(force_author
, " <");
495 const char *rb
= strchr(force_author
, '>');
498 die("malformed --author parameter");
499 name
= xstrndup(force_author
, lb
- force_author
);
500 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
505 strbuf_addstr(author_ident
, fmt_ident(name
, email
, date
,
506 IDENT_ERROR_ON_NO_NAME
));
509 static int ends_rfc2822_footer(struct strbuf
*sb
)
516 const char *buf
= sb
->buf
;
518 for (i
= len
- 1; i
> 0; i
--) {
519 if (hit
&& buf
[i
] == '\n')
521 hit
= (buf
[i
] == '\n');
524 while (i
< len
- 1 && buf
[i
] == '\n')
527 for (; i
< len
; i
= k
) {
528 for (k
= i
; k
< len
&& buf
[k
] != '\n'; k
++)
532 if ((buf
[k
] == ' ' || buf
[k
] == '\t') && !first
)
537 for (j
= 0; i
+ j
< len
; j
++) {
550 static char *cut_ident_timestamp_part(char *string
)
552 char *ket
= strrchr(string
, '>');
553 if (!ket
|| ket
[1] != ' ')
554 die("Malformed ident string: '%s'", string
);
559 static int prepare_to_commit(const char *index_file
, const char *prefix
,
561 struct strbuf
*author_ident
)
564 struct strbuf committer_ident
= STRBUF_INIT
;
565 int commitable
, saved_color_setting
;
566 struct strbuf sb
= STRBUF_INIT
;
569 const char *hook_arg1
= NULL
;
570 const char *hook_arg2
= NULL
;
573 if (!no_verify
&& run_hook(index_file
, "pre-commit", NULL
))
577 strbuf_addbuf(&sb
, &message
);
578 hook_arg1
= "message";
579 } else if (logfile
&& !strcmp(logfile
, "-")) {
581 fprintf(stderr
, "(reading log message from standard input)\n");
582 if (strbuf_read(&sb
, 0, 0) < 0)
583 die_errno("could not read log from standard input");
584 hook_arg1
= "message";
585 } else if (logfile
) {
586 if (strbuf_read_file(&sb
, logfile
, 0) < 0)
587 die_errno("could not read log file '%s'",
589 hook_arg1
= "message";
590 } else if (use_message
) {
591 buffer
= strstr(use_message_buffer
, "\n\n");
592 if (!buffer
|| buffer
[2] == '\0')
593 die("commit has empty message");
594 strbuf_add(&sb
, buffer
+ 2, strlen(buffer
+ 2));
595 hook_arg1
= "commit";
596 hook_arg2
= use_message
;
597 } else if (!stat(git_path("MERGE_MSG"), &statbuf
)) {
598 if (strbuf_read_file(&sb
, git_path("MERGE_MSG"), 0) < 0)
599 die_errno("could not read MERGE_MSG");
601 } else if (!stat(git_path("SQUASH_MSG"), &statbuf
)) {
602 if (strbuf_read_file(&sb
, git_path("SQUASH_MSG"), 0) < 0)
603 die_errno("could not read SQUASH_MSG");
604 hook_arg1
= "squash";
605 } else if (template_file
) {
606 if (strbuf_read_file(&sb
, template_file
, 0) < 0)
607 die_errno("could not read '%s'", template_file
);
608 hook_arg1
= "template";
612 * This final case does not modify the template message,
613 * it just sets the argument to the prepare-commit-msg hook.
618 fp
= fopen(git_path(commit_editmsg
), "w");
620 die_errno("could not open '%s'", git_path(commit_editmsg
));
622 if (cleanup_mode
!= CLEANUP_NONE
)
626 struct strbuf sob
= STRBUF_INIT
;
629 strbuf_addstr(&sob
, sign_off_header
);
630 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
631 getenv("GIT_COMMITTER_EMAIL")));
632 strbuf_addch(&sob
, '\n');
633 for (i
= sb
.len
- 1; i
> 0 && sb
.buf
[i
- 1] != '\n'; i
--)
635 if (prefixcmp(sb
.buf
+ i
, sob
.buf
)) {
636 if (!i
|| !ends_rfc2822_footer(&sb
))
637 strbuf_addch(&sb
, '\n');
638 strbuf_addbuf(&sb
, &sob
);
640 strbuf_release(&sob
);
643 if (fwrite(sb
.buf
, 1, sb
.len
, fp
) < sb
.len
)
644 die_errno("could not write commit template");
648 /* This checks and barfs if author is badly specified */
649 determine_author_info(author_ident
);
651 /* This checks if committer ident is explicitly given */
652 strbuf_addstr(&committer_ident
, git_committer_info(0));
653 if (use_editor
&& include_status
) {
654 char *ai_tmp
, *ci_tmp
;
658 "# It looks like you may be committing a MERGE.\n"
659 "# If this is not correct, please remove the file\n"
663 git_path("MERGE_HEAD"));
667 "# Please enter the commit message for your changes.");
668 if (cleanup_mode
== CLEANUP_ALL
)
671 "# with '#' will be ignored, and an empty"
672 " message aborts the commit.\n");
673 else /* CLEANUP_SPACE, that is. */
676 "# with '#' will be kept; you may remove them"
677 " yourself if you want to.\n"
678 "# An empty message aborts the commit.\n");
679 if (only_include_assumed
)
680 fprintf(fp
, "# %s\n", only_include_assumed
);
682 ai_tmp
= cut_ident_timestamp_part(author_ident
->buf
);
683 ci_tmp
= cut_ident_timestamp_part(committer_ident
.buf
);
684 if (strcmp(author_ident
->buf
, committer_ident
.buf
))
688 ident_shown
++ ? "" : "#\n",
691 if (!user_ident_sufficiently_given())
695 ident_shown
++ ? "" : "#\n",
696 committer_ident
.buf
);
701 saved_color_setting
= s
->use_color
;
703 commitable
= run_status(fp
, index_file
, prefix
, 1, s
);
704 s
->use_color
= saved_color_setting
;
709 unsigned char sha1
[20];
710 const char *parent
= "HEAD";
712 if (!active_nr
&& read_cache() < 0)
713 die("Cannot read index");
718 if (get_sha1(parent
, sha1
))
719 commitable
= !!active_nr
;
721 commitable
= index_differs_from(parent
, 0);
723 strbuf_release(&committer_ident
);
727 if (!commitable
&& !in_merge
&& !allow_empty
&&
728 !(amend
&& is_a_merge(head_sha1
))) {
729 run_status(stdout
, index_file
, prefix
, 0, s
);
731 fputs(empty_amend_advice
, stderr
);
736 * Re-read the index as pre-commit hook could have updated it,
737 * and write it out as a tree. We must do this before we invoke
738 * the editor and after we invoke run_status above.
741 read_cache_from(index_file
);
742 if (!active_cache_tree
)
743 active_cache_tree
= cache_tree();
744 if (cache_tree_update(active_cache_tree
,
745 active_cache
, active_nr
, 0, 0) < 0) {
746 error("Error building trees");
750 if (run_hook(index_file
, "prepare-commit-msg",
751 git_path(commit_editmsg
), hook_arg1
, hook_arg2
, NULL
))
755 char index
[PATH_MAX
];
756 const char *env
[2] = { NULL
};
758 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
759 if (launch_editor(git_path(commit_editmsg
), NULL
, env
)) {
761 "Please supply the message using either -m or -F option.\n");
767 run_hook(index_file
, "commit-msg", git_path(commit_editmsg
), NULL
)) {
775 * Find out if the message in the strbuf contains only whitespace and
776 * Signed-off-by lines.
778 static int message_is_empty(struct strbuf
*sb
)
780 struct strbuf tmpl
= STRBUF_INIT
;
782 int eol
, i
, start
= 0;
784 if (cleanup_mode
== CLEANUP_NONE
&& sb
->len
)
787 /* See if the template is just a prefix of the message. */
788 if (template_file
&& strbuf_read_file(&tmpl
, template_file
, 0) > 0) {
789 stripspace(&tmpl
, cleanup_mode
== CLEANUP_ALL
);
790 if (start
+ tmpl
.len
<= sb
->len
&&
791 memcmp(tmpl
.buf
, sb
->buf
+ start
, tmpl
.len
) == 0)
794 strbuf_release(&tmpl
);
796 /* Check if the rest is just whitespace and Signed-of-by's. */
797 for (i
= start
; i
< sb
->len
; i
++) {
798 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
804 if (strlen(sign_off_header
) <= eol
- i
&&
805 !prefixcmp(sb
->buf
+ i
, sign_off_header
)) {
810 if (!isspace(sb
->buf
[i
++]))
817 static const char *find_author_by_nickname(const char *name
)
819 struct rev_info revs
;
820 struct commit
*commit
;
821 struct strbuf buf
= STRBUF_INIT
;
825 init_revisions(&revs
, NULL
);
826 strbuf_addf(&buf
, "--author=%s", name
);
831 setup_revisions(ac
, av
, &revs
, NULL
);
832 prepare_revision_walk(&revs
);
833 commit
= get_revision(&revs
);
835 struct pretty_print_context ctx
= {0};
836 ctx
.date_mode
= DATE_NORMAL
;
837 strbuf_release(&buf
);
838 format_commit_message(commit
, "%an <%ae>", &buf
, &ctx
);
839 return strbuf_detach(&buf
, NULL
);
841 die("No existing author found with '%s'", name
);
845 static void handle_untracked_files_arg(struct wt_status
*s
)
847 if (!untracked_files_arg
)
848 ; /* default already initialized */
849 else if (!strcmp(untracked_files_arg
, "no"))
850 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
851 else if (!strcmp(untracked_files_arg
, "normal"))
852 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
853 else if (!strcmp(untracked_files_arg
, "all"))
854 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
856 die("Invalid untracked files mode '%s'", untracked_files_arg
);
859 static int parse_and_validate_options(int argc
, const char *argv
[],
860 const char * const usage
[],
866 argc
= parse_options(argc
, argv
, prefix
, builtin_commit_options
, usage
,
869 if (force_author
&& !strchr(force_author
, '>'))
870 force_author
= find_author_by_nickname(force_author
);
872 if (force_author
&& renew_authorship
)
873 die("Using both --reset-author and --author does not make sense");
875 if (logfile
|| message
.len
|| use_message
)
880 setenv("GIT_EDITOR", ":", 1);
882 if (get_sha1("HEAD", head_sha1
))
885 /* Sanity check options */
886 if (amend
&& initial_commit
)
887 die("You have nothing to amend.");
888 if (amend
&& in_merge
)
889 die("You are in the middle of a merge -- cannot amend.");
898 die("Only one of -c/-C/-F can be used.");
899 if (message
.len
&& f
> 0)
900 die("Option -m cannot be combined with -c/-C/-F.");
902 use_message
= edit_message
;
903 if (amend
&& !use_message
)
904 use_message
= "HEAD";
905 if (!use_message
&& renew_authorship
)
906 die("--reset-author can be used only with -C, -c or --amend.");
908 unsigned char sha1
[20];
909 static char utf8
[] = "UTF-8";
912 struct commit
*commit
;
914 if (get_sha1(use_message
, sha1
))
915 die("could not lookup commit %s", use_message
);
916 commit
= lookup_commit_reference(sha1
);
917 if (!commit
|| parse_commit(commit
))
918 die("could not parse commit %s", use_message
);
920 enc
= strstr(commit
->buffer
, "\nencoding");
922 end
= strchr(enc
+ 10, '\n');
923 enc
= xstrndup(enc
+ 10, end
- (enc
+ 10));
927 out_enc
= git_commit_encoding
? git_commit_encoding
: utf8
;
929 if (strcmp(out_enc
, enc
))
931 reencode_string(commit
->buffer
, out_enc
, enc
);
934 * If we failed to reencode the buffer, just copy it
935 * byte for byte so the user can try to fix it up.
936 * This also handles the case where input and output
937 * encodings are identical.
939 if (use_message_buffer
== NULL
)
940 use_message_buffer
= xstrdup(commit
->buffer
);
945 if (!!also
+ !!only
+ !!all
+ !!interactive
> 1)
946 die("Only one of --include/--only/--all/--interactive can be used.");
947 if (argc
== 0 && (also
|| (only
&& !amend
)))
948 die("No paths with --include/--only does not make sense.");
949 if (argc
== 0 && only
&& amend
)
950 only_include_assumed
= "Clever... amending the last one with dirty index.";
951 if (argc
> 0 && !also
&& !only
)
952 only_include_assumed
= "Explicit paths specified without -i nor -o; assuming --only paths...";
953 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "default"))
954 cleanup_mode
= use_editor
? CLEANUP_ALL
: CLEANUP_SPACE
;
955 else if (!strcmp(cleanup_arg
, "verbatim"))
956 cleanup_mode
= CLEANUP_NONE
;
957 else if (!strcmp(cleanup_arg
, "whitespace"))
958 cleanup_mode
= CLEANUP_SPACE
;
959 else if (!strcmp(cleanup_arg
, "strip"))
960 cleanup_mode
= CLEANUP_ALL
;
962 die("Invalid cleanup mode %s", cleanup_arg
);
964 handle_untracked_files_arg(s
);
967 die("Paths with -a does not make sense.");
968 else if (interactive
&& argc
> 0)
969 die("Paths with --interactive does not make sense.");
971 if (null_termination
&& status_format
== STATUS_FORMAT_LONG
)
972 status_format
= STATUS_FORMAT_PORCELAIN
;
973 if (status_format
!= STATUS_FORMAT_LONG
)
979 static int dry_run_commit(int argc
, const char **argv
, const char *prefix
,
983 const char *index_file
;
985 index_file
= prepare_index(argc
, argv
, prefix
, 1);
986 commitable
= run_status(stdout
, index_file
, prefix
, 0, s
);
987 rollback_index_files();
989 return commitable
? 0 : 1;
992 static int parse_status_slot(const char *var
, int offset
)
994 if (!strcasecmp(var
+offset
, "header"))
995 return WT_STATUS_HEADER
;
996 if (!strcasecmp(var
+offset
, "updated")
997 || !strcasecmp(var
+offset
, "added"))
998 return WT_STATUS_UPDATED
;
999 if (!strcasecmp(var
+offset
, "changed"))
1000 return WT_STATUS_CHANGED
;
1001 if (!strcasecmp(var
+offset
, "untracked"))
1002 return WT_STATUS_UNTRACKED
;
1003 if (!strcasecmp(var
+offset
, "nobranch"))
1004 return WT_STATUS_NOBRANCH
;
1005 if (!strcasecmp(var
+offset
, "unmerged"))
1006 return WT_STATUS_UNMERGED
;
1010 static int git_status_config(const char *k
, const char *v
, void *cb
)
1012 struct wt_status
*s
= cb
;
1014 if (!strcmp(k
, "status.submodulesummary")) {
1016 s
->submodule_summary
= git_config_bool_or_int(k
, v
, &is_bool
);
1017 if (is_bool
&& s
->submodule_summary
)
1018 s
->submodule_summary
= -1;
1021 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
1022 s
->use_color
= git_config_colorbool(k
, v
, -1);
1025 if (!prefixcmp(k
, "status.color.") || !prefixcmp(k
, "color.status.")) {
1026 int slot
= parse_status_slot(k
, 13);
1030 return config_error_nonbool(k
);
1031 color_parse(v
, k
, s
->color_palette
[slot
]);
1034 if (!strcmp(k
, "status.relativepaths")) {
1035 s
->relative_paths
= git_config_bool(k
, v
);
1038 if (!strcmp(k
, "status.showuntrackedfiles")) {
1040 return config_error_nonbool(k
);
1041 else if (!strcmp(v
, "no"))
1042 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
1043 else if (!strcmp(v
, "normal"))
1044 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
1045 else if (!strcmp(v
, "all"))
1046 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
1048 return error("Invalid untracked files mode '%s'", v
);
1051 return git_diff_ui_config(k
, v
, NULL
);
1054 int cmd_status(int argc
, const char **argv
, const char *prefix
)
1058 unsigned char sha1
[20];
1059 static struct option builtin_status_options
[] = {
1060 OPT__VERBOSE(&verbose
),
1061 OPT_SET_INT('s', "short", &status_format
,
1062 "show status concisely", STATUS_FORMAT_SHORT
),
1063 OPT_BOOLEAN('b', "branch", &status_show_branch
,
1064 "show branch information"),
1065 OPT_SET_INT(0, "porcelain", &status_format
,
1066 "show porcelain output format",
1067 STATUS_FORMAT_PORCELAIN
),
1068 OPT_BOOLEAN('z', "null", &null_termination
,
1069 "terminate entries with NUL"),
1070 { OPTION_STRING
, 'u', "untracked-files", &untracked_files_arg
,
1072 "show untracked files, optional modes: all, normal, no. (Default: all)",
1073 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1074 OPT_BOOLEAN(0, "ignored", &show_ignored_in_status
,
1075 "show ignored files"),
1076 { OPTION_STRING
, 0, "ignore-submodules", &ignore_submodule_arg
, "when",
1077 "ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)",
1078 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1082 if (null_termination
&& status_format
== STATUS_FORMAT_LONG
)
1083 status_format
= STATUS_FORMAT_PORCELAIN
;
1085 wt_status_prepare(&s
);
1086 gitmodules_config();
1087 git_config(git_status_config
, &s
);
1088 in_merge
= file_exists(git_path("MERGE_HEAD"));
1089 argc
= parse_options(argc
, argv
, prefix
,
1090 builtin_status_options
,
1091 builtin_status_usage
, 0);
1092 handle_untracked_files_arg(&s
);
1093 if (show_ignored_in_status
)
1094 s
.show_ignored_files
= 1;
1096 s
.pathspec
= get_pathspec(prefix
, argv
);
1098 read_cache_preload(s
.pathspec
);
1099 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, s
.pathspec
, NULL
, NULL
);
1101 fd
= hold_locked_index(&index_lock
, 0);
1103 if (active_cache_changed
&&
1104 !write_cache(fd
, active_cache
, active_nr
))
1105 commit_locked_index(&index_lock
);
1107 rollback_lock_file(&index_lock
);
1110 s
.is_initial
= get_sha1(s
.reference
, sha1
) ? 1 : 0;
1111 s
.in_merge
= in_merge
;
1112 s
.ignore_submodule_arg
= ignore_submodule_arg
;
1113 wt_status_collect(&s
);
1115 if (s
.relative_paths
)
1117 if (s
.use_color
== -1)
1118 s
.use_color
= git_use_color_default
;
1119 if (diff_use_color_default
== -1)
1120 diff_use_color_default
= git_use_color_default
;
1122 switch (status_format
) {
1123 case STATUS_FORMAT_SHORT
:
1124 wt_shortstatus_print(&s
, null_termination
, status_show_branch
);
1126 case STATUS_FORMAT_PORCELAIN
:
1127 wt_porcelain_print(&s
, null_termination
);
1129 case STATUS_FORMAT_LONG
:
1130 s
.verbose
= verbose
;
1131 s
.ignore_submodule_arg
= ignore_submodule_arg
;
1132 wt_status_print(&s
);
1138 static void print_summary(const char *prefix
, const unsigned char *sha1
)
1140 struct rev_info rev
;
1141 struct commit
*commit
;
1142 struct strbuf format
= STRBUF_INIT
;
1143 unsigned char junk_sha1
[20];
1144 const char *head
= resolve_ref("HEAD", junk_sha1
, 0, NULL
);
1145 struct pretty_print_context pctx
= {0};
1146 struct strbuf author_ident
= STRBUF_INIT
;
1147 struct strbuf committer_ident
= STRBUF_INIT
;
1149 commit
= lookup_commit(sha1
);
1151 die("couldn't look up newly created commit");
1152 if (!commit
|| parse_commit(commit
))
1153 die("could not parse newly created commit");
1155 strbuf_addstr(&format
, "format:%h] %s");
1157 format_commit_message(commit
, "%an <%ae>", &author_ident
, &pctx
);
1158 format_commit_message(commit
, "%cn <%ce>", &committer_ident
, &pctx
);
1159 if (strbuf_cmp(&author_ident
, &committer_ident
)) {
1160 strbuf_addstr(&format
, "\n Author: ");
1161 strbuf_addbuf_percentquote(&format
, &author_ident
);
1163 if (!user_ident_sufficiently_given()) {
1164 strbuf_addstr(&format
, "\n Committer: ");
1165 strbuf_addbuf_percentquote(&format
, &committer_ident
);
1166 if (advice_implicit_identity
) {
1167 strbuf_addch(&format
, '\n');
1168 strbuf_addstr(&format
, implicit_ident_advice
);
1171 strbuf_release(&author_ident
);
1172 strbuf_release(&committer_ident
);
1174 init_revisions(&rev
, prefix
);
1175 setup_revisions(0, NULL
, &rev
, NULL
);
1178 rev
.diffopt
.output_format
=
1179 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
1181 rev
.verbose_header
= 1;
1182 rev
.show_root_diff
= 1;
1183 get_commit_format(format
.buf
, &rev
);
1184 rev
.always_show_header
= 0;
1185 rev
.diffopt
.detect_rename
= 1;
1186 rev
.diffopt
.rename_limit
= 100;
1187 rev
.diffopt
.break_opt
= 0;
1188 diff_setup_done(&rev
.diffopt
);
1191 !prefixcmp(head
, "refs/heads/") ?
1193 !strcmp(head
, "HEAD") ?
1196 initial_commit
? " (root-commit)" : "");
1198 if (!log_tree_commit(&rev
, commit
)) {
1199 rev
.always_show_header
= 1;
1200 rev
.use_terminator
= 1;
1201 log_tree_commit(&rev
, commit
);
1204 strbuf_release(&format
);
1207 static int git_commit_config(const char *k
, const char *v
, void *cb
)
1209 struct wt_status
*s
= cb
;
1211 if (!strcmp(k
, "commit.template"))
1212 return git_config_pathname(&template_file
, k
, v
);
1213 if (!strcmp(k
, "commit.status")) {
1214 include_status
= git_config_bool(k
, v
);
1218 return git_status_config(k
, v
, s
);
1221 static const char post_rewrite_hook
[] = "hooks/post-rewrite";
1223 static int run_rewrite_hook(const unsigned char *oldsha1
,
1224 const unsigned char *newsha1
)
1226 /* oldsha1 SP newsha1 LF NUL */
1227 static char buf
[2*40 + 3];
1228 struct child_process proc
;
1229 const char *argv
[3];
1233 if (access(git_path(post_rewrite_hook
), X_OK
) < 0)
1236 argv
[0] = git_path(post_rewrite_hook
);
1240 memset(&proc
, 0, sizeof(proc
));
1243 proc
.stdout_to_stderr
= 1;
1245 code
= start_command(&proc
);
1248 n
= snprintf(buf
, sizeof(buf
), "%s %s\n",
1249 sha1_to_hex(oldsha1
), sha1_to_hex(newsha1
));
1250 write_in_full(proc
.in
, buf
, n
);
1252 return finish_command(&proc
);
1255 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
1257 struct strbuf sb
= STRBUF_INIT
;
1258 struct strbuf author_ident
= STRBUF_INIT
;
1259 const char *index_file
, *reflog_msg
;
1261 unsigned char commit_sha1
[20];
1262 struct ref_lock
*ref_lock
;
1263 struct commit_list
*parents
= NULL
, **pptr
= &parents
;
1264 struct stat statbuf
;
1265 int allow_fast_forward
= 1;
1268 wt_status_prepare(&s
);
1269 git_config(git_commit_config
, &s
);
1270 in_merge
= file_exists(git_path("MERGE_HEAD"));
1271 s
.in_merge
= in_merge
;
1273 if (s
.use_color
== -1)
1274 s
.use_color
= git_use_color_default
;
1275 argc
= parse_and_validate_options(argc
, argv
, builtin_commit_usage
,
1278 if (diff_use_color_default
== -1)
1279 diff_use_color_default
= git_use_color_default
;
1280 return dry_run_commit(argc
, argv
, prefix
, &s
);
1282 index_file
= prepare_index(argc
, argv
, prefix
, 0);
1284 /* Set up everything for writing the commit object. This includes
1285 running hooks, writing the trees, and interacting with the user. */
1286 if (!prepare_to_commit(index_file
, prefix
, &s
, &author_ident
)) {
1287 rollback_index_files();
1291 /* Determine parents */
1292 reflog_msg
= getenv("GIT_REFLOG_ACTION");
1293 if (initial_commit
) {
1295 reflog_msg
= "commit (initial)";
1297 struct commit_list
*c
;
1298 struct commit
*commit
;
1301 reflog_msg
= "commit (amend)";
1302 commit
= lookup_commit(head_sha1
);
1303 if (!commit
|| parse_commit(commit
))
1304 die("could not parse HEAD commit");
1306 for (c
= commit
->parents
; c
; c
= c
->next
)
1307 pptr
= &commit_list_insert(c
->item
, pptr
)->next
;
1308 } else if (in_merge
) {
1309 struct strbuf m
= STRBUF_INIT
;
1313 reflog_msg
= "commit (merge)";
1314 pptr
= &commit_list_insert(lookup_commit(head_sha1
), pptr
)->next
;
1315 fp
= fopen(git_path("MERGE_HEAD"), "r");
1317 die_errno("could not open '%s' for reading",
1318 git_path("MERGE_HEAD"));
1319 while (strbuf_getline(&m
, fp
, '\n') != EOF
) {
1320 unsigned char sha1
[20];
1321 if (get_sha1_hex(m
.buf
, sha1
) < 0)
1322 die("Corrupt MERGE_HEAD file (%s)", m
.buf
);
1323 pptr
= &commit_list_insert(lookup_commit(sha1
), pptr
)->next
;
1327 if (!stat(git_path("MERGE_MODE"), &statbuf
)) {
1328 if (strbuf_read_file(&sb
, git_path("MERGE_MODE"), 0) < 0)
1329 die_errno("could not read MERGE_MODE");
1330 if (!strcmp(sb
.buf
, "no-ff"))
1331 allow_fast_forward
= 0;
1333 if (allow_fast_forward
)
1334 parents
= reduce_heads(parents
);
1337 reflog_msg
= "commit";
1338 pptr
= &commit_list_insert(lookup_commit(head_sha1
), pptr
)->next
;
1341 /* Finally, get the commit message */
1343 if (strbuf_read_file(&sb
, git_path(commit_editmsg
), 0) < 0) {
1344 int saved_errno
= errno
;
1345 rollback_index_files();
1346 die("could not read commit message: %s", strerror(saved_errno
));
1349 /* Truncate the message just before the diff, if any. */
1351 p
= strstr(sb
.buf
, "\ndiff --git ");
1353 strbuf_setlen(&sb
, p
- sb
.buf
+ 1);
1356 if (cleanup_mode
!= CLEANUP_NONE
)
1357 stripspace(&sb
, cleanup_mode
== CLEANUP_ALL
);
1358 if (message_is_empty(&sb
) && !allow_empty_message
) {
1359 rollback_index_files();
1360 fprintf(stderr
, "Aborting commit due to empty commit message.\n");
1364 if (commit_tree(sb
.buf
, active_cache_tree
->sha1
, parents
, commit_sha1
,
1365 author_ident
.buf
)) {
1366 rollback_index_files();
1367 die("failed to write commit object");
1369 strbuf_release(&author_ident
);
1371 ref_lock
= lock_any_ref_for_update("HEAD",
1372 initial_commit
? NULL
: head_sha1
,
1375 nl
= strchr(sb
.buf
, '\n');
1377 strbuf_setlen(&sb
, nl
+ 1 - sb
.buf
);
1379 strbuf_addch(&sb
, '\n');
1380 strbuf_insert(&sb
, 0, reflog_msg
, strlen(reflog_msg
));
1381 strbuf_insert(&sb
, strlen(reflog_msg
), ": ", 2);
1384 rollback_index_files();
1385 die("cannot lock HEAD ref");
1387 if (write_ref_sha1(ref_lock
, commit_sha1
, sb
.buf
) < 0) {
1388 rollback_index_files();
1389 die("cannot update HEAD ref");
1392 unlink(git_path("MERGE_HEAD"));
1393 unlink(git_path("MERGE_MSG"));
1394 unlink(git_path("MERGE_MODE"));
1395 unlink(git_path("SQUASH_MSG"));
1397 if (commit_index_files())
1398 die ("Repository has been updated, but unable to write\n"
1399 "new_index file. Check that disk is not full or quota is\n"
1400 "not exceeded, and then \"git reset HEAD\" to recover.");
1403 run_hook(get_index_file(), "post-commit", NULL
);
1404 if (amend
&& !no_post_rewrite
) {
1405 struct notes_rewrite_cfg
*cfg
;
1406 cfg
= init_copy_notes_for_rewrite("amend");
1408 copy_note_for_rewrite(cfg
, head_sha1
, commit_sha1
);
1409 finish_copy_notes_for_rewrite(cfg
);
1411 run_rewrite_hook(head_sha1
, commit_sha1
);
1414 print_summary(prefix
, commit_sha1
);