Skip to content

Commit 05a59a0

Browse files
knittlgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d0b16c8 commit 05a59a0

File tree

3 files changed

+75
-6
lines changed

3 files changed

+75
-6
lines changed

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);
@@ -1030,6 +1032,8 @@ int cmd_status(int argc, const char **argv, const char *prefix)
10301032
OPT__VERBOSE(&verbose),
10311033
OPT_SET_INT('s', "short", &status_format,
10321034
"show status concisely", STATUS_FORMAT_SHORT),
1035+
OPT_BOOLEAN('b', "branch", &status_show_branch,
1036+
"show branch information"),
10331037
OPT_SET_INT(0, "porcelain", &status_format,
10341038
"show porcelain output format",
10351039
STATUS_FORMAT_PORCELAIN),
@@ -1082,7 +1086,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
10821086

10831087
switch (status_format) {
10841088
case STATUS_FORMAT_SHORT:
1085-
wt_shortstatus_print(&s, null_termination);
1089+
wt_shortstatus_print(&s, null_termination, status_show_branch);
10861090
break;
10871091
case STATUS_FORMAT_PORCELAIN:
10881092
wt_porcelain_print(&s, null_termination);

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)