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 * const builtin_stat_usage
[] = {
44 static unsigned char head_sha1
[20], merge_head_sha1
[20];
45 static char *use_message_buffer
;
46 static const char commit_editmsg
[] = "COMMIT_EDITMSG";
47 static struct lock_file index_lock
; /* real index */
48 static struct lock_file false_lock
; /* used only for partial commits */
55 static const char *logfile
, *force_author
;
56 static const char *template_file
;
57 static char *edit_message
, *use_message
;
58 static char *author_name
, *author_email
, *author_date
;
59 static int all
, edit_flag
, also
, interactive
, only
, amend
, signoff
;
60 static int quiet
, verbose
, no_verify
, allow_empty
, dry_run
;
61 static char *untracked_files_arg
;
63 * The default commit message cleanup mode will remove the lines
64 * beginning with # (shell comments) and leading and trailing
65 * whitespaces (empty lines or containing only whitespaces)
66 * if editor is used, and only the whitespaces if the message
67 * is specified explicitly.
74 static char *cleanup_arg
;
76 static int use_editor
= 1, initial_commit
, in_merge
;
77 static const char *only_include_assumed
;
78 static struct strbuf message
;
80 static int opt_parse_m(const struct option
*opt
, const char *arg
, int unset
)
82 struct strbuf
*buf
= opt
->value
;
84 strbuf_setlen(buf
, 0);
86 strbuf_addstr(buf
, arg
);
87 strbuf_addstr(buf
, "\n\n");
92 static struct option builtin_commit_options
[] = {
94 OPT__VERBOSE(&verbose
),
95 OPT_GROUP("Commit message options"),
97 OPT_FILENAME('F', "file", &logfile
, "read log from file"),
98 OPT_STRING(0, "author", &force_author
, "AUTHOR", "override author for commit"),
99 OPT_CALLBACK('m', "message", &message
, "MESSAGE", "specify commit message", opt_parse_m
),
100 OPT_STRING('c', "reedit-message", &edit_message
, "COMMIT", "reuse and edit message from specified commit "),
101 OPT_STRING('C', "reuse-message", &use_message
, "COMMIT", "reuse message from specified commit"),
102 OPT_BOOLEAN('s', "signoff", &signoff
, "add Signed-off-by:"),
103 OPT_FILENAME('t', "template", &template_file
, "use specified template file"),
104 OPT_BOOLEAN('e', "edit", &edit_flag
, "force edit of commit"),
106 OPT_GROUP("Commit contents options"),
107 OPT_BOOLEAN('a', "all", &all
, "commit all changed files"),
108 OPT_BOOLEAN('i', "include", &also
, "add specified files to index for commit"),
109 OPT_BOOLEAN(0, "interactive", &interactive
, "interactively add files"),
110 OPT_BOOLEAN('o', "only", &only
, "commit only specified files"),
111 OPT_BOOLEAN('n', "no-verify", &no_verify
, "bypass pre-commit hook"),
112 OPT_BOOLEAN(0, "dry-run", &dry_run
, "show what would be committed"),
113 OPT_BOOLEAN(0, "amend", &amend
, "amend previous commit"),
114 { 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" },
115 OPT_BOOLEAN(0, "allow-empty", &allow_empty
, "ok to record an empty change"),
116 OPT_STRING(0, "cleanup", &cleanup_arg
, "default", "how to strip spaces and #comments from message"),
121 static void rollback_index_files(void)
123 switch (commit_style
) {
125 break; /* nothing to do */
127 rollback_lock_file(&index_lock
);
130 rollback_lock_file(&index_lock
);
131 rollback_lock_file(&false_lock
);
136 static int commit_index_files(void)
140 switch (commit_style
) {
142 break; /* nothing to do */
144 err
= commit_lock_file(&index_lock
);
147 err
= commit_lock_file(&index_lock
);
148 rollback_lock_file(&false_lock
);
156 * Take a union of paths in the index and the named tree (typically, "HEAD"),
157 * and return the paths that match the given pattern in list.
159 static int list_paths(struct string_list
*list
, const char *with_tree
,
160 const char *prefix
, const char **pattern
)
165 for (i
= 0; pattern
[i
]; i
++)
170 overlay_tree_on_cache(with_tree
, prefix
);
172 for (i
= 0; i
< active_nr
; i
++) {
173 struct cache_entry
*ce
= active_cache
[i
];
174 if (ce
->ce_flags
& CE_UPDATE
)
176 if (!match_pathspec(pattern
, ce
->name
, ce_namelen(ce
), 0, m
))
178 string_list_insert(ce
->name
, list
);
181 return report_path_error(m
, pattern
, prefix
? strlen(prefix
) : 0);
184 static void add_remove_files(struct string_list
*list
)
187 for (i
= 0; i
< list
->nr
; i
++) {
189 struct string_list_item
*p
= &(list
->items
[i
]);
191 if (!lstat(p
->string
, &st
)) {
192 if (add_to_cache(p
->string
, &st
, 0))
193 die("updating files failed");
195 remove_file_from_cache(p
->string
);
199 static void create_base_index(void)
202 struct unpack_trees_options opts
;
205 if (initial_commit
) {
210 memset(&opts
, 0, sizeof(opts
));
214 opts
.src_index
= &the_index
;
215 opts
.dst_index
= &the_index
;
217 opts
.fn
= oneway_merge
;
218 tree
= parse_tree_indirect(head_sha1
);
220 die("failed to unpack HEAD tree object");
222 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
223 if (unpack_trees(1, &t
, &opts
))
224 exit(128); /* We've already reported the error, finish dying */
227 static char *prepare_index(int argc
, const char **argv
, const char *prefix
, int is_status
)
230 struct string_list partial
;
231 const char **pathspec
= NULL
;
232 int refresh_flags
= REFRESH_QUIET
;
235 refresh_flags
|= REFRESH_UNMERGED
;
237 if (interactive_add(argc
, argv
, prefix
) != 0)
238 die("interactive add failed");
239 if (read_cache_preload(NULL
) < 0)
240 die("index file corrupt");
241 commit_style
= COMMIT_AS_IS
;
242 return get_index_file();
246 pathspec
= get_pathspec(prefix
, argv
);
248 if (read_cache_preload(pathspec
) < 0)
249 die("index file corrupt");
252 * Non partial, non as-is commit.
254 * (1) get the real index;
255 * (2) update the_index as necessary;
256 * (3) write the_index out to the real index (still locked);
257 * (4) return the name of the locked index file.
259 * The caller should run hooks on the locked real index, and
260 * (A) if all goes well, commit the real index;
261 * (B) on failure, rollback the real index.
263 if (all
|| (also
&& pathspec
&& *pathspec
)) {
264 int fd
= hold_locked_index(&index_lock
, 1);
265 add_files_to_cache(also
? prefix
: NULL
, pathspec
, 0);
266 refresh_cache(refresh_flags
);
267 if (write_cache(fd
, active_cache
, active_nr
) ||
268 close_lock_file(&index_lock
))
269 die("unable to write new_index file");
270 commit_style
= COMMIT_NORMAL
;
271 return index_lock
.filename
;
277 * (1) return the name of the real index file.
279 * The caller should run hooks on the real index, and run
280 * hooks on the real index, and create commit from the_index.
281 * We still need to refresh the index here.
283 if (!pathspec
|| !*pathspec
) {
284 fd
= hold_locked_index(&index_lock
, 1);
285 refresh_cache(refresh_flags
);
286 if (write_cache(fd
, active_cache
, active_nr
) ||
287 commit_locked_index(&index_lock
))
288 die("unable to write new_index file");
289 commit_style
= COMMIT_AS_IS
;
290 return get_index_file();
296 * (0) find the set of affected paths;
297 * (1) get lock on the real index file;
298 * (2) update the_index with the given paths;
299 * (3) write the_index out to the real index (still locked);
300 * (4) get lock on the false index file;
301 * (5) reset the_index from HEAD;
302 * (6) update the_index the same way as (2);
303 * (7) write the_index out to the false index file;
304 * (8) return the name of the false index file (still locked);
306 * The caller should run hooks on the locked false index, and
307 * create commit from it. Then
308 * (A) if all goes well, commit the real index;
309 * (B) on failure, rollback the real index;
310 * In either case, rollback the false index.
312 commit_style
= COMMIT_PARTIAL
;
314 if (file_exists(git_path("MERGE_HEAD")))
315 die("cannot do a partial commit during a merge.");
317 memset(&partial
, 0, sizeof(partial
));
318 partial
.strdup_strings
= 1;
319 if (list_paths(&partial
, initial_commit
? NULL
: "HEAD", prefix
, pathspec
))
323 if (read_cache() < 0)
324 die("cannot read the index");
326 fd
= hold_locked_index(&index_lock
, 1);
327 add_remove_files(&partial
);
328 refresh_cache(REFRESH_QUIET
);
329 if (write_cache(fd
, active_cache
, active_nr
) ||
330 close_lock_file(&index_lock
))
331 die("unable to write new_index file");
333 fd
= hold_lock_file_for_update(&false_lock
,
334 git_path("next-index-%"PRIuMAX
,
335 (uintmax_t) getpid()),
339 add_remove_files(&partial
);
340 refresh_cache(REFRESH_QUIET
);
342 if (write_cache(fd
, active_cache
, active_nr
) ||
343 close_lock_file(&false_lock
))
344 die("unable to write temporary index file");
347 read_cache_from(false_lock
.filename
);
349 return false_lock
.filename
;
352 static int run_status(FILE *fp
, const char *index_file
, const char *prefix
, int nowarn
,
355 unsigned char sha1
[20];
357 if (s
->relative_paths
)
362 s
->reference
= "HEAD^1";
364 s
->verbose
= verbose
;
365 s
->index_file
= index_file
;
368 s
->is_initial
= get_sha1(s
->reference
, sha1
) ? 1 : 0;
370 wt_status_collect(s
);
373 return s
->commitable
;
376 static int is_a_merge(const unsigned char *sha1
)
378 struct commit
*commit
= lookup_commit(sha1
);
379 if (!commit
|| parse_commit(commit
))
380 die("could not parse HEAD commit");
381 return !!(commit
->parents
&& commit
->parents
->next
);
384 static const char sign_off_header
[] = "Signed-off-by: ";
386 static void determine_author_info(void)
388 char *name
, *email
, *date
;
390 name
= getenv("GIT_AUTHOR_NAME");
391 email
= getenv("GIT_AUTHOR_EMAIL");
392 date
= getenv("GIT_AUTHOR_DATE");
395 const char *a
, *lb
, *rb
, *eol
;
397 a
= strstr(use_message_buffer
, "\nauthor ");
399 die("invalid commit: %s", use_message
);
401 lb
= strstr(a
+ 8, " <");
402 rb
= strstr(a
+ 8, "> ");
403 eol
= strchr(a
+ 8, '\n');
404 if (!lb
|| !rb
|| !eol
)
405 die("invalid commit: %s", use_message
);
407 name
= xstrndup(a
+ 8, lb
- (a
+ 8));
408 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
409 date
= xstrndup(rb
+ 2, eol
- (rb
+ 2));
413 const char *lb
= strstr(force_author
, " <");
414 const char *rb
= strchr(force_author
, '>');
417 die("malformed --author parameter");
418 name
= xstrndup(force_author
, lb
- force_author
);
419 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
423 author_email
= email
;
427 static int prepare_to_commit(const char *index_file
, const char *prefix
,
431 int commitable
, saved_color_setting
;
432 struct strbuf sb
= STRBUF_INIT
;
435 const char *hook_arg1
= NULL
;
436 const char *hook_arg2
= NULL
;
439 if (!no_verify
&& run_hook(index_file
, "pre-commit", NULL
))
443 strbuf_addbuf(&sb
, &message
);
444 hook_arg1
= "message";
445 } else if (logfile
&& !strcmp(logfile
, "-")) {
447 fprintf(stderr
, "(reading log message from standard input)\n");
448 if (strbuf_read(&sb
, 0, 0) < 0)
449 die_errno("could not read log from standard input");
450 hook_arg1
= "message";
451 } else if (logfile
) {
452 if (strbuf_read_file(&sb
, logfile
, 0) < 0)
453 die_errno("could not read log file '%s'",
455 hook_arg1
= "message";
456 } else if (use_message
) {
457 buffer
= strstr(use_message_buffer
, "\n\n");
458 if (!buffer
|| buffer
[2] == '\0')
459 die("commit has empty message");
460 strbuf_add(&sb
, buffer
+ 2, strlen(buffer
+ 2));
461 hook_arg1
= "commit";
462 hook_arg2
= use_message
;
463 } else if (!stat(git_path("MERGE_MSG"), &statbuf
)) {
464 if (strbuf_read_file(&sb
, git_path("MERGE_MSG"), 0) < 0)
465 die_errno("could not read MERGE_MSG");
467 } else if (!stat(git_path("SQUASH_MSG"), &statbuf
)) {
468 if (strbuf_read_file(&sb
, git_path("SQUASH_MSG"), 0) < 0)
469 die_errno("could not read SQUASH_MSG");
470 hook_arg1
= "squash";
471 } else if (template_file
&& !stat(template_file
, &statbuf
)) {
472 if (strbuf_read_file(&sb
, template_file
, 0) < 0)
473 die_errno("could not read '%s'", template_file
);
474 hook_arg1
= "template";
478 * This final case does not modify the template message,
479 * it just sets the argument to the prepare-commit-msg hook.
484 fp
= fopen(git_path(commit_editmsg
), "w");
486 die_errno("could not open '%s'", git_path(commit_editmsg
));
488 if (cleanup_mode
!= CLEANUP_NONE
)
492 struct strbuf sob
= STRBUF_INIT
;
495 strbuf_addstr(&sob
, sign_off_header
);
496 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
497 getenv("GIT_COMMITTER_EMAIL")));
498 strbuf_addch(&sob
, '\n');
499 for (i
= sb
.len
- 1; i
> 0 && sb
.buf
[i
- 1] != '\n'; i
--)
501 if (prefixcmp(sb
.buf
+ i
, sob
.buf
)) {
502 if (prefixcmp(sb
.buf
+ i
, sign_off_header
))
503 strbuf_addch(&sb
, '\n');
504 strbuf_addbuf(&sb
, &sob
);
506 strbuf_release(&sob
);
509 if (fwrite(sb
.buf
, 1, sb
.len
, fp
) < sb
.len
)
510 die_errno("could not write commit template");
514 determine_author_info();
516 /* This checks if committer ident is explicitly given */
517 git_committer_info(0);
520 const char *committer_ident
;
525 "# It looks like you may be committing a MERGE.\n"
526 "# If this is not correct, please remove the file\n"
530 git_path("MERGE_HEAD"));
534 "# Please enter the commit message for your changes.");
535 if (cleanup_mode
== CLEANUP_ALL
)
538 "# with '#' will be ignored, and an empty"
539 " message aborts the commit.\n");
540 else /* CLEANUP_SPACE, that is. */
543 "# with '#' will be kept; you may remove them"
544 " yourself if you want to.\n"
545 "# An empty message aborts the commit.\n");
546 if (only_include_assumed
)
547 fprintf(fp
, "# %s\n", only_include_assumed
);
549 author_ident
= xstrdup(fmt_name(author_name
, author_email
));
550 committer_ident
= fmt_name(getenv("GIT_COMMITTER_NAME"),
551 getenv("GIT_COMMITTER_EMAIL"));
552 if (strcmp(author_ident
, committer_ident
))
556 ident_shown
++ ? "" : "#\n",
560 if (!user_ident_explicitly_given
)
564 ident_shown
++ ? "" : "#\n",
570 saved_color_setting
= s
->use_color
;
572 commitable
= run_status(fp
, index_file
, prefix
, 1, s
);
573 s
->use_color
= saved_color_setting
;
575 unsigned char sha1
[20];
576 const char *parent
= "HEAD";
578 if (!active_nr
&& read_cache() < 0)
579 die("Cannot read index");
584 if (get_sha1(parent
, sha1
))
585 commitable
= !!active_nr
;
587 commitable
= index_differs_from(parent
, 0);
592 if (!commitable
&& !in_merge
&& !allow_empty
&&
593 !(amend
&& is_a_merge(head_sha1
))) {
594 run_status(stdout
, index_file
, prefix
, 0, s
);
599 * Re-read the index as pre-commit hook could have updated it,
600 * and write it out as a tree. We must do this before we invoke
601 * the editor and after we invoke run_status above.
604 read_cache_from(index_file
);
605 if (!active_cache_tree
)
606 active_cache_tree
= cache_tree();
607 if (cache_tree_update(active_cache_tree
,
608 active_cache
, active_nr
, 0, 0) < 0) {
609 error("Error building trees");
613 if (run_hook(index_file
, "prepare-commit-msg",
614 git_path(commit_editmsg
), hook_arg1
, hook_arg2
, NULL
))
618 char index
[PATH_MAX
];
619 const char *env
[2] = { index
, NULL
};
620 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
621 if (launch_editor(git_path(commit_editmsg
), NULL
, env
)) {
623 "Please supply the message using either -m or -F option.\n");
629 run_hook(index_file
, "commit-msg", git_path(commit_editmsg
), NULL
)) {
637 * Find out if the message in the strbuf contains only whitespace and
638 * Signed-off-by lines.
640 static int message_is_empty(struct strbuf
*sb
)
642 struct strbuf tmpl
= STRBUF_INIT
;
644 int eol
, i
, start
= 0;
646 if (cleanup_mode
== CLEANUP_NONE
&& sb
->len
)
649 /* See if the template is just a prefix of the message. */
650 if (template_file
&& strbuf_read_file(&tmpl
, template_file
, 0) > 0) {
651 stripspace(&tmpl
, cleanup_mode
== CLEANUP_ALL
);
652 if (start
+ tmpl
.len
<= sb
->len
&&
653 memcmp(tmpl
.buf
, sb
->buf
+ start
, tmpl
.len
) == 0)
656 strbuf_release(&tmpl
);
658 /* Check if the rest is just whitespace and Signed-of-by's. */
659 for (i
= start
; i
< sb
->len
; i
++) {
660 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
666 if (strlen(sign_off_header
) <= eol
- i
&&
667 !prefixcmp(sb
->buf
+ i
, sign_off_header
)) {
672 if (!isspace(sb
->buf
[i
++]))
679 static const char *find_author_by_nickname(const char *name
)
681 struct rev_info revs
;
682 struct commit
*commit
;
683 struct strbuf buf
= STRBUF_INIT
;
687 init_revisions(&revs
, NULL
);
688 strbuf_addf(&buf
, "--author=%s", name
);
693 setup_revisions(ac
, av
, &revs
, NULL
);
694 prepare_revision_walk(&revs
);
695 commit
= get_revision(&revs
);
697 strbuf_release(&buf
);
698 format_commit_message(commit
, "%an <%ae>", &buf
, DATE_NORMAL
);
699 return strbuf_detach(&buf
, NULL
);
701 die("No existing author found with '%s'", name
);
705 static void handle_untracked_files_arg(struct wt_status
*s
)
707 if (!untracked_files_arg
)
708 ; /* default already initialized */
709 else if (!strcmp(untracked_files_arg
, "no"))
710 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
711 else if (!strcmp(untracked_files_arg
, "normal"))
712 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
713 else if (!strcmp(untracked_files_arg
, "all"))
714 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
716 die("Invalid untracked files mode '%s'", untracked_files_arg
);
719 static int parse_and_validate_options(int argc
, const char *argv
[],
720 const char * const usage
[],
726 argc
= parse_options(argc
, argv
, prefix
, builtin_commit_options
, usage
,
729 if (force_author
&& !strchr(force_author
, '>'))
730 force_author
= find_author_by_nickname(force_author
);
732 if (logfile
|| message
.len
|| use_message
)
737 setenv("GIT_EDITOR", ":", 1);
739 if (get_sha1("HEAD", head_sha1
))
742 if (!get_sha1("MERGE_HEAD", merge_head_sha1
))
745 /* Sanity check options */
746 if (amend
&& initial_commit
)
747 die("You have nothing to amend.");
748 if (amend
&& in_merge
)
749 die("You are in the middle of a merge -- cannot amend.");
758 die("Only one of -c/-C/-F can be used.");
759 if (message
.len
&& f
> 0)
760 die("Option -m cannot be combined with -c/-C/-F.");
762 use_message
= edit_message
;
763 if (amend
&& !use_message
)
764 use_message
= "HEAD";
766 unsigned char sha1
[20];
767 static char utf8
[] = "UTF-8";
770 struct commit
*commit
;
772 if (get_sha1(use_message
, sha1
))
773 die("could not lookup commit %s", use_message
);
774 commit
= lookup_commit_reference(sha1
);
775 if (!commit
|| parse_commit(commit
))
776 die("could not parse commit %s", use_message
);
778 enc
= strstr(commit
->buffer
, "\nencoding");
780 end
= strchr(enc
+ 10, '\n');
781 enc
= xstrndup(enc
+ 10, end
- (enc
+ 10));
785 out_enc
= git_commit_encoding
? git_commit_encoding
: utf8
;
787 if (strcmp(out_enc
, enc
))
789 reencode_string(commit
->buffer
, out_enc
, enc
);
792 * If we failed to reencode the buffer, just copy it
793 * byte for byte so the user can try to fix it up.
794 * This also handles the case where input and output
795 * encodings are identical.
797 if (use_message_buffer
== NULL
)
798 use_message_buffer
= xstrdup(commit
->buffer
);
803 if (!!also
+ !!only
+ !!all
+ !!interactive
> 1)
804 die("Only one of --include/--only/--all/--interactive can be used.");
805 if (argc
== 0 && (also
|| (only
&& !amend
)))
806 die("No paths with --include/--only does not make sense.");
807 if (argc
== 0 && only
&& amend
)
808 only_include_assumed
= "Clever... amending the last one with dirty index.";
809 if (argc
> 0 && !also
&& !only
)
810 only_include_assumed
= "Explicit paths specified without -i nor -o; assuming --only paths...";
811 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "default"))
812 cleanup_mode
= use_editor
? CLEANUP_ALL
: CLEANUP_SPACE
;
813 else if (!strcmp(cleanup_arg
, "verbatim"))
814 cleanup_mode
= CLEANUP_NONE
;
815 else if (!strcmp(cleanup_arg
, "whitespace"))
816 cleanup_mode
= CLEANUP_SPACE
;
817 else if (!strcmp(cleanup_arg
, "strip"))
818 cleanup_mode
= CLEANUP_ALL
;
820 die("Invalid cleanup mode %s", cleanup_arg
);
822 handle_untracked_files_arg(s
);
825 die("Paths with -a does not make sense.");
826 else if (interactive
&& argc
> 0)
827 die("Paths with --interactive does not make sense.");
832 static int dry_run_commit(int argc
, const char **argv
, const char *prefix
,
836 const char *index_file
;
838 index_file
= prepare_index(argc
, argv
, prefix
, 1);
839 commitable
= run_status(stdout
, index_file
, prefix
, 0, s
);
840 rollback_index_files();
842 return commitable
? 0 : 1;
845 static int parse_status_slot(const char *var
, int offset
)
847 if (!strcasecmp(var
+offset
, "header"))
848 return WT_STATUS_HEADER
;
849 if (!strcasecmp(var
+offset
, "updated")
850 || !strcasecmp(var
+offset
, "added"))
851 return WT_STATUS_UPDATED
;
852 if (!strcasecmp(var
+offset
, "changed"))
853 return WT_STATUS_CHANGED
;
854 if (!strcasecmp(var
+offset
, "untracked"))
855 return WT_STATUS_UNTRACKED
;
856 if (!strcasecmp(var
+offset
, "nobranch"))
857 return WT_STATUS_NOBRANCH
;
858 if (!strcasecmp(var
+offset
, "unmerged"))
859 return WT_STATUS_UNMERGED
;
860 die("bad config variable '%s'", var
);
863 static int git_status_config(const char *k
, const char *v
, void *cb
)
865 struct wt_status
*s
= cb
;
867 if (!strcmp(k
, "status.submodulesummary")) {
869 s
->submodule_summary
= git_config_bool_or_int(k
, v
, &is_bool
);
870 if (is_bool
&& s
->submodule_summary
)
871 s
->submodule_summary
= -1;
874 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
875 s
->use_color
= git_config_colorbool(k
, v
, -1);
878 if (!prefixcmp(k
, "status.color.") || !prefixcmp(k
, "color.status.")) {
879 int slot
= parse_status_slot(k
, 13);
881 return config_error_nonbool(k
);
882 color_parse(v
, k
, s
->color_palette
[slot
]);
885 if (!strcmp(k
, "status.relativepaths")) {
886 s
->relative_paths
= git_config_bool(k
, v
);
889 if (!strcmp(k
, "status.showuntrackedfiles")) {
891 return config_error_nonbool(k
);
892 else if (!strcmp(v
, "no"))
893 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
894 else if (!strcmp(v
, "normal"))
895 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
896 else if (!strcmp(v
, "all"))
897 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
899 return error("Invalid untracked files mode '%s'", v
);
902 return git_diff_ui_config(k
, v
, NULL
);
905 int cmd_stat(int argc
, const char **argv
, const char *prefix
)
908 unsigned char sha1
[20];
909 static struct option builtin_stat_options
[] = {
910 OPT__VERBOSE(&verbose
),
911 { OPTION_STRING
, 'u', "untracked-files", &untracked_files_arg
,
913 "show untracked files, optional modes: all, normal, no. (Default: all)",
914 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
918 wt_status_prepare(&s
);
919 git_config(git_status_config
, &s
);
920 argc
= parse_options(argc
, argv
, prefix
,
921 builtin_stat_options
,
922 builtin_stat_usage
, 0);
923 handle_untracked_files_arg(&s
);
926 s
.pathspec
= get_pathspec(prefix
, argv
);
929 refresh_cache(REFRESH_QUIET
|REFRESH_UNMERGED
);
930 s
.is_initial
= get_sha1(s
.reference
, sha1
) ? 1 : 0;
931 wt_status_collect(&s
);
934 if (s
.relative_paths
)
936 if (s
.use_color
== -1)
937 s
.use_color
= git_use_color_default
;
938 if (diff_use_color_default
== -1)
939 diff_use_color_default
= git_use_color_default
;
944 int cmd_status(int argc
, const char **argv
, const char *prefix
)
948 wt_status_prepare(&s
);
949 git_config(git_status_config
, &s
);
950 if (s
.use_color
== -1)
951 s
.use_color
= git_use_color_default
;
952 if (diff_use_color_default
== -1)
953 diff_use_color_default
= git_use_color_default
;
955 argc
= parse_and_validate_options(argc
, argv
, builtin_status_usage
,
957 return dry_run_commit(argc
, argv
, prefix
, &s
);
960 static void print_summary(const char *prefix
, const unsigned char *sha1
)
963 struct commit
*commit
;
964 static const char *format
= "format:%h] %s";
965 unsigned char junk_sha1
[20];
966 const char *head
= resolve_ref("HEAD", junk_sha1
, 0, NULL
);
968 commit
= lookup_commit(sha1
);
970 die("couldn't look up newly created commit");
971 if (!commit
|| parse_commit(commit
))
972 die("could not parse newly created commit");
974 init_revisions(&rev
, prefix
);
975 setup_revisions(0, NULL
, &rev
, NULL
);
979 rev
.diffopt
.output_format
=
980 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
982 rev
.verbose_header
= 1;
983 rev
.show_root_diff
= 1;
984 get_commit_format(format
, &rev
);
985 rev
.always_show_header
= 0;
986 rev
.diffopt
.detect_rename
= 1;
987 rev
.diffopt
.rename_limit
= 100;
988 rev
.diffopt
.break_opt
= 0;
989 diff_setup_done(&rev
.diffopt
);
992 !prefixcmp(head
, "refs/heads/") ?
994 !strcmp(head
, "HEAD") ?
997 initial_commit
? " (root-commit)" : "");
999 if (!log_tree_commit(&rev
, commit
)) {
1000 struct strbuf buf
= STRBUF_INIT
;
1001 format_commit_message(commit
, format
+ 7, &buf
, DATE_NORMAL
);
1002 printf("%s\n", buf
.buf
);
1003 strbuf_release(&buf
);
1007 static int git_commit_config(const char *k
, const char *v
, void *cb
)
1009 struct wt_status
*s
= cb
;
1011 if (!strcmp(k
, "commit.template"))
1012 return git_config_string(&template_file
, k
, v
);
1014 return git_status_config(k
, v
, s
);
1017 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
1019 struct strbuf sb
= STRBUF_INIT
;
1020 const char *index_file
, *reflog_msg
;
1022 unsigned char commit_sha1
[20];
1023 struct ref_lock
*ref_lock
;
1024 struct commit_list
*parents
= NULL
, **pptr
= &parents
;
1025 struct stat statbuf
;
1026 int allow_fast_forward
= 1;
1029 wt_status_prepare(&s
);
1030 git_config(git_commit_config
, &s
);
1032 if (s
.use_color
== -1)
1033 s
.use_color
= git_use_color_default
;
1035 argc
= parse_and_validate_options(argc
, argv
, builtin_commit_usage
,
1038 if (diff_use_color_default
== -1)
1039 diff_use_color_default
= git_use_color_default
;
1040 return dry_run_commit(argc
, argv
, prefix
, &s
);
1042 index_file
= prepare_index(argc
, argv
, prefix
, 0);
1044 /* Set up everything for writing the commit object. This includes
1045 running hooks, writing the trees, and interacting with the user. */
1046 if (!prepare_to_commit(index_file
, prefix
, &s
)) {
1047 rollback_index_files();
1051 /* Determine parents */
1052 if (initial_commit
) {
1053 reflog_msg
= "commit (initial)";
1055 struct commit_list
*c
;
1056 struct commit
*commit
;
1058 reflog_msg
= "commit (amend)";
1059 commit
= lookup_commit(head_sha1
);
1060 if (!commit
|| parse_commit(commit
))
1061 die("could not parse HEAD commit");
1063 for (c
= commit
->parents
; c
; c
= c
->next
)
1064 pptr
= &commit_list_insert(c
->item
, pptr
)->next
;
1065 } else if (in_merge
) {
1066 struct strbuf m
= STRBUF_INIT
;
1069 reflog_msg
= "commit (merge)";
1070 pptr
= &commit_list_insert(lookup_commit(head_sha1
), pptr
)->next
;
1071 fp
= fopen(git_path("MERGE_HEAD"), "r");
1073 die_errno("could not open '%s' for reading",
1074 git_path("MERGE_HEAD"));
1075 while (strbuf_getline(&m
, fp
, '\n') != EOF
) {
1076 unsigned char sha1
[20];
1077 if (get_sha1_hex(m
.buf
, sha1
) < 0)
1078 die("Corrupt MERGE_HEAD file (%s)", m
.buf
);
1079 pptr
= &commit_list_insert(lookup_commit(sha1
), pptr
)->next
;
1083 if (!stat(git_path("MERGE_MODE"), &statbuf
)) {
1084 if (strbuf_read_file(&sb
, git_path("MERGE_MODE"), 0) < 0)
1085 die_errno("could not read MERGE_MODE");
1086 if (!strcmp(sb
.buf
, "no-ff"))
1087 allow_fast_forward
= 0;
1089 if (allow_fast_forward
)
1090 parents
= reduce_heads(parents
);
1092 reflog_msg
= "commit";
1093 pptr
= &commit_list_insert(lookup_commit(head_sha1
), pptr
)->next
;
1096 /* Finally, get the commit message */
1098 if (strbuf_read_file(&sb
, git_path(commit_editmsg
), 0) < 0) {
1099 int saved_errno
= errno
;
1100 rollback_index_files();
1101 die("could not read commit message: %s", strerror(saved_errno
));
1104 /* Truncate the message just before the diff, if any. */
1106 p
= strstr(sb
.buf
, "\ndiff --git ");
1108 strbuf_setlen(&sb
, p
- sb
.buf
+ 1);
1111 if (cleanup_mode
!= CLEANUP_NONE
)
1112 stripspace(&sb
, cleanup_mode
== CLEANUP_ALL
);
1113 if (message_is_empty(&sb
)) {
1114 rollback_index_files();
1115 fprintf(stderr
, "Aborting commit due to empty commit message.\n");
1119 if (commit_tree(sb
.buf
, active_cache_tree
->sha1
, parents
, commit_sha1
,
1120 fmt_ident(author_name
, author_email
, author_date
,
1121 IDENT_ERROR_ON_NO_NAME
))) {
1122 rollback_index_files();
1123 die("failed to write commit object");
1126 ref_lock
= lock_any_ref_for_update("HEAD",
1127 initial_commit
? NULL
: head_sha1
,
1130 nl
= strchr(sb
.buf
, '\n');
1132 strbuf_setlen(&sb
, nl
+ 1 - sb
.buf
);
1134 strbuf_addch(&sb
, '\n');
1135 strbuf_insert(&sb
, 0, reflog_msg
, strlen(reflog_msg
));
1136 strbuf_insert(&sb
, strlen(reflog_msg
), ": ", 2);
1139 rollback_index_files();
1140 die("cannot lock HEAD ref");
1142 if (write_ref_sha1(ref_lock
, commit_sha1
, sb
.buf
) < 0) {
1143 rollback_index_files();
1144 die("cannot update HEAD ref");
1147 unlink(git_path("MERGE_HEAD"));
1148 unlink(git_path("MERGE_MSG"));
1149 unlink(git_path("MERGE_MODE"));
1150 unlink(git_path("SQUASH_MSG"));
1152 if (commit_index_files())
1153 die ("Repository has been updated, but unable to write\n"
1154 "new_index file. Check that disk is not full or quota is\n"
1155 "not exceeded, and then \"git reset HEAD\" to recover.");
1158 run_hook(get_index_file(), "post-commit", NULL
);
1160 print_summary(prefix
, commit_sha1
);