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 unsigned char head_sha1
[20];
40 static char *use_message_buffer
;
41 static const char commit_editmsg
[] = "COMMIT_EDITMSG";
42 static struct lock_file index_lock
; /* real index */
43 static struct lock_file false_lock
; /* used only for partial commits */
50 static const char *logfile
, *force_author
;
51 static const char *template_file
;
52 static char *edit_message
, *use_message
;
53 static char *author_name
, *author_email
, *author_date
;
54 static int all
, edit_flag
, also
, interactive
, only
, amend
, signoff
;
55 static int quiet
, verbose
, no_verify
, allow_empty
, dry_run
, renew_authorship
;
56 static char *untracked_files_arg
, *force_date
;
58 * The default commit message cleanup mode will remove the lines
59 * beginning with # (shell comments) and leading and trailing
60 * whitespaces (empty lines or containing only whitespaces)
61 * if editor is used, and only the whitespaces if the message
62 * is specified explicitly.
69 static char *cleanup_arg
;
71 static int use_editor
= 1, initial_commit
, in_merge
;
72 static const char *only_include_assumed
;
73 static struct strbuf message
;
75 static int null_termination
;
79 STATUS_FORMAT_PORCELAIN
,
80 } status_format
= STATUS_FORMAT_LONG
;
82 static int opt_parse_m(const struct option
*opt
, const char *arg
, int unset
)
84 struct strbuf
*buf
= opt
->value
;
86 strbuf_setlen(buf
, 0);
88 strbuf_addstr(buf
, arg
);
89 strbuf_addstr(buf
, "\n\n");
94 static struct option builtin_commit_options
[] = {
96 OPT__VERBOSE(&verbose
),
98 OPT_GROUP("Commit message options"),
99 OPT_FILENAME('F', "file", &logfile
, "read log from file"),
100 OPT_STRING(0, "author", &force_author
, "AUTHOR", "override author for commit"),
101 OPT_STRING(0, "date", &force_date
, "DATE", "override date for commit"),
102 OPT_CALLBACK('m', "message", &message
, "MESSAGE", "specify commit message", opt_parse_m
),
103 OPT_STRING('c', "reedit-message", &edit_message
, "COMMIT", "reuse and edit message from specified commit"),
104 OPT_STRING('C', "reuse-message", &use_message
, "COMMIT", "reuse message from specified commit"),
105 OPT_BOOLEAN(0, "reset-author", &renew_authorship
, "the commit is authored by me now (used with -C-c/--amend)"),
106 OPT_BOOLEAN('s', "signoff", &signoff
, "add Signed-off-by:"),
107 OPT_FILENAME('t', "template", &template_file
, "use specified template file"),
108 OPT_BOOLEAN('e', "edit", &edit_flag
, "force edit of commit"),
109 OPT_STRING(0, "cleanup", &cleanup_arg
, "default", "how to strip spaces and #comments from message"),
110 /* end commit message options */
112 OPT_GROUP("Commit contents options"),
113 OPT_BOOLEAN('a', "all", &all
, "commit all changed files"),
114 OPT_BOOLEAN('i', "include", &also
, "add specified files to index for commit"),
115 OPT_BOOLEAN(0, "interactive", &interactive
, "interactively add files"),
116 OPT_BOOLEAN('o', "only", &only
, "commit only specified files"),
117 OPT_BOOLEAN('n', "no-verify", &no_verify
, "bypass pre-commit hook"),
118 OPT_BOOLEAN(0, "dry-run", &dry_run
, "show what would be committed"),
119 OPT_SET_INT(0, "short", &status_format
, "show status concisely",
120 STATUS_FORMAT_SHORT
),
121 OPT_SET_INT(0, "porcelain", &status_format
,
122 "show porcelain output format", STATUS_FORMAT_PORCELAIN
),
123 OPT_BOOLEAN('z', "null", &null_termination
,
124 "terminate entries with NUL"),
125 OPT_BOOLEAN(0, "amend", &amend
, "amend previous commit"),
126 { 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" },
127 OPT_BOOLEAN(0, "allow-empty", &allow_empty
, "ok to record an empty change"),
128 /* end commit contents options */
133 static void rollback_index_files(void)
135 switch (commit_style
) {
137 break; /* nothing to do */
139 rollback_lock_file(&index_lock
);
142 rollback_lock_file(&index_lock
);
143 rollback_lock_file(&false_lock
);
148 static int commit_index_files(void)
152 switch (commit_style
) {
154 break; /* nothing to do */
156 err
= commit_lock_file(&index_lock
);
159 err
= commit_lock_file(&index_lock
);
160 rollback_lock_file(&false_lock
);
168 * Take a union of paths in the index and the named tree (typically, "HEAD"),
169 * and return the paths that match the given pattern in list.
171 static int list_paths(struct string_list
*list
, const char *with_tree
,
172 const char *prefix
, const char **pattern
)
177 for (i
= 0; pattern
[i
]; i
++)
182 overlay_tree_on_cache(with_tree
, prefix
);
184 for (i
= 0; i
< active_nr
; i
++) {
185 struct cache_entry
*ce
= active_cache
[i
];
186 if (ce
->ce_flags
& CE_UPDATE
)
188 if (!match_pathspec(pattern
, ce
->name
, ce_namelen(ce
), 0, m
))
190 string_list_insert(ce
->name
, list
);
193 return report_path_error(m
, pattern
, prefix
? strlen(prefix
) : 0);
196 static void add_remove_files(struct string_list
*list
)
199 for (i
= 0; i
< list
->nr
; i
++) {
201 struct string_list_item
*p
= &(list
->items
[i
]);
203 if (!lstat(p
->string
, &st
)) {
204 if (add_to_cache(p
->string
, &st
, 0))
205 die("updating files failed");
207 remove_file_from_cache(p
->string
);
211 static void create_base_index(void)
214 struct unpack_trees_options opts
;
217 if (initial_commit
) {
222 memset(&opts
, 0, sizeof(opts
));
226 opts
.src_index
= &the_index
;
227 opts
.dst_index
= &the_index
;
229 opts
.fn
= oneway_merge
;
230 tree
= parse_tree_indirect(head_sha1
);
232 die("failed to unpack HEAD tree object");
234 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
235 if (unpack_trees(1, &t
, &opts
))
236 exit(128); /* We've already reported the error, finish dying */
239 static char *prepare_index(int argc
, const char **argv
, const char *prefix
, int is_status
)
242 struct string_list partial
;
243 const char **pathspec
= NULL
;
244 int refresh_flags
= REFRESH_QUIET
;
247 refresh_flags
|= REFRESH_UNMERGED
;
249 if (interactive_add(argc
, argv
, prefix
) != 0)
250 die("interactive add failed");
251 if (read_cache_preload(NULL
) < 0)
252 die("index file corrupt");
253 commit_style
= COMMIT_AS_IS
;
254 return get_index_file();
258 pathspec
= get_pathspec(prefix
, argv
);
260 if (read_cache_preload(pathspec
) < 0)
261 die("index file corrupt");
264 * Non partial, non as-is commit.
266 * (1) get the real index;
267 * (2) update the_index as necessary;
268 * (3) write the_index out to the real index (still locked);
269 * (4) return the name of the locked index file.
271 * The caller should run hooks on the locked real index, and
272 * (A) if all goes well, commit the real index;
273 * (B) on failure, rollback the real index.
275 if (all
|| (also
&& pathspec
&& *pathspec
)) {
276 int fd
= hold_locked_index(&index_lock
, 1);
277 add_files_to_cache(also
? prefix
: NULL
, pathspec
, 0);
278 refresh_cache(refresh_flags
);
279 if (write_cache(fd
, active_cache
, active_nr
) ||
280 close_lock_file(&index_lock
))
281 die("unable to write new_index file");
282 commit_style
= COMMIT_NORMAL
;
283 return index_lock
.filename
;
289 * (1) return the name of the real index file.
291 * The caller should run hooks on the real index, and run
292 * hooks on the real index, and create commit from the_index.
293 * We still need to refresh the index here.
295 if (!pathspec
|| !*pathspec
) {
296 fd
= hold_locked_index(&index_lock
, 1);
297 refresh_cache(refresh_flags
);
298 if (write_cache(fd
, active_cache
, active_nr
) ||
299 commit_locked_index(&index_lock
))
300 die("unable to write new_index file");
301 commit_style
= COMMIT_AS_IS
;
302 return get_index_file();
308 * (0) find the set of affected paths;
309 * (1) get lock on the real index file;
310 * (2) update the_index with the given paths;
311 * (3) write the_index out to the real index (still locked);
312 * (4) get lock on the false index file;
313 * (5) reset the_index from HEAD;
314 * (6) update the_index the same way as (2);
315 * (7) write the_index out to the false index file;
316 * (8) return the name of the false index file (still locked);
318 * The caller should run hooks on the locked false index, and
319 * create commit from it. Then
320 * (A) if all goes well, commit the real index;
321 * (B) on failure, rollback the real index;
322 * In either case, rollback the false index.
324 commit_style
= COMMIT_PARTIAL
;
327 die("cannot do a partial commit during a merge.");
329 memset(&partial
, 0, sizeof(partial
));
330 partial
.strdup_strings
= 1;
331 if (list_paths(&partial
, initial_commit
? NULL
: "HEAD", prefix
, pathspec
))
335 if (read_cache() < 0)
336 die("cannot read the index");
338 fd
= hold_locked_index(&index_lock
, 1);
339 add_remove_files(&partial
);
340 refresh_cache(REFRESH_QUIET
);
341 if (write_cache(fd
, active_cache
, active_nr
) ||
342 close_lock_file(&index_lock
))
343 die("unable to write new_index file");
345 fd
= hold_lock_file_for_update(&false_lock
,
346 git_path("next-index-%"PRIuMAX
,
347 (uintmax_t) getpid()),
351 add_remove_files(&partial
);
352 refresh_cache(REFRESH_QUIET
);
354 if (write_cache(fd
, active_cache
, active_nr
) ||
355 close_lock_file(&false_lock
))
356 die("unable to write temporary index file");
359 read_cache_from(false_lock
.filename
);
361 return false_lock
.filename
;
364 static int run_status(FILE *fp
, const char *index_file
, const char *prefix
, int nowarn
,
367 unsigned char sha1
[20];
369 if (s
->relative_paths
)
374 s
->reference
= "HEAD^1";
376 s
->verbose
= verbose
;
377 s
->index_file
= index_file
;
380 s
->is_initial
= get_sha1(s
->reference
, sha1
) ? 1 : 0;
382 wt_status_collect(s
);
384 switch (status_format
) {
385 case STATUS_FORMAT_SHORT
:
386 wt_shortstatus_print(s
, null_termination
);
388 case STATUS_FORMAT_PORCELAIN
:
389 wt_porcelain_print(s
, null_termination
);
391 case STATUS_FORMAT_LONG
:
396 return s
->commitable
;
399 static int is_a_merge(const unsigned char *sha1
)
401 struct commit
*commit
= lookup_commit(sha1
);
402 if (!commit
|| parse_commit(commit
))
403 die("could not parse HEAD commit");
404 return !!(commit
->parents
&& commit
->parents
->next
);
407 static const char sign_off_header
[] = "Signed-off-by: ";
409 static void determine_author_info(void)
411 char *name
, *email
, *date
;
413 name
= getenv("GIT_AUTHOR_NAME");
414 email
= getenv("GIT_AUTHOR_EMAIL");
415 date
= getenv("GIT_AUTHOR_DATE");
417 if (use_message
&& !renew_authorship
) {
418 const char *a
, *lb
, *rb
, *eol
;
420 a
= strstr(use_message_buffer
, "\nauthor ");
422 die("invalid commit: %s", use_message
);
424 lb
= strstr(a
+ 8, " <");
425 rb
= strstr(a
+ 8, "> ");
426 eol
= strchr(a
+ 8, '\n');
427 if (!lb
|| !rb
|| !eol
)
428 die("invalid commit: %s", use_message
);
430 name
= xstrndup(a
+ 8, lb
- (a
+ 8));
431 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
432 date
= xstrndup(rb
+ 2, eol
- (rb
+ 2));
436 const char *lb
= strstr(force_author
, " <");
437 const char *rb
= strchr(force_author
, '>');
440 die("malformed --author parameter");
441 name
= xstrndup(force_author
, lb
- force_author
);
442 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
449 author_email
= email
;
453 static int ends_rfc2822_footer(struct strbuf
*sb
)
460 const char *buf
= sb
->buf
;
462 for (i
= len
- 1; i
> 0; i
--) {
463 if (hit
&& buf
[i
] == '\n')
465 hit
= (buf
[i
] == '\n');
468 while (i
< len
- 1 && buf
[i
] == '\n')
471 for (; i
< len
; i
= k
) {
472 for (k
= i
; k
< len
&& buf
[k
] != '\n'; k
++)
476 if ((buf
[k
] == ' ' || buf
[k
] == '\t') && !first
)
481 for (j
= 0; i
+ j
< len
; j
++) {
494 static int prepare_to_commit(const char *index_file
, const char *prefix
,
498 int commitable
, saved_color_setting
;
499 struct strbuf sb
= STRBUF_INIT
;
502 const char *hook_arg1
= NULL
;
503 const char *hook_arg2
= NULL
;
506 if (!no_verify
&& run_hook(index_file
, "pre-commit", NULL
))
510 strbuf_addbuf(&sb
, &message
);
511 hook_arg1
= "message";
512 } else if (logfile
&& !strcmp(logfile
, "-")) {
514 fprintf(stderr
, "(reading log message from standard input)\n");
515 if (strbuf_read(&sb
, 0, 0) < 0)
516 die_errno("could not read log from standard input");
517 hook_arg1
= "message";
518 } else if (logfile
) {
519 if (strbuf_read_file(&sb
, logfile
, 0) < 0)
520 die_errno("could not read log file '%s'",
522 hook_arg1
= "message";
523 } else if (use_message
) {
524 buffer
= strstr(use_message_buffer
, "\n\n");
525 if (!buffer
|| buffer
[2] == '\0')
526 die("commit has empty message");
527 strbuf_add(&sb
, buffer
+ 2, strlen(buffer
+ 2));
528 hook_arg1
= "commit";
529 hook_arg2
= use_message
;
530 } else if (!stat(git_path("MERGE_MSG"), &statbuf
)) {
531 if (strbuf_read_file(&sb
, git_path("MERGE_MSG"), 0) < 0)
532 die_errno("could not read MERGE_MSG");
534 } else if (!stat(git_path("SQUASH_MSG"), &statbuf
)) {
535 if (strbuf_read_file(&sb
, git_path("SQUASH_MSG"), 0) < 0)
536 die_errno("could not read SQUASH_MSG");
537 hook_arg1
= "squash";
538 } else if (template_file
&& !stat(template_file
, &statbuf
)) {
539 if (strbuf_read_file(&sb
, template_file
, 0) < 0)
540 die_errno("could not read '%s'", template_file
);
541 hook_arg1
= "template";
545 * This final case does not modify the template message,
546 * it just sets the argument to the prepare-commit-msg hook.
551 fp
= fopen(git_path(commit_editmsg
), "w");
553 die_errno("could not open '%s'", git_path(commit_editmsg
));
555 if (cleanup_mode
!= CLEANUP_NONE
)
559 struct strbuf sob
= STRBUF_INIT
;
562 strbuf_addstr(&sob
, sign_off_header
);
563 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
564 getenv("GIT_COMMITTER_EMAIL")));
565 strbuf_addch(&sob
, '\n');
566 for (i
= sb
.len
- 1; i
> 0 && sb
.buf
[i
- 1] != '\n'; i
--)
568 if (prefixcmp(sb
.buf
+ i
, sob
.buf
)) {
569 if (!i
|| !ends_rfc2822_footer(&sb
))
570 strbuf_addch(&sb
, '\n');
571 strbuf_addbuf(&sb
, &sob
);
573 strbuf_release(&sob
);
576 if (fwrite(sb
.buf
, 1, sb
.len
, fp
) < sb
.len
)
577 die_errno("could not write commit template");
581 determine_author_info();
583 /* This checks if committer ident is explicitly given */
584 git_committer_info(0);
587 const char *committer_ident
;
592 "# It looks like you may be committing a MERGE.\n"
593 "# If this is not correct, please remove the file\n"
597 git_path("MERGE_HEAD"));
601 "# Please enter the commit message for your changes.");
602 if (cleanup_mode
== CLEANUP_ALL
)
605 "# with '#' will be ignored, and an empty"
606 " message aborts the commit.\n");
607 else /* CLEANUP_SPACE, that is. */
610 "# with '#' will be kept; you may remove them"
611 " yourself if you want to.\n"
612 "# An empty message aborts the commit.\n");
613 if (only_include_assumed
)
614 fprintf(fp
, "# %s\n", only_include_assumed
);
616 author_ident
= xstrdup(fmt_name(author_name
, author_email
));
617 committer_ident
= fmt_name(getenv("GIT_COMMITTER_NAME"),
618 getenv("GIT_COMMITTER_EMAIL"));
619 if (strcmp(author_ident
, committer_ident
))
623 ident_shown
++ ? "" : "#\n",
627 if (user_ident_explicitly_given
!= IDENT_ALL_GIVEN
)
631 ident_shown
++ ? "" : "#\n",
637 saved_color_setting
= s
->use_color
;
639 commitable
= run_status(fp
, index_file
, prefix
, 1, s
);
640 s
->use_color
= saved_color_setting
;
642 unsigned char sha1
[20];
643 const char *parent
= "HEAD";
645 if (!active_nr
&& read_cache() < 0)
646 die("Cannot read index");
651 if (get_sha1(parent
, sha1
))
652 commitable
= !!active_nr
;
654 commitable
= index_differs_from(parent
, 0);
659 if (!commitable
&& !in_merge
&& !allow_empty
&&
660 !(amend
&& is_a_merge(head_sha1
))) {
661 run_status(stdout
, index_file
, prefix
, 0, s
);
666 * Re-read the index as pre-commit hook could have updated it,
667 * and write it out as a tree. We must do this before we invoke
668 * the editor and after we invoke run_status above.
671 read_cache_from(index_file
);
672 if (!active_cache_tree
)
673 active_cache_tree
= cache_tree();
674 if (cache_tree_update(active_cache_tree
,
675 active_cache
, active_nr
, 0, 0) < 0) {
676 error("Error building trees");
680 if (run_hook(index_file
, "prepare-commit-msg",
681 git_path(commit_editmsg
), hook_arg1
, hook_arg2
, NULL
))
685 char index
[PATH_MAX
];
686 const char *env
[2] = { index
, NULL
};
687 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
688 if (launch_editor(git_path(commit_editmsg
), NULL
, env
)) {
690 "Please supply the message using either -m or -F option.\n");
696 run_hook(index_file
, "commit-msg", git_path(commit_editmsg
), NULL
)) {
704 * Find out if the message in the strbuf contains only whitespace and
705 * Signed-off-by lines.
707 static int message_is_empty(struct strbuf
*sb
)
709 struct strbuf tmpl
= STRBUF_INIT
;
711 int eol
, i
, start
= 0;
713 if (cleanup_mode
== CLEANUP_NONE
&& sb
->len
)
716 /* See if the template is just a prefix of the message. */
717 if (template_file
&& strbuf_read_file(&tmpl
, template_file
, 0) > 0) {
718 stripspace(&tmpl
, cleanup_mode
== CLEANUP_ALL
);
719 if (start
+ tmpl
.len
<= sb
->len
&&
720 memcmp(tmpl
.buf
, sb
->buf
+ start
, tmpl
.len
) == 0)
723 strbuf_release(&tmpl
);
725 /* Check if the rest is just whitespace and Signed-of-by's. */
726 for (i
= start
; i
< sb
->len
; i
++) {
727 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
733 if (strlen(sign_off_header
) <= eol
- i
&&
734 !prefixcmp(sb
->buf
+ i
, sign_off_header
)) {
739 if (!isspace(sb
->buf
[i
++]))
746 static const char *find_author_by_nickname(const char *name
)
748 struct rev_info revs
;
749 struct commit
*commit
;
750 struct strbuf buf
= STRBUF_INIT
;
754 init_revisions(&revs
, NULL
);
755 strbuf_addf(&buf
, "--author=%s", name
);
760 setup_revisions(ac
, av
, &revs
, NULL
);
761 prepare_revision_walk(&revs
);
762 commit
= get_revision(&revs
);
764 struct pretty_print_context ctx
= {0};
765 ctx
.date_mode
= DATE_NORMAL
;
766 strbuf_release(&buf
);
767 format_commit_message(commit
, "%an <%ae>", &buf
, &ctx
);
768 return strbuf_detach(&buf
, NULL
);
770 die("No existing author found with '%s'", name
);
774 static void handle_untracked_files_arg(struct wt_status
*s
)
776 if (!untracked_files_arg
)
777 ; /* default already initialized */
778 else if (!strcmp(untracked_files_arg
, "no"))
779 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
780 else if (!strcmp(untracked_files_arg
, "normal"))
781 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
782 else if (!strcmp(untracked_files_arg
, "all"))
783 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
785 die("Invalid untracked files mode '%s'", untracked_files_arg
);
788 static int parse_and_validate_options(int argc
, const char *argv
[],
789 const char * const usage
[],
795 argc
= parse_options(argc
, argv
, prefix
, builtin_commit_options
, usage
,
798 if (force_author
&& !strchr(force_author
, '>'))
799 force_author
= find_author_by_nickname(force_author
);
801 if (force_author
&& renew_authorship
)
802 die("Using both --reset-author and --author does not make sense");
804 if (logfile
|| message
.len
|| use_message
)
809 setenv("GIT_EDITOR", ":", 1);
811 if (get_sha1("HEAD", head_sha1
))
814 /* Sanity check options */
815 if (amend
&& initial_commit
)
816 die("You have nothing to amend.");
817 if (amend
&& in_merge
)
818 die("You are in the middle of a merge -- cannot amend.");
827 die("Only one of -c/-C/-F can be used.");
828 if (message
.len
&& f
> 0)
829 die("Option -m cannot be combined with -c/-C/-F.");
831 use_message
= edit_message
;
832 if (amend
&& !use_message
)
833 use_message
= "HEAD";
834 if (!use_message
&& renew_authorship
)
835 die("--reset-author can be used only with -C, -c or --amend.");
837 unsigned char sha1
[20];
838 static char utf8
[] = "UTF-8";
841 struct commit
*commit
;
843 if (get_sha1(use_message
, sha1
))
844 die("could not lookup commit %s", use_message
);
845 commit
= lookup_commit_reference(sha1
);
846 if (!commit
|| parse_commit(commit
))
847 die("could not parse commit %s", use_message
);
849 enc
= strstr(commit
->buffer
, "\nencoding");
851 end
= strchr(enc
+ 10, '\n');
852 enc
= xstrndup(enc
+ 10, end
- (enc
+ 10));
856 out_enc
= git_commit_encoding
? git_commit_encoding
: utf8
;
858 if (strcmp(out_enc
, enc
))
860 reencode_string(commit
->buffer
, out_enc
, enc
);
863 * If we failed to reencode the buffer, just copy it
864 * byte for byte so the user can try to fix it up.
865 * This also handles the case where input and output
866 * encodings are identical.
868 if (use_message_buffer
== NULL
)
869 use_message_buffer
= xstrdup(commit
->buffer
);
874 if (!!also
+ !!only
+ !!all
+ !!interactive
> 1)
875 die("Only one of --include/--only/--all/--interactive can be used.");
876 if (argc
== 0 && (also
|| (only
&& !amend
)))
877 die("No paths with --include/--only does not make sense.");
878 if (argc
== 0 && only
&& amend
)
879 only_include_assumed
= "Clever... amending the last one with dirty index.";
880 if (argc
> 0 && !also
&& !only
)
881 only_include_assumed
= "Explicit paths specified without -i nor -o; assuming --only paths...";
882 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "default"))
883 cleanup_mode
= use_editor
? CLEANUP_ALL
: CLEANUP_SPACE
;
884 else if (!strcmp(cleanup_arg
, "verbatim"))
885 cleanup_mode
= CLEANUP_NONE
;
886 else if (!strcmp(cleanup_arg
, "whitespace"))
887 cleanup_mode
= CLEANUP_SPACE
;
888 else if (!strcmp(cleanup_arg
, "strip"))
889 cleanup_mode
= CLEANUP_ALL
;
891 die("Invalid cleanup mode %s", cleanup_arg
);
893 handle_untracked_files_arg(s
);
896 die("Paths with -a does not make sense.");
897 else if (interactive
&& argc
> 0)
898 die("Paths with --interactive does not make sense.");
900 if (null_termination
&& status_format
== STATUS_FORMAT_LONG
)
901 status_format
= STATUS_FORMAT_PORCELAIN
;
902 if (status_format
!= STATUS_FORMAT_LONG
)
908 static int dry_run_commit(int argc
, const char **argv
, const char *prefix
,
912 const char *index_file
;
914 index_file
= prepare_index(argc
, argv
, prefix
, 1);
915 commitable
= run_status(stdout
, index_file
, prefix
, 0, s
);
916 rollback_index_files();
918 return commitable
? 0 : 1;
921 static int parse_status_slot(const char *var
, int offset
)
923 if (!strcasecmp(var
+offset
, "header"))
924 return WT_STATUS_HEADER
;
925 if (!strcasecmp(var
+offset
, "updated")
926 || !strcasecmp(var
+offset
, "added"))
927 return WT_STATUS_UPDATED
;
928 if (!strcasecmp(var
+offset
, "changed"))
929 return WT_STATUS_CHANGED
;
930 if (!strcasecmp(var
+offset
, "untracked"))
931 return WT_STATUS_UNTRACKED
;
932 if (!strcasecmp(var
+offset
, "nobranch"))
933 return WT_STATUS_NOBRANCH
;
934 if (!strcasecmp(var
+offset
, "unmerged"))
935 return WT_STATUS_UNMERGED
;
939 static int git_status_config(const char *k
, const char *v
, void *cb
)
941 struct wt_status
*s
= cb
;
943 if (!strcmp(k
, "status.submodulesummary")) {
945 s
->submodule_summary
= git_config_bool_or_int(k
, v
, &is_bool
);
946 if (is_bool
&& s
->submodule_summary
)
947 s
->submodule_summary
= -1;
950 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
951 s
->use_color
= git_config_colorbool(k
, v
, -1);
954 if (!prefixcmp(k
, "status.color.") || !prefixcmp(k
, "color.status.")) {
955 int slot
= parse_status_slot(k
, 13);
959 return config_error_nonbool(k
);
960 color_parse(v
, k
, s
->color_palette
[slot
]);
963 if (!strcmp(k
, "status.relativepaths")) {
964 s
->relative_paths
= git_config_bool(k
, v
);
967 if (!strcmp(k
, "status.showuntrackedfiles")) {
969 return config_error_nonbool(k
);
970 else if (!strcmp(v
, "no"))
971 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
972 else if (!strcmp(v
, "normal"))
973 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
974 else if (!strcmp(v
, "all"))
975 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
977 return error("Invalid untracked files mode '%s'", v
);
980 return git_diff_ui_config(k
, v
, NULL
);
983 int cmd_status(int argc
, const char **argv
, const char *prefix
)
986 unsigned char sha1
[20];
987 static struct option builtin_status_options
[] = {
988 OPT__VERBOSE(&verbose
),
989 OPT_SET_INT('s', "short", &status_format
,
990 "show status concisely", STATUS_FORMAT_SHORT
),
991 OPT_SET_INT(0, "porcelain", &status_format
,
992 "show porcelain output format",
993 STATUS_FORMAT_PORCELAIN
),
994 OPT_BOOLEAN('z', "null", &null_termination
,
995 "terminate entries with NUL"),
996 { OPTION_STRING
, 'u', "untracked-files", &untracked_files_arg
,
998 "show untracked files, optional modes: all, normal, no. (Default: all)",
999 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1003 if (null_termination
&& status_format
== STATUS_FORMAT_LONG
)
1004 status_format
= STATUS_FORMAT_PORCELAIN
;
1006 wt_status_prepare(&s
);
1007 git_config(git_status_config
, &s
);
1008 in_merge
= file_exists(git_path("MERGE_HEAD"));
1009 argc
= parse_options(argc
, argv
, prefix
,
1010 builtin_status_options
,
1011 builtin_status_usage
, 0);
1012 handle_untracked_files_arg(&s
);
1015 s
.pathspec
= get_pathspec(prefix
, argv
);
1018 refresh_cache(REFRESH_QUIET
|REFRESH_UNMERGED
);
1019 s
.is_initial
= get_sha1(s
.reference
, sha1
) ? 1 : 0;
1020 s
.in_merge
= in_merge
;
1021 wt_status_collect(&s
);
1023 if (s
.relative_paths
)
1025 if (s
.use_color
== -1)
1026 s
.use_color
= git_use_color_default
;
1027 if (diff_use_color_default
== -1)
1028 diff_use_color_default
= git_use_color_default
;
1030 switch (status_format
) {
1031 case STATUS_FORMAT_SHORT
:
1032 wt_shortstatus_print(&s
, null_termination
);
1034 case STATUS_FORMAT_PORCELAIN
:
1035 wt_porcelain_print(&s
, null_termination
);
1037 case STATUS_FORMAT_LONG
:
1038 s
.verbose
= verbose
;
1039 wt_status_print(&s
);
1045 static void print_summary(const char *prefix
, const unsigned char *sha1
)
1047 struct rev_info rev
;
1048 struct commit
*commit
;
1049 static const char *format
= "format:%h] %s";
1050 unsigned char junk_sha1
[20];
1051 const char *head
= resolve_ref("HEAD", junk_sha1
, 0, NULL
);
1053 commit
= lookup_commit(sha1
);
1055 die("couldn't look up newly created commit");
1056 if (!commit
|| parse_commit(commit
))
1057 die("could not parse newly created commit");
1059 init_revisions(&rev
, prefix
);
1060 setup_revisions(0, NULL
, &rev
, NULL
);
1064 rev
.diffopt
.output_format
=
1065 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
1067 rev
.verbose_header
= 1;
1068 rev
.show_root_diff
= 1;
1069 get_commit_format(format
, &rev
);
1070 rev
.always_show_header
= 0;
1071 rev
.diffopt
.detect_rename
= 1;
1072 rev
.diffopt
.rename_limit
= 100;
1073 rev
.diffopt
.break_opt
= 0;
1074 diff_setup_done(&rev
.diffopt
);
1077 !prefixcmp(head
, "refs/heads/") ?
1079 !strcmp(head
, "HEAD") ?
1082 initial_commit
? " (root-commit)" : "");
1084 if (!log_tree_commit(&rev
, commit
)) {
1085 struct pretty_print_context ctx
= {0};
1086 struct strbuf buf
= STRBUF_INIT
;
1087 ctx
.date_mode
= DATE_NORMAL
;
1088 format_commit_message(commit
, format
+ 7, &buf
, &ctx
);
1089 printf("%s\n", buf
.buf
);
1090 strbuf_release(&buf
);
1094 static int git_commit_config(const char *k
, const char *v
, void *cb
)
1096 struct wt_status
*s
= cb
;
1098 if (!strcmp(k
, "commit.template"))
1099 return git_config_pathname(&template_file
, k
, v
);
1101 return git_status_config(k
, v
, s
);
1104 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
1106 struct strbuf sb
= STRBUF_INIT
;
1107 const char *index_file
, *reflog_msg
;
1109 unsigned char commit_sha1
[20];
1110 struct ref_lock
*ref_lock
;
1111 struct commit_list
*parents
= NULL
, **pptr
= &parents
;
1112 struct stat statbuf
;
1113 int allow_fast_forward
= 1;
1116 wt_status_prepare(&s
);
1117 git_config(git_commit_config
, &s
);
1118 in_merge
= file_exists(git_path("MERGE_HEAD"));
1119 s
.in_merge
= in_merge
;
1121 if (s
.use_color
== -1)
1122 s
.use_color
= git_use_color_default
;
1123 argc
= parse_and_validate_options(argc
, argv
, builtin_commit_usage
,
1126 if (diff_use_color_default
== -1)
1127 diff_use_color_default
= git_use_color_default
;
1128 return dry_run_commit(argc
, argv
, prefix
, &s
);
1130 index_file
= prepare_index(argc
, argv
, prefix
, 0);
1132 /* Set up everything for writing the commit object. This includes
1133 running hooks, writing the trees, and interacting with the user. */
1134 if (!prepare_to_commit(index_file
, prefix
, &s
)) {
1135 rollback_index_files();
1139 /* Determine parents */
1140 if (initial_commit
) {
1141 reflog_msg
= "commit (initial)";
1143 struct commit_list
*c
;
1144 struct commit
*commit
;
1146 reflog_msg
= "commit (amend)";
1147 commit
= lookup_commit(head_sha1
);
1148 if (!commit
|| parse_commit(commit
))
1149 die("could not parse HEAD commit");
1151 for (c
= commit
->parents
; c
; c
= c
->next
)
1152 pptr
= &commit_list_insert(c
->item
, pptr
)->next
;
1153 } else if (in_merge
) {
1154 struct strbuf m
= STRBUF_INIT
;
1157 reflog_msg
= "commit (merge)";
1158 pptr
= &commit_list_insert(lookup_commit(head_sha1
), pptr
)->next
;
1159 fp
= fopen(git_path("MERGE_HEAD"), "r");
1161 die_errno("could not open '%s' for reading",
1162 git_path("MERGE_HEAD"));
1163 while (strbuf_getline(&m
, fp
, '\n') != EOF
) {
1164 unsigned char sha1
[20];
1165 if (get_sha1_hex(m
.buf
, sha1
) < 0)
1166 die("Corrupt MERGE_HEAD file (%s)", m
.buf
);
1167 pptr
= &commit_list_insert(lookup_commit(sha1
), pptr
)->next
;
1171 if (!stat(git_path("MERGE_MODE"), &statbuf
)) {
1172 if (strbuf_read_file(&sb
, git_path("MERGE_MODE"), 0) < 0)
1173 die_errno("could not read MERGE_MODE");
1174 if (!strcmp(sb
.buf
, "no-ff"))
1175 allow_fast_forward
= 0;
1177 if (allow_fast_forward
)
1178 parents
= reduce_heads(parents
);
1180 reflog_msg
= "commit";
1181 pptr
= &commit_list_insert(lookup_commit(head_sha1
), pptr
)->next
;
1184 /* Finally, get the commit message */
1186 if (strbuf_read_file(&sb
, git_path(commit_editmsg
), 0) < 0) {
1187 int saved_errno
= errno
;
1188 rollback_index_files();
1189 die("could not read commit message: %s", strerror(saved_errno
));
1192 /* Truncate the message just before the diff, if any. */
1194 p
= strstr(sb
.buf
, "\ndiff --git ");
1196 strbuf_setlen(&sb
, p
- sb
.buf
+ 1);
1199 if (cleanup_mode
!= CLEANUP_NONE
)
1200 stripspace(&sb
, cleanup_mode
== CLEANUP_ALL
);
1201 if (message_is_empty(&sb
)) {
1202 rollback_index_files();
1203 fprintf(stderr
, "Aborting commit due to empty commit message.\n");
1207 if (commit_tree(sb
.buf
, active_cache_tree
->sha1
, parents
, commit_sha1
,
1208 fmt_ident(author_name
, author_email
, author_date
,
1209 IDENT_ERROR_ON_NO_NAME
))) {
1210 rollback_index_files();
1211 die("failed to write commit object");
1214 ref_lock
= lock_any_ref_for_update("HEAD",
1215 initial_commit
? NULL
: head_sha1
,
1218 nl
= strchr(sb
.buf
, '\n');
1220 strbuf_setlen(&sb
, nl
+ 1 - sb
.buf
);
1222 strbuf_addch(&sb
, '\n');
1223 strbuf_insert(&sb
, 0, reflog_msg
, strlen(reflog_msg
));
1224 strbuf_insert(&sb
, strlen(reflog_msg
), ": ", 2);
1227 rollback_index_files();
1228 die("cannot lock HEAD ref");
1230 if (write_ref_sha1(ref_lock
, commit_sha1
, sb
.buf
) < 0) {
1231 rollback_index_files();
1232 die("cannot update HEAD ref");
1235 unlink(git_path("MERGE_HEAD"));
1236 unlink(git_path("MERGE_MSG"));
1237 unlink(git_path("MERGE_MODE"));
1238 unlink(git_path("SQUASH_MSG"));
1240 if (commit_index_files())
1241 die ("Repository has been updated, but unable to write\n"
1242 "new_index file. Check that disk is not full or quota is\n"
1243 "not exceeded, and then \"git reset HEAD\" to recover.");
1246 run_hook(get_index_file(), "post-commit", NULL
);
1248 print_summary(prefix
, commit_sha1
);