Skip to content

Commit b33b6d3

Browse files
authored
Merge pull request libgit2#4640 from mkeeler/worktree-convenience2
worktree: add functions to get name and path
2 parents 5ace149 + 3da1ad2 commit b33b6d3

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-1
lines changed

include/git2/worktree.h

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

152+
/**
153+
* Retrieve the name of the worktree
154+
*
155+
* @param wt Worktree to get the name for
156+
* @return The worktree's name. The pointer returned is valid for the
157+
* lifetime of the git_worktree
158+
*/
159+
GIT_EXTERN(const char *) git_worktree_name(const git_worktree *wt);
160+
161+
/**
162+
* Retrieve the filesystem path for the worktree
163+
*
164+
* @param wt Worktree to get the path for
165+
* @return The worktree's filesystem path. The pointer returned
166+
* is valid for the lifetime of the git_worktree.
167+
*/
168+
GIT_EXTERN(const char *) git_worktree_path(const git_worktree *wt);
169+
152170
/**
153171
* Flags which can be passed to git_worktree_prune to alter its
154172
* 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);
@@ -472,6 +474,18 @@ int git_worktree_is_locked(git_buf *reason, const git_worktree *wt)
472474
return ret;
473475
}
474476

477+
const char *git_worktree_name(const git_worktree *wt)
478+
{
479+
assert(wt);
480+
return wt->name;
481+
}
482+
483+
const char *git_worktree_path(const git_worktree *wt)
484+
{
485+
assert(wt);
486+
return wt->worktree_path;
487+
}
488+
475489
int git_worktree_prune_init_options(
476490
git_worktree_prune_options *opts,
477491
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/clar/sandbox.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#ifdef __APPLE__
2+
#include <sys/syslimits.h>
3+
#endif
4+
15
static char _clar_path[4096];
26

37
static int
@@ -31,13 +35,21 @@ find_tmp_path(char *buffer, size_t length)
3135
continue;
3236

3337
if (is_valid_tmp_path(env)) {
38+
#ifdef __APPLE__
39+
if (length >= PATH_MAX && realpath(env, buffer) != NULL)
40+
return 0;
41+
#endif
3442
strncpy(buffer, env, length);
3543
return 0;
3644
}
3745
}
3846

3947
/* If the environment doesn't say anything, try to use /tmp */
4048
if (is_valid_tmp_path("/tmp")) {
49+
#ifdef __APPLE__
50+
if (length >= PATH_MAX && realpath("/tmp", buffer) != NULL)
51+
return 0;
52+
#endif
4153
strncpy(buffer, "/tmp", length);
4254
return 0;
4355
}

tests/worktree/worktree.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,29 @@ void test_worktree_worktree__validate(void)
386386
git_worktree_free(wt);
387387
}
388388

389+
void test_worktree_worktree__name(void)
390+
{
391+
git_worktree *wt;
392+
393+
cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
394+
cl_assert_equal_s(git_worktree_name(wt), "testrepo-worktree");
395+
396+
git_worktree_free(wt);
397+
}
398+
399+
void test_worktree_worktree__path(void)
400+
{
401+
git_worktree *wt;
402+
git_buf expected_path = GIT_BUF_INIT;
403+
404+
cl_git_pass(git_buf_joinpath(&expected_path, clar_sandbox_path(), "testrepo-worktree"));
405+
cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
406+
cl_assert_equal_s(git_worktree_path(wt), expected_path.ptr);
407+
408+
git_buf_free(&expected_path);
409+
git_worktree_free(wt);
410+
}
411+
389412
void test_worktree_worktree__validate_invalid_commondir(void)
390413
{
391414
git_worktree *wt;

0 commit comments

Comments
 (0)