mirror of
https://github.com/git/git.git
synced 2025-12-12 20:36:24 +01:00
worktree: return allocated string from get_worktree_git_dir()
The `get_worktree_git_dir()` function returns a string constant that
does not need to be free'd by the caller. This string is computed for
three different cases:
- If we don't have a worktree we return a path into the Git directory.
The returned string is owned by `the_repository`, so there is no
need for the caller to free it.
- If we have a worktree, but no worktree ID then the caller requests
the main worktree. In this case we return a path into the common
directory, which again is owned by `the_repository` and thus does
not need to be free'd.
- In the third case, where we have an actual worktree, we compute the
path relative to "$GIT_COMMON_DIR/worktrees/". This string does not
need to be released either, even though `git_common_path()` ends up
allocating memory. But this doesn't result in a memory leak either
because we write into a buffer returned by `get_pathname()`, which
returns one out of four static buffers.
We're about to drop `git_common_path()` in favor of `repo_common_path()`,
which doesn't use the same mechanism but instead returns an allocated
string owned by the caller. While we could adapt `get_worktree_git_dir()`
to also use `get_pathname()` and print the derived common path into that
buffer, the whole schema feels a lot like premature optimization in this
context. There are some callsites where we call `get_worktree_git_dir()`
in a loop that iterates through all worktrees. But none of these loops
seem to be even remotely in the hot path, so saving a single allocation
there does not feel worth it.
Refactor the function to instead consistently return an allocated path
so that we can start using `repo_common_path()` in a subsequent commit.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
3859e39659
commit
8e4710f011
11
worktree.c
11
worktree.c
@@ -59,8 +59,9 @@ static void add_head_info(struct worktree *wt)
|
||||
static int is_current_worktree(struct worktree *wt)
|
||||
{
|
||||
char *git_dir = absolute_pathdup(repo_get_git_dir(the_repository));
|
||||
const char *wt_git_dir = get_worktree_git_dir(wt);
|
||||
char *wt_git_dir = get_worktree_git_dir(wt);
|
||||
int is_current = !fspathcmp(git_dir, absolute_path(wt_git_dir));
|
||||
free(wt_git_dir);
|
||||
free(git_dir);
|
||||
return is_current;
|
||||
}
|
||||
@@ -175,14 +176,14 @@ struct worktree **get_worktrees(void)
|
||||
return get_worktrees_internal(0);
|
||||
}
|
||||
|
||||
const char *get_worktree_git_dir(const struct worktree *wt)
|
||||
char *get_worktree_git_dir(const struct worktree *wt)
|
||||
{
|
||||
if (!wt)
|
||||
return repo_get_git_dir(the_repository);
|
||||
return xstrdup(repo_get_git_dir(the_repository));
|
||||
else if (!wt->id)
|
||||
return repo_get_common_dir(the_repository);
|
||||
return xstrdup(repo_get_common_dir(the_repository));
|
||||
else
|
||||
return git_common_path("worktrees/%s", wt->id);
|
||||
return xstrdup(git_common_path("worktrees/%s", wt->id));
|
||||
}
|
||||
|
||||
static struct worktree *find_worktree_by_suffix(struct worktree **list,
|
||||
|
||||
Reference in New Issue
Block a user