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 <limits.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <sha1.h>
21 #include <string.h>
23 #include "got_error.h"
24 #include "got_refs.h"
25 #include "got_repository.h"
27 #include "got_path_priv.h"
29 struct got_repository {
30 char *path;
31 };
33 #define GOT_GIT_DIR ".git"
35 /* Mandatory files and directories inside the git directory. */
36 #define GOT_OBJECTS_DIR "objects"
37 #define GOT_REFS_DIR "refs"
38 #define GOT_HEAD_FILE "HEAD"
40 /* Other files and directories inside the git directory. */
41 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
42 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
43 #define GOT_OBJECTS_PACK_DIR "objects/pack"
45 char *
46 got_repo_get_path_git_dir(struct got_repository *repo)
47 {
48 char *path_git;
50 if (asprintf(&path_git, "%s/%s", repo->path, GOT_GIT_DIR) == -1)
51 return NULL;
53 return path_git;
54 }
56 static char *
57 get_path_git_child(struct got_repository *repo, const char *basename)
58 {
59 char *path_child;
61 if (asprintf(&path_child, "%s/%s/%s", repo->path, GOT_GIT_DIR,
62 basename) == -1)
63 return NULL;
65 return path_child;
66 }
68 char *
69 got_repo_get_path_objects(struct got_repository *repo)
70 {
71 return get_path_git_child(repo, GOT_OBJECTS_DIR);
72 }
74 char *
75 got_repo_get_path_objects_pack(struct got_repository *repo)
76 {
77 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
78 }
80 char *
81 got_repo_get_path_refs(struct got_repository *repo)
82 {
83 return get_path_git_child(repo, GOT_REFS_DIR);
84 }
86 static char *
87 get_path_head(struct got_repository *repo)
88 {
89 return get_path_git_child(repo, GOT_HEAD_FILE);
90 }
92 static int
93 is_git_repo(struct got_repository *repo)
94 {
95 char *path_git = got_repo_get_path_git_dir(repo);
96 char *path_objects = got_repo_get_path_objects(repo);
97 char *path_refs = got_repo_get_path_refs(repo);
98 char *path_head = get_path_head(repo);
99 int ret;
101 ret = (path_git != NULL) && (path_objects != NULL) &&
102 (path_refs != NULL) && (path_head != NULL);
104 free(path_git);
105 free(path_objects);
106 free(path_refs);
107 free(path_head);
108 return ret;
112 const struct got_error *
113 got_repo_open(struct got_repository **ret, const char *path)
115 struct got_repository *repo = NULL;
116 const struct got_error *err = NULL;
117 char *abspath = got_path_get_absolute(path);
119 if (abspath == NULL)
120 return got_error(GOT_ERR_BAD_PATH);
122 repo = calloc(1, sizeof(*repo));
123 if (repo == NULL) {
124 err = got_error(GOT_ERR_NO_MEM);
125 goto done;
128 repo->path = got_path_normalize(abspath);
129 if (repo->path == NULL) {
130 err = got_error(GOT_ERR_BAD_PATH);
131 goto done;
134 if (!is_git_repo(repo)) {
135 err = got_error(GOT_ERR_NOT_GIT_REPO);
136 goto done;
139 *ret = repo;
140 done:
141 if (err)
142 free(repo);
143 free(abspath);
144 return err;
147 void
148 got_repo_close(struct got_repository *repo)
150 free(repo->path);
151 free(repo);