@matheusbsbs / matheus-bagliano / commits / 76e2f7ce32
Fork 0

git stat: the beginning of "status that is not a dry-run of commit"

Tentatively add "git stat" as a new command. This is not "preview of commit with the same arguments"; the path parameters are not paths to be added to the pristine index (aka "--only" option), but are taken as pathspecs to limit the output. Later in 1.7.0 release, it will take over "git status". Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 7, 2009 at 23:31 UTC 76e2f7ce323c7ddb3a7c29e56407da6a69a4fa53
6 files changed +75 -14
Makefile
+1
@@ -378,6 +378,7 @@ BUILT_INS += git-init$X
378 BUILT_INS += git-merge-subtree$X
379 BUILT_INS += git-peek-remote$X
380 BUILT_INS += git-repo-config$X
381 +BUILT_INS += git-stat$X
382 BUILT_INS += git-show$X
383 BUILT_INS += git-stage$X
384 BUILT_INS += git-status$X
builtin-commit.c
+65 -10
@@ -24,6 +24,7 @@
24 #include "string-list.h"
25 #include "rerere.h"
26 #include "unpack-trees.h"
27 +#include "quote.h"
28
29 static const char * const builtin_commit_usage[] = {
30 "git commit [options] [--] <filepattern>...",
@@ -35,6 +36,11 @@ static const char * const builtin_status_usage[] = {
36 NULL
37 };
38
39 +static const char * const builtin_stat_usage[] = {
40 + "git stat [options]",
41 + NULL
42 +};
43 +
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";
@@ -346,6 +352,8 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
352 static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
353 struct wt_status *s)
354 {
355 + unsigned char sha1[20];
356 +
357 if (s->relative_paths)
358 s->prefix = prefix;
359
@@ -357,7 +365,9 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
365 s->index_file = index_file;
366 s->fp = fp;
367 s->nowarn = nowarn;
368 + s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
369
370 + wt_status_collect(s);
371 wt_status_print(s);
372
373 return s->commitable;
@@ -691,6 +701,21 @@ static const char *find_author_by_nickname(const char *name)
701 die("No existing author found with '%s'", name);
702 }
703
704 +
705 +static void handle_untracked_files_arg(struct wt_status *s)
706 +{
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;
715 + else
716 + die("Invalid untracked files mode '%s'", untracked_files_arg);
717 +}
718 +
719 static int parse_and_validate_options(int argc, const char *argv[],
720 const char * const usage[],
721 const char *prefix,
@@ -794,16 +819,7 @@ static int parse_and_validate_options(int argc, const char *argv[],
819 else
820 die("Invalid cleanup mode %s", cleanup_arg);
821
797 - if (!untracked_files_arg)
798 - ; /* default already initialized */
799 - else if (!strcmp(untracked_files_arg, "no"))
800 - s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
801 - else if (!strcmp(untracked_files_arg, "normal"))
802 - s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
803 - else if (!strcmp(untracked_files_arg, "all"))
804 - s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
805 - else
806 - die("Invalid untracked files mode '%s'", untracked_files_arg);
822 + handle_untracked_files_arg(s);
823
824 if (all && argc > 0)
825 die("Paths with -a does not make sense.");
@@ -886,6 +902,45 @@ static int git_status_config(const char *k, const char *v, void *cb)
902 return git_diff_ui_config(k, v, NULL);
903 }
904
905 +int cmd_stat(int argc, const char **argv, const char *prefix)
906 +{
907 + struct wt_status s;
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,
912 + "mode",
913 + "show untracked files, optional modes: all, normal, no. (Default: all)",
914 + PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
915 + OPT_END(),
916 + };
917 +
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);
924 +
925 + if (*argv)
926 + s.pathspec = get_pathspec(prefix, argv);
927 +
928 + read_cache();
929 + refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
930 + s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
931 + wt_status_collect(&s);
932 +
933 + s.verbose = verbose;
934 + if (s.relative_paths)
935 + s.prefix = prefix;
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;
940 + wt_status_print(&s);
941 + return 0;
942 +}
943 +
944 int cmd_status(int argc, const char **argv, const char *prefix)
945 {
946 struct wt_status s;
builtin.h
+1
@@ -95,6 +95,7 @@ extern int cmd_send_pack(int argc, const char **argv, const char *prefix);
95 extern int cmd_shortlog(int argc, const char **argv, const char *prefix);
96 extern int cmd_show(int argc, const char **argv, const char *prefix);
97 extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
98 +extern int cmd_stat(int argc, const char **argv, const char *prefix);
99 extern int cmd_status(int argc, const char **argv, const char *prefix);
100 extern int cmd_stripspace(int argc, const char **argv, const char *prefix);
101 extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix);
git.c
+1
@@ -350,6 +350,7 @@ static void handle_internal_command(int argc, const char **argv)
350 { "shortlog", cmd_shortlog, USE_PAGER },
351 { "show-branch", cmd_show_branch, RUN_SETUP },
352 { "show", cmd_show, RUN_SETUP | USE_PAGER },
353 + { "stat", cmd_stat, RUN_SETUP | NEED_WORK_TREE },
354 { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
355 { "stripspace", cmd_stripspace },
356 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
wt-status.c
+6 -4
@@ -269,6 +269,7 @@ static void wt_status_collect_changes_worktree(struct wt_status *s)
269 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
270 rev.diffopt.format_callback = wt_status_collect_changed_cb;
271 rev.diffopt.format_callback_data = s;
272 + rev.prune_data = s->pathspec;
273 run_diff_files(&rev, 0);
274 }
275
@@ -285,6 +286,7 @@ static void wt_status_collect_changes_index(struct wt_status *s)
286 rev.diffopt.detect_rename = 1;
287 rev.diffopt.rename_limit = 200;
288 rev.diffopt.break_opt = 0;
289 + rev.prune_data = s->pathspec;
290 run_diff_index(&rev, 1);
291 }
292
@@ -297,6 +299,8 @@ static void wt_status_collect_changes_initial(struct wt_status *s)
299 struct wt_status_change_data *d;
300 struct cache_entry *ce = active_cache[i];
301
302 + if (!ce_path_match(ce, s->pathspec))
303 + continue;
304 it = string_list_insert(ce->name, &s->change);
305 d = it->util;
306 if (!d) {
@@ -330,6 +334,8 @@ static void wt_status_collect_untracked(struct wt_status *s)
334 struct dir_entry *ent = dir.entries[i];
335 if (!cache_name_is_other(ent->name, ent->len))
336 continue;
337 + if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
338 + continue;
339 s->workdir_untracked = 1;
340 string_list_insert(ent->name, &s->untracked);
341 }
@@ -533,10 +539,8 @@ static void wt_status_print_tracking(struct wt_status *s)
539
540 void wt_status_print(struct wt_status *s)
541 {
536 - unsigned char sha1[20];
542 const char *branch_color = color(WT_STATUS_HEADER, s);
543
539 - s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
544 if (s->branch) {
545 const char *on_what = "On branch ";
546 const char *branch_name = s->branch;
@@ -553,8 +557,6 @@ void wt_status_print(struct wt_status *s)
557 wt_status_print_tracking(s);
558 }
559
556 - wt_status_collect(s);
557 -
560 if (s->is_initial) {
561 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
562 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "# Initial commit");
wt-status.h
+1
@@ -31,6 +31,7 @@ struct wt_status {
31 int is_initial;
32 char *branch;
33 const char *reference;
34 + const char **pathspec;
35 int verbose;
36 int amend;
37 int nowarn;