Unverified Commit b33b6d33 by Patrick Steinhardt Committed by GitHub

Merge pull request #4640 from mkeeler/worktree-convenience2

worktree:  add functions to get name and path
parents 5ace1494 3da1ad20
...@@ -150,6 +150,24 @@ GIT_EXTERN(int) git_worktree_unlock(git_worktree *wt); ...@@ -150,6 +150,24 @@ GIT_EXTERN(int) git_worktree_unlock(git_worktree *wt);
GIT_EXTERN(int) git_worktree_is_locked(git_buf *reason, const git_worktree *wt); GIT_EXTERN(int) git_worktree_is_locked(git_buf *reason, const git_worktree *wt);
/** /**
* Retrieve the name of the worktree
*
* @param wt Worktree to get the name for
* @return The worktree's name. The pointer returned is valid for the
* lifetime of the git_worktree
*/
GIT_EXTERN(const char *) git_worktree_name(const git_worktree *wt);
/**
* Retrieve the filesystem path for the worktree
*
* @param wt Worktree to get the path for
* @return The worktree's filesystem path. The pointer returned
* is valid for the lifetime of the git_worktree.
*/
GIT_EXTERN(const char *) git_worktree_path(const git_worktree *wt);
/**
* Flags which can be passed to git_worktree_prune to alter its * Flags which can be passed to git_worktree_prune to alter its
* behavior. * behavior.
*/ */
......
...@@ -139,7 +139,8 @@ static int open_worktree_dir(git_worktree **out, const char *parent, const char ...@@ -139,7 +139,8 @@ static int open_worktree_dir(git_worktree **out, const char *parent, const char
if ((wt->name = git__strdup(name)) == NULL if ((wt->name = git__strdup(name)) == NULL
|| (wt->commondir_path = git_worktree__read_link(dir, "commondir")) == NULL || (wt->commondir_path = git_worktree__read_link(dir, "commondir")) == NULL
|| (wt->gitlink_path = git_worktree__read_link(dir, "gitdir")) == NULL || (wt->gitlink_path = git_worktree__read_link(dir, "gitdir")) == NULL
|| (wt->parent_path = git__strdup(parent)) == NULL) { || (wt->parent_path = git__strdup(parent)) == NULL
|| (wt->worktree_path = git_path_dirname(wt->gitlink_path)) == NULL) {
error = -1; error = -1;
goto out; goto out;
} }
...@@ -223,6 +224,7 @@ void git_worktree_free(git_worktree *wt) ...@@ -223,6 +224,7 @@ void git_worktree_free(git_worktree *wt)
return; return;
git__free(wt->commondir_path); git__free(wt->commondir_path);
git__free(wt->worktree_path);
git__free(wt->gitlink_path); git__free(wt->gitlink_path);
git__free(wt->gitdir_path); git__free(wt->gitdir_path);
git__free(wt->parent_path); git__free(wt->parent_path);
...@@ -472,6 +474,18 @@ out: ...@@ -472,6 +474,18 @@ out:
return ret; return ret;
} }
const char *git_worktree_name(const git_worktree *wt)
{
assert(wt);
return wt->name;
}
const char *git_worktree_path(const git_worktree *wt)
{
assert(wt);
return wt->worktree_path;
}
int git_worktree_prune_init_options( int git_worktree_prune_init_options(
git_worktree_prune_options *opts, git_worktree_prune_options *opts,
unsigned int version) unsigned int version)
......
...@@ -18,6 +18,8 @@ struct git_worktree { ...@@ -18,6 +18,8 @@ struct git_worktree {
* directory. */ * directory. */
char *name; char *name;
/* Path to the where the worktree lives in the filesystem */
char *worktree_path;
/* Path to the .git file in the working tree's repository */ /* Path to the .git file in the working tree's repository */
char *gitlink_path; char *gitlink_path;
/* Path to the .git directory inside the parent's /* Path to the .git directory inside the parent's
......
#ifdef __APPLE__
#include <sys/syslimits.h>
#endif
static char _clar_path[4096]; static char _clar_path[4096];
static int static int
...@@ -31,6 +35,10 @@ find_tmp_path(char *buffer, size_t length) ...@@ -31,6 +35,10 @@ find_tmp_path(char *buffer, size_t length)
continue; continue;
if (is_valid_tmp_path(env)) { if (is_valid_tmp_path(env)) {
#ifdef __APPLE__
if (length >= PATH_MAX && realpath(env, buffer) != NULL)
return 0;
#endif
strncpy(buffer, env, length); strncpy(buffer, env, length);
return 0; return 0;
} }
...@@ -38,6 +46,10 @@ find_tmp_path(char *buffer, size_t length) ...@@ -38,6 +46,10 @@ find_tmp_path(char *buffer, size_t length)
/* If the environment doesn't say anything, try to use /tmp */ /* If the environment doesn't say anything, try to use /tmp */
if (is_valid_tmp_path("/tmp")) { if (is_valid_tmp_path("/tmp")) {
#ifdef __APPLE__
if (length >= PATH_MAX && realpath("/tmp", buffer) != NULL)
return 0;
#endif
strncpy(buffer, "/tmp", length); strncpy(buffer, "/tmp", length);
return 0; return 0;
} }
......
...@@ -386,6 +386,29 @@ void test_worktree_worktree__validate(void) ...@@ -386,6 +386,29 @@ void test_worktree_worktree__validate(void)
git_worktree_free(wt); git_worktree_free(wt);
} }
void test_worktree_worktree__name(void)
{
git_worktree *wt;
cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
cl_assert_equal_s(git_worktree_name(wt), "testrepo-worktree");
git_worktree_free(wt);
}
void test_worktree_worktree__path(void)
{
git_worktree *wt;
git_buf expected_path = GIT_BUF_INIT;
cl_git_pass(git_buf_joinpath(&expected_path, clar_sandbox_path(), "testrepo-worktree"));
cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
cl_assert_equal_s(git_worktree_path(wt), expected_path.ptr);
git_buf_free(&expected_path);
git_worktree_free(wt);
}
void test_worktree_worktree__validate_invalid_commondir(void) void test_worktree_worktree__validate_invalid_commondir(void)
{ {
git_worktree *wt; git_worktree *wt;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment