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 unsigned char head_sha1
[20];
53 static char *use_message_buffer
;
54 static const char commit_editmsg
[] = "COMMIT_EDITMSG";
55 static struct lock_file index_lock
; /* real index */
56 static struct lock_file false_lock
; /* used only for partial commits */
63 static const char *logfile
, *force_author
;
64 static const char *template_file
;
65 static char *edit_message
, *use_message
;
66 static char *author_name
, *author_email
, *author_date
;
67 static int all
, edit_flag
, also
, interactive
, only
, amend
, signoff
;
68 static int quiet
, verbose
, no_verify
, allow_empty
, dry_run
, renew_authorship
;
69 static int no_post_rewrite
;
70 static char *untracked_files_arg
, *force_date
;
72 * The default commit message cleanup mode will remove the lines
73 * beginning with # (shell comments) and leading and trailing
74 * whitespaces (empty lines or containing only whitespaces)
75 * if editor is used, and only the whitespaces if the message
76 * is specified explicitly.
83 static char *cleanup_arg
;
85 static int use_editor
= 1, initial_commit
, in_merge
, include_status
= 1;
86 static const char *only_include_assumed
;
87 static struct strbuf message
;
89 static int null_termination
;
93 STATUS_FORMAT_PORCELAIN
,
94 } status_format
= STATUS_FORMAT_LONG
;
96 static int opt_parse_m(const struct option
*opt
, const char *arg
, int unset
)
98 struct strbuf
*buf
= opt
->value
;
100 strbuf_setlen(buf
, 0);
102 strbuf_addstr(buf
, arg
);
103 strbuf_addstr(buf
, "\n\n");
108 static struct option builtin_commit_options
[] = {
110 OPT__VERBOSE(&verbose
),
112 OPT_GROUP("Commit message options"),
113 OPT_FILENAME('F', "file", &logfile
, "read log from file"),
114 OPT_STRING(0, "author", &force_author
, "AUTHOR", "override author for commit"),
115 OPT_STRING(0, "date", &force_date
, "DATE", "override date for commit"),
116 OPT_CALLBACK('m', "message", &message
, "MESSAGE", "specify commit message", opt_parse_m
),
117 OPT_STRING('c', "reedit-message", &edit_message
, "COMMIT", "reuse and edit message from specified commit"),
118 OPT_STRING('C', "reuse-message", &use_message
, "COMMIT", "reuse message from specified commit"),
119 OPT_BOOLEAN(0, "reset-author", &renew_authorship
, "the commit is authored by me now (used with -C-c/--amend)"),
120 OPT_BOOLEAN('s', "signoff", &signoff
, "add Signed-off-by:"),
121 OPT_FILENAME('t', "template", &template_file
, "use specified template file"),
122 OPT_BOOLEAN('e', "edit", &edit_flag
, "force edit of commit"),
123 OPT_STRING(0, "cleanup", &cleanup_arg
, "default", "how to strip spaces and #comments from message"),
124 OPT_BOOLEAN(0, "status", &include_status
, "include status in commit message template"),
125 /* end commit message options */
127 OPT_GROUP("Commit contents options"),
128 OPT_BOOLEAN('a', "all", &all
, "commit all changed files"),
129 OPT_BOOLEAN('i', "include", &also
, "add specified files to index for commit"),
130 OPT_BOOLEAN(0, "interactive", &interactive
, "interactively add files"),
131 OPT_BOOLEAN('o', "only", &only
, "commit only specified files"),
132 OPT_BOOLEAN('n', "no-verify", &no_verify
, "bypass pre-commit hook"),
133 OPT_BOOLEAN(0, "dry-run", &dry_run
, "show what would be committed"),
134 OPT_SET_INT(0, "short", &status_format
, "show status concisely",
135 STATUS_FORMAT_SHORT
),
136 OPT_SET_INT(0, "porcelain", &status_format
,
137 "show porcelain output format", STATUS_FORMAT_PORCELAIN
),
138 OPT_BOOLEAN('z', "null", &null_termination
,
139 "terminate entries with NUL"),
140 OPT_BOOLEAN(0, "amend", &amend
, "amend previous commit"),
141 OPT_BOOLEAN(0, "no-post-rewrite", &no_post_rewrite
, "bypass post-rewrite hook"),
142 { 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" },
143 OPT_BOOLEAN(0, "allow-empty", &allow_empty
, "ok to record an empty change"),
144 /* end commit contents options */
149 static void rollback_index_files(void)
151 switch (commit_style
) {
153 break; /* nothing to do */
155 rollback_lock_file(&index_lock
);
158 rollback_lock_file(&index_lock
);
159 rollback_lock_file(&false_lock
);
164 static int commit_index_files(void)
168 switch (commit_style
) {
170 break; /* nothing to do */
172 err
= commit_lock_file(&index_lock
);
175 err
= commit_lock_file(&index_lock
);
176 rollback_lock_file(&false_lock
);
184 * Take a union of paths in the index and the named tree (typically, "HEAD"),
185 * and return the paths that match the given pattern in list.
187 static int list_paths(struct string_list
*list
, const char *with_tree
,
188 const char *prefix
, const char **pattern
)
193 for (i
= 0; pattern
[i
]; i
++)
198 overlay_tree_on_cache(with_tree
, prefix
);
200 for (i
= 0; i
< active_nr
; i
++) {
201 struct cache_entry
*ce
= active_cache
[i
];
202 struct string_list_item
*item
;
204 if (ce
->ce_flags
& CE_UPDATE
)
206 if (!match_pathspec(pattern
, ce
->name
, ce_namelen(ce
), 0, m
))
208 item
= string_list_insert(ce
->name
, list
);
209 if (ce_skip_worktree(ce
))
210 item
->util
= item
; /* better a valid pointer than a fake one */
213 return report_path_error(m
, pattern
, prefix
? strlen(prefix
) : 0);
216 static void add_remove_files(struct string_list
*list
)
219 for (i
= 0; i
< list
->nr
; i
++) {
221 struct string_list_item
*p
= &(list
->items
[i
]);
223 /* p->util is skip-worktree */
227 if (!lstat(p
->string
, &st
)) {
228 if (add_to_cache(p
->string
, &st
, 0))
229 die("updating files failed");
231 remove_file_from_cache(p
->string
);
235 static void create_base_index(void)
238 struct unpack_trees_options opts
;
241 if (initial_commit
) {
246 memset(&opts
, 0, sizeof(opts
));
250 opts
.src_index
= &the_index
;
251 opts
.dst_index
= &the_index
;
253 opts
.fn
= oneway_merge
;
254 tree
= parse_tree_indirect(head_sha1
);
256 die("failed to unpack HEAD tree object");
258 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
259 if (unpack_trees(1, &t
, &opts
))
260 exit(128); /* We've already reported the error, finish dying */
263 static void refresh_cache_or_die(int refresh_flags
)
266 * refresh_flags contains REFRESH_QUIET, so the only errors
267 * are for unmerged entries.
269 if (refresh_cache(refresh_flags
| REFRESH_IN_PORCELAIN
))
270 die_resolve_conflict("commit");
273 static char *prepare_index(int argc
, const char **argv
, const char *prefix
, int is_status
)
276 struct string_list partial
;
277 const char **pathspec
= NULL
;
278 int refresh_flags
= REFRESH_QUIET
;
281 refresh_flags
|= REFRESH_UNMERGED
;
283 if (interactive_add(argc
, argv
, prefix
) != 0)
284 die("interactive add failed");
285 if (read_cache_preload(NULL
) < 0)
286 die("index file corrupt");
287 commit_style
= COMMIT_AS_IS
;
288 return get_index_file();
292 pathspec
= get_pathspec(prefix
, argv
);
294 if (read_cache_preload(pathspec
) < 0)
295 die("index file corrupt");
298 * Non partial, non as-is commit.
300 * (1) get the real index;
301 * (2) update the_index as necessary;
302 * (3) write the_index out to the real index (still locked);
303 * (4) return the name of the locked index file.
305 * The caller should run hooks on the locked real index, and
306 * (A) if all goes well, commit the real index;
307 * (B) on failure, rollback the real index.
309 if (all
|| (also
&& pathspec
&& *pathspec
)) {
310 fd
= hold_locked_index(&index_lock
, 1);
311 add_files_to_cache(also
? prefix
: NULL
, pathspec
, 0);
312 refresh_cache_or_die(refresh_flags
);
313 if (write_cache(fd
, active_cache
, active_nr
) ||
314 close_lock_file(&index_lock
))
315 die("unable to write new_index file");
316 commit_style
= COMMIT_NORMAL
;
317 return index_lock
.filename
;
323 * (1) return the name of the real index file.
325 * The caller should run hooks on the real index,
326 * and create commit from the_index.
327 * We still need to refresh the index here.
329 if (!pathspec
|| !*pathspec
) {
330 fd
= hold_locked_index(&index_lock
, 1);
331 refresh_cache_or_die(refresh_flags
);
332 if (write_cache(fd
, active_cache
, active_nr
) ||
333 commit_locked_index(&index_lock
))
334 die("unable to write new_index file");
335 commit_style
= COMMIT_AS_IS
;
336 return get_index_file();
342 * (0) find the set of affected paths;
343 * (1) get lock on the real index file;
344 * (2) update the_index with the given paths;
345 * (3) write the_index out to the real index (still locked);
346 * (4) get lock on the false index file;
347 * (5) reset the_index from HEAD;
348 * (6) update the_index the same way as (2);
349 * (7) write the_index out to the false index file;
350 * (8) return the name of the false index file (still locked);
352 * The caller should run hooks on the locked false index, and
353 * create commit from it. Then
354 * (A) if all goes well, commit the real index;
355 * (B) on failure, rollback the real index;
356 * In either case, rollback the false index.
358 commit_style
= COMMIT_PARTIAL
;
361 die("cannot do a partial commit during a merge.");
363 memset(&partial
, 0, sizeof(partial
));
364 partial
.strdup_strings
= 1;
365 if (list_paths(&partial
, initial_commit
? NULL
: "HEAD", prefix
, pathspec
))
369 if (read_cache() < 0)
370 die("cannot read the index");
372 fd
= hold_locked_index(&index_lock
, 1);
373 add_remove_files(&partial
);
374 refresh_cache(REFRESH_QUIET
);
375 if (write_cache(fd
, active_cache
, active_nr
) ||
376 close_lock_file(&index_lock
))
377 die("unable to write new_index file");
379 fd
= hold_lock_file_for_update(&false_lock
,
380 git_path("next-index-%"PRIuMAX
,
381 (uintmax_t) getpid()),
385 add_remove_files(&partial
);
386 refresh_cache(REFRESH_QUIET
);
388 if (write_cache(fd
, active_cache
, active_nr
) ||
389 close_lock_file(&false_lock
))
390 die("unable to write temporary index file");
393 read_cache_from(false_lock
.filename
);
395 return false_lock
.filename
;
398 static int run_status(FILE *fp
, const char *index_file
, const char *prefix
, int nowarn
,
401 unsigned char sha1
[20];
403 if (s
->relative_paths
)
408 s
->reference
= "HEAD^1";
410 s
->verbose
= verbose
;
411 s
->index_file
= index_file
;
414 s
->is_initial
= get_sha1(s
->reference
, sha1
) ? 1 : 0;
416 wt_status_collect(s
);
418 switch (status_format
) {
419 case STATUS_FORMAT_SHORT
:
420 wt_shortstatus_print(s
, null_termination
);
422 case STATUS_FORMAT_PORCELAIN
:
423 wt_porcelain_print(s
, null_termination
);
425 case STATUS_FORMAT_LONG
:
430 return s
->commitable
;
433 static int is_a_merge(const unsigned char *sha1
)
435 struct commit
*commit
= lookup_commit(sha1
);
436 if (!commit
|| parse_commit(commit
))
437 die("could not parse HEAD commit");
438 return !!(commit
->parents
&& commit
->parents
->next
);
441 static const char sign_off_header
[] = "Signed-off-by: ";
443 static void determine_author_info(void)
445 char *name
, *email
, *date
;
447 name
= getenv("GIT_AUTHOR_NAME");
448 email
= getenv("GIT_AUTHOR_EMAIL");
449 date
= getenv("GIT_AUTHOR_DATE");
451 if (use_message
&& !renew_authorship
) {
452 const char *a
, *lb
, *rb
, *eol
;
454 a
= strstr(use_message_buffer
, "\nauthor ");
456 die("invalid commit: %s", use_message
);
458 lb
= strstr(a
+ 8, " <");
459 rb
= strstr(a
+ 8, "> ");
460 eol
= strchr(a
+ 8, '\n');
461 if (!lb
|| !rb
|| !eol
)
462 die("invalid commit: %s", use_message
);
464 name
= xstrndup(a
+ 8, lb
- (a
+ 8));
465 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
466 date
= xstrndup(rb
+ 2, eol
- (rb
+ 2));
470 const char *lb
= strstr(force_author
, " <");
471 const char *rb
= strchr(force_author
, '>');
474 die("malformed --author parameter");
475 name
= xstrndup(force_author
, lb
- force_author
);
476 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
483 author_email
= email
;
487 static int ends_rfc2822_footer(struct strbuf
*sb
)
494 const char *buf
= sb
->buf
;
496 for (i
= len
- 1; i
> 0; i
--) {
497 if (hit
&& buf
[i
] == '\n')
499 hit
= (buf
[i
] == '\n');
502 while (i
< len
- 1 && buf
[i
] == '\n')
505 for (; i
< len
; i
= k
) {
506 for (k
= i
; k
< len
&& buf
[k
] != '\n'; k
++)
510 if ((buf
[k
] == ' ' || buf
[k
] == '\t') && !first
)
515 for (j
= 0; i
+ j
< len
; j
++) {
528 static int prepare_to_commit(const char *index_file
, const char *prefix
,
532 int commitable
, saved_color_setting
;
533 struct strbuf sb
= STRBUF_INIT
;
536 const char *hook_arg1
= NULL
;
537 const char *hook_arg2
= NULL
;
540 if (!no_verify
&& run_hook(index_file
, "pre-commit", NULL
))
544 strbuf_addbuf(&sb
, &message
);
545 hook_arg1
= "message";
546 } else if (logfile
&& !strcmp(logfile
, "-")) {
548 fprintf(stderr
, "(reading log message from standard input)\n");
549 if (strbuf_read(&sb
, 0, 0) < 0)
550 die_errno("could not read log from standard input");
551 hook_arg1
= "message";
552 } else if (logfile
) {
553 if (strbuf_read_file(&sb
, logfile
, 0) < 0)
554 die_errno("could not read log file '%s'",
556 hook_arg1
= "message";
557 } else if (use_message
) {
558 buffer
= strstr(use_message_buffer
, "\n\n");
559 if (!buffer
|| buffer
[2] == '\0')
560 die("commit has empty message");
561 strbuf_add(&sb
, buffer
+ 2, strlen(buffer
+ 2));
562 hook_arg1
= "commit";
563 hook_arg2
= use_message
;
564 } else if (!stat(git_path("MERGE_MSG"), &statbuf
)) {
565 if (strbuf_read_file(&sb
, git_path("MERGE_MSG"), 0) < 0)
566 die_errno("could not read MERGE_MSG");
568 } else if (!stat(git_path("SQUASH_MSG"), &statbuf
)) {
569 if (strbuf_read_file(&sb
, git_path("SQUASH_MSG"), 0) < 0)
570 die_errno("could not read SQUASH_MSG");
571 hook_arg1
= "squash";
572 } else if (template_file
&& !stat(template_file
, &statbuf
)) {
573 if (strbuf_read_file(&sb
, template_file
, 0) < 0)
574 die_errno("could not read '%s'", template_file
);
575 hook_arg1
= "template";
579 * This final case does not modify the template message,
580 * it just sets the argument to the prepare-commit-msg hook.
585 fp
= fopen(git_path(commit_editmsg
), "w");
587 die_errno("could not open '%s'", git_path(commit_editmsg
));
589 if (cleanup_mode
!= CLEANUP_NONE
)
593 struct strbuf sob
= STRBUF_INIT
;
596 strbuf_addstr(&sob
, sign_off_header
);
597 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
598 getenv("GIT_COMMITTER_EMAIL")));
599 strbuf_addch(&sob
, '\n');
600 for (i
= sb
.len
- 1; i
> 0 && sb
.buf
[i
- 1] != '\n'; i
--)
602 if (prefixcmp(sb
.buf
+ i
, sob
.buf
)) {
603 if (!i
|| !ends_rfc2822_footer(&sb
))
604 strbuf_addch(&sb
, '\n');
605 strbuf_addbuf(&sb
, &sob
);
607 strbuf_release(&sob
);
610 if (fwrite(sb
.buf
, 1, sb
.len
, fp
) < sb
.len
)
611 die_errno("could not write commit template");
615 determine_author_info();
617 /* This checks if committer ident is explicitly given */
618 git_committer_info(0);
619 if (use_editor
&& include_status
) {
621 const char *committer_ident
;
626 "# It looks like you may be committing a MERGE.\n"
627 "# If this is not correct, please remove the file\n"
631 git_path("MERGE_HEAD"));
635 "# Please enter the commit message for your changes.");
636 if (cleanup_mode
== CLEANUP_ALL
)
639 "# with '#' will be ignored, and an empty"
640 " message aborts the commit.\n");
641 else /* CLEANUP_SPACE, that is. */
644 "# with '#' will be kept; you may remove them"
645 " yourself if you want to.\n"
646 "# An empty message aborts the commit.\n");
647 if (only_include_assumed
)
648 fprintf(fp
, "# %s\n", only_include_assumed
);
650 author_ident
= xstrdup(fmt_name(author_name
, author_email
));
651 committer_ident
= fmt_name(getenv("GIT_COMMITTER_NAME"),
652 getenv("GIT_COMMITTER_EMAIL"));
653 if (strcmp(author_ident
, committer_ident
))
657 ident_shown
++ ? "" : "#\n",
661 if (!user_ident_sufficiently_given())
665 ident_shown
++ ? "" : "#\n",
671 saved_color_setting
= s
->use_color
;
673 commitable
= run_status(fp
, index_file
, prefix
, 1, s
);
674 s
->use_color
= saved_color_setting
;
676 unsigned char sha1
[20];
677 const char *parent
= "HEAD";
679 if (!active_nr
&& read_cache() < 0)
680 die("Cannot read index");
685 if (get_sha1(parent
, sha1
))
686 commitable
= !!active_nr
;
688 commitable
= index_differs_from(parent
, 0);
693 if (!commitable
&& !in_merge
&& !allow_empty
&&
694 !(amend
&& is_a_merge(head_sha1
))) {
695 run_status(stdout
, index_file
, prefix
, 0, s
);
700 * Re-read the index as pre-commit hook could have updated it,
701 * and write it out as a tree. We must do this before we invoke
702 * the editor and after we invoke run_status above.
705 read_cache_from(index_file
);
706 if (!active_cache_tree
)
707 active_cache_tree
= cache_tree();
708 if (cache_tree_update(active_cache_tree
,
709 active_cache
, active_nr
, 0, 0) < 0) {
710 error("Error building trees");
714 if (run_hook(index_file
, "prepare-commit-msg",
715 git_path(commit_editmsg
), hook_arg1
, hook_arg2
, NULL
))
719 char index
[PATH_MAX
];
720 const char *env
[2] = { index
, NULL
};
721 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
722 if (launch_editor(git_path(commit_editmsg
), NULL
, env
)) {
724 "Please supply the message using either -m or -F option.\n");
730 run_hook(index_file
, "commit-msg", git_path(commit_editmsg
), NULL
)) {
738 * Find out if the message in the strbuf contains only whitespace and
739 * Signed-off-by lines.
741 static int message_is_empty(struct strbuf
*sb
)
743 struct strbuf tmpl
= STRBUF_INIT
;
745 int eol
, i
, start
= 0;
747 if (cleanup_mode
== CLEANUP_NONE
&& sb
->len
)
750 /* See if the template is just a prefix of the message. */
751 if (template_file
&& strbuf_read_file(&tmpl
, template_file
, 0) > 0) {
752 stripspace(&tmpl
, cleanup_mode
== CLEANUP_ALL
);
753 if (start
+ tmpl
.len
<= sb
->len
&&
754 memcmp(tmpl
.buf
, sb
->buf
+ start
, tmpl
.len
) == 0)
757 strbuf_release(&tmpl
);
759 /* Check if the rest is just whitespace and Signed-of-by's. */
760 for (i
= start
; i
< sb
->len
; i
++) {
761 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
767 if (strlen(sign_off_header
) <= eol
- i
&&
768 !prefixcmp(sb
->buf
+ i
, sign_off_header
)) {
773 if (!isspace(sb
->buf
[i
++]))
780 static const char *find_author_by_nickname(const char *name
)
782 struct rev_info revs
;
783 struct commit
*commit
;
784 struct strbuf buf
= STRBUF_INIT
;
788 init_revisions(&revs
, NULL
);
789 strbuf_addf(&buf
, "--author=%s", name
);
794 setup_revisions(ac
, av
, &revs
, NULL
);
795 prepare_revision_walk(&revs
);
796 commit
= get_revision(&revs
);
798 struct pretty_print_context ctx
= {0};
799 ctx
.date_mode
= DATE_NORMAL
;
800 strbuf_release(&buf
);
801 format_commit_message(commit
, "%an <%ae>", &buf
, &ctx
);
802 return strbuf_detach(&buf
, NULL
);
804 die("No existing author found with '%s'", name
);
808 static void handle_untracked_files_arg(struct wt_status
*s
)
810 if (!untracked_files_arg
)
811 ; /* default already initialized */
812 else if (!strcmp(untracked_files_arg
, "no"))
813 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
814 else if (!strcmp(untracked_files_arg
, "normal"))
815 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
816 else if (!strcmp(untracked_files_arg
, "all"))
817 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
819 die("Invalid untracked files mode '%s'", untracked_files_arg
);
822 static int parse_and_validate_options(int argc
, const char *argv
[],
823 const char * const usage
[],
829 argc
= parse_options(argc
, argv
, prefix
, builtin_commit_options
, usage
,
832 if (force_author
&& !strchr(force_author
, '>'))
833 force_author
= find_author_by_nickname(force_author
);
835 if (force_author
&& renew_authorship
)
836 die("Using both --reset-author and --author does not make sense");
838 if (logfile
|| message
.len
|| use_message
)
843 setenv("GIT_EDITOR", ":", 1);
845 if (get_sha1("HEAD", head_sha1
))
848 /* Sanity check options */
849 if (amend
&& initial_commit
)
850 die("You have nothing to amend.");
851 if (amend
&& in_merge
)
852 die("You are in the middle of a merge -- cannot amend.");
861 die("Only one of -c/-C/-F can be used.");
862 if (message
.len
&& f
> 0)
863 die("Option -m cannot be combined with -c/-C/-F.");
865 use_message
= edit_message
;
866 if (amend
&& !use_message
)
867 use_message
= "HEAD";
868 if (!use_message
&& renew_authorship
)
869 die("--reset-author can be used only with -C, -c or --amend.");
871 unsigned char sha1
[20];
872 static char utf8
[] = "UTF-8";
875 struct commit
*commit
;
877 if (get_sha1(use_message
, sha1
))
878 die("could not lookup commit %s", use_message
);
879 commit
= lookup_commit_reference(sha1
);
880 if (!commit
|| parse_commit(commit
))
881 die("could not parse commit %s", use_message
);
883 enc
= strstr(commit
->buffer
, "\nencoding");
885 end
= strchr(enc
+ 10, '\n');
886 enc
= xstrndup(enc
+ 10, end
- (enc
+ 10));
890 out_enc
= git_commit_encoding
? git_commit_encoding
: utf8
;
892 if (strcmp(out_enc
, enc
))
894 reencode_string(commit
->buffer
, out_enc
, enc
);
897 * If we failed to reencode the buffer, just copy it
898 * byte for byte so the user can try to fix it up.
899 * This also handles the case where input and output
900 * encodings are identical.
902 if (use_message_buffer
== NULL
)
903 use_message_buffer
= xstrdup(commit
->buffer
);
908 if (!!also
+ !!only
+ !!all
+ !!interactive
> 1)
909 die("Only one of --include/--only/--all/--interactive can be used.");
910 if (argc
== 0 && (also
|| (only
&& !amend
)))
911 die("No paths with --include/--only does not make sense.");
912 if (argc
== 0 && only
&& amend
)
913 only_include_assumed
= "Clever... amending the last one with dirty index.";
914 if (argc
> 0 && !also
&& !only
)
915 only_include_assumed
= "Explicit paths specified without -i nor -o; assuming --only paths...";
916 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "default"))
917 cleanup_mode
= use_editor
? CLEANUP_ALL
: CLEANUP_SPACE
;
918 else if (!strcmp(cleanup_arg
, "verbatim"))
919 cleanup_mode
= CLEANUP_NONE
;
920 else if (!strcmp(cleanup_arg
, "whitespace"))
921 cleanup_mode
= CLEANUP_SPACE
;
922 else if (!strcmp(cleanup_arg
, "strip"))
923 cleanup_mode
= CLEANUP_ALL
;
925 die("Invalid cleanup mode %s", cleanup_arg
);
927 handle_untracked_files_arg(s
);
930 die("Paths with -a does not make sense.");
931 else if (interactive
&& argc
> 0)
932 die("Paths with --interactive does not make sense.");
934 if (null_termination
&& status_format
== STATUS_FORMAT_LONG
)
935 status_format
= STATUS_FORMAT_PORCELAIN
;
936 if (status_format
!= STATUS_FORMAT_LONG
)
942 static int dry_run_commit(int argc
, const char **argv
, const char *prefix
,
946 const char *index_file
;
948 index_file
= prepare_index(argc
, argv
, prefix
, 1);
949 commitable
= run_status(stdout
, index_file
, prefix
, 0, s
);
950 rollback_index_files();
952 return commitable
? 0 : 1;
955 static int parse_status_slot(const char *var
, int offset
)
957 if (!strcasecmp(var
+offset
, "header"))
958 return WT_STATUS_HEADER
;
959 if (!strcasecmp(var
+offset
, "updated")
960 || !strcasecmp(var
+offset
, "added"))
961 return WT_STATUS_UPDATED
;
962 if (!strcasecmp(var
+offset
, "changed"))
963 return WT_STATUS_CHANGED
;
964 if (!strcasecmp(var
+offset
, "untracked"))
965 return WT_STATUS_UNTRACKED
;
966 if (!strcasecmp(var
+offset
, "nobranch"))
967 return WT_STATUS_NOBRANCH
;
968 if (!strcasecmp(var
+offset
, "unmerged"))
969 return WT_STATUS_UNMERGED
;
973 static int git_status_config(const char *k
, const char *v
, void *cb
)
975 struct wt_status
*s
= cb
;
977 if (!strcmp(k
, "status.submodulesummary")) {
979 s
->submodule_summary
= git_config_bool_or_int(k
, v
, &is_bool
);
980 if (is_bool
&& s
->submodule_summary
)
981 s
->submodule_summary
= -1;
984 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
985 s
->use_color
= git_config_colorbool(k
, v
, -1);
988 if (!prefixcmp(k
, "status.color.") || !prefixcmp(k
, "color.status.")) {
989 int slot
= parse_status_slot(k
, 13);
993 return config_error_nonbool(k
);
994 color_parse(v
, k
, s
->color_palette
[slot
]);
997 if (!strcmp(k
, "status.relativepaths")) {
998 s
->relative_paths
= git_config_bool(k
, v
);
1001 if (!strcmp(k
, "status.showuntrackedfiles")) {
1003 return config_error_nonbool(k
);
1004 else if (!strcmp(v
, "no"))
1005 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
1006 else if (!strcmp(v
, "normal"))
1007 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
1008 else if (!strcmp(v
, "all"))
1009 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
1011 return error("Invalid untracked files mode '%s'", v
);
1014 return git_diff_ui_config(k
, v
, NULL
);
1017 int cmd_status(int argc
, const char **argv
, const char *prefix
)
1021 unsigned char sha1
[20];
1022 static struct option builtin_status_options
[] = {
1023 OPT__VERBOSE(&verbose
),
1024 OPT_SET_INT('s', "short", &status_format
,
1025 "show status concisely", STATUS_FORMAT_SHORT
),
1026 OPT_SET_INT(0, "porcelain", &status_format
,
1027 "show porcelain output format",
1028 STATUS_FORMAT_PORCELAIN
),
1029 OPT_BOOLEAN('z', "null", &null_termination
,
1030 "terminate entries with NUL"),
1031 { OPTION_STRING
, 'u', "untracked-files", &untracked_files_arg
,
1033 "show untracked files, optional modes: all, normal, no. (Default: all)",
1034 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1038 if (null_termination
&& status_format
== STATUS_FORMAT_LONG
)
1039 status_format
= STATUS_FORMAT_PORCELAIN
;
1041 wt_status_prepare(&s
);
1042 git_config(git_status_config
, &s
);
1043 in_merge
= file_exists(git_path("MERGE_HEAD"));
1044 argc
= parse_options(argc
, argv
, prefix
,
1045 builtin_status_options
,
1046 builtin_status_usage
, 0);
1047 handle_untracked_files_arg(&s
);
1050 s
.pathspec
= get_pathspec(prefix
, argv
);
1052 read_cache_preload(s
.pathspec
);
1053 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, s
.pathspec
, NULL
, NULL
);
1055 fd
= hold_locked_index(&index_lock
, 0);
1057 if (!write_cache(fd
, active_cache
, active_nr
))
1058 commit_locked_index(&index_lock
);
1059 rollback_lock_file(&index_lock
);
1062 s
.is_initial
= get_sha1(s
.reference
, sha1
) ? 1 : 0;
1063 s
.in_merge
= in_merge
;
1064 wt_status_collect(&s
);
1066 if (s
.relative_paths
)
1068 if (s
.use_color
== -1)
1069 s
.use_color
= git_use_color_default
;
1070 if (diff_use_color_default
== -1)
1071 diff_use_color_default
= git_use_color_default
;
1073 switch (status_format
) {
1074 case STATUS_FORMAT_SHORT
:
1075 wt_shortstatus_print(&s
, null_termination
);
1077 case STATUS_FORMAT_PORCELAIN
:
1078 wt_porcelain_print(&s
, null_termination
);
1080 case STATUS_FORMAT_LONG
:
1081 s
.verbose
= verbose
;
1082 wt_status_print(&s
);
1088 static void print_summary(const char *prefix
, const unsigned char *sha1
)
1090 struct rev_info rev
;
1091 struct commit
*commit
;
1092 struct strbuf format
= STRBUF_INIT
;
1093 unsigned char junk_sha1
[20];
1094 const char *head
= resolve_ref("HEAD", junk_sha1
, 0, NULL
);
1095 struct pretty_print_context pctx
= {0};
1096 struct strbuf author_ident
= STRBUF_INIT
;
1097 struct strbuf committer_ident
= STRBUF_INIT
;
1099 commit
= lookup_commit(sha1
);
1101 die("couldn't look up newly created commit");
1102 if (!commit
|| parse_commit(commit
))
1103 die("could not parse newly created commit");
1105 strbuf_addstr(&format
, "format:%h] %s");
1107 format_commit_message(commit
, "%an <%ae>", &author_ident
, &pctx
);
1108 format_commit_message(commit
, "%cn <%ce>", &committer_ident
, &pctx
);
1109 if (strbuf_cmp(&author_ident
, &committer_ident
)) {
1110 strbuf_addstr(&format
, "\n Author: ");
1111 strbuf_addbuf_percentquote(&format
, &author_ident
);
1113 if (!user_ident_sufficiently_given()) {
1114 strbuf_addstr(&format
, "\n Committer: ");
1115 strbuf_addbuf_percentquote(&format
, &committer_ident
);
1116 if (advice_implicit_identity
) {
1117 strbuf_addch(&format
, '\n');
1118 strbuf_addstr(&format
, implicit_ident_advice
);
1121 strbuf_release(&author_ident
);
1122 strbuf_release(&committer_ident
);
1124 init_revisions(&rev
, prefix
);
1125 setup_revisions(0, NULL
, &rev
, NULL
);
1129 rev
.diffopt
.output_format
=
1130 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
1132 rev
.verbose_header
= 1;
1133 rev
.show_root_diff
= 1;
1134 get_commit_format(format
.buf
, &rev
);
1135 rev
.always_show_header
= 0;
1136 rev
.diffopt
.detect_rename
= 1;
1137 rev
.diffopt
.rename_limit
= 100;
1138 rev
.diffopt
.break_opt
= 0;
1139 diff_setup_done(&rev
.diffopt
);
1142 !prefixcmp(head
, "refs/heads/") ?
1144 !strcmp(head
, "HEAD") ?
1147 initial_commit
? " (root-commit)" : "");
1149 if (!log_tree_commit(&rev
, commit
)) {
1150 struct pretty_print_context ctx
= {0};
1151 struct strbuf buf
= STRBUF_INIT
;
1152 ctx
.date_mode
= DATE_NORMAL
;
1153 format_commit_message(commit
, format
.buf
+ 7, &buf
, &ctx
);
1154 printf("%s\n", buf
.buf
);
1155 strbuf_release(&buf
);
1157 strbuf_release(&format
);
1160 static int git_commit_config(const char *k
, const char *v
, void *cb
)
1162 struct wt_status
*s
= cb
;
1164 if (!strcmp(k
, "commit.template"))
1165 return git_config_pathname(&template_file
, k
, v
);
1166 if (!strcmp(k
, "commit.status")) {
1167 include_status
= git_config_bool(k
, v
);
1171 return git_status_config(k
, v
, s
);
1174 static const char post_rewrite_hook
[] = "hooks/post-rewrite";
1176 static int run_rewrite_hook(const unsigned char *oldsha1
,
1177 const unsigned char *newsha1
)
1179 /* oldsha1 SP newsha1 LF NUL */
1180 static char buf
[2*40 + 3];
1181 struct child_process proc
;
1182 const char *argv
[3];
1186 if (access(git_path(post_rewrite_hook
), X_OK
) < 0)
1189 argv
[0] = git_path(post_rewrite_hook
);
1193 memset(&proc
, 0, sizeof(proc
));
1196 proc
.stdout_to_stderr
= 1;
1198 code
= start_command(&proc
);
1201 n
= snprintf(buf
, sizeof(buf
), "%s %s\n",
1202 sha1_to_hex(oldsha1
), sha1_to_hex(newsha1
));
1203 write_in_full(proc
.in
, buf
, n
);
1205 return finish_command(&proc
);
1208 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
1210 struct strbuf sb
= STRBUF_INIT
;
1211 const char *index_file
, *reflog_msg
;
1213 unsigned char commit_sha1
[20];
1214 struct ref_lock
*ref_lock
;
1215 struct commit_list
*parents
= NULL
, **pptr
= &parents
;
1216 struct stat statbuf
;
1217 int allow_fast_forward
= 1;
1220 wt_status_prepare(&s
);
1221 git_config(git_commit_config
, &s
);
1222 in_merge
= file_exists(git_path("MERGE_HEAD"));
1223 s
.in_merge
= in_merge
;
1225 if (s
.use_color
== -1)
1226 s
.use_color
= git_use_color_default
;
1227 argc
= parse_and_validate_options(argc
, argv
, builtin_commit_usage
,
1230 if (diff_use_color_default
== -1)
1231 diff_use_color_default
= git_use_color_default
;
1232 return dry_run_commit(argc
, argv
, prefix
, &s
);
1234 index_file
= prepare_index(argc
, argv
, prefix
, 0);
1236 /* Set up everything for writing the commit object. This includes
1237 running hooks, writing the trees, and interacting with the user. */
1238 if (!prepare_to_commit(index_file
, prefix
, &s
)) {
1239 rollback_index_files();
1243 /* Determine parents */
1244 if (initial_commit
) {
1245 reflog_msg
= "commit (initial)";
1247 struct commit_list
*c
;
1248 struct commit
*commit
;
1250 reflog_msg
= "commit (amend)";
1251 commit
= lookup_commit(head_sha1
);
1252 if (!commit
|| parse_commit(commit
))
1253 die("could not parse HEAD commit");
1255 for (c
= commit
->parents
; c
; c
= c
->next
)
1256 pptr
= &commit_list_insert(c
->item
, pptr
)->next
;
1257 } else if (in_merge
) {
1258 struct strbuf m
= STRBUF_INIT
;
1261 reflog_msg
= "commit (merge)";
1262 pptr
= &commit_list_insert(lookup_commit(head_sha1
), pptr
)->next
;
1263 fp
= fopen(git_path("MERGE_HEAD"), "r");
1265 die_errno("could not open '%s' for reading",
1266 git_path("MERGE_HEAD"));
1267 while (strbuf_getline(&m
, fp
, '\n') != EOF
) {
1268 unsigned char sha1
[20];
1269 if (get_sha1_hex(m
.buf
, sha1
) < 0)
1270 die("Corrupt MERGE_HEAD file (%s)", m
.buf
);
1271 pptr
= &commit_list_insert(lookup_commit(sha1
), pptr
)->next
;
1275 if (!stat(git_path("MERGE_MODE"), &statbuf
)) {
1276 if (strbuf_read_file(&sb
, git_path("MERGE_MODE"), 0) < 0)
1277 die_errno("could not read MERGE_MODE");
1278 if (!strcmp(sb
.buf
, "no-ff"))
1279 allow_fast_forward
= 0;
1281 if (allow_fast_forward
)
1282 parents
= reduce_heads(parents
);
1284 reflog_msg
= "commit";
1285 pptr
= &commit_list_insert(lookup_commit(head_sha1
), pptr
)->next
;
1288 /* Finally, get the commit message */
1290 if (strbuf_read_file(&sb
, git_path(commit_editmsg
), 0) < 0) {
1291 int saved_errno
= errno
;
1292 rollback_index_files();
1293 die("could not read commit message: %s", strerror(saved_errno
));
1296 /* Truncate the message just before the diff, if any. */
1298 p
= strstr(sb
.buf
, "\ndiff --git ");
1300 strbuf_setlen(&sb
, p
- sb
.buf
+ 1);
1303 if (cleanup_mode
!= CLEANUP_NONE
)
1304 stripspace(&sb
, cleanup_mode
== CLEANUP_ALL
);
1305 if (message_is_empty(&sb
)) {
1306 rollback_index_files();
1307 fprintf(stderr
, "Aborting commit due to empty commit message.\n");
1311 if (commit_tree(sb
.buf
, active_cache_tree
->sha1
, parents
, commit_sha1
,
1312 fmt_ident(author_name
, author_email
, author_date
,
1313 IDENT_ERROR_ON_NO_NAME
))) {
1314 rollback_index_files();
1315 die("failed to write commit object");
1318 ref_lock
= lock_any_ref_for_update("HEAD",
1319 initial_commit
? NULL
: head_sha1
,
1322 nl
= strchr(sb
.buf
, '\n');
1324 strbuf_setlen(&sb
, nl
+ 1 - sb
.buf
);
1326 strbuf_addch(&sb
, '\n');
1327 strbuf_insert(&sb
, 0, reflog_msg
, strlen(reflog_msg
));
1328 strbuf_insert(&sb
, strlen(reflog_msg
), ": ", 2);
1331 rollback_index_files();
1332 die("cannot lock HEAD ref");
1334 if (write_ref_sha1(ref_lock
, commit_sha1
, sb
.buf
) < 0) {
1335 rollback_index_files();
1336 die("cannot update HEAD ref");
1339 unlink(git_path("MERGE_HEAD"));
1340 unlink(git_path("MERGE_MSG"));
1341 unlink(git_path("MERGE_MODE"));
1342 unlink(git_path("SQUASH_MSG"));
1344 if (commit_index_files())
1345 die ("Repository has been updated, but unable to write\n"
1346 "new_index file. Check that disk is not full or quota is\n"
1347 "not exceeded, and then \"git reset HEAD\" to recover.");
1350 run_hook(get_index_file(), "post-commit", NULL
);
1351 if (amend
&& !no_post_rewrite
) {
1352 struct notes_rewrite_cfg
*cfg
;
1353 cfg
= init_copy_notes_for_rewrite("amend");
1355 copy_note_for_rewrite(cfg
, head_sha1
, commit_sha1
);
1356 finish_copy_notes_for_rewrite(cfg
);
1358 run_rewrite_hook(head_sha1
, commit_sha1
);
1361 print_summary(prefix
, commit_sha1
);