Skip to content

Commit 8e4710f

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3859e39 commit 8e4710f

File tree

8 files changed

+40
-15
lines changed

8 files changed

+40
-15
lines changed

branch.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ static void prepare_checked_out_branches(void)
397397
worktrees = get_worktrees();
398398

399399
while (worktrees[i]) {
400-
char *old;
400+
char *old, *wt_gitdir;
401401
struct wt_status_state state = { 0 };
402402
struct worktree *wt = worktrees[i++];
403403
struct string_list update_refs = STRING_LIST_INIT_DUP;
@@ -437,7 +437,8 @@ static void prepare_checked_out_branches(void)
437437
}
438438
wt_status_state_free_buffers(&state);
439439

440-
if (!sequencer_get_update_refs_state(get_worktree_git_dir(wt),
440+
wt_gitdir = get_worktree_git_dir(wt);
441+
if (!sequencer_get_update_refs_state(wt_gitdir,
441442
&update_refs)) {
442443
struct string_list_item *item;
443444
for_each_string_list_item(item, &update_refs) {
@@ -448,6 +449,8 @@ static void prepare_checked_out_branches(void)
448449
}
449450
string_list_clear(&update_refs, 1);
450451
}
452+
453+
free(wt_gitdir);
451454
}
452455

453456
free_worktrees(worktrees);

builtin/fsck.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,17 +1057,21 @@ int cmd_fsck(int argc,
10571057
struct worktree *wt = *p;
10581058
struct index_state istate =
10591059
INDEX_STATE_INIT(the_repository);
1060-
char *path;
1060+
char *path, *wt_gitdir;
10611061

10621062
/*
10631063
* Make a copy since the buffer is reusable
10641064
* and may get overwritten by other calls
10651065
* while we're examining the index.
10661066
*/
10671067
path = xstrdup(worktree_git_path(the_repository, wt, "index"));
1068-
read_index_from(&istate, path, get_worktree_git_dir(wt));
1068+
wt_gitdir = get_worktree_git_dir(wt);
1069+
1070+
read_index_from(&istate, path, wt_gitdir);
10691071
fsck_index(&istate, path, wt->is_current);
1072+
10701073
discard_index(&istate);
1074+
free(wt_gitdir);
10711075
free(path);
10721076
}
10731077
free_worktrees(worktrees);

builtin/receive-pack.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,8 @@ static const char *push_to_checkout(unsigned char *hash,
14351435

14361436
static const char *update_worktree(unsigned char *sha1, const struct worktree *worktree)
14371437
{
1438-
const char *retval, *git_dir;
1438+
const char *retval;
1439+
char *git_dir;
14391440
struct strvec env = STRVEC_INIT;
14401441
int invoked_hook;
14411442

@@ -1453,6 +1454,7 @@ static const char *update_worktree(unsigned char *sha1, const struct worktree *w
14531454
retval = push_to_deploy(sha1, &env, worktree->path);
14541455

14551456
strvec_clear(&env);
1457+
free(git_dir);
14561458
return retval;
14571459
}
14581460

builtin/worktree.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,8 +657,9 @@ static int can_use_local_refs(const struct add_opts *opts)
657657
if (!opts->quiet) {
658658
struct strbuf path = STRBUF_INIT;
659659
struct strbuf contents = STRBUF_INIT;
660+
char *wt_gitdir = get_worktree_git_dir(NULL);
660661

661-
strbuf_add_real_path(&path, get_worktree_git_dir(NULL));
662+
strbuf_add_real_path(&path, wt_gitdir);
662663
strbuf_addstr(&path, "/HEAD");
663664
strbuf_read_file(&contents, path.buf, 64);
664665
strbuf_stripspace(&contents, NULL);
@@ -670,6 +671,7 @@ static int can_use_local_refs(const struct add_opts *opts)
670671
path.buf, contents.buf);
671672
strbuf_release(&path);
672673
strbuf_release(&contents);
674+
free(wt_gitdir);
673675
}
674676
return 1;
675677
}
@@ -1157,6 +1159,9 @@ static void validate_no_submodules(const struct worktree *wt)
11571159
struct index_state istate = INDEX_STATE_INIT(the_repository);
11581160
struct strbuf path = STRBUF_INIT;
11591161
int i, found_submodules = 0;
1162+
char *wt_gitdir;
1163+
1164+
wt_gitdir = get_worktree_git_dir(wt);
11601165

11611166
if (is_directory(worktree_git_path(the_repository, wt, "modules"))) {
11621167
/*
@@ -1166,7 +1171,7 @@ static void validate_no_submodules(const struct worktree *wt)
11661171
*/
11671172
found_submodules = 1;
11681173
} else if (read_index_from(&istate, worktree_git_path(the_repository, wt, "index"),
1169-
get_worktree_git_dir(wt)) > 0) {
1174+
wt_gitdir) > 0) {
11701175
for (i = 0; i < istate.cache_nr; i++) {
11711176
struct cache_entry *ce = istate.cache[i];
11721177
int err;
@@ -1185,6 +1190,7 @@ static void validate_no_submodules(const struct worktree *wt)
11851190
}
11861191
discard_index(&istate);
11871192
strbuf_release(&path);
1193+
free(wt_gitdir);
11881194

11891195
if (found_submodules)
11901196
die(_("working trees containing submodules cannot be moved or removed"));

reachable.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,19 @@ static void add_rebase_files(struct rev_info *revs)
6565
struct worktree **worktrees = get_worktrees();
6666

6767
for (struct worktree **wt = worktrees; *wt; wt++) {
68+
char *wt_gitdir = get_worktree_git_dir(*wt);
69+
6870
strbuf_reset(&buf);
69-
strbuf_addstr(&buf, get_worktree_git_dir(*wt));
71+
strbuf_addstr(&buf, wt_gitdir);
7072
strbuf_complete(&buf, '/');
7173
len = buf.len;
7274
for (size_t i = 0; i < ARRAY_SIZE(path); i++) {
7375
strbuf_setlen(&buf, len);
7476
strbuf_addstr(&buf, path[i]);
7577
add_one_file(buf.buf, revs);
7678
}
79+
80+
free(wt_gitdir);
7781
}
7882
strbuf_release(&buf);
7983
free_worktrees(worktrees);

revision.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1874,15 +1874,20 @@ void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
18741874
for (p = worktrees; *p; p++) {
18751875
struct worktree *wt = *p;
18761876
struct index_state istate = INDEX_STATE_INIT(revs->repo);
1877+
char *wt_gitdir;
18771878

18781879
if (wt->is_current)
18791880
continue; /* current index already taken care of */
18801881

1882+
wt_gitdir = get_worktree_git_dir(wt);
1883+
18811884
if (read_index_from(&istate,
18821885
worktree_git_path(the_repository, wt, "index"),
1883-
get_worktree_git_dir(wt)) > 0)
1886+
wt_gitdir) > 0)
18841887
do_add_index_objects_to_pending(revs, &istate, flags);
1888+
18851889
discard_index(&istate);
1890+
free(wt_gitdir);
18861891
}
18871892
free_worktrees(worktrees);
18881893
}

