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 4027f31a 2017-11-04 stsp #include <limits.h>
18 4027f31a 2017-11-04 stsp #include <stdlib.h>
19 4027f31a 2017-11-04 stsp #include <stdio.h>
20 4027f31a 2017-11-04 stsp #include <sha1.h>
21 4027f31a 2017-11-04 stsp #include <string.h>
22 4027f31a 2017-11-04 stsp
23 4027f31a 2017-11-04 stsp #include "got_error.h"
24 4027f31a 2017-11-04 stsp #include "got_refs.h"
25 4027f31a 2017-11-04 stsp #include "got_repository.h"
26 4027f31a 2017-11-04 stsp
27 1411938b 2018-02-12 stsp #include "got_path_priv.h"
28 c3f94f68 2017-11-05 stsp
29 3b339b2f 2018-02-12 stsp struct got_repository {
30 3b339b2f 2018-02-12 stsp char *path;
31 3b339b2f 2018-02-12 stsp };
32 3b339b2f 2018-02-12 stsp
33 4027f31a 2017-11-04 stsp #define GOT_GIT_DIR ".git"
34 4027f31a 2017-11-04 stsp
35 4027f31a 2017-11-04 stsp /* Mandatory files and directories inside the git directory. */
36 4df642d9 2017-11-05 stsp #define GOT_OBJECTS_DIR "objects"
37 4df642d9 2017-11-05 stsp #define GOT_REFS_DIR "refs"
38 4df642d9 2017-11-05 stsp #define GOT_HEAD_FILE "HEAD"
39 4027f31a 2017-11-04 stsp
40 a1fd68d8 2018-01-12 stsp /* Other files and directories inside the git directory. */
41 4df642d9 2017-11-05 stsp #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
42 4df642d9 2017-11-05 stsp #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
43 a1fd68d8 2018-01-12 stsp #define GOT_OBJECTS_PACK_DIR "objects/pack"
44 4df642d9 2017-11-05 stsp
45 11995603 2017-11-05 stsp char *
46 11995603 2017-11-05 stsp got_repo_get_path_git_dir(struct got_repository *repo)
47 4027f31a 2017-11-04 stsp {
48 4027f31a 2017-11-04 stsp char *path_git;
49 4027f31a 2017-11-04 stsp
50 4027f31a 2017-11-04 stsp if (asprintf(&path_git, "%s/%s", repo->path, GOT_GIT_DIR) == -1)
51 4027f31a 2017-11-04 stsp return NULL;
52 4027f31a 2017-11-04 stsp
53 4027f31a 2017-11-04 stsp return path_git;
54 4027f31a 2017-11-04 stsp }
55 4027f31a 2017-11-04 stsp
56 4027f31a 2017-11-04 stsp static char *
57 4027f31a 2017-11-04 stsp get_path_git_child(struct got_repository *repo, const char *basename)
58 4027f31a 2017-11-04 stsp {
59 4027f31a 2017-11-04 stsp char *path_child;
60 4027f31a 2017-11-04 stsp
61 4027f31a 2017-11-04 stsp if (asprintf(&path_child, "%s/%s/%s", repo->path, GOT_GIT_DIR,
62 4027f31a 2017-11-04 stsp basename) == -1)
63 4027f31a 2017-11-04 stsp return NULL;
64 4027f31a 2017-11-04 stsp
65 4027f31a 2017-11-04 stsp return path_child;
66 4027f31a 2017-11-04 stsp }
67 4027f31a 2017-11-04 stsp
68 11995603 2017-11-05 stsp char *
69 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
70 4027f31a 2017-11-04 stsp {
71 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
72 4027f31a 2017-11-04 stsp }
73 4027f31a 2017-11-04 stsp
74 11995603 2017-11-05 stsp char *
75 a1fd68d8 2018-01-12 stsp got_repo_get_path_objects_pack(struct got_repository *repo)
76 a1fd68d8 2018-01-12 stsp {
77 a1fd68d8 2018-01-12 stsp return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
78 a1fd68d8 2018-01-12 stsp }
79 a1fd68d8 2018-01-12 stsp
80 a1fd68d8 2018-01-12 stsp char *
81 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
82 4027f31a 2017-11-04 stsp {
83 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
84 4027f31a 2017-11-04 stsp }
85 4027f31a 2017-11-04 stsp
86 4027f31a 2017-11-04 stsp static char *
87 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
88 4027f31a 2017-11-04 stsp {
89 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
90 4027f31a 2017-11-04 stsp }
91 4027f31a 2017-11-04 stsp
92 4027f31a 2017-11-04 stsp static int
93 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
94 4027f31a 2017-11-04 stsp {
95 11995603 2017-11-05 stsp char *path_git = got_repo_get_path_git_dir(repo);
96 11995603 2017-11-05 stsp char *path_objects = got_repo_get_path_objects(repo);
97 11995603 2017-11-05 stsp char *path_refs = got_repo_get_path_refs(repo);
98 4027f31a 2017-11-04 stsp char *path_head = get_path_head(repo);
99 4027f31a 2017-11-04 stsp int ret;
100 4027f31a 2017-11-04 stsp
101 4027f31a 2017-11-04 stsp ret = (path_git != NULL) && (path_objects != NULL) &&
102 4027f31a 2017-11-04 stsp (path_refs != NULL) && (path_head != NULL);
103 4027f31a 2017-11-04 stsp
104 4027f31a 2017-11-04 stsp free(path_git);
105 4027f31a 2017-11-04 stsp free(path_objects);
106 4027f31a 2017-11-04 stsp free(path_refs);
107 4027f31a 2017-11-04 stsp free(path_head);
108 4027f31a 2017-11-04 stsp return ret;
109 4027f31a 2017-11-04 stsp
110 4027f31a 2017-11-04 stsp }
111 4027f31a 2017-11-04 stsp
112 4027f31a 2017-11-04 stsp const struct got_error *
113 92af5469 2017-11-05 stsp got_repo_open(struct got_repository **ret, const char *path)
114 4027f31a 2017-11-04 stsp {
115 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
116 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
117 92af5469 2017-11-05 stsp char *abspath = got_path_get_absolute(path);
118 4027f31a 2017-11-04 stsp
119 92af5469 2017-11-05 stsp if (abspath == NULL)
120 92af5469 2017-11-05 stsp return got_error(GOT_ERR_BAD_PATH);
121 4027f31a 2017-11-04 stsp
122 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
123 92af5469 2017-11-05 stsp if (repo == NULL) {
124 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_NO_MEM);
125 92af5469 2017-11-05 stsp goto done;
126 92af5469 2017-11-05 stsp }
127 4027f31a 2017-11-04 stsp
128 4027f31a 2017-11-04 stsp repo->path = got_path_normalize(abspath);
129 92af5469 2017-11-05 stsp if (repo->path == NULL) {
130 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_BAD_PATH);
131 92af5469 2017-11-05 stsp goto done;
132 92af5469 2017-11-05 stsp }
133 4027f31a 2017-11-04 stsp
134 92af5469 2017-11-05 stsp if (!is_git_repo(repo)) {
135 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
136 92af5469 2017-11-05 stsp goto done;
137 92af5469 2017-11-05 stsp }
138 4027f31a 2017-11-04 stsp
139 4027f31a 2017-11-04 stsp *ret = repo;
140 92af5469 2017-11-05 stsp done:
141 92af5469 2017-11-05 stsp if (err)
142 92af5469 2017-11-05 stsp free(repo);
143 92af5469 2017-11-05 stsp free(abspath);
144 92af5469 2017-11-05 stsp return err;
145 4027f31a 2017-11-04 stsp }
146 4027f31a 2017-11-04 stsp
147 4027f31a 2017-11-04 stsp void
148 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
149 4027f31a 2017-11-04 stsp {
150 4027f31a 2017-11-04 stsp free(repo->path);
151 4027f31a 2017-11-04 stsp free(repo);
152 4027f31a 2017-11-04 stsp }