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 286 287 288 289 290 291 292 293 294 295
/*
* Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/queue.h>
#include <sys/stat.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <sha1.h>
#include <string.h>
#include <zlib.h>
#include "got_error.h"
#include "got_reference.h"
#include "got_repository.h"
#include "got_worktree.h"
#include "got_object.h"
#include "got_lib_path.h"
#include "got_lib_delta.h"
#include "got_lib_zbuf.h"
#include "got_lib_object.h"
#include "got_lib_pack.h"
#include "got_lib_repository.h"
#include "got_lib_worktree.h"
#ifndef nitems
#define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
#endif
#define GOT_GIT_DIR ".git"
/* Mandatory files and directories inside the git directory. */
#define GOT_OBJECTS_DIR "objects"
#define GOT_REFS_DIR "refs"
#define GOT_HEAD_FILE "HEAD"
/* Other files and directories inside the git directory. */
#define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
#define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
#define GOT_OBJECTS_PACK_DIR "objects/pack"
char *
got_repo_get_path(struct got_repository *repo)
{
return strdup(repo->path);
}
char *
got_repo_get_path_git_dir(struct got_repository *repo)
{
return strdup(repo->path_git_dir);
}
static char *
get_path_git_child(struct got_repository *repo, const char *basename)
{
char *path_child;
if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
basename) == -1)
return NULL;
return path_child;
}
char *
got_repo_get_path_objects(struct got_repository *repo)
{
return get_path_git_child(repo, GOT_OBJECTS_DIR);
}
char *
got_repo_get_path_objects_pack(struct got_repository *repo)
{
return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
}
char *
got_repo_get_path_refs(struct got_repository *repo)
{
return get_path_git_child(repo, GOT_REFS_DIR);
}
static char *
get_path_head(struct got_repository *repo)
{
return get_path_git_child(repo, GOT_HEAD_FILE);
}
static int
is_git_repo(struct got_repository *repo)
{
char *path_git = got_repo_get_path_git_dir(repo);
char *path_objects = got_repo_get_path_objects(repo);
char *path_refs = got_repo_get_path_refs(repo);
char *path_head = get_path_head(repo);
int ret = 0;
struct stat sb;
struct got_reference *head_ref;
if (lstat(path_git, &sb) == -1)
goto done;
if (!S_ISDIR(sb.st_mode))
goto done;
if (lstat(path_objects, &sb) == -1)
goto done;
if (!S_ISDIR(sb.st_mode))
goto done;
if (lstat(path_refs, &sb) == -1)
goto done;
if (!S_ISDIR(sb.st_mode))
goto done;
if (lstat(path_head, &sb) == -1)
goto done;
if (!S_ISREG(sb.st_mode))
goto done;
/* Check if the HEAD reference can be opened. */
if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
goto done;
got_ref_close(head_ref);
ret = 1;
done:
free(path_git);
free(path_objects);
free(path_refs);
free(path_head);
return ret;
}
const struct got_error*
got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
struct got_object *obj)
{
struct got_objcache_entry *ce;
if (repo->ncached >= GOT_OBJECT_CACHE_SIZE) {
ce = SIMPLEQ_FIRST(&repo->objcache);
SIMPLEQ_REMOVE_HEAD(&repo->objcache, entry);
got_object_close(ce->obj);
free(ce);
repo->ncached--;
}
ce = calloc(1, sizeof(*ce));
if (ce == NULL)
return got_error_from_errno();
memcpy(&ce->id, id, sizeof(ce->id));
ce->obj = obj;
obj->refcnt++;
SIMPLEQ_INSERT_HEAD(&repo->objcache, ce, entry);
repo->ncached++;
return NULL;
}
struct got_object *
got_repo_get_cached_object(struct got_repository *repo,
struct got_object_id *id)
{
struct got_objcache_entry *ce;
SIMPLEQ_FOREACH(ce, &repo->objcache, entry) {
if (got_object_id_cmp(&ce->id, id) != 0)
continue;
repo->cache_hit++;
return ce->obj;
}
repo->cache_miss++;
return NULL;
}
const struct got_error *
got_repo_open(struct got_repository **ret, const char *path)
{
struct got_repository *repo = NULL;
const struct got_error *err = NULL;
char *abspath;
if (got_path_is_absolute(path))
abspath = strdup(path);
else
abspath = got_path_get_absolute(path);
if (abspath == NULL)
return got_error(GOT_ERR_BAD_PATH);
repo = calloc(1, sizeof(*repo));
if (repo == NULL) {
err = got_error_from_errno();
goto done;
}
repo->path = got_path_normalize(abspath);
if (repo->path == NULL) {
err = got_error(GOT_ERR_BAD_PATH);
goto done;
}
repo->path_git_dir = strdup(repo->path);
if (repo->path_git_dir == NULL) {
err = got_error_from_errno();
goto done;
}
if (!is_git_repo(repo)) {
free(repo->path_git_dir);
if (asprintf(&repo->path_git_dir, "%s/%s", repo->path,
GOT_GIT_DIR) == -1) {
err = got_error_from_errno();
goto done;
}
if (!is_git_repo(repo)) {
struct got_worktree *worktree;
if (got_worktree_open(&worktree, repo->path) == NULL) {
free(repo->path_git_dir);
repo->path_git_dir =
strdup(worktree->repo_path);
if (repo->path_git_dir == NULL) {
err = got_error_from_errno();
goto done;
}
if (!is_git_repo(repo)) {
free(repo->path_git_dir);
if (asprintf(&repo->path_git_dir,
"%s/%s", worktree->repo_path,
GOT_GIT_DIR) == -1) {
err = got_error_from_errno();
goto done;
}
}
got_worktree_close(worktree);
}
}
if (!is_git_repo(repo)) {
err = got_error(GOT_ERR_NOT_GIT_REPO);
goto done;
}
}
*ret = repo;
done:
if (err)
got_repo_close(repo);
free(abspath);
return err;
}
void
got_repo_close(struct got_repository *repo)
{
int i;
for (i = 0; i < nitems(repo->packidx_cache); i++) {
if (repo->packidx_cache[i] == NULL)
break;
got_packidx_close(repo->packidx_cache[i]);
}
for (i = 0; i < nitems(repo->packs); i++) {
if (repo->packs[i].path_packfile == NULL)
break;
got_pack_close(&repo->packs[i]);
}
while (!SIMPLEQ_EMPTY(&repo->objcache)) {
struct got_objcache_entry *ce;
ce = SIMPLEQ_FIRST(&repo->objcache);
SIMPLEQ_REMOVE_HEAD(&repo->objcache, entry);
got_object_close(ce->obj);
free(ce);
}
fprintf(stderr, "repo cache size: %d hit: %d miss: %d\n", repo->ncached, repo->cache_hit, repo->cache_miss);
free(repo->path);
free(repo->path_git_dir);
free(repo);
}