worktree.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ static void add_head_info(struct worktree *wt)
5959
static int is_current_worktree(struct worktree *wt)
6060
{
6161
char *git_dir = absolute_pathdup(repo_get_git_dir(the_repository));
62-
const char *wt_git_dir = get_worktree_git_dir(wt);
62+
char *wt_git_dir = get_worktree_git_dir(wt);
6363
int is_current = !fspathcmp(git_dir, absolute_path(wt_git_dir));
64+
free(wt_git_dir);
6465
free(git_dir);
6566
return is_current;
6667
}
@@ -175,14 +176,14 @@ struct worktree **get_worktrees(void)
175176
return get_worktrees_internal(0);
176177
}
177178

178-
const char *get_worktree_git_dir(const struct worktree *wt)
179+
char *get_worktree_git_dir(const struct worktree *wt)
179180
{
180181
if (!wt)
181-
return repo_get_git_dir(the_repository);
182+
return xstrdup(repo_get_git_dir(the_repository));
182183
else if (!wt->id)
183-
return repo_get_common_dir(the_repository);
184+
return xstrdup(repo_get_common_dir(the_repository));
184185
else
185-
return git_common_path("worktrees/%s", wt->id);
186+
return xstrdup(git_common_path("worktrees/%s", wt->id));
186187
}
187188

188189
static struct worktree *find_worktree_by_suffix(struct worktree **list,

worktree.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int submodule_uses_worktrees(const char *path);
3939
* Return git dir of the worktree. Note that the path may be relative.
4040
* If wt is NULL, git dir of current worktree is returned.
4141
*/
42-
const char *get_worktree_git_dir(const struct worktree *wt);
42+
char *get_worktree_git_dir(const struct worktree *wt);
4343

4444
/*
4545
* Search for the worktree identified unambiguously by `arg` -- typically

0 commit comments

Comments
 (0)