Hash :
dea7488e
Author :
Date :
2015-10-23T14:11:44
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "common.h"
#include "git2/branch.h"
#include "git2/commit.h"
#include "git2/worktree.h"
#include "repository.h"
#include "worktree.h"
static bool is_worktree_dir(git_buf *dir)
{
return git_path_contains_file(dir, "commondir")
&& git_path_contains_file(dir, "gitdir")
&& git_path_contains_file(dir, "HEAD");
}
int git_worktree_list(git_strarray *wts, git_repository *repo)
{
git_vector worktrees = GIT_VECTOR_INIT;
git_buf path = GIT_BUF_INIT;
char *worktree;
unsigned i, len;
int error;
assert(wts && repo);
wts->count = 0;
wts->strings = NULL;
if ((error = git_buf_printf(&path, "%s/worktrees/", repo->commondir)) < 0)
goto exit;
if (!git_path_exists(path.ptr) || git_path_is_empty_dir(path.ptr))
goto exit;
if ((error = git_path_dirload(&worktrees, path.ptr, path.size, 0x0)) < 0)
goto exit;
len = path.size;
git_vector_foreach(&worktrees, i, worktree) {
git_buf_truncate(&path, len);
git_buf_puts(&path, worktree);
if (!is_worktree_dir(&path)) {
git_vector_remove(&worktrees, i);
git__free(worktree);
}
}
wts->strings = (char **)git_vector_detach(&wts->count, NULL, &worktrees);
exit:
git_buf_free(&path);
return error;
}
static char *read_link(const char *base, const char *file)
{
git_buf path = GIT_BUF_INIT, buf = GIT_BUF_INIT;
assert(base && file);
if (git_buf_joinpath(&path, base, file) < 0)
goto err;
if (git_futils_readbuffer(&buf, path.ptr) < 0)
goto err;
git_buf_free(&path);
git_buf_rtrim(&buf);
if (!git_path_is_relative(buf.ptr))
return git_buf_detach(&buf);
if (git_buf_sets(&path, base) < 0)
goto err;
if (git_path_apply_relative(&path, buf.ptr) < 0)
goto err;
git_buf_free(&buf);
return git_buf_detach(&path);
err:
git_buf_free(&buf);
git_buf_free(&path);
return NULL;
}
static int write_wtfile(const char *base, const char *file, const git_buf *buf)
{
git_buf path = GIT_BUF_INIT;
int err;
assert(base && file && buf);
if ((err = git_buf_joinpath(&path, base, file)) < 0)
goto out;
if ((err = git_futils_writebuffer(buf, path.ptr, O_CREAT|O_EXCL|O_WRONLY, 0644)) < 0)
goto out;
out:
git_buf_free(&path);
return err;
}
int git_worktree_lookup(git_worktree **out, git_repository *repo, const char *name)
{
git_buf path = GIT_BUF_INIT;
git_worktree *wt = NULL;
int error;
assert(repo && name);
*out = NULL;
if ((error = git_buf_printf(&path, "%s/worktrees/%s", repo->commondir, name)) < 0)
goto out;
if (!is_worktree_dir(&path)) {
error = -1;
goto out;
}
if ((wt = git__malloc(sizeof(struct git_repository))) == NULL) {
error = -1;
goto out;
}
if ((wt->name = git__strdup(name)) == NULL
|| (wt->commondir_path = read_link(path.ptr, "commondir")) == NULL
|| (wt->gitlink_path = read_link(path.ptr, "gitdir")) == NULL
|| (wt->parent_path = git__strdup(git_repository_path(repo))) == NULL) {
error = -1;
goto out;
}
wt->gitdir_path = git_buf_detach(&path);
(*out) = wt;
out:
git_buf_free(&path);
if (error)
git_worktree_free(wt);
return error;
}
void git_worktree_free(git_worktree *wt)
{
if (!wt)
return;
git__free(wt->commondir_path);
git__free(wt->gitlink_path);
git__free(wt->gitdir_path);
git__free(wt->parent_path);
git__free(wt->name);
git__free(wt);
}
int git_worktree_validate(const git_worktree *wt)
{
git_buf buf = GIT_BUF_INIT;
int err = 0;
assert(wt);
git_buf_puts(&buf, wt->gitdir_path);
if (!is_worktree_dir(&buf)) {
giterr_set(GITERR_WORKTREE,
"Worktree gitdir ('%s') is not valid",
wt->gitlink_path);
err = -1;
goto out;
}
if (!git_path_exists(wt->parent_path)) {
giterr_set(GITERR_WORKTREE,
"Worktree parent directory ('%s') does not exist ",
wt->parent_path);
err = -2;
goto out;
}
if (!git_path_exists(wt->commondir_path)) {
giterr_set(GITERR_WORKTREE,
"Worktree common directory ('%s') does not exist ",
wt->commondir_path);
err = -3;
goto out;
}
out:
git_buf_free(&buf);
return err;
}
int git_worktree_add(git_worktree **out, git_repository *repo, const char *name, const char *worktree)
{
git_buf path = GIT_BUF_INIT, buf = GIT_BUF_INIT;
git_reference *ref = NULL, *head = NULL;
git_commit *commit = NULL;
git_repository *wt = NULL;
git_checkout_options coopts = GIT_CHECKOUT_OPTIONS_INIT;
int err;
assert(out && repo && name && worktree);
*out = NULL;
/* Create worktree related files in commondir */
if ((err = git_buf_joinpath(&path, repo->commondir, "worktrees")) < 0)
goto out;
if (!git_path_exists(path.ptr))
if ((err = git_futils_mkdir(path.ptr, 0755, GIT_MKDIR_EXCL)) < 0)
goto out;
if ((err = git_buf_joinpath(&path, path.ptr, name)) < 0)
goto out;
if ((err = git_futils_mkdir(path.ptr, 0755, GIT_MKDIR_EXCL)) < 0)
goto out;
/* Create worktree work dir */
if ((err = git_futils_mkdir(worktree, 0755, GIT_MKDIR_EXCL)) < 0)
goto out;
/* Create worktree .git file */
if ((err = git_buf_printf(&buf, "gitdir: %s\n", path.ptr)) < 0)
goto out;
if ((err = write_wtfile(worktree, ".git", &buf)) < 0)
goto out;
/* Create commondir files */
if ((err = git_buf_sets(&buf, repo->commondir)) < 0
|| (err = git_buf_putc(&buf, '\n')) < 0
|| (err = write_wtfile(path.ptr, "commondir", &buf)) < 0)
goto out;
if ((err = git_buf_joinpath(&buf, worktree, ".git")) < 0
|| (err = git_buf_putc(&buf, '\n')) < 0
|| (err = write_wtfile(path.ptr, "gitdir", &buf)) < 0)
goto out;
/* Create new branch */
if ((err = git_repository_head(&head, repo)) < 0)
goto out;
if ((err = git_commit_lookup(&commit, repo, &head->target.oid)) < 0)
goto out;
if ((err = git_branch_create(&ref, repo, name, commit, false)) < 0)
goto out;
/* Set worktree's HEAD */
if ((err = git_repository_create_head(path.ptr, name)) < 0)
goto out;
if ((err = git_repository_open(&wt, worktree)) < 0)
goto out;
/* Checkout worktree's HEAD */
coopts.checkout_strategy = GIT_CHECKOUT_FORCE;
if ((err = git_checkout_head(wt, &coopts)) < 0)
goto out;
/* Load result */
if ((err = git_worktree_lookup(out, repo, name)) < 0)
goto out;
out:
git_buf_free(&path);
git_buf_free(&buf);
git_reference_free(ref);
git_reference_free(head);
git_commit_free(commit);
git_repository_free(wt);
return err;
}