Skip to content

Commit 880bd9d

Browse files
committed
Merge branch 'mg/status-b'
* mg/status-b: Documentation+t5708: document and test status -s -b Show branch information in short output of git status
2 parents 7c1b228 + 46077fa commit 880bd9d

File tree

5 files changed

+130
-8
lines changed

5 files changed

+130
-8
lines changed

Documentation/git-status.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ OPTIONS
2727
--short::
2828
Give the output in the short-format.
2929

30+
-b::
31+
--branch::
32+
Show the branch and tracking info even in short-format.
33+
3034
--porcelain::
3135
Give the output in a stable, easy-to-parse format for scripts.
3236
Currently this is identical to --short output, but is guaranteed
@@ -120,6 +124,10 @@ Ignored files are not listed.
120124
? ? untracked
121125
-------------------------------------------------
122126

127+
If -b is used the short-format status is preceded by a line
128+
129+
## branchname tracking info
130+
123131
There is an alternate -z format recommended for machine parsing. In
124132
that format, the status field is the same, but some other things
125133
change. First, the '->' is omitted from rename entries and the field
@@ -128,7 +136,7 @@ order is reversed (e.g 'from -> to' becomes 'to from'). Second, a NUL
128136
and the terminating newline (but a space still separates the status
129137
field from the first filename). Third, filenames containing special
130138
characters are not specially formatted; no quoting or
131-
backslash-escaping is performed.
139+
backslash-escaping is performed. Fourth, there is no branch line.
132140

133141
CONFIGURATION
134142
-------------

builtin/commit.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ static enum {
9393
STATUS_FORMAT_SHORT,
9494
STATUS_FORMAT_PORCELAIN,
9595
} status_format = STATUS_FORMAT_LONG;
96+
static int status_show_branch;
9697

