{"thread":{"id":"23900","subject":"[PATCH] Show branch information in short output of git status","startedAt":"2010-05-25T13:45:51Z","lastAt":"2010-05-25T14:52:03Z","messageCount":3,"participants":["Michael J Gruber"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"142280","messageId":"284d7af64fec2dc0cabdb5cf29ab3fd34fad79f0.1274794889.git.git@drmicha.warpmail.net","threadId":"23900","inReplyTo":"AANLkTinNYcuiyRpgMGpQAEaStj2MDg9UooozooLPwv_0@mail.gmail.com","subject":"[PATCH] Show branch information in short output of git status","fromName":"Michael J Gruber","fromEmail":"git@drmicha.warpmail.net","sentAt":"2010-05-25T13:45:51Z","receivedAt":"2010-05-25T13:45:51Z","isPatch":true,"sender":{"key":"git@grubix.eu","avatar":"https://avatars.githubusercontent.com/u/233215?v=4"},"body":"From: Daniel Knittl-Frank <knittl89+git@googlemail.com>\n\nThis patch adds a first line in the output of `git status -s` when given\nthe option `-b` or `--branch`, showing which branch the user is\ncurrently on, and in case of tracking branches the number of commits on\neach branch.\n\nSigned-off-by: Daniel Knittl-Frank <knittl89+git@googlemail.com>\n---\nSent by the not-so-atomatic mob-robot...\nI noticed some white space issue but assume Junio fixes that automatically\nwhen applying.\n\n builtin/commit.c |    8 ++++-\n wt-status.c      |   68 ++++++++++++++++++++++++++++++++++++++++++++++++++++-\n wt-status.h      |    6 +++-\n 3 files changed, 76 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex ddf77e4..2328293 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -93,6 +93,7 @@ static enum {\n \tSTATUS_FORMAT_SHORT,\n \tSTATUS_FORMAT_PORCELAIN,\n } status_format = STATUS_FORMAT_LONG;\n+static int status_show_branch;\n \n static int opt_parse_m(const struct option *opt, const char *arg, int unset)\n {\n@@ -134,6 +135,7 @@ static struct option builtin_commit_options[] = {\n \tOPT_BOOLEAN(0, \"dry-run\", &dry_run, \"show what would be committed\"),\n \tOPT_SET_INT(0, \"short\", &status_format, \"show status concisely\",\n \t\t    STATUS_FORMAT_SHORT),\n+\tOPT_BOOLEAN(0, \"branch\", &status_show_branch, \"show branch information\"),\n \tOPT_SET_INT(0, \"porcelain\", &status_format,\n \t\t    \"show porcelain output format\", STATUS_FORMAT_PORCELAIN),\n \tOPT_BOOLEAN('z', \"null\", &null_termination,\n@@ -424,7 +426,7 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int\n \n \tswitch (status_format) {\n \tcase STATUS_FORMAT_SHORT:\n-\t\twt_shortstatus_print(s, null_termination);\n+\t\twt_shortstatus_print(s, null_termination, status_show_branch);\n \t\tbreak;\n \tcase STATUS_FORMAT_PORCELAIN:\n \t\twt_porcelain_print(s, null_termination);\n@@ -1030,6 +1032,8 @@ int cmd_status(int argc, const char **argv, const char *prefix)\n \t\tOPT__VERBOSE(&verbose),\n \t\tOPT_SET_INT('s', \"short\", &status_format,\n \t\t\t    \"show status concisely\", STATUS_FORMAT_SHORT),\n+\t\tOPT_BOOLEAN('b', \"branch\", &status_show_branch,\n+\t\t            \"show branch information\"),\n \t\tOPT_SET_INT(0, \"porcelain\", &status_format,\n \t\t\t    \"show porcelain output format\",\n \t\t\t    STATUS_FORMAT_PORCELAIN),\n@@ -1082,7 +1086,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)\n \n \tswitch (status_format) {\n \tcase STATUS_FORMAT_SHORT:\n-\t\twt_shortstatus_print(&s, null_termination);\n+\t\twt_shortstatus_print(&s, null_termination, status_show_branch);\n \t\tbreak;\n \tcase STATUS_FORMAT_PORCELAIN:\n \t\twt_porcelain_print(&s, null_termination);\ndiff --git a/wt-status.c b/wt-status.c\nindex 14e0acc..871315a 100644\n--- a/wt-status.c\n+++ b/wt-status.c\n@@ -9,6 +9,7 @@\n #include \"quote.h\"\n #include \"run-command.h\"\n #include \"remote.h\"\n+#include \"refs.h\"\n \n static char default_wt_status_colors[][COLOR_MAXLEN] = {\n \tGIT_COLOR_NORMAL, /* WT_STATUS_HEADER */\n@@ -17,6 +18,8 @@ static char default_wt_status_colors[][COLOR_MAXLEN] = {\n \tGIT_COLOR_RED,    /* WT_STATUS_UNTRACKED */\n \tGIT_COLOR_RED,    /* WT_STATUS_NOBRANCH */\n \tGIT_COLOR_RED,    /* WT_STATUS_UNMERGED */\n+\tGIT_COLOR_GREEN,  /* WT_STATUS_LOCAL_BRANCH */\n+\tGIT_COLOR_RED,    /* WT_STATUS_REMOTE_BRANCH */\n };\n \n static const char *color(int slot, struct wt_status *s)\n@@ -756,9 +759,70 @@ static void wt_shortstatus_other(int null_termination, struct string_list_item *\n \t}\n }\n \n-void wt_shortstatus_print(struct wt_status *s, int null_termination)\n+static void wt_shortstatus_print_tracking(struct wt_status *s)\n+{\n+\tstruct branch *branch;\n+\tconst char *header_color = color(WT_STATUS_HEADER, s);\n+\tconst char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);\n+\tconst char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);\n+\n+\tconst char *base;\n+\tconst char *branch_name;\n+\tint num_ours, num_theirs;\n+\n+\tcolor_fprintf(s->fp, color(WT_STATUS_HEADER, s), \"## \");\n+\n+\tif(!s->branch)\n+\t\treturn;\n+\tbranch_name = s->branch;\n+\n+\tif (!prefixcmp(branch_name, \"refs/heads/\"))\n+\t\tbranch_name += 11;\n+\telse if (!strcmp(branch_name, \"HEAD\")) {\n+\t\tbranch_name = \"HEAD (no branch)\";\n+\t\tbranch_color_local = color(WT_STATUS_NOBRANCH, s);\n+\t}\n+\n+\tbranch = branch_get(s->branch + 11);\n+\tif(s->is_initial) {\n+\t\tcolor_fprintf(s->fp, header_color, \"Initial commit on \");\n+\t}\n+\tif (!stat_tracking_info(branch, &num_ours, &num_theirs)) {\n+\t\tcolor_fprintf_ln(s->fp, branch_color_local,\n+\t\t\t\"%s\", branch_name);\n+\t\treturn;\n+\t}\n+\n+\tbase = branch->merge[0]->dst;\n+\tbase = shorten_unambiguous_ref(base, 0);\n+\tcolor_fprintf(s->fp, branch_color_local, \"%s\", branch_name);\n+\tcolor_fprintf(s->fp, header_color, \"...\");\n+\tcolor_fprintf(s->fp, branch_color_remote, \"%s\", base);\n+\n+\tcolor_fprintf(s->fp, header_color, \" [\");\n+\tif (!num_ours) {\n+\t\tcolor_fprintf(s->fp, header_color, \"behind \");\n+\t\tcolor_fprintf(s->fp, branch_color_remote, \"%d\", num_theirs);\n+\t} else if (!num_theirs) {\n+\t\tcolor_fprintf(s->fp, header_color, \"ahead \");\n+\t\tcolor_fprintf(s->fp, branch_color_local, \"%d\", num_ours);\n+\t} else {\n+\t\tcolor_fprintf(s->fp, header_color, \"ahead \");\n+\t\tcolor_fprintf(s->fp, branch_color_local, \"%d\", num_ours);\n+\t\tcolor_fprintf(s->fp, header_color, \", behind \");\n+\t\tcolor_fprintf(s->fp, branch_color_remote, \"%d\", num_theirs);\n+\t}\n+\n+\tcolor_fprintf_ln(s->fp, header_color, \"]\");\n+}\n+\n+void wt_shortstatus_print(struct wt_status *s, int null_termination, int show_branch)\n {\n \tint i;\n+\n+\tif(show_branch)\n+\t\twt_shortstatus_print_tracking(s);\n+\n \tfor (i = 0; i < s->change.nr; i++) {\n \t\tstruct wt_status_change_data *d;\n \t\tstruct string_list_item *it;\n@@ -789,5 +853,5 @@ void wt_porcelain_print(struct wt_status *s, int null_termination)\n \ts->use_color = 0;\n \ts->relative_paths = 0;\n \ts->prefix = NULL;\n-\twt_shortstatus_print(s, null_termination);\n+\twt_shortstatus_print(s, null_termination, 0);\n }\ndiff --git a/wt-status.h b/wt-status.h\nindex 1093e65..4f19045 100644\n--- a/wt-status.h\n+++ b/wt-status.h\n@@ -12,6 +12,8 @@ enum color_wt_status {\n \tWT_STATUS_UNTRACKED,\n \tWT_STATUS_NOBRANCH,\n \tWT_STATUS_UNMERGED,\n+\tWT_STATUS_LOCAL_BRANCH,\n+\tWT_STATUS_REMOTE_BRANCH,\n };\n \n enum untracked_status_type {\n@@ -43,7 +45,7 @@ struct wt_status {\n \tint submodule_summary;\n \tint show_ignored_files;\n \tenum untracked_status_type show_untracked_files;\n-\tchar color_palette[WT_STATUS_UNMERGED+1][COLOR_MAXLEN];\n+\tchar color_palette[WT_STATUS_REMOTE_BRANCH+1][COLOR_MAXLEN];\n \n \t/* These are computed during processing of the individual sections */\n \tint commitable;\n@@ -60,7 +62,7 @@ void wt_status_prepare(struct wt_status *s);\n void wt_status_print(struct wt_status *s);\n void wt_status_collect(struct wt_status *s);\n \n-void wt_shortstatus_print(struct wt_status *s, int null_termination);\n+void wt_shortstatus_print(struct wt_status *s, int null_termination, int show_branch);\n void wt_porcelain_print(struct wt_status *s, int null_termination);\n \n #endif /* STATUS_H */\n-- \n1.7.1.342.g1c280\n"},{"id":"142281","messageId":"4BFBD8A3.8040203@drmicha.warpmail.net","threadId":"23900","inReplyTo":"284d7af64fec2dc0cabdb5cf29ab3fd34fad79f0.1274794889.git.git@drmicha.warpmail.net","subject":"Re: [PATCH] Show branch information in short output of git status","fromName":"Michael J Gruber","fromEmail":"git@drmicha.warpmail.net","sentAt":"2010-05-25T14:03:15Z","receivedAt":"2010-05-25T14:03:15Z","isPatch":true,"sender":{"key":"git@grubix.eu","avatar":"https://avatars.githubusercontent.com/u/233215?v=4"},"body":"> From: Daniel Knittl-Frank <knittl89+git@googlemail.com>\n> \n> This patch adds a first line in the output of `git status -s` when given\n> the option `-b` or `--branch`, showing which branch the user is\n> currently on, and in case of tracking branches the number of commits on\n> each branch.\n> \n> Signed-off-by: Daniel Knittl-Frank <knittl89+git@googlemail.com>\n> ---\n> Sent by the not-so-atomatic mob-robot...\n\n...with the not so automagic spell-checker...\n\n> I noticed some white space issue but assume Junio fixes that automatically\n> when applying.\n> \n>  builtin/commit.c |    8 ++++-\n>  wt-status.c      |   68 ++++++++++++++++++++++++++++++++++++++++++++++++++++-\n>  wt-status.h      |    6 +++-\n>  3 files changed, 76 insertions(+), 6 deletions(-)\n\nI tried it and I like it! [Ordinary status could use the same colors for\ntracking info, they're helpful.]\n\nI also like the fact that --porcelain -b does what it should (unchanged\nby the patch).\n\nI'm wondering what \"git status -b\" should do. Currently, \"-b\" is ignored\nsilently. One may argue it's okay since ordinary status shows tracking\ninfo anyways.\n\nMichael\n"},{"id":"142283","messageId":"d007399dfec8e1346f6b3ba5d1f34469026fb90f.1274798738.git.git@drmicha.warpmail.net","threadId":"23900","inReplyTo":"4BFBD8A3.8040203@drmicha.warpmail.net","subject":"[PATCH] Documentation+t5708: document and test status -s -b","fromName":"Michael J Gruber","fromEmail":"git@drmicha.warpmail.net","sentAt":"2010-05-25T14:52:03Z","receivedAt":"2010-05-25T14:52:03Z","isPatch":true,"sender":{"key":"git@grubix.eu","avatar":"https://avatars.githubusercontent.com/u/233215?v=4"},"body":"Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net>\n---\nSince I liked -b so much...\nCheers!\n\n Documentation/git-status.txt |   12 +++++++++-\n t/t7508-status.sh            |   47 +++++++++++++++++++++++++++++++++++++++++-\n 2 files changed, 56 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/git-status.txt b/Documentation/git-status.txt\nindex 2d4bbfc..beb1c8a 100644\n--- a/Documentation/git-status.txt\n+++ b/Documentation/git-status.txt\n@@ -27,6 +27,10 @@ OPTIONS\n --short::\n \tGive the output in the short-format.\n \n+-b::\n+--branch::\n+\tShow the branch and tracking info even in short-format.\n+\n --porcelain::\n \tGive the output in a stable, easy-to-parse format for scripts.\n \tCurrently this is identical to --short output, but is guaranteed\n@@ -73,7 +77,7 @@ In short-format, the status of each path is shown as\n where `PATH1` is the path in the `HEAD`, and ` -> PATH2` part is\n shown only when `PATH1` corresponds to a different path in the\n index/worktree (i.e. the file is renamed). The 'XY' is a two-letter\n-status code.\n+status code. \n \n The fields (including the `->`) are separated from each other by a\n single space. If a filename contains whitespace or other nonprintable\n@@ -120,6 +124,10 @@ Ignored files are not listed.\n     ?           ?    untracked\n     -------------------------------------------------\n \n+If -b is used the short-format status is preceded by a line\n+\n+## branchname tracking info\n+\n There is an alternate -z format recommended for machine parsing.  In\n that format, the status field is the same, but some other things\n change.  First, the '->' is omitted from rename entries and the field\n@@ -128,7 +136,7 @@ order is reversed (e.g 'from -> to' becomes 'to from'). Second, a NUL\n and the terminating newline (but a space still separates the status\n field from the first filename).  Third, filenames containing special\n characters are not specially formatted; no quoting or\n-backslash-escaping is performed.\n+backslash-escaping is performed. Fourth, there is no branch line.\n \n CONFIGURATION\n -------------\ndiff --git a/t/t7508-status.sh b/t/t7508-status.sh\nindex 008d571..9e08107 100755\n--- a/t/t7508-status.sh\n+++ b/t/t7508-status.sh\n@@ -107,13 +107,32 @@ A  dir2/added\n ?? untracked\n EOF\n \n-test_expect_success 'status -s (2)' '\n+test_expect_success 'status -s' '\n \n \tgit status -s >output &&\n \ttest_cmp expect output\n \n '\n \n+cat >expect <<\\EOF\n+## master\n+ M dir1/modified\n+A  dir2/added\n+?? dir1/untracked\n+?? dir2/modified\n+?? dir2/untracked\n+?? expect\n+?? output\n+?? untracked\n+EOF\n+\n+test_expect_success 'status -s -b' '\n+\n+\tgit status -s -b >output &&\n+\ttest_cmp expect output\n+\n+'\n+\n cat >expect <<EOF\n # On branch master\n # Changes to be committed:\n@@ -437,6 +456,25 @@ test_expect_success 'status -s with color.status' '\n '\n \n cat >expect <<\\EOF\n+## <GREEN>master<RESET>\n+ <RED>M<RESET> dir1/modified\n+<GREEN>A<RESET>  dir2/added\n+<BLUE>??<RESET> dir1/untracked\n+<BLUE>??<RESET> dir2/modified\n+<BLUE>??<RESET> dir2/untracked\n+<BLUE>??<RESET> expect\n+<BLUE>??<RESET> output\n+<BLUE>??<RESET> untracked\n+EOF\n+\n+test_expect_success 'status -s -b with color.status' '\n+\n+\tgit status -s -b | test_decode_color >output &&\n+\ttest_cmp expect output\n+\n+'\n+\n+cat >expect <<\\EOF\n  M dir1/modified\n A  dir2/added\n ?? dir1/untracked\n@@ -469,6 +507,13 @@ test_expect_success 'status --porcelain ignores color.status' '\n git config --unset color.status\n git config --unset color.ui\n \n+test_expect_success 'status --porcelain ignores -b' '\n+\n+\tgit status --porcelain -b >output &&\n+\ttest_cmp expect output\n+\n+'\n+\n cat >expect <<\\EOF\n # On branch master\n # Changes to be committed:\n-- \n1.7.1.342.g1c280\n"}]}