Skip to content

Commit e67b2c5

Browse files
Samuel Lijingitster
authored andcommitted
commit: fix exit code for --short/--porcelain
In wt-status.c, the s->commitable bit is set only in the call tree of wt_longstatus_print(), which means that when there are changes to be committed or all merge conflicts have been resolved, --dry-run and --long return the correct exit code, but --short and --porcelain do not, even though they both imply --dry-run. Teach wt_status_collect() to set s->committable correctly so that --short and --porcelain return the correct exit code in the above described situations and mark the documenting tests as fixed. Also stop setting s->committable in wt_longstatus_print_updated() and show_merge_in_progress(), and const-ify wt_status_state in the method signatures in those callpaths. Signed-off-by: Samuel Lijin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d8ba74a commit e67b2c5

File tree

2 files changed

+55
-35
lines changed

2 files changed

+55
-35
lines changed

t/t7501-commit.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ test_expect_success '--dry-run with stuff to commit returns ok' '
9999
git commit -m next -a --dry-run
100100
'
101101

102-
test_expect_failure '--short with stuff to commit returns ok' '
102+
test_expect_success '--short with stuff to commit returns ok' '
103103
echo bongo bongo bongo >>file &&
104104
git commit -m next -a --short
105105
'
106106

107-
test_expect_failure '--porcelain with stuff to commit returns ok' '
107+
test_expect_success '--porcelain with stuff to commit returns ok' '
108108
echo bongo bongo bongo >>file &&
109109
git commit -m next -a --porcelain
110110
'
@@ -703,11 +703,11 @@ test_expect_success '--dry-run with conflicts fixed from a merge' '
703703
git commit --dry-run
704704
'
705705

706-
test_expect_failure '--short with conflicts fixed from a merge' '
706+
test_expect_success '--short with conflicts fixed from a merge' '
707707
git commit --short
708708
'
709709

710-
test_expect_failure '--porcelain with conflicts fixed from a merge' '
710+
test_expect_success '--porcelain with conflicts fixed from a merge' '
711711
git commit --porcelain
712712
'
713713

wt-status.c

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,39 @@ static void wt_status_collect_untracked(struct wt_status *s)
718718
s->untracked_in_ms = (getnanotime() - t_begin) / 1000000;
719719
}
720720

721+
static int has_unmerged(const struct wt_status *s)
722+
{
723+
int i;
724+
725+
for (i = 0; i < s->change.nr; i++) {
726+
struct wt_status_change_data *d;
727+
d = s->change.items[i].util;
728+
if (d->stagemask)
729+
return 1;
730+
}
731+
return 0;
732+
}
733+
734+
static void wt_status_mark_committable(
735+
struct wt_status *s, const struct wt_status_state *state)
736+
{
737+
int i;
738+
739+
if (state->merge_in_progress && !has_unmerged(s)) {
740+
s->committable = 1;
741+
return;
742+
}
743+
744+
for (i = 0; i < s->change.nr; i++) {
745+
struct wt_status_change_data *d = (s->change.items[i]).util;
746+
747+
if (d->index_status && d->index_status != DIFF_STATUS_UNMERGED) {
748+
s->committable = 1;
749+
return;
750+
}
751+
}
752+
}
753+
721754
void wt_status_collect(struct wt_status *s, const struct wt_status_state *state)
722755
{
723756
wt_status_collect_changes_worktree(s);
@@ -728,6 +761,8 @@ void wt_status_collect(struct wt_status *s, const struct wt_status_state *state)
728761
wt_status_collect_changes_index(s);
729762

730763
wt_status_collect_untracked(s);
764+
765+
wt_status_mark_committable(s, state);
731766
}
732767

733768
static void wt_longstatus_print_unmerged(const struct wt_status *s)
@@ -753,28 +788,28 @@ static void wt_longstatus_print_unmerged(const struct wt_status *s)
753788

754789
}
755790

756-
static void wt_longstatus_print_updated(struct wt_status *s)
791+
static void wt_longstatus_print_updated(const struct wt_status *s)
757792
{
758-
int shown_header = 0;
759793
int i;
760794

795+
if (!s->committable) {
796+
return;
797+
}
798+
799+
wt_longstatus_print_cached_header(s);
800+
761801
for (i = 0; i < s->change.nr; i++) {
762802
struct wt_status_change_data *d;
763803
struct string_list_item *it;
764804
it = &(s->change.items[i]);
765805
d = it->util;
766-
if (!d->index_status ||
767-
d->index_status == DIFF_STATUS_UNMERGED)
768-
continue;
769-
if (!shown_header) {
770-
wt_longstatus_print_cached_header(s);
771-
s->committable = 1;
772-
shown_header = 1;
806+
if (d->index_status &&
807+
d->index_status != DIFF_STATUS_UNMERGED) {
808+
wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
773809
}
774-
wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
775810
}
776-
if (shown_header)
777-
wt_longstatus_print_trailer(s);
811+
812+
wt_longstatus_print_trailer(s);
778813
}
779814

780815
/*
@@ -1056,21 +1091,7 @@ static void wt_longstatus_print_tracking(const struct wt_status *s)
10561091
strbuf_release(&sb);
10571092
}
10581093

1059-
static int has_unmerged(const struct wt_status *s)
1060-
{
1061-
int i;
1062-
1063-
for (i = 0; i < s->change.nr; i++) {
1064-
struct wt_status_change_data *d;
1065-
d = s->change.items[i].util;
1066-
if (d->stagemask)
1067-
return 1;
1068-
}
1069-
return 0;
1070-
}
1071-
1072-
static void show_merge_in_progress(struct wt_status *s,
1073-
const struct wt_status_state *state,
1094+
static void show_merge_in_progress(const struct wt_status *s,
10741095
const char *color)
10751096
{
10761097
if (has_unmerged(s)) {
@@ -1082,7 +1103,6 @@ static void show_merge_in_progress(struct wt_status *s,
10821103
_(" (use \"git merge --abort\" to abort the merge)"));
10831104
}
10841105
} else {
1085-
s-> committable = 1;
10861106
status_printf_ln(s, color,
10871107
_("All conflicts fixed but you are still merging."));
10881108
if (s->hints)
@@ -1576,12 +1596,12 @@ void wt_status_clear_state(struct wt_status_state *state)
15761596
free(state->detached_from);
15771597
}
15781598

1579-
static void wt_longstatus_print_state(struct wt_status *s,
1599+
static void wt_longstatus_print_state(const struct wt_status *s,
15801600
const struct wt_status_state *state)
15811601
{
15821602
const char *state_color = color(WT_STATUS_HEADER, s);
15831603
if (state->merge_in_progress)
1584-
show_merge_in_progress(s, state, state_color);
1604+
show_merge_in_progress(s, state_color);
15851605
else if (state->am_in_progress)
15861606
show_am_in_progress(s, state, state_color);
15871607
else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
@@ -1594,7 +1614,7 @@ static void wt_longstatus_print_state(struct wt_status *s,
15941614
show_bisect_in_progress(s, state, state_color);
15951615
}
15961616

1597-
static void wt_longstatus_print(struct wt_status *s, const struct wt_status_state *state)
1617+
static void wt_longstatus_print(const struct wt_status *s, const struct wt_status_state *state)
15981618
{
15991619
const char *branch_color = color(WT_STATUS_ONBRANCH, s);
16001620
const char *branch_status_color = color(WT_STATUS_HEADER, s);

0 commit comments

Comments
 (0)