Blame


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