Blob


1 /*
2 * Copyright (c) 2017 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 "path.h"
29 #define GOT_GIT_DIR ".git"
31 /* Mandatory files and directories inside the git directory. */
32 #define GOT_OBJECTS_DIR "objects"
33 #define GOT_REFS_DIR "refs"
34 #define GOT_HEAD_FILE "HEAD"
36 /* Other files and directories inside the git directory. */
37 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
38 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
39 #define GOT_OBJECTS_PACK_DIR "objects/pack"
41 char *
42 got_repo_get_path_git_dir(struct got_repository *repo)
43 {
44 char *path_git;
46 if (asprintf(&path_git, "%s/%s", repo->path, GOT_GIT_DIR) == -1)
47 return NULL;
49 return path_git;
50 }
52 static char *
53 get_path_git_child(struct got_repository *repo, const char *basename)
54 {
55 char *path_child;
57 if (asprintf(&path_child, "%s/%s/%s", repo->path, GOT_GIT_DIR,
58 basename) == -1)
59 return NULL;
61 return path_child;
62 }
64 char *
65 got_repo_get_path_objects(struct got_repository *repo)
66 {
67 return get_path_git_child(repo, GOT_OBJECTS_DIR);
68 }
70 char *
71 got_repo_get_path_objects_pack(struct got_repository *repo)
72 {
73 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
74 }
76 char *
77 got_repo_get_path_refs(struct got_repository *repo)
78 {
79 return get_path_git_child(repo, GOT_REFS_DIR);
80 }
82 static char *
83 get_path_head(struct got_repository *repo)
84 {
85 return get_path_git_child(repo, GOT_HEAD_FILE);
86 }
88 static int
89 is_git_repo(struct got_repository *repo)
90 {
91 char *path_git = got_repo_get_path_git_dir(repo);
92 char *path_objects = got_repo_get_path_objects(repo);
93 char *path_refs = got_repo_get_path_refs(repo);
94 char *path_head = get_path_head(repo);
95 int ret;
97 ret = (path_git != NULL) && (path_objects != NULL) &&
98 (path_refs != NULL) && (path_head != NULL);
100 free(path_git);
101 free(path_objects);
102 free(path_refs);
103 free(path_head);
104 return ret;
108 const struct got_error *
109 got_repo_open(struct got_repository **ret, const char *path)
111 struct got_repository *repo = NULL;
112 const struct got_error *err = NULL;
113 char *abspath = got_path_get_absolute(path);
115 if (abspath == NULL)
116 return got_error(GOT_ERR_BAD_PATH);
118 repo = calloc(1, sizeof(*repo));
119 if (repo == NULL) {
120 err = got_error(GOT_ERR_NO_MEM);
121 goto done;
124 repo->path = got_path_normalize(abspath);
125 if (repo->path == NULL) {
126 err = got_error(GOT_ERR_BAD_PATH);
127 goto done;
130 if (!is_git_repo(repo)) {
131 err = got_error(GOT_ERR_NOT_GIT_REPO);
132 goto done;
135 *ret = repo;
136 done:
137 if (err)
138 free(repo);
139 free(abspath);
140 return err;
143 void
144 got_repo_close(struct got_repository *repo)
146 free(repo->path);
147 free(repo);