Skip to content

Commit 73ba5d7

Browse files
Stephen P. Smithgitster
authored andcommitted
roll wt_status_state into wt_status and populate in the collect phase
Status variables were initialized in the collect phase and some variables were later freed in the print functions. A "struct wt_status" used to be sufficient for the output phase to work. It was designed to be filled in the collect phase and consumed in the output phase, but over time some fields were added and output phase started filling the fields. A "struct wt_status_state" that was used in other codepaths turned out to be useful in the "git status" output. This is not tied to "struct wt_status", so filling in the collect phase was not consistently followed. Move the status state structure variables into the status state structure and populate them in the collect functions. Create a new function to free the buffers that were being freed in the print function. Call this new function in commit.c where both the collect and print functions were being called. Based on a patch suggestion by Junio C Hamano. [1] [1] https://public-inbox.org/git/[email protected]/ Signed-off-by: Stephen P. Smith <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f3bd35f commit 73ba5d7

File tree

3 files changed

+82
-93
lines changed

3 files changed

+82
-93
lines changed

builtin/commit.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
506506

507507
wt_status_collect(s);
508508
wt_status_print(s);
509+
wt_status_collect_free_buffers(s);
509510

510511
return s->committable;
511512
}
@@ -1388,6 +1389,8 @@ int cmd_status(int argc, const char **argv, const char *prefix)
13881389
s.prefix = prefix;
13891390

13901391
wt_status_print(&s);
1392+
wt_status_collect_free_buffers(&s);
1393+
13911394
return 0;
13921395
}
13931396

wt-status.c

Lines changed: 59 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -744,21 +744,25 @@ static int has_unmerged(struct wt_status *s)
744744

745745
void wt_status_collect(struct wt_status *s)
746746
{
747-
struct wt_status_state state;
748747
wt_status_collect_changes_worktree(s);
749-
750748
if (s->is_initial)
751749
wt_status_collect_changes_initial(s);
752750
else
753751
wt_status_collect_changes_index(s);
754752
wt_status_collect_untracked(s);
755753

756-
memset(&state, 0, sizeof(state));
757-
wt_status_get_state(&state, s->branch && !strcmp(s->branch, "HEAD"));
758-
if (state.merge_in_progress && !has_unmerged(s))
754+
wt_status_get_state(&s->state, s->branch && !strcmp(s->branch, "HEAD"));
755+
if (s->state.merge_in_progress && !has_unmerged(s))
759756
s->committable = 1;
760757
}
761758

759+
void wt_status_collect_free_buffers(struct wt_status *s)
760+
{
761+
free(s->state.branch);
762+
free(s->state.onto);
763+
free(s->state.detached_from);
764+
}
765+
762766
static void wt_longstatus_print_unmerged(struct wt_status *s)
763767
{
764768
int shown_header = 0;
@@ -1087,8 +1091,7 @@ static void wt_longstatus_print_tracking(struct wt_status *s)
10871091
}
10881092

