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
;
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_CALLBACK('m', "message", &message
, "MESSAGE", "specify commit message", opt_parse_m
),
102 OPT_STRING('c', "reedit-message", &edit_message
, "COMMIT", "reuse and edit message from specified commit"),
103 OPT_STRING('C', "reuse-message", &use_message
, "COMMIT", "reuse message from specified commit"),
104 OPT_BOOLEAN(0, "reset-author", &renew_authorship
, "the commit is authored by me now (used with -C-c/--amend)"),
105 OPT_BOOLEAN('s', "signoff", &signoff
, "add Signed-off-by:"),
106 OPT_FILENAME('t', "template", &template_file
, "use specified template file"),
107 OPT_BOOLEAN('e', "edit", &edit_flag
, "force edit of commit"),
108 OPT_STRING(0, "cleanup", &cleanup_arg
, "default", "how to strip spaces and #comments from message"),
109 /* end commit message options */
111 OPT_GROUP("Commit contents options"),
112 OPT_BOOLEAN('a', "all", &all
, "commit all changed files"),
113 OPT_BOOLEAN('i', "include", &also
, "add specified files to index for commit"),
114 OPT_BOOLEAN(0, "interactive", &interactive
, "interactively add files"),
115 OPT_BOOLEAN('o', "only", &only
, "commit only specified files"),
116 OPT_BOOLEAN('n', "no-verify", &no_verify
, "bypass pre-commit hook"),
117 OPT_BOOLEAN(0, "dry-run", &dry_run
, "show what would be committed"),
118 OPT_SET_INT(0, "short", &status_format
, "show status concisely",
119 STATUS_FORMAT_SHORT
),
120 OPT_SET_INT(0, "porcelain", &status_format
,
121 "show porcelain output format", STATUS_FORMAT_PORCELAIN
),
122 OPT_BOOLEAN('z', "null", &null_termination
,
123 "terminate entries with NUL"),
124 OPT_BOOLEAN(0, "amend", &amend
, "amend previous commit"),
125 { 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" },
126 OPT_BOOLEAN(0, "allow-empty", &allow_empty
, "ok to record an empty change"),
127 /* end commit contents options */
132 static void rollback_index_files(void)
134 switch (commit_style
) {
136 break; /* nothing to do */
138 rollback_lock_file(&index_lock
);
141 rollback_lock_file(&index_lock
);
142 rollback_lock_file(&false_lock
);
147 static int commit_index_files(void)
151 switch (commit_style
) {
153 break; /* nothing to do */
155 err
= commit_lock_file(&index_lock
);
158 err
= commit_lock_file(&index_lock
);
159 rollback_lock_file(&false_lock
);
167 * Take a union of paths in the index and the named tree (typically, "HEAD"),
168 * and return the paths that match the given pattern in list.
170 static int list_paths(struct string_list
*list
, const char *with_tree
,
171 const char *prefix
, const char **pattern
)
176 for (i
= 0; pattern
[i
]; i
++)
181 overlay_tree_on_cache(with_tree
, prefix
);
183 for (i
= 0; i
< active_nr
; i
++) {
184 struct cache_entry
*ce
= active_cache
[i
];
185 if (ce
->ce_flags
& CE_UPDATE
)
187 if (!match_pathspec(pattern
, ce
->name
, ce_namelen(ce
), 0, m
))
189 string_list_insert(ce
->name
, list
);
192 return report_path_error(m
, pattern
, prefix
? strlen(prefix
) : 0);
195 static void add_remove_files(struct string_list
*list
)
198 for (i
= 0; i
< list
->nr
; i
++) {
200 struct string_list_item
*p
= &(list
->items
[i
]);
202 if (!lstat(p
->string
, &st
)) {
203 if (add_to_cache(p
->string
, &st
, 0))
204 die("updating files failed");
206 remove_file_from_cache(p
->string
);
210 static void create_base_index(void)
213 struct unpack_trees_options opts
;
216 if (initial_commit
) {
221 memset(&opts
, 0, sizeof(opts
));
225 opts
.src_index
= &the_index
;
226 opts
.dst_index
= &the_index
;
228 opts
.fn
= oneway_merge
;
229 tree
= parse_tree_indirect(head_sha1
);
231 die("failed to unpack HEAD tree object");
233 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
234 if (unpack_trees(1, &t
, &opts
))
235 exit(128); /* We've already reported the error, finish dying */
238 static char *prepare_index(int argc
, const char **argv
, const char *prefix
, int is_status
)
241 struct string_list partial
;
242 const char **pathspec
= NULL
;
243 int refresh_flags
= REFRESH_QUIET
;
246 refresh_flags
|= REFRESH_UNMERGED
;
248 if (interactive_add(argc
, argv
, prefix
) != 0)
249 die("interactive add failed");
250 if (read_cache_preload(NULL
) < 0)
251 die("index file corrupt");
252 commit_style
= COMMIT_AS_IS
;
253 return get_index_file();
257 pathspec
= get_pathspec(prefix
, argv
);
259 if (read_cache_preload(pathspec
) < 0)
260 die("index file corrupt");
263 * Non partial, non as-is commit.
265 * (1) get the real index;
266 * (2) update the_index as necessary;
267 * (3) write the_index out to the real index (still locked);
268 * (4) return the name of the locked index file.
270 * The caller should run hooks on the locked real index, and
271 * (A) if all goes well, commit the real index;
272 * (B) on failure, rollback the real index.
274 if (all
|| (also
&& pathspec
&& *pathspec
)) {
275 int fd
= hold_locked_index(&index_lock
, 1);
276 add_files_to_cache(also
? prefix
: NULL
, pathspec
, 0);
277 refresh_cache(refresh_flags
);
278 if (write_cache(fd
, active_cache
, active_nr
) ||
279 close_lock_file(&index_lock
))
280 die("unable to write new_index file");
281 commit_style
= COMMIT_NORMAL
;
282 return index_lock
.filename
;
288 * (1) return the name of the real index file.
290 * The caller should run hooks on the real index, and run
291 * hooks on the real index, and create commit from the_index.
292 * We still need to refresh the index here.
294 if (!pathspec
|| !*pathspec
) {
295 fd
= hold_locked_index(&index_lock
, 1);
296 refresh_cache(refresh_flags
);
297 if (write_cache(fd
, active_cache
, active_nr
) ||
298 commit_locked_index(&index_lock
))
299 die("unable to write new_index file");
300 commit_style
= COMMIT_AS_IS
;
301 return get_index_file();
307 * (0) find the set of affected paths;
308 * (1) get lock on the real index file;
309 * (2) update the_index with the given paths;
310 * (3) write the_index out to the real index (still locked);
311 * (4) get lock on the false index file;
312 * (5) reset the_index from HEAD;
313 * (6) update the_index the same way as (2);
314 * (7) write the_index out to the false index file;
315 * (8) return the name of the false index file (still locked);
317 * The caller should run hooks on the locked false index, and
318 * create commit from it. Then
319 * (A) if all goes well, commit the real index;
320 * (B) on failure, rollback the real index;
321 * In either case, rollback the false index.
323 commit_style
= COMMIT_PARTIAL
;
326 die("cannot do a partial commit during a merge.");
328 memset(&partial
, 0, sizeof(partial
));
329 partial
.strdup_strings
= 1;
330 if (list_paths(&partial
, initial_commit
? NULL
: "HEAD", prefix
, pathspec
))
334 if (read_cache() < 0)
335 die("cannot read the index");
337 fd
= hold_locked_index(&index_lock
, 1);
338 add_remove_files(&partial
);
339 refresh_cache(REFRESH_QUIET
);
340 if (write_cache(fd
, active_cache
, active_nr
) ||
341 close_lock_file(&index_lock
))
342 die("unable to write new_index file");
344 fd
= hold_lock_file_for_update(&false_lock
,
345 git_path("next-index-%"PRIuMAX
,
346 (uintmax_t) getpid()),
350 add_remove_files(&partial
);
351 refresh_cache(REFRESH_QUIET
);
353 if (write_cache(fd
, active_cache
, active_nr
) ||
354 close_lock_file(&false_lock
))
355 die("unable to write temporary index file");
358 read_cache_from(false_lock
.filename
);
360 return false_lock
.filename
;
363 static int run_status(FILE *fp
, const char *index_file
, const char *prefix
, int nowarn
,
366 unsigned char sha1
[20];
368 if (s
->relative_paths
)
373 s
->reference
= "HEAD^1";
375 s
->verbose
= verbose
;
376 s
->index_file
= index_file
;
379 s
->is_initial
= get_sha1(s
->reference
, sha1
) ? 1 : 0;
381 wt_status_collect(s
);
383 switch (status_format
) {
384 case STATUS_FORMAT_SHORT
:
385 wt_shortstatus_print(s
, null_termination
);
387 case STATUS_FORMAT_PORCELAIN
:
388 wt_porcelain_print(s
, null_termination
);
390 case STATUS_FORMAT_LONG
:
395 return s
->commitable
;
398 static int is_a_merge(const unsigned char *sha1
)
400 struct commit
*commit
= lookup_commit(sha1
);
401 if (!commit
|| parse_commit(commit
))
402 die("could not parse HEAD commit");
403 return !!(commit
->parents
&& commit
->parents
->next
);
406 static const char sign_off_header
[] = "Signed-off-by: ";
408 static void determine_author_info(void)
410 char *name
, *email
, *date
;
412 name
= getenv("GIT_AUTHOR_NAME");
413 email
= getenv("GIT_AUTHOR_EMAIL");
414 date
= getenv("GIT_AUTHOR_DATE");
416 if (use_message
&& !renew_authorship
) {
417 const char *a
, *lb
, *rb
, *eol
;
419 a
= strstr(use_message_buffer
, "\nauthor ");
421 die("invalid commit: %s", use_message
);
423 lb
= strstr(a
+ 8, " <");
424 rb
= strstr(a
+ 8, "> ");
425 eol
= strchr(a
+ 8, '\n');
426 if (!lb
|| !rb
|| !eol
)
427 die("invalid commit: %s", use_message
);
429 name
= xstrndup(a
+ 8, lb
- (a
+ 8));
430 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
431 date
= xstrndup(rb
+ 2, eol
- (rb
+ 2));
435 const char *lb
= strstr(force_author
, " <");
436 const char *rb
= strchr(force_author
, '>');
439 die("malformed --author parameter");
440 name
= xstrndup(force_author
, lb
- force_author
);
441 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
445 author_email
= email
;
449 static int ends_rfc2822_footer(struct strbuf
*sb
)
456 const char *buf
= sb
->buf
;
458 for (i
= len
- 1; i
> 0; i
--) {
459 if (hit
&& buf
[i
] == '\n')
461 hit
= (buf
[i
] == '\n');
464 while (i
< len
- 1 && buf
[i
] == '\n')
467 for (; i
< len
; i
= k
) {
468 for (k
= i
; k
< len
&& buf
[k
] != '\n'; k
++)
472 if ((buf
[k
] == ' ' || buf
[k
] == '\t') && !first
)
477 for (j
= 0; i
+ j
< len
; j
++) {
490 static int prepare_to_commit(const char *index_file
, const char *prefix
,
494 int commitable
, saved_color_setting
;
495 struct strbuf sb
= STRBUF_INIT
;
498 const char *hook_arg1
= NULL
;
499 const char *hook_arg2
= NULL
;
502 if (!no_verify
&& run_hook(index_file
, "pre-commit", NULL
))
506 strbuf_addbuf(&sb
, &message
);
507 hook_arg1
= "message";
508 } else if (logfile
&& !strcmp(logfile
, "-")) {
510 fprintf(stderr
, "(reading log message from standard input)\n");
511 if (strbuf_read(&sb
, 0, 0) < 0)
512 die_errno("could not read log from standard input");
513 hook_arg1
= "message";
514 } else if (logfile
) {
515 if (strbuf_read_file(&sb
, logfile
, 0) < 0)
516 die_errno("could not read log file '%s'",
518 hook_arg1
= "message";
519 } else if (use_message
) {
520 buffer
= strstr(use_message_buffer
, "\n\n");
521 if (!buffer
|| buffer
[2] == '\0')
522 die("commit has empty message");
523 strbuf_add(&sb
, buffer
+ 2, strlen(buffer
+ 2));
524 hook_arg1
= "commit";
525 hook_arg2
= use_message
;
526 } else if (!stat(git_path("MERGE_MSG"), &statbuf
)) {
527 if (strbuf_read_file(&sb
, git_path("MERGE_MSG"), 0) < 0)
528 die_errno("could not read MERGE_MSG");
530 } else if (!stat(git_path("SQUASH_MSG"), &statbuf
)) {
531 if (strbuf_read_file(&sb
, git_path("SQUASH_MSG"), 0) < 0)
532 die_errno("could not read SQUASH_MSG");
533 hook_arg1
= "squash";
534 } else if (template_file
&& !stat(template_file
, &statbuf
)) {
535 if (strbuf_read_file(&sb
, template_file
, 0) < 0)
536 die_errno("could not read '%s'", template_file
);
537 hook_arg1
= "template";
541 * This final case does not modify the template message,
542 * it just sets the argument to the prepare-commit-msg hook.
547 fp
= fopen(git_path(commit_editmsg
), "w");
549 die_errno("could not open '%s'", git_path(commit_editmsg
));
551 if (cleanup_mode
!= CLEANUP_NONE
)
555 struct strbuf sob
= STRBUF_INIT
;
558 strbuf_addstr(&sob
, sign_off_header
);
559 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
560 getenv("GIT_COMMITTER_EMAIL")));
561 strbuf_addch(&sob
, '\n');
562 for (i
= sb
.len
- 1; i
> 0 && sb
.buf
[i
- 1] != '\n'; i
--)
564 if (prefixcmp(sb
.buf
+ i
, sob
.buf
)) {
565 if (!i
|| !ends_rfc2822_footer(&sb
))
566 strbuf_addch(&sb
, '\n');
567 strbuf_addbuf(&sb
, &sob
);
569 strbuf_release(&sob
);
572 if (fwrite(sb
.buf
, 1, sb
.len
, fp
) < sb
.len
)
573 die_errno("could not write commit template");
577 determine_author_info();
579 /* This checks if committer ident is explicitly given */
580 git_committer_info(0);
583 const char *committer_ident
;
588 "# It looks like you may be committing a MERGE.\n"
589 "# If this is not correct, please remove the file\n"
593 git_path("MERGE_HEAD"));
597 "# Please enter the commit message for your changes.");
598 if (cleanup_mode
== CLEANUP_ALL
)
601 "# with '#' will be ignored, and an empty"
602 " message aborts the commit.\n");
603 else /* CLEANUP_SPACE, that is. */
606 "# with '#' will be kept; you may remove them"
607 " yourself if you want to.\n"
608 "# An empty message aborts the commit.\n");
609 if (only_include_assumed
)
610 fprintf(fp
, "# %s\n", only_include_assumed
);
612 author_ident
= xstrdup(fmt_name(author_name
, author_email
));
613 committer_ident
= fmt_name(getenv("GIT_COMMITTER_NAME"),
614 getenv("GIT_COMMITTER_EMAIL"));
615 if (strcmp(author_ident
, committer_ident
))
619 ident_shown
++ ? "" : "#\n",
623 if (!user_ident_explicitly_given
)
627 ident_shown
++ ? "" : "#\n",
633 saved_color_setting
= s
->use_color
;
635 commitable
= run_status(fp
, index_file
, prefix
, 1, s
);
636 s
->use_color
= saved_color_setting
;
638 unsigned char sha1
[20];
639 const char *parent
= "HEAD";
641 if (!active_nr
&& read_cache() < 0)
642 die("Cannot read index");
647 if (get_sha1(parent
, sha1
))
648 commitable
= !!active_nr
;
650 commitable
= index_differs_from(parent
, 0);
655 if (!commitable
&& !in_merge
&& !allow_empty
&&
656 !(amend
&& is_a_merge(head_sha1
))) {
657 run_status(stdout
, index_file
, prefix
, 0, s
);
662 * Re-read the index as pre-commit hook could have updated it,
663 * and write it out as a tree. We must do this before we invoke
664 * the editor and after we invoke run_status above.
667 read_cache_from(index_file
);
668 if (!active_cache_tree
)
669 active_cache_tree
= cache_tree();
670 if (cache_tree_update(active_cache_tree
,
671 active_cache
, active_nr
, 0, 0) < 0) {
672 error("Error building trees");
676 if (run_hook(index_file
, "prepare-commit-msg",
677 git_path(commit_editmsg
), hook_arg1
, hook_arg2
, NULL
))
681 char index
[PATH_MAX
];
682 const char *env
[2] = { index
, NULL
};
683 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
684 if (launch_editor(git_path(commit_editmsg
), NULL
, env
)) {
686 "Please supply the message using either -m or -F option.\n");
692 run_hook(index_file
, "commit-msg", git_path(commit_editmsg
), NULL
)) {
700 * Find out if the message in the strbuf contains only whitespace and
701 * Signed-off-by lines.
703 static int message_is_empty(struct strbuf
*sb
)
705 struct strbuf tmpl
= STRBUF_INIT
;
707 int eol
, i
, start
= 0;
709 if (cleanup_mode
== CLEANUP_NONE
&& sb
->len
)
712 /* See if the template is just a prefix of the message. */
713 if (template_file
&& strbuf_read_file(&tmpl
, template_file
, 0) > 0) {
714 stripspace(&tmpl
, cleanup_mode
== CLEANUP_ALL
);
715 if (start
+ tmpl
.len
<= sb
->len
&&
716 memcmp(tmpl
.buf
, sb
->buf
+ start
, tmpl
.len
) == 0)
719 strbuf_release(&tmpl
);
721 /* Check if the rest is just whitespace and Signed-of-by's. */
722 for (i
= start
; i
< sb
->len
; i
++) {
723 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
729 if (strlen(sign_off_header
) <= eol
- i
&&
730 !prefixcmp(sb
->buf
+ i
, sign_off_header
)) {
735 if (!isspace(sb
->buf
[i
++]))
742 static const char *find_author_by_nickname(const char *name
)
744 struct rev_info revs
;
745 struct commit
*commit
;
746 struct strbuf buf
= STRBUF_INIT
;
750 init_revisions(&revs
, NULL
);
751 strbuf_addf(&buf
, "--author=%s", name
);
756 setup_revisions(ac
, av
, &revs
, NULL
);
757 prepare_revision_walk(&revs
);
758 commit
= get_revision(&revs
);
760 struct pretty_print_context ctx
= {0};
761 ctx
.date_mode
= DATE_NORMAL
;
762 strbuf_release(&buf
);
763 format_commit_message(commit
, "%an <%ae>", &buf
, &ctx
);
764 return strbuf_detach(&buf
, NULL
);
766 die("No existing author found with '%s'", name
);
770 static void handle_untracked_files_arg(struct wt_status
*s
)
772 if (!untracked_files_arg
)
773 ; /* default already initialized */
774 else if (!strcmp(untracked_files_arg
, "no"))
775 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
776 else if (!strcmp(untracked_files_arg
, "normal"))
777 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
778 else if (!strcmp(untracked_files_arg
, "all"))
779 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
781 die("Invalid untracked files mode '%s'", untracked_files_arg
);
784 static int parse_and_validate_options(int argc
, const char *argv
[],
785 const char * const usage
[],
791 argc
= parse_options(argc
, argv
, prefix
, builtin_commit_options
, usage
,
794 if (force_author
&& !strchr(force_author
, '>'))
795 force_author
= find_author_by_nickname(force_author
);
797 if (force_author
&& renew_authorship
)
798 die("Using both --reset-author and --author does not make sense");
800 if (logfile
|| message
.len
|| use_message
)
805 setenv("GIT_EDITOR", ":", 1);
807 if (get_sha1("HEAD", head_sha1
))
810 /* Sanity check options */
811 if (amend
&& initial_commit
)
812 die("You have nothing to amend.");
813 if (amend
&& in_merge
)
814 die("You are in the middle of a merge -- cannot amend.");
823 die("Only one of -c/-C/-F can be used.");
824 if (message
.len
&& f
> 0)
825 die("Option -m cannot be combined with -c/-C/-F.");
827 use_message
= edit_message
;
828 if (amend
&& !use_message
)
829 use_message
= "HEAD";
830 if (!use_message
&& renew_authorship
)
831 die("--reset-author can be used only with -C, -c or --amend.");
833 unsigned char sha1
[20];
834 static char utf8
[] = "UTF-8";
837 struct commit
*commit
;
839 if (get_sha1(use_message
, sha1
))
840 die("could not lookup commit %s", use_message
);
841 commit
= lookup_commit_reference(sha1
);
842 if (!commit
|| parse_commit(commit
))
843 die("could not parse commit %s", use_message
);
845 enc
= strstr(commit
->buffer
, "\nencoding");
847 end
= strchr(enc
+ 10, '\n');
848 enc
= xstrndup(enc
+ 10, end
- (enc
+ 10));
852 out_enc
= git_commit_encoding
? git_commit_encoding
: utf8
;
854 if (strcmp(out_enc
, enc
))
856 reencode_string(commit
->buffer
, out_enc
, enc
);
859 * If we failed to reencode the buffer, just copy it
860 * byte for byte so the user can try to fix it up.
861 * This also handles the case where input and output
862 * encodings are identical.
864 if (use_message_buffer
== NULL
)
865 use_message_buffer
= xstrdup(commit
->buffer
);
870 if (!!also
+ !!only
+ !!all
+ !!interactive
> 1)
871 die("Only one of --include/--only/--all/--interactive can be used.");
872 if (argc
== 0 && (also
|| (only
&& !amend
)))
873 die("No paths with --include/--only does not make sense.");
874 if (argc
== 0 && only
&& amend
)
875 only_include_assumed
= "Clever... amending the last one with dirty index.";
876 if (argc
> 0 && !also
&& !only
)
877 only_include_assumed
= "Explicit paths specified without -i nor -o; assuming --only paths...";
878 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "default"))
879 cleanup_mode
= use_editor
? CLEANUP_ALL
: CLEANUP_SPACE
;
880 else if (!strcmp(cleanup_arg
, "verbatim"))
881 cleanup_mode
= CLEANUP_NONE
;
882 else if (!strcmp(cleanup_arg
, "whitespace"))
883 cleanup_mode
= CLEANUP_SPACE
;
884 else if (!strcmp(cleanup_arg
, "strip"))
885 cleanup_mode
= CLEANUP_ALL
;
887 die("Invalid cleanup mode %s", cleanup_arg
);
889 handle_untracked_files_arg(s
);
892 die("Paths with -a does not make sense.");
893 else if (interactive
&& argc
> 0)
894 die("Paths with --interactive does not make sense.");
896 if (null_termination
&& status_format
== STATUS_FORMAT_LONG
)
897 status_format
= STATUS_FORMAT_PORCELAIN
;
898 if (status_format
!= STATUS_FORMAT_LONG
)
904 static int dry_run_commit(int argc
, const char **argv
, const char *prefix
,
908 const char *index_file
;
910 index_file
= prepare_index(argc
, argv
, prefix
, 1);
911 commitable
= run_status(stdout
, index_file
, prefix
, 0, s
);
912 rollback_index_files();
914 return commitable
? 0 : 1;
917 static int parse_status_slot(const char *var
, int offset
)
919 if (!strcasecmp(var
+offset
, "header"))
920 return WT_STATUS_HEADER
;
921 if (!strcasecmp(var
+offset
, "updated")
922 || !strcasecmp(var
+offset
, "added"))
923 return WT_STATUS_UPDATED
;
924 if (!strcasecmp(var
+offset
, "changed"))
925 return WT_STATUS_CHANGED
;
926 if (!strcasecmp(var
+offset
, "untracked"))
927 return WT_STATUS_UNTRACKED
;
928 if (!strcasecmp(var
+offset
, "nobranch"))
929 return WT_STATUS_NOBRANCH
;
930 if (!strcasecmp(var
+offset
, "unmerged"))
931 return WT_STATUS_UNMERGED
;
935 static int git_status_config(const char *k
, const char *v
, void *cb
)
937 struct wt_status
*s
= cb
;
939 if (!strcmp(k
, "status.submodulesummary")) {
941 s
->submodule_summary
= git_config_bool_or_int(k
, v
, &is_bool
);
942 if (is_bool
&& s
->submodule_summary
)
943 s
->submodule_summary
= -1;
946 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
947 s
->use_color
= git_config_colorbool(k
, v
, -1);
950 if (!prefixcmp(k
, "status.color.") || !prefixcmp(k
, "color.status.")) {
951 int slot
= parse_status_slot(k
, 13);
955 return config_error_nonbool(k
);
956 color_parse(v
, k
, s
->color_palette
[slot
]);
959 if (!strcmp(k
, "status.relativepaths")) {
960 s
->relative_paths
= git_config_bool(k
, v
);
963 if (!strcmp(k
, "status.showuntrackedfiles")) {
965 return config_error_nonbool(k
);
966 else if (!strcmp(v
, "no"))
967 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
968 else if (!strcmp(v
, "normal"))
969 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
970 else if (!strcmp(v
, "all"))
971 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
973 return error("Invalid untracked files mode '%s'", v
);
976 return git_diff_ui_config(k
, v
, NULL
);
979 int cmd_status(int argc
, const char **argv
, const char *prefix
)
982 unsigned char sha1
[20];
983 static struct option builtin_status_options
[] = {
984 OPT__VERBOSE(&verbose
),
985 OPT_SET_INT('s', "short", &status_format
,
986 "show status concisely", STATUS_FORMAT_SHORT
),
987 OPT_SET_INT(0, "porcelain", &status_format
,
988 "show porcelain output format",
989 STATUS_FORMAT_PORCELAIN
),
990 OPT_BOOLEAN('z', "null", &null_termination
,
991 "terminate entries with NUL"),
992 { OPTION_STRING
, 'u', "untracked-files", &untracked_files_arg
,
994 "show untracked files, optional modes: all, normal, no. (Default: all)",
995 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
999 if (null_termination
&& status_format
== STATUS_FORMAT_LONG
)
1000 status_format
= STATUS_FORMAT_PORCELAIN
;
1002 wt_status_prepare(&s
);
1003 git_config(git_status_config
, &s
);
1004 in_merge
= file_exists(git_path("MERGE_HEAD"));
1005 argc
= parse_options(argc
, argv
, prefix
,
1006 builtin_status_options
,
1007 builtin_status_usage
, 0);
1008 handle_untracked_files_arg(&s
);
1011 s
.pathspec
= get_pathspec(prefix
, argv
);
1014 refresh_cache(REFRESH_QUIET
|REFRESH_UNMERGED
);
1015 s
.is_initial
= get_sha1(s
.reference
, sha1
) ? 1 : 0;
1016 s
.in_merge
= in_merge
;
1017 wt_status_collect(&s
);
1019 if (s
.relative_paths
)
1021 if (s
.use_color
== -1)
1022 s
.use_color
= git_use_color_default
;
1023 if (diff_use_color_default
== -1)
1024 diff_use_color_default
= git_use_color_default
;
1026 switch (status_format
) {
1027 case STATUS_FORMAT_SHORT
:
1028 wt_shortstatus_print(&s
, null_termination
);
1030 case STATUS_FORMAT_PORCELAIN
:
1031 wt_porcelain_print(&s
, null_termination
);
1033 case STATUS_FORMAT_LONG
:
1034 s
.verbose
= verbose
;
1035 wt_status_print(&s
);
1041 static void print_summary(const char *prefix
, const unsigned char *sha1
)
1043 struct rev_info rev
;
1044 struct commit
*commit
;
1045 static const char *format
= "format:%h] %s";
1046 unsigned char junk_sha1
[20];
1047 const char *head
= resolve_ref("HEAD", junk_sha1
, 0, NULL
);
1049 commit
= lookup_commit(sha1
);
1051 die("couldn't look up newly created commit");
1052 if (!commit
|| parse_commit(commit
))
1053 die("could not parse newly created commit");
1055 init_revisions(&rev
, prefix
);
1056 setup_revisions(0, NULL
, &rev
, NULL
);
1060 rev
.diffopt
.output_format
=
1061 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
1063 rev
.verbose_header
= 1;
1064 rev
.show_root_diff
= 1;
1065 get_commit_format(format
, &rev
);
1066 rev
.always_show_header
= 0;
1067 rev
.diffopt
.detect_rename
= 1;
1068 rev
.diffopt
.rename_limit
= 100;
1069 rev
.diffopt
.break_opt
= 0;
1070 diff_setup_done(&rev
.diffopt
);
1073 !prefixcmp(head
, "refs/heads/") ?
1075 !strcmp(head
, "HEAD") ?
1078 initial_commit
? " (root-commit)" : "");
1080 if (!log_tree_commit(&rev
, commit
)) {
1081 struct pretty_print_context ctx
= {0};
1082 struct strbuf buf
= STRBUF_INIT
;
1083 ctx
.date_mode
= DATE_NORMAL
;
1084 format_commit_message(commit
, format
+ 7, &buf
, &ctx
);
1085 printf("%s\n", buf
.buf
);
1086 strbuf_release(&buf
);
1090 static int git_commit_config(const char *k
, const char *v
, void *cb
)
1092 struct wt_status
*s
= cb
;
1094 if (!strcmp(k
, "commit.template"))
1095 return git_config_pathname(&template_file
, k
, v
);
1097 return git_status_config(k
, v
, s
);
1100 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
1102 struct strbuf sb
= STRBUF_INIT
;
1103 const char *index_file
, *reflog_msg
;
1105 unsigned char commit_sha1
[20];
1106 struct ref_lock
*ref_lock
;
1107 struct commit_list
*parents
= NULL
, **pptr
= &parents
;
1108 struct stat statbuf
;
1109 int allow_fast_forward
= 1;
1112 wt_status_prepare(&s
);
1113 git_config(git_commit_config
, &s
);
1114 in_merge
= file_exists(git_path("MERGE_HEAD"));
1115 s
.in_merge
= in_merge
;
1117 if (s
.use_color
== -1)
1118 s
.use_color
= git_use_color_default
;
1119 argc
= parse_and_validate_options(argc
, argv
, builtin_commit_usage
,
1122 if (diff_use_color_default
== -1)
1123 diff_use_color_default
= git_use_color_default
;
1124 return dry_run_commit(argc
, argv
, prefix
, &s
);
1126 index_file
= prepare_index(argc
, argv
, prefix
, 0);
1128 /* Set up everything for writing the commit object. This includes
1129 running hooks, writing the trees, and interacting with the user. */
1130 if (!prepare_to_commit(index_file
, prefix
, &s
)) {
1131 rollback_index_files();
1135 /* Determine parents */
1136 if (initial_commit
) {
1137 reflog_msg
= "commit (initial)";
1139 struct commit_list
*c
;
1140 struct commit
*commit
;
1142 reflog_msg
= "commit (amend)";
1143 commit
= lookup_commit(head_sha1
);
1144 if (!commit
|| parse_commit(commit
))
1145 die("could not parse HEAD commit");
1147 for (c
= commit
->parents
; c
; c
= c
->next
)
1148 pptr
= &commit_list_insert(c
->item
, pptr
)->next
;
1149 } else if (in_merge
) {
1150 struct strbuf m
= STRBUF_INIT
;
1153 reflog_msg
= "commit (merge)";
1154 pptr
= &commit_list_insert(lookup_commit(head_sha1
), pptr
)->next
;
1155 fp
= fopen(git_path("MERGE_HEAD"), "r");
1157 die_errno("could not open '%s' for reading",
1158 git_path("MERGE_HEAD"));
1159 while (strbuf_getline(&m
, fp
, '\n') != EOF
) {
1160 unsigned char sha1
[20];
1161 if (get_sha1_hex(m
.buf
, sha1
) < 0)
1162 die("Corrupt MERGE_HEAD file (%s)", m
.buf
);
1163 pptr
= &commit_list_insert(lookup_commit(sha1
), pptr
)->next
;
1167 if (!stat(git_path("MERGE_MODE"), &statbuf
)) {
1168 if (strbuf_read_file(&sb
, git_path("MERGE_MODE"), 0) < 0)
1169 die_errno("could not read MERGE_MODE");
1170 if (!strcmp(sb
.buf
, "no-ff"))
1171 allow_fast_forward
= 0;
1173 if (allow_fast_forward
)
1174 parents
= reduce_heads(parents
);
1176 reflog_msg
= "commit";
1177 pptr
= &commit_list_insert(lookup_commit(head_sha1
), pptr
)->next
;
1180 /* Finally, get the commit message */
1182 if (strbuf_read_file(&sb
, git_path(commit_editmsg
), 0) < 0) {
1183 int saved_errno
= errno
;
1184 rollback_index_files();
1185 die("could not read commit message: %s", strerror(saved_errno
));
1188 /* Truncate the message just before the diff, if any. */
1190 p
= strstr(sb
.buf
, "\ndiff --git ");
1192 strbuf_setlen(&sb
, p
- sb
.buf
+ 1);
1195 if (cleanup_mode
!= CLEANUP_NONE
)
1196 stripspace(&sb
, cleanup_mode
== CLEANUP_ALL
);
1197 if (message_is_empty(&sb
)) {
1198 rollback_index_files();
1199 fprintf(stderr
, "Aborting commit due to empty commit message.\n");
1203 if (commit_tree(sb
.buf
, active_cache_tree
->sha1
, parents
, commit_sha1
,
1204 fmt_ident(author_name
, author_email
, author_date
,
1205 IDENT_ERROR_ON_NO_NAME
))) {
1206 rollback_index_files();
1207 die("failed to write commit object");
1210 ref_lock
= lock_any_ref_for_update("HEAD",
1211 initial_commit
? NULL
: head_sha1
,
1214 nl
= strchr(sb
.buf
, '\n');
1216 strbuf_setlen(&sb
, nl
+ 1 - sb
.buf
);
1218 strbuf_addch(&sb
, '\n');
1219 strbuf_insert(&sb
, 0, reflog_msg
, strlen(reflog_msg
));
1220 strbuf_insert(&sb
, strlen(reflog_msg
), ": ", 2);
1223 rollback_index_files();
1224 die("cannot lock HEAD ref");
1226 if (write_ref_sha1(ref_lock
, commit_sha1
, sb
.buf
) < 0) {
1227 rollback_index_files();
1228 die("cannot update HEAD ref");
1231 unlink(git_path("MERGE_HEAD"));
1232 unlink(git_path("MERGE_MSG"));
1233 unlink(git_path("MERGE_MODE"));
1234 unlink(git_path("SQUASH_MSG"));
1236 if (commit_index_files())
1237 die ("Repository has been updated, but unable to write\n"
1238 "new_index file. Check that disk is not full or quota is\n"
1239 "not exceeded, and then \"git reset HEAD\" to recover.");
1242 run_hook(get_index_file(), "post-commit", NULL
);
1244 print_summary(prefix
, commit_sha1
);