Skip to content

Commit 360af2d

Browse files
pcloudsgitster
authored andcommitted
worktree.c: rewrite mark_current_worktree() to avoid strbuf
strbuf is a bit overkill for this function. What we need is to call absolute_path() twice and make sure the second call does not destroy the result of the first. One buffer allocation is enough. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Reviewed-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent b462c02 commit 360af2d

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

worktree.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,19 @@ static struct worktree *get_linked_worktree(const char *id)
153153

154154
static void mark_current_worktree(struct worktree **worktrees)
155155
{
156-
struct strbuf git_dir = STRBUF_INIT;
157-
struct strbuf path = STRBUF_INIT;
156+
char *git_dir = xstrdup(absolute_path(get_git_dir()));
158157
int i;
159158

160-
strbuf_addstr(&git_dir, absolute_path(get_git_dir()));
161159
for (i = 0; worktrees[i]; i++) {
162160
struct worktree *wt = worktrees[i];
163-
strbuf_addstr(&path, absolute_path(get_worktree_git_dir(wt)));
164-
wt->is_current = !fspathcmp(git_dir.buf, path.buf);
165-
strbuf_reset(&path);
166-
if (wt->is_current)
161+
const char *wt_git_dir = get_worktree_git_dir(wt);
162+
163+
if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) {
164+
wt->is_current = 1;
167165
break;
166+
}
168167
}
169-
strbuf_release(&git_dir);
170-
strbuf_release(&path);
168+
free(git_dir);
171169
}
172170

173171
struct worktree **get_worktrees(void)

0 commit comments

Comments
 (0)