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
, allow_empty_message
;
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 int show_ignored_in_status
;
87 static const char *only_include_assumed
;
88 static struct strbuf message
;
90 static int null_termination
;
94 STATUS_FORMAT_PORCELAIN
,
95 } status_format
= STATUS_FORMAT_LONG
;
96 static int status_show_branch
;
98 static int opt_parse_m(const struct option
*opt
, const char *arg
, int unset
)
100 struct strbuf
*buf
= opt
->value
;
102 strbuf_setlen(buf
, 0);
104 strbuf_addstr(buf
, arg
);
105 strbuf_addstr(buf
, "\n\n");
110 static struct option builtin_commit_options
[] = {
112 OPT__VERBOSE(&verbose
),
114 OPT_GROUP("Commit message options"),
115 OPT_FILENAME('F', "file", &logfile
, "read log from file"),
116 OPT_STRING(0, "author", &force_author
, "AUTHOR", "override author for commit"),
117 OPT_STRING(0, "date", &force_date
, "DATE", "override date for commit"),
118 OPT_CALLBACK('m', "message", &message
, "MESSAGE", "specify commit message", opt_parse_m
),
119 OPT_STRING('c', "reedit-message", &edit_message
, "COMMIT", "reuse and edit message from specified commit"),
120 OPT_STRING('C', "reuse-message", &use_message
, "COMMIT", "reuse message from specified commit"),
121 OPT_BOOLEAN(0, "reset-author", &renew_authorship
, "the commit is authored by me now (used with -C-c/--amend)"),
122 OPT_BOOLEAN('s', "signoff", &signoff
, "add Signed-off-by:"),
123 OPT_FILENAME('t', "template", &template_file
, "use specified template file"),
124 OPT_BOOLEAN('e', "edit", &edit_flag
, "force edit of commit"),
125 OPT_STRING(0, "cleanup", &cleanup_arg
, "default", "how to strip spaces and #comments from message"),
126 OPT_BOOLEAN(0, "status", &include_status
, "include status in commit message template"),
127 /* end commit message options */
129 OPT_GROUP("Commit contents options"),
130 OPT_BOOLEAN('a', "all", &all
, "commit all changed files"),
131 OPT_BOOLEAN('i', "include", &also
, "add specified files to index for commit"),
132 OPT_BOOLEAN(0, "interactive", &interactive
, "interactively add files"),
133 OPT_BOOLEAN('o', "only", &only
, "commit only specified files"),
134 OPT_BOOLEAN('n', "no-verify", &no_verify
, "bypass pre-commit hook"),
135 OPT_BOOLEAN(0, "dry-run", &dry_run
, "show what would be committed"),
136 OPT_SET_INT(0, "short", &status_format
, "show status concisely",
137 STATUS_FORMAT_SHORT
),
138 OPT_BOOLEAN(0, "branch", &status_show_branch
, "show branch information"),
139 OPT_SET_INT(0, "porcelain", &status_format
,
140 "show porcelain output format", STATUS_FORMAT_PORCELAIN
),
141 OPT_BOOLEAN('z', "null", &null_termination
,
142 "terminate entries with NUL"),
143 OPT_BOOLEAN(0, "amend", &amend
, "amend previous commit"),
144 OPT_BOOLEAN(0, "no-post-rewrite", &no_post_rewrite
, "bypass post-rewrite hook"),
145 { 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" },
146 /* end commit contents options */
148 { OPTION_BOOLEAN
, 0, "allow-empty", &allow_empty
, NULL
,
149 "ok to record an empty change",
150 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
},
151 { OPTION_BOOLEAN
, 0, "allow-empty-message", &allow_empty_message
, NULL
,
152 "ok to record a change with an empty message",
153 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
},
158 static void rollback_index_files(void)
160 switch (commit_style
) {
162 break; /* nothing to do */
164 rollback_lock_file(&index_lock
);
167 rollback_lock_file(&index_lock
);
168 rollback_lock_file(&false_lock
);
173 static int commit_index_files(void)
177 switch (commit_style
) {
179 break; /* nothing to do */
181 err
= commit_lock_file(&index_lock
);
184 err
= commit_lock_file(&index_lock
);
185 rollback_lock_file(&false_lock
);
193 * Take a union of paths in the index and the named tree (typically, "HEAD"),
194 * and return the paths that match the given pattern in list.
196 static int list_paths(struct string_list
*list
, const char *with_tree
,
197 const char *prefix
, const char **pattern
)
202 for (i
= 0; pattern
[i
]; i
++)
207 overlay_tree_on_cache(with_tree
, prefix
);
209 for (i
= 0; i
< active_nr
; i
++) {
210 struct cache_entry
*ce
= active_cache
[i
];
211 struct string_list_item
*item
;
213 if (ce
->ce_flags
& CE_UPDATE
)
215 if (!match_pathspec(pattern
, ce
->name
, ce_namelen(ce
), 0, m
))
217 item
= string_list_insert(ce
->name
, list
);
218 if (ce_skip_worktree(ce
))
219 item
->util
= item
; /* better a valid pointer than a fake one */
222 return report_path_error(m
, pattern
, prefix
? strlen(prefix
) : 0);
225 static void add_remove_files(struct string_list
*list
)
228 for (i
= 0; i
< list
->nr
; i
++) {
230 struct string_list_item
*p
= &(list
->items
[i
]);
232 /* p->util is skip-worktree */
236 if (!lstat(p
->string
, &st
)) {
237 if (add_to_cache(p
->string
, &st
, 0))
238 die("updating files failed");
240 remove_file_from_cache(p
->string
);
244 static void create_base_index(void)
247 struct unpack_trees_options opts
;
250 if (initial_commit
) {
255 memset(&opts
, 0, sizeof(opts
));
259 opts
.src_index
= &the_index
;
260 opts
.dst_index
= &the_index
;
262 opts
.fn
= oneway_merge
;
263 tree
= parse_tree_indirect(head_sha1
);
265 die("failed to unpack HEAD tree object");
267 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
268 if (unpack_trees(1, &t
, &opts
))
269 exit(128); /* We've already reported the error, finish dying */
272 static void refresh_cache_or_die(int refresh_flags
)
275 * refresh_flags contains REFRESH_QUIET, so the only errors
276 * are for unmerged entries.
278 if (refresh_cache(refresh_flags
| REFRESH_IN_PORCELAIN
))
279 die_resolve_conflict("commit");
282 static char *prepare_index(int argc
, const char **argv
, const char *prefix
, int is_status
)
285 struct string_list partial
;
286 const char **pathspec
= NULL
;
287 int refresh_flags
= REFRESH_QUIET
;
290 refresh_flags
|= REFRESH_UNMERGED
;
292 if (interactive_add(argc
, argv
, prefix
) != 0)
293 die("interactive add failed");
294 if (read_cache_preload(NULL
) < 0)
295 die("index file corrupt");
296 commit_style
= COMMIT_AS_IS
;
297 return get_index_file();
301 pathspec
= get_pathspec(prefix
, argv
);
303 if (read_cache_preload(pathspec
) < 0)
304 die("index file corrupt");
307 * Non partial, non as-is commit.
309 * (1) get the real index;
310 * (2) update the_index as necessary;
311 * (3) write the_index out to the real index (still locked);
312 * (4) return the name of the locked index file.
314 * The caller should run hooks on the locked real index, and
315 * (A) if all goes well, commit the real index;
316 * (B) on failure, rollback the real index.
318 if (all
|| (also
&& pathspec
&& *pathspec
)) {
319 fd
= hold_locked_index(&index_lock
, 1);
320 add_files_to_cache(also
? prefix
: NULL
, pathspec
, 0);
321 refresh_cache_or_die(refresh_flags
);
322 if (write_cache(fd
, active_cache
, active_nr
) ||
323 close_lock_file(&index_lock
))
324 die("unable to write new_index file");
325 commit_style
= COMMIT_NORMAL
;
326 return index_lock
.filename
;
332 * (1) return the name of the real index file.
334 * The caller should run hooks on the real index,
335 * and create commit from the_index.
336 * We still need to refresh the index here.
338 if (!pathspec
|| !*pathspec
) {
339 fd
= hold_locked_index(&index_lock
, 1);
340 refresh_cache_or_die(refresh_flags
);
341 if (write_cache(fd
, active_cache
, active_nr
) ||
342 commit_locked_index(&index_lock
))
343 die("unable to write new_index file");
344 commit_style
= COMMIT_AS_IS
;
345 return get_index_file();
351 * (0) find the set of affected paths;
352 * (1) get lock on the real index file;
353 * (2) update the_index with the given paths;
354 * (3) write the_index out to the real index (still locked);
355 * (4) get lock on the false index file;
356 * (5) reset the_index from HEAD;
357 * (6) update the_index the same way as (2);
358 * (7) write the_index out to the false index file;
359 * (8) return the name of the false index file (still locked);
361 * The caller should run hooks on the locked false index, and
362 * create commit from it. Then
363 * (A) if all goes well, commit the real index;
364 * (B) on failure, rollback the real index;
365 * In either case, rollback the false index.
367 commit_style
= COMMIT_PARTIAL
;
370 die("cannot do a partial commit during a merge.");
372 memset(&partial
, 0, sizeof(partial
));
373 partial
.strdup_strings
= 1;
374 if (list_paths(&partial
, initial_commit
? NULL
: "HEAD", prefix
, pathspec
))
378 if (read_cache() < 0)
379 die("cannot read the index");
381 fd
= hold_locked_index(&index_lock
, 1);
382 add_remove_files(&partial
);
383 refresh_cache(REFRESH_QUIET
);
384 if (write_cache(fd
, active_cache
, active_nr
) ||
385 close_lock_file(&index_lock
))
386 die("unable to write new_index file");
388 fd
= hold_lock_file_for_update(&false_lock
,
389 git_path("next-index-%"PRIuMAX
,
390 (uintmax_t) getpid()),
394 add_remove_files(&partial
);
395 refresh_cache(REFRESH_QUIET
);
397 if (write_cache(fd
, active_cache
, active_nr
) ||
398 close_lock_file(&false_lock
))
399 die("unable to write temporary index file");
402 read_cache_from(false_lock
.filename
);
404 return false_lock
.filename
;
407 static int run_status(FILE *fp
, const char *index_file
, const char *prefix
, int nowarn
,
410 unsigned char sha1
[20];
412 if (s
->relative_paths
)
417 s
->reference
= "HEAD^1";
419 s
->verbose
= verbose
;
420 s
->index_file
= index_file
;
423 s
->is_initial
= get_sha1(s
->reference
, sha1
) ? 1 : 0;
425 wt_status_collect(s
);
427 switch (status_format
) {
428 case STATUS_FORMAT_SHORT
:
429 wt_shortstatus_print(s
, null_termination
, status_show_branch
);
431 case STATUS_FORMAT_PORCELAIN
:
432 wt_porcelain_print(s
, null_termination
);
434 case STATUS_FORMAT_LONG
:
439 return s
->commitable
;
442 static int is_a_merge(const unsigned char *sha1
)
444 struct commit
*commit
= lookup_commit(sha1
);
445 if (!commit
|| parse_commit(commit
))
446 die("could not parse HEAD commit");
447 return !!(commit
->parents
&& commit
->parents
->next
);
450 static const char sign_off_header
[] = "Signed-off-by: ";
452 static void determine_author_info(void)
454 char *name
, *email
, *date
;
456 name
= getenv("GIT_AUTHOR_NAME");
457 email
= getenv("GIT_AUTHOR_EMAIL");
458 date
= getenv("GIT_AUTHOR_DATE");
460 if (use_message
&& !renew_authorship
) {
461 const char *a
, *lb
, *rb
, *eol
;
463 a
= strstr(use_message_buffer
, "\nauthor ");
465 die("invalid commit: %s", use_message
);
467 lb
= strstr(a
+ 8, " <");
468 rb
= strstr(a
+ 8, "> ");
469 eol
= strchr(a
+ 8, '\n');
470 if (!lb
|| !rb
|| !eol
)
471 die("invalid commit: %s", use_message
);
473 name
= xstrndup(a
+ 8, lb
- (a
+ 8));
474 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
475 date
= xstrndup(rb
+ 2, eol
- (rb
+ 2));
479 const char *lb
= strstr(force_author
, " <");
480 const char *rb
= strchr(force_author
, '>');
483 die("malformed --author parameter");
484 name
= xstrndup(force_author
, lb
- force_author
);
485 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
492 author_email
= email
;
496 static int ends_rfc2822_footer(struct strbuf
*sb
)
503 const char *buf
= sb
->buf
;
505 for (i
= len
- 1; i
> 0; i
--) {
506 if (hit
&& buf
[i
] == '\n')
508 hit
= (buf
[i
] == '\n');
511 while (i
< len
- 1 && buf
[i
] == '\n')
514 for (; i
< len
; i
= k
) {
515 for (k
= i
; k
< len
&& buf
[k
] != '\n'; k
++)
519 if ((buf
[k
] == ' ' || buf
[k
] == '\t') && !first
)
524 for (j
= 0; i
+ j
< len
; j
++) {
537 static int prepare_to_commit(const char *index_file
, const char *prefix
,
541 int commitable
, saved_color_setting
;
542 struct strbuf sb
= STRBUF_INIT
;
545 const char *hook_arg1
= NULL
;
546 const char *hook_arg2
= NULL
;
549 if (!no_verify
&& run_hook(index_file
, "pre-commit", NULL
))
553 strbuf_addbuf(&sb
, &message
);
554 hook_arg1
= "message";
555 } else if (logfile
&& !strcmp(logfile
, "-")) {
557 fprintf(stderr
, "(reading log message from standard input)\n");
558 if (strbuf_read(&sb
, 0, 0) < 0)
559 die_errno("could not read log from standard input");
560 hook_arg1
= "message";
561 } else if (logfile
) {
562 if (strbuf_read_file(&sb
, logfile
, 0) < 0)
563 die_errno("could not read log file '%s'",
565 hook_arg1
= "message";
566 } else if (use_message
) {
567 buffer
= strstr(use_message_buffer
, "\n\n");
568 if (!buffer
|| buffer
[2] == '\0')
569 die("commit has empty message");
570 strbuf_add(&sb
, buffer
+ 2, strlen(buffer
+ 2));
571 hook_arg1
= "commit";
572 hook_arg2
= use_message
;
573 } else if (!stat(git_path("MERGE_MSG"), &statbuf
)) {
574 if (strbuf_read_file(&sb
, git_path("MERGE_MSG"), 0) < 0)
575 die_errno("could not read MERGE_MSG");
577 } else if (!stat(git_path("SQUASH_MSG"), &statbuf
)) {
578 if (strbuf_read_file(&sb
, git_path("SQUASH_MSG"), 0) < 0)
579 die_errno("could not read SQUASH_MSG");
580 hook_arg1
= "squash";
581 } else if (template_file
&& !stat(template_file
, &statbuf
)) {
582 if (strbuf_read_file(&sb
, template_file
, 0) < 0)
583 die_errno("could not read '%s'", template_file
);
584 hook_arg1
= "template";
588 * This final case does not modify the template message,
589 * it just sets the argument to the prepare-commit-msg hook.
594 fp
= fopen(git_path(commit_editmsg
), "w");
596 die_errno("could not open '%s'", git_path(commit_editmsg
));
598 if (cleanup_mode
!= CLEANUP_NONE
)
602 struct strbuf sob
= STRBUF_INIT
;
605 strbuf_addstr(&sob
, sign_off_header
);
606 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
607 getenv("GIT_COMMITTER_EMAIL")));
608 strbuf_addch(&sob
, '\n');
609 for (i
= sb
.len
- 1; i
> 0 && sb
.buf
[i
- 1] != '\n'; i
--)
611 if (prefixcmp(sb
.buf
+ i
, sob
.buf
)) {
612 if (!i
|| !ends_rfc2822_footer(&sb
))
613 strbuf_addch(&sb
, '\n');
614 strbuf_addbuf(&sb
, &sob
);
616 strbuf_release(&sob
);
619 if (fwrite(sb
.buf
, 1, sb
.len
, fp
) < sb
.len
)
620 die_errno("could not write commit template");
624 determine_author_info();
626 /* This checks if committer ident is explicitly given */
627 git_committer_info(0);
628 if (use_editor
&& include_status
) {
630 const char *committer_ident
;
635 "# It looks like you may be committing a MERGE.\n"
636 "# If this is not correct, please remove the file\n"
640 git_path("MERGE_HEAD"));
644 "# Please enter the commit message for your changes.");
645 if (cleanup_mode
== CLEANUP_ALL
)
648 "# with '#' will be ignored, and an empty"
649 " message aborts the commit.\n");
650 else /* CLEANUP_SPACE, that is. */
653 "# with '#' will be kept; you may remove them"
654 " yourself if you want to.\n"
655 "# An empty message aborts the commit.\n");
656 if (only_include_assumed
)
657 fprintf(fp
, "# %s\n", only_include_assumed
);
659 author_ident
= xstrdup(fmt_name(author_name
, author_email
));
660 committer_ident
= fmt_name(getenv("GIT_COMMITTER_NAME"),
661 getenv("GIT_COMMITTER_EMAIL"));
662 if (strcmp(author_ident
, committer_ident
))
666 ident_shown
++ ? "" : "#\n",
670 if (!user_ident_sufficiently_given())
674 ident_shown
++ ? "" : "#\n",
680 saved_color_setting
= s
->use_color
;
682 commitable
= run_status(fp
, index_file
, prefix
, 1, s
);
683 s
->use_color
= saved_color_setting
;
685 unsigned char sha1
[20];
686 const char *parent
= "HEAD";
688 if (!active_nr
&& read_cache() < 0)
689 die("Cannot read index");
694 if (get_sha1(parent
, sha1
))
695 commitable
= !!active_nr
;
697 commitable
= index_differs_from(parent
, 0);
702 if (!commitable
&& !in_merge
&& !allow_empty
&&
703 !(amend
&& is_a_merge(head_sha1
))) {
704 run_status(stdout
, index_file
, prefix
, 0, s
);
709 * Re-read the index as pre-commit hook could have updated it,
710 * and write it out as a tree. We must do this before we invoke
711 * the editor and after we invoke run_status above.
714 read_cache_from(index_file
);
715 if (!active_cache_tree
)
716 active_cache_tree
= cache_tree();
717 if (cache_tree_update(active_cache_tree
,
718 active_cache
, active_nr
, 0, 0) < 0) {
719 error("Error building trees");
723 if (run_hook(index_file
, "prepare-commit-msg",
724 git_path(commit_editmsg
), hook_arg1
, hook_arg2
, NULL
))
728 char index
[PATH_MAX
];
729 const char *env
[2] = { index
, NULL
};
730 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
731 if (launch_editor(git_path(commit_editmsg
), NULL
, env
)) {
733 "Please supply the message using either -m or -F option.\n");
739 run_hook(index_file
, "commit-msg", git_path(commit_editmsg
), NULL
)) {
747 * Find out if the message in the strbuf contains only whitespace and
748 * Signed-off-by lines.
750 static int message_is_empty(struct strbuf
*sb
)
752 struct strbuf tmpl
= STRBUF_INIT
;
754 int eol
, i
, start
= 0;
756 if (cleanup_mode
== CLEANUP_NONE
&& sb
->len
)
759 /* See if the template is just a prefix of the message. */
760 if (template_file
&& strbuf_read_file(&tmpl
, template_file
, 0) > 0) {
761 stripspace(&tmpl
, cleanup_mode
== CLEANUP_ALL
);
762 if (start
+ tmpl
.len
<= sb
->len
&&
763 memcmp(tmpl
.buf
, sb
->buf
+ start
, tmpl
.len
) == 0)
766 strbuf_release(&tmpl
);
768 /* Check if the rest is just whitespace and Signed-of-by's. */
769 for (i
= start
; i
< sb
->len
; i
++) {
770 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
776 if (strlen(sign_off_header
) <= eol
- i
&&
777 !prefixcmp(sb
->buf
+ i
, sign_off_header
)) {
782 if (!isspace(sb
->buf
[i
++]))
789 static const char *find_author_by_nickname(const char *name
)
791 struct rev_info revs
;
792 struct commit
*commit
;
793 struct strbuf buf
= STRBUF_INIT
;
797 init_revisions(&revs
, NULL
);
798 strbuf_addf(&buf
, "--author=%s", name
);
803 setup_revisions(ac
, av
, &revs
, NULL
);
804 prepare_revision_walk(&revs
);
805 commit
= get_revision(&revs
);
807 struct pretty_print_context ctx
= {0};
808 ctx
.date_mode
= DATE_NORMAL
;
809 strbuf_release(&buf
);
810 format_commit_message(commit
, "%an <%ae>", &buf
, &ctx
);
811 return strbuf_detach(&buf
, NULL
);
813 die("No existing author found with '%s'", name
);
817 static void handle_untracked_files_arg(struct wt_status
*s
)
819 if (!untracked_files_arg
)
820 ; /* default already initialized */
821 else if (!strcmp(untracked_files_arg
, "no"))
822 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
823 else if (!strcmp(untracked_files_arg
, "normal"))
824 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
825 else if (!strcmp(untracked_files_arg
, "all"))
826 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
828 die("Invalid untracked files mode '%s'", untracked_files_arg
);
831 static int parse_and_validate_options(int argc
, const char *argv
[],
832 const char * const usage
[],
838 argc
= parse_options(argc
, argv
, prefix
, builtin_commit_options
, usage
,
841 if (force_author
&& !strchr(force_author
, '>'))
842 force_author
= find_author_by_nickname(force_author
);
844 if (force_author
&& renew_authorship
)
845 die("Using both --reset-author and --author does not make sense");
847 if (logfile
|| message
.len
|| use_message
)
852 setenv("GIT_EDITOR", ":", 1);
854 if (get_sha1("HEAD", head_sha1
))
857 /* Sanity check options */
858 if (amend
&& initial_commit
)
859 die("You have nothing to amend.");
860 if (amend
&& in_merge
)
861 die("You are in the middle of a merge -- cannot amend.");
870 die("Only one of -c/-C/-F can be used.");
871 if (message
.len
&& f
> 0)
872 die("Option -m cannot be combined with -c/-C/-F.");
874 use_message
= edit_message
;
875 if (amend
&& !use_message
)
876 use_message
= "HEAD";
877 if (!use_message
&& renew_authorship
)
878 die("--reset-author can be used only with -C, -c or --amend.");
880 unsigned char sha1
[20];
881 static char utf8
[] = "UTF-8";
884 struct commit
*commit
;
886 if (get_sha1(use_message
, sha1
))
887 die("could not lookup commit %s", use_message
);
888 commit
= lookup_commit_reference(sha1
);
889 if (!commit
|| parse_commit(commit
))
890 die("could not parse commit %s", use_message
);
892 enc
= strstr(commit
->buffer
, "\nencoding");
894 end
= strchr(enc
+ 10, '\n');
895 enc
= xstrndup(enc
+ 10, end
- (enc
+ 10));
899 out_enc
= git_commit_encoding
? git_commit_encoding
: utf8
;
901 if (strcmp(out_enc
, enc
))
903 reencode_string(commit
->buffer
, out_enc
, enc
);
906 * If we failed to reencode the buffer, just copy it
907 * byte for byte so the user can try to fix it up.
908 * This also handles the case where input and output
909 * encodings are identical.
911 if (use_message_buffer
== NULL
)
912 use_message_buffer
= xstrdup(commit
->buffer
);
917 if (!!also
+ !!only
+ !!all
+ !!interactive
> 1)
918 die("Only one of --include/--only/--all/--interactive can be used.");
919 if (argc
== 0 && (also
|| (only
&& !amend
)))
920 die("No paths with --include/--only does not make sense.");
921 if (argc
== 0 && only
&& amend
)
922 only_include_assumed
= "Clever... amending the last one with dirty index.";
923 if (argc
> 0 && !also
&& !only
)
924 only_include_assumed
= "Explicit paths specified without -i nor -o; assuming --only paths...";
925 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "default"))
926 cleanup_mode
= use_editor
? CLEANUP_ALL
: CLEANUP_SPACE
;
927 else if (!strcmp(cleanup_arg
, "verbatim"))
928 cleanup_mode
= CLEANUP_NONE
;
929 else if (!strcmp(cleanup_arg
, "whitespace"))
930 cleanup_mode
= CLEANUP_SPACE
;
931 else if (!strcmp(cleanup_arg
, "strip"))
932 cleanup_mode
= CLEANUP_ALL
;
934 die("Invalid cleanup mode %s", cleanup_arg
);
936 handle_untracked_files_arg(s
);
939 die("Paths with -a does not make sense.");
940 else if (interactive
&& argc
> 0)
941 die("Paths with --interactive does not make sense.");
943 if (null_termination
&& status_format
== STATUS_FORMAT_LONG
)
944 status_format
= STATUS_FORMAT_PORCELAIN
;
945 if (status_format
!= STATUS_FORMAT_LONG
)
951 static int dry_run_commit(int argc
, const char **argv
, const char *prefix
,
955 const char *index_file
;
957 index_file
= prepare_index(argc
, argv
, prefix
, 1);
958 commitable
= run_status(stdout
, index_file
, prefix
, 0, s
);
959 rollback_index_files();
961 return commitable
? 0 : 1;
964 static int parse_status_slot(const char *var
, int offset
)
966 if (!strcasecmp(var
+offset
, "header"))
967 return WT_STATUS_HEADER
;
968 if (!strcasecmp(var
+offset
, "updated")
969 || !strcasecmp(var
+offset
, "added"))
970 return WT_STATUS_UPDATED
;
971 if (!strcasecmp(var
+offset
, "changed"))
972 return WT_STATUS_CHANGED
;
973 if (!strcasecmp(var
+offset
, "untracked"))
974 return WT_STATUS_UNTRACKED
;
975 if (!strcasecmp(var
+offset
, "nobranch"))
976 return WT_STATUS_NOBRANCH
;
977 if (!strcasecmp(var
+offset
, "unmerged"))
978 return WT_STATUS_UNMERGED
;
982 static int git_status_config(const char *k
, const char *v
, void *cb
)
984 struct wt_status
*s
= cb
;
986 if (!strcmp(k
, "status.submodulesummary")) {
988 s
->submodule_summary
= git_config_bool_or_int(k
, v
, &is_bool
);
989 if (is_bool
&& s
->submodule_summary
)
990 s
->submodule_summary
= -1;
993 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
994 s
->use_color
= git_config_colorbool(k
, v
, -1);
997 if (!prefixcmp(k
, "status.color.") || !prefixcmp(k
, "color.status.")) {
998 int slot
= parse_status_slot(k
, 13);
1002 return config_error_nonbool(k
);
1003 color_parse(v
, k
, s
->color_palette
[slot
]);
1006 if (!strcmp(k
, "status.relativepaths")) {
1007 s
->relative_paths
= git_config_bool(k
, v
);
1010 if (!strcmp(k
, "status.showuntrackedfiles")) {
1012 return config_error_nonbool(k
);
1013 else if (!strcmp(v
, "no"))
1014 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
1015 else if (!strcmp(v
, "normal"))
1016 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
1017 else if (!strcmp(v
, "all"))
1018 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
1020 return error("Invalid untracked files mode '%s'", v
);
1023 return git_diff_ui_config(k
, v
, NULL
);
1026 int cmd_status(int argc
, const char **argv
, const char *prefix
)
1030 unsigned char sha1
[20];
1031 static struct option builtin_status_options
[] = {
1032 OPT__VERBOSE(&verbose
),
1033 OPT_SET_INT('s', "short", &status_format
,
1034 "show status concisely", STATUS_FORMAT_SHORT
),
1035 OPT_BOOLEAN('b', "branch", &status_show_branch
,
1036 "show branch information"),
1037 OPT_SET_INT(0, "porcelain", &status_format
,
1038 "show porcelain output format",
1039 STATUS_FORMAT_PORCELAIN
),
1040 OPT_BOOLEAN('z', "null", &null_termination
,
1041 "terminate entries with NUL"),
1042 { OPTION_STRING
, 'u', "untracked-files", &untracked_files_arg
,
1044 "show untracked files, optional modes: all, normal, no. (Default: all)",
1045 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1046 OPT_BOOLEAN(0, "ignored", &show_ignored_in_status
,
1047 "show ignored files"),
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
);
1061 if (show_ignored_in_status
)
1062 s
.show_ignored_files
= 1;
1064 s
.pathspec
= get_pathspec(prefix
, argv
);
1066 read_cache_preload(s
.pathspec
);
1067 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, s
.pathspec
, NULL
, NULL
);
1069 fd
= hold_locked_index(&index_lock
, 0);
1071 if (!write_cache(fd
, active_cache
, active_nr
))
1072 commit_locked_index(&index_lock
);
1073 rollback_lock_file(&index_lock
);
1076 s
.is_initial
= get_sha1(s
.reference
, sha1
) ? 1 : 0;
1077 s
.in_merge
= in_merge
;
1078 wt_status_collect(&s
);
1080 if (s
.relative_paths
)
1082 if (s
.use_color
== -1)
1083 s
.use_color
= git_use_color_default
;
1084 if (diff_use_color_default
== -1)
1085 diff_use_color_default
= git_use_color_default
;
1087 switch (status_format
) {
1088 case STATUS_FORMAT_SHORT
:
1089 wt_shortstatus_print(&s
, null_termination
, status_show_branch
);
1091 case STATUS_FORMAT_PORCELAIN
:
1092 wt_porcelain_print(&s
, null_termination
);
1094 case STATUS_FORMAT_LONG
:
1095 s
.verbose
= verbose
;
1096 wt_status_print(&s
);
1102 static void print_summary(const char *prefix
, const unsigned char *sha1
)
1104 struct rev_info rev
;
1105 struct commit
*commit
;
1106 struct strbuf format
= STRBUF_INIT
;
1107 unsigned char junk_sha1
[20];
1108 const char *head
= resolve_ref("HEAD", junk_sha1
, 0, NULL
);
1109 struct pretty_print_context pctx
= {0};
1110 struct strbuf author_ident
= STRBUF_INIT
;
1111 struct strbuf committer_ident
= STRBUF_INIT
;
1113 commit
= lookup_commit(sha1
);
1115 die("couldn't look up newly created commit");
1116 if (!commit
|| parse_commit(commit
))
1117 die("could not parse newly created commit");
1119 strbuf_addstr(&format
, "format:%h] %s");
1121 format_commit_message(commit
, "%an <%ae>", &author_ident
, &pctx
);
1122 format_commit_message(commit
, "%cn <%ce>", &committer_ident
, &pctx
);
1123 if (strbuf_cmp(&author_ident
, &committer_ident
)) {
1124 strbuf_addstr(&format
, "\n Author: ");
1125 strbuf_addbuf_percentquote(&format
, &author_ident
);
1127 if (!user_ident_sufficiently_given()) {
1128 strbuf_addstr(&format
, "\n Committer: ");
1129 strbuf_addbuf_percentquote(&format
, &committer_ident
);
1130 if (advice_implicit_identity
) {
1131 strbuf_addch(&format
, '\n');
1132 strbuf_addstr(&format
, implicit_ident_advice
);
1135 strbuf_release(&author_ident
);
1136 strbuf_release(&committer_ident
);
1138 init_revisions(&rev
, prefix
);
1139 setup_revisions(0, NULL
, &rev
, NULL
);
1143 rev
.diffopt
.output_format
=
1144 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
1146 rev
.verbose_header
= 1;
1147 rev
.show_root_diff
= 1;
1148 get_commit_format(format
.buf
, &rev
);
1149 rev
.always_show_header
= 0;
1150 rev
.diffopt
.detect_rename
= 1;
1151 rev
.diffopt
.rename_limit
= 100;
1152 rev
.diffopt
.break_opt
= 0;
1153 diff_setup_done(&rev
.diffopt
);
1156 !prefixcmp(head
, "refs/heads/") ?
1158 !strcmp(head
, "HEAD") ?
1161 initial_commit
? " (root-commit)" : "");
1163 if (!log_tree_commit(&rev
, commit
)) {
1164 struct pretty_print_context ctx
= {0};
1165 struct strbuf buf
= STRBUF_INIT
;
1166 ctx
.date_mode
= DATE_NORMAL
;
1167 format_commit_message(commit
, format
.buf
+ 7, &buf
, &ctx
);
1168 printf("%s\n", buf
.buf
);
1169 strbuf_release(&buf
);
1171 strbuf_release(&format
);
1174 static int git_commit_config(const char *k
, const char *v
, void *cb
)
1176 struct wt_status
*s
= cb
;
1178 if (!strcmp(k
, "commit.template"))
1179 return git_config_pathname(&template_file
, k
, v
);
1180 if (!strcmp(k
, "commit.status")) {
1181 include_status
= git_config_bool(k
, v
);
1185 return git_status_config(k
, v
, s
);
1188 static const char post_rewrite_hook
[] = "hooks/post-rewrite";
1190 static int run_rewrite_hook(const unsigned char *oldsha1
,
1191 const unsigned char *newsha1
)
1193 /* oldsha1 SP newsha1 LF NUL */
1194 static char buf
[2*40 + 3];
1195 struct child_process proc
;
1196 const char *argv
[3];
1200 if (access(git_path(post_rewrite_hook
), X_OK
) < 0)
1203 argv
[0] = git_path(post_rewrite_hook
);
1207 memset(&proc
, 0, sizeof(proc
));
1210 proc
.stdout_to_stderr
= 1;
1212 code
= start_command(&proc
);
1215 n
= snprintf(buf
, sizeof(buf
), "%s %s\n",
1216 sha1_to_hex(oldsha1
), sha1_to_hex(newsha1
));
1217 write_in_full(proc
.in
, buf
, n
);
1219 return finish_command(&proc
);
1222 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
1224 struct strbuf sb
= STRBUF_INIT
;
1225 const char *index_file
, *reflog_msg
;
1227 unsigned char commit_sha1
[20];
1228 struct ref_lock
*ref_lock
;
1229 struct commit_list
*parents
= NULL
, **pptr
= &parents
;
1230 struct stat statbuf
;
1231 int allow_fast_forward
= 1;
1234 wt_status_prepare(&s
);
1235 git_config(git_commit_config
, &s
);
1236 in_merge
= file_exists(git_path("MERGE_HEAD"));
1237 s
.in_merge
= in_merge
;
1239 if (s
.use_color
== -1)
1240 s
.use_color
= git_use_color_default
;
1241 argc
= parse_and_validate_options(argc
, argv
, builtin_commit_usage
,
1244 if (diff_use_color_default
== -1)
1245 diff_use_color_default
= git_use_color_default
;
1246 return dry_run_commit(argc
, argv
, prefix
, &s
);
1248 index_file
= prepare_index(argc
, argv
, prefix
, 0);
1250 /* Set up everything for writing the commit object. This includes
1251 running hooks, writing the trees, and interacting with the user. */
1252 if (!prepare_to_commit(index_file
, prefix
, &s
)) {
1253 rollback_index_files();
1257 /* Determine parents */
1258 if (initial_commit
) {
1259 reflog_msg
= "commit (initial)";
1261 struct commit_list
*c
;
1262 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
;
1275 reflog_msg
= "commit (merge)";
1276 pptr
= &commit_list_insert(lookup_commit(head_sha1
), pptr
)->next
;
1277 fp
= fopen(git_path("MERGE_HEAD"), "r");
1279 die_errno("could not open '%s' for reading",
1280 git_path("MERGE_HEAD"));
1281 while (strbuf_getline(&m
, fp
, '\n') != EOF
) {
1282 unsigned char sha1
[20];
1283 if (get_sha1_hex(m
.buf
, sha1
) < 0)
1284 die("Corrupt MERGE_HEAD file (%s)", m
.buf
);
1285 pptr
= &commit_list_insert(lookup_commit(sha1
), pptr
)->next
;
1289 if (!stat(git_path("MERGE_MODE"), &statbuf
)) {
1290 if (strbuf_read_file(&sb
, git_path("MERGE_MODE"), 0) < 0)
1291 die_errno("could not read MERGE_MODE");
1292 if (!strcmp(sb
.buf
, "no-ff"))
1293 allow_fast_forward
= 0;
1295 if (allow_fast_forward
)
1296 parents
= reduce_heads(parents
);
1298 reflog_msg
= "commit";
1299 pptr
= &commit_list_insert(lookup_commit(head_sha1
), pptr
)->next
;
1302 /* Finally, get the commit message */
1304 if (strbuf_read_file(&sb
, git_path(commit_editmsg
), 0) < 0) {
1305 int saved_errno
= errno
;
1306 rollback_index_files();
1307 die("could not read commit message: %s", strerror(saved_errno
));
1310 /* Truncate the message just before the diff, if any. */
1312 p
= strstr(sb
.buf
, "\ndiff --git ");
1314 strbuf_setlen(&sb
, p
- sb
.buf
+ 1);
1317 if (cleanup_mode
!= CLEANUP_NONE
)
1318 stripspace(&sb
, cleanup_mode
== CLEANUP_ALL
);
1319 if (message_is_empty(&sb
) && !allow_empty_message
) {
1320 rollback_index_files();
1321 fprintf(stderr
, "Aborting commit due to empty commit message.\n");
1325 if (commit_tree(sb
.buf
, active_cache_tree
->sha1
, parents
, commit_sha1
,
1326 fmt_ident(author_name
, author_email
, author_date
,
1327 IDENT_ERROR_ON_NO_NAME
))) {
1328 rollback_index_files();
1329 die("failed to write commit object");
1332 ref_lock
= lock_any_ref_for_update("HEAD",
1333 initial_commit
? NULL
: head_sha1
,
1336 nl
= strchr(sb
.buf
, '\n');
1338 strbuf_setlen(&sb
, nl
+ 1 - sb
.buf
);
1340 strbuf_addch(&sb
, '\n');
1341 strbuf_insert(&sb
, 0, reflog_msg
, strlen(reflog_msg
));
1342 strbuf_insert(&sb
, strlen(reflog_msg
), ": ", 2);
1345 rollback_index_files();
1346 die("cannot lock HEAD ref");
1348 if (write_ref_sha1(ref_lock
, commit_sha1
, sb
.buf
) < 0) {
1349 rollback_index_files();
1350 die("cannot update HEAD ref");
1353 unlink(git_path("MERGE_HEAD"));
1354 unlink(git_path("MERGE_MSG"));
1355 unlink(git_path("MERGE_MODE"));
1356 unlink(git_path("SQUASH_MSG"));
1358 if (commit_index_files())
1359 die ("Repository has been updated, but unable to write\n"
1360 "new_index file. Check that disk is not full or quota is\n"
1361 "not exceeded, and then \"git reset HEAD\" to recover.");
1364 run_hook(get_index_file(), "post-commit", NULL
);
1365 if (amend
&& !no_post_rewrite
) {
1366 struct notes_rewrite_cfg
*cfg
;
1367 cfg
= init_copy_notes_for_rewrite("amend");
1369 copy_note_for_rewrite(cfg
, head_sha1
, commit_sha1
);
1370 finish_copy_notes_for_rewrite(cfg
);
1372 run_rewrite_hook(head_sha1
, commit_sha1
);
1375 print_summary(prefix
, commit_sha1
);