10891093
static void show_merge_in_progress(struct wt_status *s,
1090-
struct wt_status_state *state,
1091-
const char *color)
1094+
const char *color)
10921095
{
10931096
if (has_unmerged(s)) {
10941097
status_printf_ln(s, color, _("You have unmerged paths."));
@@ -1109,16 +1112,15 @@ static void show_merge_in_progress(struct wt_status *s,
11091112
}
11101113

11111114
static void show_am_in_progress(struct wt_status *s,
1112-
struct wt_status_state *state,
11131115
const char *color)
11141116
{
11151117
status_printf_ln(s, color,
11161118
_("You are in the middle of an am session."));
1117-
if (state->am_empty_patch)
1119+
if (s->state.am_empty_patch)
11181120
status_printf_ln(s, color,
11191121
_("The current patch is empty."));
11201122
if (s->hints) {
1121-
if (!state->am_empty_patch)
1123+
if (!s->state.am_empty_patch)
11221124
status_printf_ln(s, color,
11231125
_(" (fix conflicts and then run \"git am --continue\")"));
11241126
status_printf_ln(s, color,
@@ -1242,10 +1244,9 @@ static int read_rebase_todolist(const char *fname, struct string_list *lines)
12421244
}
12431245

12441246
static void show_rebase_information(struct wt_status *s,
1245-
struct wt_status_state *state,
1246-
const char *color)
1247+
const char *color)
12471248
{
1248-
if (state->rebase_interactive_in_progress) {
1249+
if (s->state.rebase_interactive_in_progress) {
12491250
int i;
12501251
int nr_lines_to_show = 2;
12511252

@@ -1296,28 +1297,26 @@ static void show_rebase_information(struct wt_status *s,
12961297
}
12971298

12981299
static void print_rebase_state(struct wt_status *s,
1299-
struct wt_status_state *state,
1300-
const char *color)
1300+
const char *color)
13011301
{
1302-
if (state->branch)
1302+
if (s->state.branch)
13031303
status_printf_ln(s, color,
13041304
_("You are currently rebasing branch '%s' on '%s'."),
1305-
state->branch,
1306-
state->onto);
1305+
s->state.branch,
1306+
s->state.onto);
13071307
else
13081308
status_printf_ln(s, color,
13091309
_("You are currently rebasing."));
13101310
}
13111311

13121312
static void show_rebase_in_progress(struct wt_status *s,
1313-
struct wt_status_state *state,
1314-
const char *color)
1313+
const char *color)
13151314
{
13161315
struct stat st;
13171316

1318-
show_rebase_information(s, state, color);
1317+
show_rebase_information(s, color);
13191318
if (has_unmerged(s)) {
1320-
print_rebase_state(s, state, color);
1319+
print_rebase_state(s, color);
13211320
if (s->hints) {
13221321
status_printf_ln(s, color,
13231322
_(" (fix conflicts and then run \"git rebase --continue\")"));
@@ -1326,29 +1325,30 @@ static void show_rebase_in_progress(struct wt_status *s,
13261325
status_printf_ln(s, color,
13271326
_(" (use \"git rebase --abort\" to check out the original branch)"));
13281327
}
1329-
} else if (state->rebase_in_progress || !stat(git_path_merge_msg(the_repository), &st)) {
1330-
print_rebase_state(s, state, color);
1328+
} else if (s->state.rebase_in_progress ||
1329+
!stat(git_path_merge_msg(the_repository), &st)) {
1330+
print_rebase_state(s, color);
13311331
if (s->hints)
13321332
status_printf_ln(s, color,
13331333
_(" (all conflicts fixed: run \"git rebase --continue\")"));
13341334
} else if (split_commit_in_progress(s)) {
1335-
if (state->branch)
1335+
if (s->state.branch)
13361336
status_printf_ln(s, color,
13371337
_("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1338-
state->branch,
1339-
state->onto);
1338+
s->state.branch,
1339+
s->state.onto);
13401340
else
13411341
status_printf_ln(s, color,
13421342
_("You are currently splitting a commit during a rebase."));
13431343
if (s->hints)
13441344
status_printf_ln(s, color,
13451345
_(" (Once your working directory is clean, run \"git rebase --continue\")"));
13461346
} else {
1347-
if (state->branch)
1347+
if (s->state.branch)
13481348
status_printf_ln(s, color,
13491349
_("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1350-
state->branch,
1351-
state->onto);
1350+
s->state.branch,
1351+
s->state.onto);
13521352
else
13531353
status_printf_ln(s, color,
13541354
_("You are currently editing a commit during a rebase."));
@@ -1363,11 +1363,10 @@ static void show_rebase_in_progress(struct wt_status *s,
13631363
}
13641364

13651365
static void show_cherry_pick_in_progress(struct wt_status *s,
1366-
struct wt_status_state *state,
1367-
const char *color)
1366+
const char *color)
13681367
{
13691368
status_printf_ln(s, color, _("You are currently cherry-picking commit %s."),
1370-
find_unique_abbrev(&state->cherry_pick_head_oid, DEFAULT_ABBREV));
1369+
find_unique_abbrev(&s->state.cherry_pick_head_oid, DEFAULT_ABBREV));
13711370
if (s->hints) {
13721371
if (has_unmerged(s))
13731372
status_printf_ln(s, color,
@@ -1382,11 +1381,10 @@ static void show_cherry_pick_in_progress(struct wt_status *s,
13821381
}
13831382

13841383
static void show_revert_in_progress(struct wt_status *s,
1385-
struct wt_status_state *state,
1386-
const char *color)
1384+
const char *color)
13871385
{
13881386
status_printf_ln(s, color, _("You are currently reverting commit %s."),
1389-
find_unique_abbrev(&state->revert_head_oid, DEFAULT_ABBREV));
1387+
find_unique_abbrev(&s->state.revert_head_oid, DEFAULT_ABBREV));
13901388
if (s->hints) {
13911389
if (has_unmerged(s))
13921390
status_printf_ln(s, color,
@@ -1401,13 +1399,12 @@ static void show_revert_in_progress(struct wt_status *s,
14011399
}
14021400

14031401
static void show_bisect_in_progress(struct wt_status *s,
1404-
struct wt_status_state *state,
1405-
const char *color)
1402+
const char *color)
14061403
{
1407-
if (state->branch)
1404+
if (s->state.branch)
14081405
status_printf_ln(s, color,
14091406
_("You are currently bisecting, started from branch '%s'."),
1410-
state->branch);
1407+
s->state.branch);
14111408
else
14121409
status_printf_ln(s, color,
14131410
_("You are currently bisecting."));
@@ -1581,48 +1578,45 @@ void wt_status_get_state(struct wt_status_state *state,
15811578
wt_status_get_detached_from(state);
15821579
}
15831580

1584-
static void wt_longstatus_print_state(struct wt_status *s,
1585-
struct wt_status_state *state)
1581+
static void wt_longstatus_print_state(struct wt_status *s)
15861582
{
15871583
const char *state_color = color(WT_STATUS_HEADER, s);
1584+
struct wt_status_state *state = &s->state;
1585+
15881586
if (state->merge_in_progress)
1589-
show_merge_in_progress(s, state, state_color);
1587+
show_merge_in_progress(s, state_color);
15901588
else if (state->am_in_progress)
1591-
show_am_in_progress(s, state, state_color);
1589+
show_am_in_progress(s, state_color);
15921590
else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
1593-
show_rebase_in_progress(s, state, state_color);
1591+
show_rebase_in_progress(s, state_color);
15941592
else if (state->cherry_pick_in_progress)
1595-
show_cherry_pick_in_progress(s, state, state_color);
1593+
show_cherry_pick_in_progress(s, state_color);
15961594
else if (state->revert_in_progress)
1597-
show_revert_in_progress(s, state, state_color);
1595+
show_revert_in_progress(s, state_color);
15981596
if (state->bisect_in_progress)
1599-
show_bisect_in_progress(s, state, state_color);
1597+
show_bisect_in_progress(s, state_color);
16001598
}
16011599

16021600
static void wt_longstatus_print(struct wt_status *s)
16031601
{
16041602
const char *branch_color = color(WT_STATUS_ONBRANCH, s);
16051603
const char *branch_status_color = color(WT_STATUS_HEADER, s);
1606-
struct wt_status_state state;
1607-
1608-
memset(&state, 0, sizeof(state));
1609-
wt_status_get_state(&state,
1610-
s->branch && !strcmp(s->branch, "HEAD"));
16111604

16121605
if (s->branch) {
16131606
const char *on_what = _("On branch ");
16141607
const char *branch_name = s->branch;
16151608
if (!strcmp(branch_name, "HEAD")) {
16161609
branch_status_color = color(WT_STATUS_NOBRANCH, s);
1617-
if (state.rebase_in_progress || state.rebase_interactive_in_progress) {
1618-
if (state.rebase_interactive_in_progress)
1610+
if (s->state.rebase_in_progress ||
1611+
s->state.rebase_interactive_in_progress) {
1612+
if (s->state.rebase_interactive_in_progress)
16191613
on_what = _("interactive rebase in progress; onto ");
16201614
else
16211615
on_what = _("rebase in progress; onto ");
1622-
branch_name = state.onto;
1623-
} else if (state.detached_from) {
1624-
branch_name = state.detached_from;
1625-
if (state.detached_at)
1616+
branch_name = s->state.onto;
1617+
} else if (s->state.detached_from) {
1618+
branch_name = s->state.detached_from;
1619+
if (s->state.detached_at)
16261620
on_what = _("HEAD detached at ");
16271621
else
16281622
on_what = _("HEAD detached from ");
@@ -1639,10 +1633,7 @@ static void wt_longstatus_print(struct wt_status *s)
16391633
wt_longstatus_print_tracking(s);
16401634
}
16411635

1642-
wt_longstatus_print_state(s, &state);
1643-
free(state.branch);
1644-
free(state.onto);
1645-
free(state.detached_from);
1636+
wt_longstatus_print_state(s);
16461637

16471638
if (s->is_initial) {
16481639
status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
@@ -1946,13 +1937,9 @@ static void wt_porcelain_v2_print_tracking(struct wt_status *s)
19461937
struct branch *branch;
19471938
const char *base;
19481939
const char *branch_name;
1949-
struct wt_status_state state;
19501940
int ab_info, nr_ahead, nr_behind;
19511941
char eol = s->null_termination ? '\0' : '\n';
19521942

1953-
memset(&state, 0, sizeof(state));
1954-
wt_status_get_state(&state, s->branch && !strcmp(s->branch, "HEAD"));
1955-
19561943
fprintf(s->fp, "# branch.oid %s%c",
19571944
(s->is_initial ? "(initial)" : sha1_to_hex(s->sha1_commit)),
19581945
eol);
@@ -1963,10 +1950,11 @@ static void wt_porcelain_v2_print_tracking(struct wt_status *s)
19631950
if (!strcmp(s->branch, "HEAD")) {
19641951
fprintf(s->fp, "# branch.head %s%c", "(detached)", eol);
19651952

1966-
if (state.rebase_in_progress || state.rebase_interactive_in_progress)
1967-
branch_name = state.onto;
1968-
else if (state.detached_from)
1969-
branch_name = state.detached_from;
1953+
if (s->state.rebase_in_progress ||
1954+
s->state.rebase_interactive_in_progress)
1955+
branch_name = s->state.onto;
1956+
else if (s->state.detached_from)
1957+
branch_name = s->state.detached_from;
19701958
else
19711959
branch_name = "";
19721960
} else {
@@ -2000,10 +1988,6 @@ static void wt_porcelain_v2_print_tracking(struct wt_status *s)
20001988
}
20011989
}
20021990
}
2003-
2004-
free(state.branch);
2005-
free(state.onto);
2006-
free(state.detached_from);
20071991
}
20081992

20091993
/*

wt-status.h

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ enum wt_status_format {
6464
STATUS_FORMAT_UNSPECIFIED
6565
};
6666

67+
struct wt_status_state {
68+
int merge_in_progress;
69+
int am_in_progress;
70+
int am_empty_patch;
71+
int rebase_in_progress;
72+
int rebase_interactive_in_progress;
73+
int cherry_pick_in_progress;
74+
int bisect_in_progress;
75+
int revert_in_progress;
76+
int detached_at;
77+
char *branch;
78+
char *onto;
79+
char *detached_from;
80+
struct object_id detached_oid;
81+
struct object_id revert_head_oid;
82+
struct object_id cherry_pick_head_oid;
83+
};
84+
6785
struct wt_status {
6886
int is_initial;
6987
char *branch;
@@ -93,6 +111,7 @@ struct wt_status {
93111
int rename_score;
94112
int rename_limit;
95113
enum wt_status_format status_format;
114+
struct wt_status_state state;
96115
unsigned char sha1_commit[GIT_MAX_RAWSZ]; /* when not Initial */
97116

98117
/* These are computed during processing of the individual sections */
@@ -107,29 +126,12 @@ struct wt_status {
107126
uint32_t untracked_in_ms;
108127
};
109128

110-
struct wt_status_state {
111-
int merge_in_progress;
112-
int am_in_progress;
113-
int am_empty_patch;
114-
int rebase_in_progress;
115-
int rebase_interactive_in_progress;
116-
int cherry_pick_in_progress;
117-
int bisect_in_progress;
118-
int revert_in_progress;
119-
int detached_at;
120-
char *branch;
121-
char *onto;
122-
char *detached_from;
123-
struct object_id detached_oid;
124-
struct object_id revert_head_oid;
125-
struct object_id cherry_pick_head_oid;
126-
};
127-
128129
size_t wt_status_locate_end(const char *s, size_t len);
129130
void wt_status_add_cut_line(FILE *fp);
130131
void wt_status_prepare(struct wt_status *s);
131132
void wt_status_print(struct wt_status *s);
132133
void wt_status_collect(struct wt_status *s);
134+
void wt_status_collect_free_buffers(struct wt_status *s);
133135
void wt_status_get_state(struct wt_status_state *state, int get_detached_from);
134136
int wt_status_check_rebase(const struct worktree *wt,
135137
struct wt_status_state *state);

0 commit comments

Comments
 (0)