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 <assert.h>
18 #include <limits.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <sha1.h>
22 #include <string.h>
24 #include "got_error.h"
25 #include "got_refs.h"
26 #include "got_repository.h"
28 #include "path.h"
30 #define GOT_GIT_DIR ".git"
32 /* Mandatory files and directories inside the git directory. */
33 #define GOT_OBJECTS_DIR "objects"
34 #define GOT_REFS_DIR "refs"
35 #define GOT_HEAD_FILE "HEAD"
37 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
38 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
40 char *
41 got_repo_get_path_git_dir(struct got_repository *repo)
42 {
43 char *path_git;
45 if (asprintf(&path_git, "%s/%s", repo->path, GOT_GIT_DIR) == -1)
46 return NULL;
48 return path_git;
49 }
51 static char *
52 get_path_git_child(struct got_repository *repo, const char *basename)
53 {
54 char *path_child;
56 if (asprintf(&path_child, "%s/%s/%s", repo->path, GOT_GIT_DIR,
57 basename) == -1)
58 return NULL;
60 return path_child;
61 }
63 char *
64 got_repo_get_path_objects(struct got_repository *repo)
65 {
66 return get_path_git_child(repo, GOT_OBJECTS_DIR);
67 }
69 char *
70 got_repo_get_path_refs(struct got_repository *repo)
71 {
72 return get_path_git_child(repo, GOT_REFS_DIR);
73 }
75 static char *
76 get_path_head(struct got_repository *repo)
77 {
78 return get_path_git_child(repo, GOT_HEAD_FILE);
79 }
81 static int
82 is_git_repo(struct got_repository *repo)
83 {
84 char *path_git = got_repo_get_path_git_dir(repo);
85 char *path_objects = got_repo_get_path_objects(repo);
86 char *path_refs = got_repo_get_path_refs(repo);
87 char *path_head = get_path_head(repo);
88 int ret;
90 ret = (path_git != NULL) && (path_objects != NULL) &&
91 (path_refs != NULL) && (path_head != NULL);
93 free(path_git);
94 free(path_objects);
95 free(path_refs);
96 free(path_head);
97 return ret;
99 }
101 const struct got_error *
102 got_repo_open(struct got_repository **ret, const char *path)
104 struct got_repository *repo = NULL;
105 const struct got_error *err = NULL;
106 char *abspath = got_path_get_absolute(path);
108 if (abspath == NULL)
109 return got_error(GOT_ERR_BAD_PATH);
111 repo = calloc(1, sizeof(*repo));
112 if (repo == NULL) {
113 err = got_error(GOT_ERR_NO_MEM);
114 goto done;
117 repo->path = got_path_normalize(abspath);
118 if (repo->path == NULL) {
119 err = got_error(GOT_ERR_BAD_PATH);
120 goto done;
123 if (!is_git_repo(repo)) {
124 err = got_error(GOT_ERR_NOT_GIT_REPO);
125 goto done;
128 *ret = repo;
129 done:
130 if (err)
131 free(repo);
132 free(abspath);
133 return err;
136 void
137 got_repo_close(struct got_repository *repo)
139 free(repo->path);
140 free(repo);
143 const char *
144 got_repo_get_path(struct got_repository *repo)
146 return repo->path;