Skip to content

Commit 8b87cfd

Browse files
pcloudsgitster
authored andcommitted
wt-status: move strbuf into read_and_strip_branch()
The strbufs are placed outside read_and_strip_branch as a premature optimization: when it reads "refs/heads/foo" to strbuf and wants to return just "foo", it could do so without memory movement. In return the caller must not use the returned pointer after releasing strbufs, which own the buffers that contain the returned strings. It's a clumsy design. By moving strbufs into read_and_strip_branch(), the returned pointer always points to a malloc'd buffer or NULL. The pointer can be passed around and freed after use. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cdd76db commit 8b87cfd

File tree

2 files changed

+32
-37
lines changed

2 files changed

+32
-37
lines changed

wt-status.c

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -967,41 +967,41 @@ static void show_bisect_in_progress(struct wt_status *s,
967967
/*
968968
* Extract branch information from rebase/bisect
969969
*/
970-
static void read_and_strip_branch(struct strbuf *sb,
971-
const char **branch,
972-
const char *path)
970+
static char *read_and_strip_branch(const char *path)
973971
{
972+
struct strbuf sb = STRBUF_INIT;
974973
unsigned char sha1[20];
975974

976-
strbuf_reset(sb);
977-
if (strbuf_read_file(sb, git_path("%s", path), 0) <= 0)
978-
return;
975+
if (strbuf_read_file(&sb, git_path("%s", path), 0) <= 0)
976+
goto got_nothing;
979977

980-
while (sb->len && sb->buf[sb->len - 1] == '\n')
981-
strbuf_setlen(sb, sb->len - 1);
982-
if (!sb->len)
983-
return;
984-
if (!prefixcmp(sb->buf, "refs/heads/"))
985-
*branch = sb->buf + strlen("refs/heads/");
986-
else if (!prefixcmp(sb->buf, "refs/"))
987-
*branch = sb->buf;
988-
else if (!get_sha1_hex(sb->buf, sha1)) {
978+
while (&sb.len && sb.buf[sb.len - 1] == '\n')
979+
strbuf_setlen(&sb, sb.len - 1);
980+
if (!sb.len)
981+
goto got_nothing;
982+
if (!prefixcmp(sb.buf, "refs/heads/"))
983+
strbuf_remove(&sb,0, strlen("refs/heads/"));
984+
else if (!prefixcmp(sb.buf, "refs/"))
985+
;
986+
else if (!get_sha1_hex(sb.buf, sha1)) {
989987
const char *abbrev;
990988
abbrev = find_unique_abbrev(sha1, DEFAULT_ABBREV);
991-
strbuf_reset(sb);
992-
strbuf_addstr(sb, abbrev);
993-
*branch = sb->buf;
994-
} else if (!strcmp(sb->buf, "detached HEAD")) /* rebase */
995-
;
989+
strbuf_reset(&sb);
990+
strbuf_addstr(&sb, abbrev);
991+
} else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
992+
goto got_nothing;
996993
else /* bisect */
997-
*branch = sb->buf;
994+
;
995+
return strbuf_detach(&sb, NULL);
996+
997+
got_nothing:
998+
strbuf_release(&sb);
999+
return NULL;
9981000
}
9991001

10001002
static void wt_status_print_state(struct wt_status *s)
10011003
{
10021004
const char *state_color = color(WT_STATUS_HEADER, s);
1003-
struct strbuf branch = STRBUF_INIT;
1004-
struct strbuf onto = STRBUF_INIT;
10051005
struct wt_status_state state;
10061006
struct stat st;
10071007

@@ -1016,27 +1016,22 @@ static void wt_status_print_state(struct wt_status *s)
10161016
state.am_empty_patch = 1;
10171017
} else {
10181018
state.rebase_in_progress = 1;
1019-
read_and_strip_branch(&branch, &state.branch,
1020-
"rebase-apply/head-name");
1021-
read_and_strip_branch(&onto, &state.onto,
1022-
"rebase-apply/onto");
1019+
state.branch = read_and_strip_branch("rebase-apply/head-name");
1020+
state.onto = read_and_strip_branch("rebase-apply/onto");
10231021
}
10241022
} else if (!stat(git_path("rebase-merge"), &st)) {
10251023
if (!stat(git_path("rebase-merge/interactive"), &st))
10261024
state.rebase_interactive_in_progress = 1;
10271025
else
10281026
state.rebase_in_progress = 1;
1029-
read_and_strip_branch(&branch, &state.branch,
1030-
"rebase-merge/head-name");
1031-
read_and_strip_branch(&onto, &state.onto,
1032-
"rebase-merge/onto");
1027+
state.branch = read_and_strip_branch("rebase-merge/head-name");
1028+
state.onto = read_and_strip_branch("rebase-merge/onto");
10331029
} else if (!stat(git_path("CHERRY_PICK_HEAD"), &st)) {
10341030
state.cherry_pick_in_progress = 1;
10351031
}
10361032
if (!stat(git_path("BISECT_LOG"), &st)) {
10371033
state.bisect_in_progress = 1;
1038-
read_and_strip_branch(&branch, &state.branch,
1039-
"BISECT_START");
1034+
state.branch = read_and_strip_branch("BISECT_START");
10401035
}
10411036

10421037
if (state.merge_in_progress)
@@ -1049,8 +1044,8 @@ static void wt_status_print_state(struct wt_status *s)
10491044
show_cherry_pick_in_progress(s, &state, state_color);
10501045
if (state.bisect_in_progress)
10511046
show_bisect_in_progress(s, &state, state_color);
1052-
strbuf_release(&branch);
1053-
strbuf_release(&onto);
1047+
free(state.branch);
1048+
free(state.onto);
10541049
}
10551050

10561051
void wt_status_print(struct wt_status *s)

wt-status.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ struct wt_status_state {
7979
int rebase_interactive_in_progress;
8080
int cherry_pick_in_progress;
8181
int bisect_in_progress;
82-
const char *branch;
83-
const char *onto;
82+
char *branch;
83+
char *onto;
8484
};
8585

8686
void wt_status_prepare(struct wt_status *s);

0 commit comments

Comments
 (0)