@matheusbsbs / matheus-bagliano / commits / 173e6c8852
Fork 0

git stat -s: short status output

Give -s(hort) option to "git stat" that shows the status of paths in a more concise way. XY PATH1 -> PATH2 format to be more machine readable than output from "git status", which is about previewing of "git commit" with the same arguments. PATH1 is the path in the HEAD, and " -> PATH2" part is shown only when PATH1 corresponds to a different path in the index/worktree. For unmerged entries, X shows the status of stage #2 (i.e. ours) and Y shows the status of stage #3 (i.e. theirs). For entries that do not have conflicts, X shows the status of the index, and Y shows the status of the work tree. For untracked paths, XY are "??". X Y Meaning ------------------------------------------------- [MD] not updated M [ MD] updated in index A [ MD] added to index D [ MD] deleted from index R [ MD] renamed in index C [ MD] copied in index [MARC] index and work tree matches [ MARC] M work tree changed since index [ MARC] D deleted in work tree D D unmerged, both deleted A U unmerged, added by us U D unmerged, deleted by them U A unmerged, added by them D U unmerged, deleted by us A A unmerged, both added U U unmerged, both modified ? ? untracked When given -z option, the records are terminated by NUL characters for better machine readability. Because the traditional long format is designed for human consumption, NUL termination does not make sense. For this reason, -z option implies -s (short output). Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 4, 2009 at 23:55 UTC 173e6c8852be3c543689f8613ddf3fdafd9ca7b9
1 file changed +106 -8
builtin-commit.c
+106 -8
@@ -902,12 +902,87 @@ 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 +#define quote_path quote_path_relative
906 +
907 +static void short_unmerged(int null_termination, struct string_list_item *it,
908 + struct wt_status *s)
909 +{
910 + struct wt_status_change_data *d = it->util;
911 + const char *how = "??";
912 +
913 + switch (d->stagemask) {
914 + case 1: how = "DD"; break; /* both deleted */
915 + case 2: how = "AU"; break; /* added by us */
916 + case 3: how = "UD"; break; /* deleted by them */
917 + case 4: how = "UA"; break; /* added by them */
918 + case 5: how = "DU"; break; /* deleted by us */
919 + case 6: how = "AA"; break; /* both added */
920 + case 7: how = "UU"; break; /* both modified */
921 + }
922 + printf("%s ", how);
923 + if (null_termination) {
924 + fprintf(stdout, "%s%c", it->string, 0);
925 + } else {
926 + struct strbuf onebuf = STRBUF_INIT;
927 + const char *one;
928 + one = quote_path(it->string, -1, &onebuf, s->prefix);
929 + printf("%s\n", one);
930 + strbuf_release(&onebuf);
931 + }
932 +}
933 +
934 +static void short_status(int null_termination, struct string_list_item *it,
935 + struct wt_status *s)
936 +{
937 + struct wt_status_change_data *d = it->util;
938 +
939 + printf("%c%c ",
940 + !d->index_status ? ' ' : d->index_status,
941 + !d->worktree_status ? ' ' : d->worktree_status);
942 + if (null_termination) {
943 + fprintf(stdout, "%s%c", it->string, 0);
944 + if (d->head_path)
945 + fprintf(stdout, "%s%c", d->head_path, 0);
946 + } else {
947 + struct strbuf onebuf = STRBUF_INIT;
948 + const char *one;
949 + if (d->head_path) {
950 + one = quote_path(d->head_path, -1, &onebuf, s->prefix);
951 + printf("%s -> ", one);
952 + strbuf_release(&onebuf);
953 + }
954 + one = quote_path(it->string, -1, &onebuf, s->prefix);
955 + printf("%s\n", one);
956 + strbuf_release(&onebuf);
957 + }
958 +}
959 +
960 +static void short_untracked(int null_termination, struct string_list_item *it,
961 + struct wt_status *s)
962 +{
963 + if (null_termination) {
964 + fprintf(stdout, "?? %s%c", it->string, 0);
965 + } else {
966 + struct strbuf onebuf = STRBUF_INIT;
967 + const char *one;
968 + one = quote_path(it->string, -1, &onebuf, s->prefix);
969 + printf("?? %s\n", one);
970 + strbuf_release(&onebuf);
971 + }
972 +}
973 +
974 int cmd_stat(int argc, const char **argv, const char *prefix)
975 {
976 struct wt_status s;
977 + static int null_termination, shortstatus;
978 + int i;
979 unsigned char sha1[20];
980 static struct option builtin_stat_options[] = {
981 OPT__VERBOSE(&verbose),
982 + OPT_BOOLEAN('s', "short", &shortstatus,
983 + "show status concicely"),
984 + OPT_BOOLEAN('z', "null", &null_termination,
985 + "terminate entries with NUL"),
986 { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
987 "mode",
988 "show untracked files, optional modes: all, normal, no. (Default: all)",
@@ -915,6 +990,9 @@ int cmd_stat(int argc, const char **argv, const char *prefix)
990 OPT_END(),
991 };
992
993 + if (null_termination)
994 + shortstatus = 1;
995 +
996 wt_status_prepare(&s);
997 git_config(git_status_config, &s);
998 argc = parse_options(argc, argv, prefix,
@@ -930,14 +1008,34 @@ int cmd_stat(int argc, const char **argv, const char *prefix)
1008 s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
1009 wt_status_collect(&s);
1010
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);
1011 + if (shortstatus) {
1012 + for (i = 0; i < s.change.nr; i++) {
1013 + struct wt_status_change_data *d;
1014 + struct string_list_item *it;
1015 +
1016 + it = &(s.change.items[i]);
1017 + d = it->util;
1018 + if (d->stagemask)
1019 + short_unmerged(null_termination, it, &s);
1020 + else
1021 + short_status(null_termination, it, &s);
1022 + }
1023 + for (i = 0; i < s.untracked.nr; i++) {
1024 + struct string_list_item *it;
1025 +
1026 + it = &(s.untracked.items[i]);
1027 + short_untracked(null_termination, it, &s);
1028 + }
1029 + } else {
1030 + s.verbose = verbose;
1031 + if (s.relative_paths)
1032 + s.prefix = prefix;
1033 + if (s.use_color == -1)
1034 + s.use_color = git_use_color_default;
1035 + if (diff_use_color_default == -1)
1036 + diff_use_color_default = git_use_color_default;
1037 + wt_status_print(&s);
1038 + }
1039 return 0;
1040 }
1041