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>
Nguyễn Thái Ngọc Duy committed
May 22, 2016 at 16:33 UTC
360af2dadaae70f29de2f21d4eb8ac38aefcc263
1 file changed
+7
-9
worktree.c
+7
-9
@@ -153,21 +153,19 @@ done:
153
154
static void mark_current_worktree(struct worktree **worktrees)
155
{
156
- struct strbuf git_dir = STRBUF_INIT;
157
- struct strbuf path = STRBUF_INIT;
156
+ char *git_dir = xstrdup(absolute_path(get_git_dir()));
157
int i;
158
160
- strbuf_addstr(&git_dir, absolute_path(get_git_dir()));
159
for (i = 0; worktrees[i]; i++) {
160
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;
165
break;
166
+ }
167
}
169
- strbuf_release(&git_dir);
170
- strbuf_release(&path);
168
+ free(git_dir);
169
}
170
171
struct worktree **get_worktrees(void)