]>
git.proxmox.com Git - libgit2.git/blob - src/worktree.c
2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
8 #include "git2/worktree.h"
11 #include "repository.h"
14 static bool is_worktree_dir(git_buf
*dir
)
16 return git_path_contains_file(dir
, "commondir")
17 && git_path_contains_file(dir
, "gitdir")
18 && git_path_contains_file(dir
, "HEAD");
21 int git_worktree_list(git_strarray
*wts
, git_repository
*repo
)
23 git_vector worktrees
= GIT_VECTOR_INIT
;
24 git_buf path
= GIT_BUF_INIT
;
34 if ((error
= git_buf_printf(&path
, "%s/worktrees/", repo
->commondir
)) < 0)
36 if (!git_path_exists(path
.ptr
) || git_path_is_empty_dir(path
.ptr
))
38 if ((error
= git_path_dirload(&worktrees
, path
.ptr
, path
.size
, 0x0)) < 0)
43 git_vector_foreach(&worktrees
, i
, worktree
) {
44 git_buf_truncate(&path
, len
);
45 git_buf_puts(&path
, worktree
);
47 if (!is_worktree_dir(&path
)) {
48 git_vector_remove(&worktrees
, i
);
53 wts
->strings
= (char **)git_vector_detach(&wts
->count
, NULL
, &worktrees
);
61 static char *read_link(const char *base
, const char *file
)
63 git_buf path
= GIT_BUF_INIT
, buf
= GIT_BUF_INIT
;
67 if (git_buf_joinpath(&path
, base
, file
) < 0)
69 if (git_futils_readbuffer(&buf
, path
.ptr
) < 0)
75 if (!git_path_is_relative(buf
.ptr
))
76 return git_buf_detach(&buf
);
78 if (git_buf_sets(&path
, base
) < 0)
80 if (git_path_apply_relative(&path
, buf
.ptr
) < 0)
84 return git_buf_detach(&path
);
93 int git_worktree_lookup(git_worktree
**out
, git_repository
*repo
, const char *name
)
95 git_buf path
= GIT_BUF_INIT
;
96 git_worktree
*wt
= NULL
;
103 if ((error
= git_buf_printf(&path
, "%s/worktrees/%s", repo
->commondir
, name
)) < 0)
106 if (!is_worktree_dir(&path
)) {
111 if ((wt
= git__malloc(sizeof(struct git_repository
))) == NULL
) {
116 if ((wt
->name
= git__strdup(name
)) == NULL
117 || (wt
->commondir_path
= read_link(path
.ptr
, "commondir")) == NULL
118 || (wt
->gitlink_path
= read_link(path
.ptr
, "gitdir")) == NULL
119 || (wt
->parent_path
= git__strdup(git_repository_path(repo
))) == NULL
) {
123 wt
->gitdir_path
= git_buf_detach(&path
);
131 git_worktree_free(wt
);
136 void git_worktree_free(git_worktree
*wt
)
141 git__free(wt
->commondir_path
);
142 git__free(wt
->gitlink_path
);
143 git__free(wt
->gitdir_path
);
144 git__free(wt
->parent_path
);