Show branch information in short output of git status
This patch adds a first line in the output of `git status -s` when given the option `-b` or `--branch`, showing which branch the user is currently on, and in case of tracking branches the number of commits on each branch. Signed-off-by: Daniel Knittl-Frank <knittl89+git@googlemail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Daniel Knittl-Frank committed
May 25, 2010 at 15:45 UTC
05a59a087c29c0b5dd267ec0bd488829427cc3d7
3 files changed
+75
-6
builtin/commit.c
+6
-2
@@ -93,6 +93,7 @@ static enum {
93
STATUS_FORMAT_SHORT,
94
STATUS_FORMAT_PORCELAIN,
95
} status_format = STATUS_FORMAT_LONG;
96
+static int status_show_branch;
97
98
static int opt_parse_m(const struct option *opt, const char *arg, int unset)
99
{
@@ -134,6 +135,7 @@ static struct option builtin_commit_options[] = {
135
OPT_BOOLEAN(0, "dry-run", &dry_run, "show what would be committed"),
136
OPT_SET_INT(0, "short", &status_format, "show status concisely",
137
STATUS_FORMAT_SHORT),
138
+ OPT_BOOLEAN(0, "branch", &status_show_branch, "show branch information"),
139
OPT_SET_INT(0, "porcelain", &status_format,
140
"show porcelain output format", STATUS_FORMAT_PORCELAIN),
141
OPT_BOOLEAN('z', "null", &null_termination,
@@ -424,7 +426,7 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
426
427
switch (status_format) {
428
case STATUS_FORMAT_SHORT:
427
- wt_shortstatus_print(s, null_termination);
429
+ wt_shortstatus_print(s, null_termination, status_show_branch);
430
break;
431
case STATUS_FORMAT_PORCELAIN:
432
wt_porcelain_print(s, null_termination);
@@ -1030,6 +1032,8 @@ int cmd_status(int argc, const char **argv, const char *prefix)
1032
OPT__VERBOSE(&verbose),
1033
OPT_SET_INT('s', "short", &status_format,
1034
"show status concisely", STATUS_FORMAT_SHORT),
1035
+ OPT_BOOLEAN('b', "branch", &status_show_branch,
1036
+ "show branch information"),
1037
OPT_SET_INT(0, "porcelain", &status_format,
1038
"show porcelain output format",
1039
STATUS_FORMAT_PORCELAIN),
@@ -1082,7 +1086,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
1086
1087
switch (status_format) {
1088
case STATUS_FORMAT_SHORT:
1085
- wt_shortstatus_print(&s, null_termination);
1089
+ wt_shortstatus_print(&s, null_termination, status_show_branch);
1090
break;
1091
case STATUS_FORMAT_PORCELAIN:
1092
wt_porcelain_print(&s, null_termination);
wt-status.c
+65
-2
@@ -9,6 +9,7 @@
9
#include "quote.h"
10
#include "run-command.h"
11
#include "remote.h"
12
+#include "refs.h"
13
14
static char default_wt_status_colors[][COLOR_MAXLEN] = {
15
GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
@@ -17,6 +18,8 @@ static char default_wt_status_colors[][COLOR_MAXLEN] = {
18
GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
19
GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
20
GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
21
+ GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
22
+ GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
23
};
24
25
static const char *color(int slot, struct wt_status *s)
@@ -756,9 +759,69 @@ static void wt_shortstatus_other(int null_termination, struct string_list_item *
759
}
760
}
761
759
-void wt_shortstatus_print(struct wt_status *s, int null_termination)
762
+static void wt_shortstatus_print_tracking(struct wt_status *s)
763
+{
764
+ struct branch *branch;
765
+ const char *header_color = color(WT_STATUS_HEADER, s);
766
+ const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
767
+ const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
768
+
769
+ const char *base;
770
+ const char *branch_name;
771
+ int num_ours, num_theirs;
772
+
773
+ color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
774
+
775
+ if (!s->branch)
776
+ return;
777
+ branch_name = s->branch;
778
+
779
+ if (!prefixcmp(branch_name, "refs/heads/"))
780
+ branch_name += 11;
781
+ else if (!strcmp(branch_name, "HEAD")) {
782
+ branch_name = "HEAD (no branch)";
783
+ branch_color_local = color(WT_STATUS_NOBRANCH, s);
784
+ }
785
+
786
+ branch = branch_get(s->branch + 11);
787
+ if (s->is_initial)
788
+ color_fprintf(s->fp, header_color, "Initial commit on ");
789
+ if (!stat_tracking_info(branch, &num_ours, &num_theirs)) {
790
+ color_fprintf_ln(s->fp, branch_color_local,
791
+ "%s", branch_name);
792
+ return;
793
+ }
794
+
795
+ base = branch->merge[0]->dst;
796
+ base = shorten_unambiguous_ref(base, 0);
797
+ color_fprintf(s->fp, branch_color_local, "%s", branch_name);
798
+ color_fprintf(s->fp, header_color, "...");
799
+ color_fprintf(s->fp, branch_color_remote, "%s", base);
800
+
801
+ color_fprintf(s->fp, header_color, " [");
802
+ if (!num_ours) {
803
+ color_fprintf(s->fp, header_color, "behind ");
804
+ color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
805
+ } else if (!num_theirs) {
806
+ color_fprintf(s->fp, header_color, "ahead ");
807
+ color_fprintf(s->fp, branch_color_local, "%d", num_ours);
808
+ } else {
809
+ color_fprintf(s->fp, header_color, "ahead ");
810
+ color_fprintf(s->fp, branch_color_local, "%d", num_ours);
811
+ color_fprintf(s->fp, header_color, ", behind ");
812
+ color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
813
+ }
814
+
815
+ color_fprintf_ln(s->fp, header_color, "]");
816
+}
817
+
818
+void wt_shortstatus_print(struct wt_status *s, int null_termination, int show_branch)
819
{
820
int i;
821
+
822
+ if (show_branch)
823
+ wt_shortstatus_print_tracking(s);
824
+
825
for (i = 0; i < s->change.nr; i++) {
826
struct wt_status_change_data *d;
827
struct string_list_item *it;
@@ -789,5 +852,5 @@ void wt_porcelain_print(struct wt_status *s, int null_termination)
852
s->use_color = 0;
853
s->relative_paths = 0;
854
s->prefix = NULL;
792
- wt_shortstatus_print(s, null_termination);
855
+ wt_shortstatus_print(s, null_termination, 0);
856
}
wt-status.h
+4
-2
@@ -12,6 +12,8 @@ enum color_wt_status {
12
WT_STATUS_UNTRACKED,
13
WT_STATUS_NOBRANCH,
14
WT_STATUS_UNMERGED,
15
+ WT_STATUS_LOCAL_BRANCH,
16
+ WT_STATUS_REMOTE_BRANCH,
17
};
18
19
enum untracked_status_type {
@@ -43,7 +45,7 @@ struct wt_status {
45
int submodule_summary;
46
int show_ignored_files;
47
enum untracked_status_type show_untracked_files;
46
- char color_palette[WT_STATUS_UNMERGED+1][COLOR_MAXLEN];
48
+ char color_palette[WT_STATUS_REMOTE_BRANCH+1][COLOR_MAXLEN];
49
50
/* These are computed during processing of the individual sections */
51
int commitable;
@@ -60,7 +62,7 @@ void wt_status_prepare(struct wt_status *s);
62
void wt_status_print(struct wt_status *s);
63
void wt_status_collect(struct wt_status *s);
64
63
-void wt_shortstatus_print(struct wt_status *s, int null_termination);
65
+void wt_shortstatus_print(struct wt_status *s, int null_termination, int show_branch);
66
void wt_porcelain_print(struct wt_status *s, int null_termination);
67
68
#endif /* STATUS_H */