Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
19 #include <limits.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <sha1.h>
23 #include <string.h>
24 #include <zlib.h>
26 #include "got_error.h"
27 #include "got_refs.h"
28 #include "got_repository.h"
30 #include "got_path_lib.h"
31 #include "got_repository_lib.h"
32 #include "got_zbuf_lib.h"
33 #include "got_delta_lib.h"
34 #include "got_object_lib.h"
35 #include "got_pack_lib.h"
37 #ifndef nitems
38 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
39 #endif
41 #define GOT_GIT_DIR ".git"
43 /* Mandatory files and directories inside the git directory. */
44 #define GOT_OBJECTS_DIR "objects"
45 #define GOT_REFS_DIR "refs"
46 #define GOT_HEAD_FILE "HEAD"
48 /* Other files and directories inside the git directory. */
49 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
50 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
51 #define GOT_OBJECTS_PACK_DIR "objects/pack"
53 char *
54 got_repo_get_path(struct got_repository *repo)
55 {
56 return strdup(repo->path);
57 }
59 char *
60 got_repo_get_path_git_dir(struct got_repository *repo)
61 {
62 char *path_git;
64 if (asprintf(&path_git, "%s/%s", repo->path, GOT_GIT_DIR) == -1)
65 return NULL;
67 return path_git;
68 }
70 static char *
71 get_path_git_child(struct got_repository *repo, const char *basename)
72 {
73 char *path_child;
75 if (asprintf(&path_child, "%s/%s/%s", repo->path, GOT_GIT_DIR,
76 basename) == -1)
77 return NULL;
79 return path_child;
80 }
82 char *
83 got_repo_get_path_objects(struct got_repository *repo)
84 {
85 return get_path_git_child(repo, GOT_OBJECTS_DIR);
86 }
88 char *
89 got_repo_get_path_objects_pack(struct got_repository *repo)
90 {
91 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
92 }
94 char *
95 got_repo_get_path_refs(struct got_repository *repo)
96 {
97 return get_path_git_child(repo, GOT_REFS_DIR);
98 }
100 static char *
101 get_path_head(struct got_repository *repo)
103 return get_path_git_child(repo, GOT_HEAD_FILE);
106 static int
107 is_git_repo(struct got_repository *repo)
109 char *path_git = got_repo_get_path_git_dir(repo);
110 char *path_objects = got_repo_get_path_objects(repo);
111 char *path_refs = got_repo_get_path_refs(repo);
112 char *path_head = get_path_head(repo);
113 int ret;
115 ret = (path_git != NULL) && (path_objects != NULL) &&
116 (path_refs != NULL) && (path_head != NULL);
118 free(path_git);
119 free(path_objects);
120 free(path_refs);
121 free(path_head);
122 return ret;
126 const struct got_error *
127 got_repo_open(struct got_repository **ret, const char *path)
129 struct got_repository *repo = NULL;
130 const struct got_error *err = NULL;
131 char *abspath;
133 if (got_path_is_absolute(path))
134 abspath = strdup(path);
135 else
136 abspath = got_path_get_absolute(path);
137 if (abspath == NULL)
138 return got_error(GOT_ERR_BAD_PATH);
140 repo = calloc(1, sizeof(*repo));
141 if (repo == NULL) {
142 err = got_error(GOT_ERR_NO_MEM);
143 goto done;
146 repo->path = got_path_normalize(abspath);
147 if (repo->path == NULL) {
148 err = got_error(GOT_ERR_BAD_PATH);
149 goto done;
152 if (!is_git_repo(repo)) {
153 err = got_error(GOT_ERR_NOT_GIT_REPO);
154 goto done;
157 *ret = repo;
158 done:
159 if (err)
160 free(repo);
161 free(abspath);
162 return err;
165 void
166 got_repo_close(struct got_repository *repo)
168 int i;
170 for (i = 0; i < nitems(repo->packidx_cache); i++) {
171 if (repo->packidx_cache[i] == NULL)
172 break;
173 got_packidx_close(repo->packidx_cache[i]);
175 free(repo->path);
176 free(repo);