Skip to content

Commit d3b9ac0

Browse files
pcloudsgitster
authored andcommitted
worktree.c: make find_shared_symref() return struct worktree *
This gives the caller more information and they can answer things like, "is it the main worktree" or "is it the current worktree". The latter question is needed for the "checkout a rebase branch" case later. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 69dfe3b commit d3b9ac0

5 files changed

Lines changed: 28 additions & 23 deletions

File tree

branch.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,14 @@ void remove_branch_state(void)
336336

337337
void die_if_checked_out(const char *branch)
338338
{
339-
char *existing;
339+
const struct worktree *wt;
340340

341-
existing = find_shared_symref("HEAD", branch);
342-
if (existing) {
343-
skip_prefix(branch, "refs/heads/", &branch);
344-
die(_("'%s' is already checked out at '%s'"), branch, existing);
345-
}
341+
wt = find_shared_symref("HEAD", branch);
342+
if (!wt)
343+
return;
344+
skip_prefix(branch, "refs/heads/", &branch);
345+
die(_("'%s' is already checked out at '%s'"),
346+
branch, wt->path);
346347
}
347348

348349
int replace_each_worktree_head_symref(const char *oldref, const char *newref)

builtin/branch.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,12 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
220220
name = mkpathdup(fmt, bname.buf);
221221

222222
if (kinds == FILTER_REFS_BRANCHES) {
223-
char *worktree = find_shared_symref("HEAD", name);
224-
if (worktree) {
223+
const struct worktree *wt =
224+
find_shared_symref("HEAD", name);
225+
if (wt) {
225226
error(_("Cannot delete branch '%s' "
226227
"checked out at '%s'"),
227-
bname.buf, worktree);
228-
free(worktree);
228+
bname.buf, wt->path);
229229
ret = 1;
230230
continue;
231231
}

builtin/notes.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -847,15 +847,15 @@ static int merge(int argc, const char **argv, const char *prefix)
847847
update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
848848
0, UPDATE_REFS_DIE_ON_ERR);
849849
else { /* Merge has unresolved conflicts */
850-
char *existing;
850+
const struct worktree *wt;
851851
/* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
852852
update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_sha1, NULL,
853853
0, UPDATE_REFS_DIE_ON_ERR);
854854
/* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
855-
existing = find_shared_symref("NOTES_MERGE_REF", default_notes_ref());
856-
if (existing)
855+
wt = find_shared_symref("NOTES_MERGE_REF", default_notes_ref());
856+
if (wt)
857857
die(_("A notes merge into %s is already in-progress at %s"),
858-
default_notes_ref(), existing);
858+
default_notes_ref(), wt->path);
859859
if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
860860
die("Failed to store link to current notes ref (%s)",
861861
default_notes_ref());

worktree.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,19 @@ const char *get_worktree_git_dir(const struct worktree *wt)
191191
return git_common_path("worktrees/%s", wt->id);
192192
}
193193

194-
char *find_shared_symref(const char *symref, const char *target)
194+
const struct worktree *find_shared_symref(const char *symref,
195+
const char *target)
195196
{
196-
char *existing = NULL;
197+
const struct worktree *existing = NULL;
197198
struct strbuf path = STRBUF_INIT;
198199
struct strbuf sb = STRBUF_INIT;
199-
struct worktree **worktrees = get_worktrees();
200+
static struct worktree **worktrees;
200201
int i = 0;
201202

203+
if (worktrees)
204+
free_worktrees(worktrees);
205+
worktrees = get_worktrees();
206+
202207
for (i = 0; worktrees[i]; i++) {
203208
strbuf_reset(&path);
204209
strbuf_reset(&sb);
@@ -211,14 +216,13 @@ char *find_shared_symref(const char *symref, const char *target)
211216
}
212217

213218
if (!strcmp(sb.buf, target)) {
214-
existing = xstrdup(worktrees[i]->path);
219+
existing = worktrees[i];
215220
break;
216221
}
217222
}
218223

219224
strbuf_release(&path);
220225
strbuf_release(&sb);
221-
free_worktrees(worktrees);
222226

223227
return existing;
224228
}

worktree.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ extern void free_worktrees(struct worktree **);
3535

3636
/*
3737
* Check if a per-worktree symref points to a ref in the main worktree
38-
* or any linked worktree, and return the path to the exising worktree
39-
* if it is. Returns NULL if there is no existing ref. The caller is
40-
* responsible for freeing the returned path.
38+
* or any linked worktree, and return the worktree that holds the ref,
39+
* or NULL otherwise. The result may be destroyed by the next call.
4140
*/
42-
extern char *find_shared_symref(const char *symref, const char *target);
41+
extern const struct worktree *find_shared_symref(const char *symref,
42+
const char *target);
4343

4444
#endif

0 commit comments

Comments
 (0)