Skip to content

Commit 3da1ad2

Browse files
committed
worktree: add functions to get name and path
1 parent 45a3b9c commit 3da1ad2

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

include/git2/worktree.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,24 @@ GIT_EXTERN(int) git_worktree_unlock(git_worktree *wt);
148148
*/
149149
GIT_EXTERN(int) git_worktree_is_locked(git_buf *reason, const git_worktree *wt);
150150

151+
/**
152+
* Retrieve the name of the worktree
153+
*
154+
* @param wt Worktree to get the name for
155+
* @return The worktree's name. The pointer returned is valid for the
156+
* lifetime of the git_worktree
157+
*/
158+
GIT_EXTERN(const char *) git_worktree_name(const git_worktree *wt);
159+
160+
/**
161+
* Retrieve the filesystem path for the worktree
162+
*
163+
* @param wt Worktree to get the path for
164+
* @return The worktree's filesystem path. The pointer returned
165+
* is valid for the lifetime of the git_worktree.
166+
*/
167+
GIT_EXTERN(const char *) git_worktree_path(const git_worktree *wt);
168+
151169
/**
152170
* Flags which can be passed to git_worktree_prune to alter its
153171
* behavior.

src/worktree.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ static int open_worktree_dir(git_worktree **out, const char *parent, const char
139139
if ((wt->name = git__strdup(name)) == NULL
140140
|| (wt->commondir_path = git_worktree__read_link(dir, "commondir")) == NULL
141141
|| (wt->gitlink_path = git_worktree__read_link(dir, "gitdir")) == NULL
142-
|| (wt->parent_path = git__strdup(parent)) == NULL) {
142+
|| (wt->parent_path = git__strdup(parent)) == NULL
143+
|| (wt->worktree_path = git_path_dirname(wt->gitlink_path)) == NULL) {
143144
error = -1;
144145
goto out;
145146
}
@@ -223,6 +224,7 @@ void git_worktree_free(git_worktree *wt)
223224
return;
224225

225226
git__free(wt->commondir_path);
227+
git__free(wt->worktree_path);
226228
git__free(wt->gitlink_path);
227229
git__free(wt->gitdir_path);
228230
git__free(wt->parent_path);
@@ -455,6 +457,18 @@ int git_worktree_is_locked(git_buf *reason, const git_worktree *wt)
455457
return ret;
456458
}
457459

460+
const char *git_worktree_name(const git_worktree *wt)
461+
{
462+
assert(wt);
463+
return wt->name;
464+
}
465+
466+
const char *git_worktree_path(const git_worktree *wt)
467+
{
468+
assert(wt);
469+
return wt->worktree_path;
470+
}
471+
458472
int git_worktree_prune_init_options(
459473
git_worktree_prune_options *opts,
460474
unsigned int version)

src/worktree.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ struct git_worktree {
1818
* directory. */
1919
char *name;
2020

21+
/* Path to the where the worktree lives in the filesystem */
22+
char *worktree_path;
2123
/* Path to the .git file in the working tree's repository */
2224
char *gitlink_path;
2325
/* Path to the .git directory inside the parent's

tests/worktree/worktree.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,29 @@ void test_worktree_worktree__validate(void)
355355
git_worktree_free(wt);
356356
}
357357

358+
void test_worktree_worktree__name(void)
359+
{
360+
git_worktree *wt;
361+
362+
cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
363+
cl_assert_equal_s(git_worktree_name(wt), "testrepo-worktree");
364+
365+
git_worktree_free(wt);
366+
}
367+
368+
void test_worktree_worktree__path(void)
369+
{
370+
git_worktree *wt;
371+
git_buf expected_path = GIT_BUF_INIT;
372+
373+
cl_git_pass(git_buf_joinpath(&expected_path, clar_sandbox_path(), "testrepo-worktree"));
374+
cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
375+
cl_assert_equal_s(git_worktree_path(wt), expected_path.ptr);
376+
377+
git_buf_free(&expected_path);
378+
git_worktree_free(wt);
379+
}
380+
358381
void test_worktree_worktree__validate_invalid_commondir(void)
359382
{
360383
git_worktree *wt;

0 commit comments

Comments
 (0)