9798
static int opt_parse_m(const struct option *opt, const char *arg, int unset)
9899
{
@@ -134,6 +135,7 @@ static struct option builtin_commit_options[] = {
134135
OPT_BOOLEAN(0, "dry-run", &dry_run, "show what would be committed"),
135136
OPT_SET_INT(0, "short", &status_format, "show status concisely",
136137
STATUS_FORMAT_SHORT),
138+
OPT_BOOLEAN(0, "branch", &status_show_branch, "show branch information"),
137139
OPT_SET_INT(0, "porcelain", &status_format,
138140
"show porcelain output format", STATUS_FORMAT_PORCELAIN),
139141
OPT_BOOLEAN('z', "null", &null_termination,
@@ -424,7 +426,7 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
424426

425427
switch (status_format) {
426428
case STATUS_FORMAT_SHORT:
427-
wt_shortstatus_print(s, null_termination);
429+
wt_shortstatus_print(s, null_termination, status_show_branch);
428430
break;
429431
case STATUS_FORMAT_PORCELAIN:
430432
wt_porcelain_print(s, null_termination);
@@ -1036,6 +1038,8 @@ int cmd_status(int argc, const char **argv, const char *prefix)
10361038
OPT__VERBOSE(&verbose),
10371039
OPT_SET_INT('s', "short", &status_format,
10381040
"show status concisely", STATUS_FORMAT_SHORT),
1041+
OPT_BOOLEAN('b', "branch", &status_show_branch,
1042+
"show branch information"),
10391043
OPT_SET_INT(0, "porcelain", &status_format,
10401044
"show porcelain output format",
10411045
STATUS_FORMAT_PORCELAIN),
@@ -1088,7 +1092,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
10881092

10891093
switch (status_format) {
10901094
case STATUS_FORMAT_SHORT:
1091-
wt_shortstatus_print(&s, null_termination);
1095+
wt_shortstatus_print(&s, null_termination, status_show_branch);
10921096
break;
10931097
case STATUS_FORMAT_PORCELAIN:
10941098
wt_porcelain_print(&s, null_termination);

t/t7508-status.sh

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,32 @@ A dir2/added
107107
?? untracked
108108
EOF
109109

110-
test_expect_success 'status -s (2)' '
110+
test_expect_success 'status -s' '
111111
112112
git status -s >output &&
113113
test_cmp expect output
114114
115115
'
116116

117+
cat >expect <<\EOF
118+
## master
119+
M dir1/modified
120+
A dir2/added
121+
?? dir1/untracked
122+
?? dir2/modified
123+
?? dir2/untracked
124+
?? expect
125+
?? output
126+
?? untracked
127+
EOF
128+
129+
test_expect_success 'status -s -b' '
130+
131+
git status -s -b >output &&
132+
test_cmp expect output
133+
134+
'
135+
117136
cat >expect <<EOF
118137
# On branch master
119138
# Changes to be committed:
@@ -436,6 +455,25 @@ test_expect_success 'status -s with color.status' '
436455
437456
'
438457

458+
cat >expect <<\EOF
459+
## <GREEN>master<RESET>
460+
<RED>M<RESET> dir1/modified
461+
<GREEN>A<RESET> dir2/added
462+
<BLUE>??<RESET> dir1/untracked
463+
<BLUE>??<RESET> dir2/modified
464+
<BLUE>??<RESET> dir2/untracked
465+
<BLUE>??<RESET> expect
466+
<BLUE>??<RESET> output
467+
<BLUE>??<RESET> untracked
468+
EOF
469+
470+
test_expect_success 'status -s -b with color.status' '
471+
472+
git status -s -b | test_decode_color >output &&
473+
test_cmp expect output
474+
475+
'
476+
439477
cat >expect <<\EOF
440478
M dir1/modified
441479
A dir2/added
@@ -469,6 +507,13 @@ test_expect_success 'status --porcelain ignores color.status' '
469507
git config --unset color.status
470508
git config --unset color.ui
471509

510+
test_expect_success 'status --porcelain ignores -b' '
511+
512+
git status --porcelain -b >output &&
513+
test_cmp expect output
514+
515+
'
516+
472517
cat >expect <<\EOF
473518
# On branch master
474519
# Changes to be committed:

wt-status.c

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "quote.h"
1010
#include "run-command.h"
1111
#include "remote.h"
12+
#include "refs.h"
1213

1314
static char default_wt_status_colors[][COLOR_MAXLEN] = {
1415
GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
@@ -17,6 +18,8 @@ static char default_wt_status_colors[][COLOR_MAXLEN] = {
1718
GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
1819
GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
1920
GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
21+
GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
22+
GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
2023
};
2124

2225
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 *
756759
}
757760
}
758761

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)
760819
{
761820
int i;
821+
822+
if (show_branch)
823+
wt_shortstatus_print_tracking(s);
824+
762825
for (i = 0; i < s->change.nr; i++) {
763826
struct wt_status_change_data *d;
764827
struct string_list_item *it;
@@ -789,5 +852,5 @@ void wt_porcelain_print(struct wt_status *s, int null_termination)
789852
s->use_color = 0;
790853
s->relative_paths = 0;
791854
s->prefix = NULL;
792-
wt_shortstatus_print(s, null_termination);
855+
wt_shortstatus_print(s, null_termination, 0);
793856
}

wt-status.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ enum color_wt_status {
1212
WT_STATUS_UNTRACKED,
1313
WT_STATUS_NOBRANCH,
1414
WT_STATUS_UNMERGED,
15+
WT_STATUS_LOCAL_BRANCH,
16+
WT_STATUS_REMOTE_BRANCH,
1517
};
1618

1719
enum untracked_status_type {
@@ -43,7 +45,7 @@ struct wt_status {
4345
int submodule_summary;
4446
int show_ignored_files;
4547
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];
4749

4850
/* These are computed during processing of the individual sections */
4951
int commitable;
@@ -60,7 +62,7 @@ void wt_status_prepare(struct wt_status *s);
6062
void wt_status_print(struct wt_status *s);
6163
void wt_status_collect(struct wt_status *s);
6264

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);
6466
void wt_porcelain_print(struct wt_status *s, int null_termination);
6567

6668
#endif /* STATUS_H */

0 commit comments

Comments
 (0)