2 7b19e0f1 2017-11-05 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 4027f31a 2017-11-04 stsp #include <assert.h>
18 4027f31a 2017-11-04 stsp #include <limits.h>
19 4027f31a 2017-11-04 stsp #include <stdlib.h>
20 4027f31a 2017-11-04 stsp #include <stdio.h>
21 4027f31a 2017-11-04 stsp #include <sha1.h>
22 4027f31a 2017-11-04 stsp #include <string.h>
24 4027f31a 2017-11-04 stsp #include "got_error.h"
25 4027f31a 2017-11-04 stsp #include "got_refs.h"
26 4027f31a 2017-11-04 stsp #include "got_repository.h"
28 c3f94f68 2017-11-05 stsp #include "path.h"
30 4027f31a 2017-11-04 stsp #define GOT_GIT_DIR ".git"
32 4027f31a 2017-11-04 stsp /* Mandatory files and directories inside the git directory. */
33 4df642d9 2017-11-05 stsp #define GOT_OBJECTS_DIR "objects"
34 4df642d9 2017-11-05 stsp #define GOT_REFS_DIR "refs"
35 4df642d9 2017-11-05 stsp #define GOT_HEAD_FILE "HEAD"
37 4df642d9 2017-11-05 stsp #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
38 4df642d9 2017-11-05 stsp #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
41 11995603 2017-11-05 stsp got_repo_get_path_git_dir(struct got_repository *repo)
43 4027f31a 2017-11-04 stsp char *path_git;
45 4027f31a 2017-11-04 stsp if (asprintf(&path_git, "%s/%s", repo->path, GOT_GIT_DIR) == -1)
46 4027f31a 2017-11-04 stsp return NULL;
48 4027f31a 2017-11-04 stsp return path_git;
51 4027f31a 2017-11-04 stsp static char *
52 4027f31a 2017-11-04 stsp get_path_git_child(struct got_repository *repo, const char *basename)
54 4027f31a 2017-11-04 stsp char *path_child;
56 4027f31a 2017-11-04 stsp if (asprintf(&path_child, "%s/%s/%s", repo->path, GOT_GIT_DIR,
57 4027f31a 2017-11-04 stsp basename) == -1)
58 4027f31a 2017-11-04 stsp return NULL;
60 4027f31a 2017-11-04 stsp return path_child;
64 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
66 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
70 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
72 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
75 4027f31a 2017-11-04 stsp static char *
76 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
78 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
82 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
84 11995603 2017-11-05 stsp char *path_git = got_repo_get_path_git_dir(repo);
85 11995603 2017-11-05 stsp char *path_objects = got_repo_get_path_objects(repo);
86 11995603 2017-11-05 stsp char *path_refs = got_repo_get_path_refs(repo);
87 4027f31a 2017-11-04 stsp char *path_head = get_path_head(repo);
90 4027f31a 2017-11-04 stsp ret = (path_git != NULL) && (path_objects != NULL) &&
91 4027f31a 2017-11-04 stsp (path_refs != NULL) && (path_head != NULL);
93 4027f31a 2017-11-04 stsp free(path_git);
94 4027f31a 2017-11-04 stsp free(path_objects);
95 4027f31a 2017-11-04 stsp free(path_refs);
96 4027f31a 2017-11-04 stsp free(path_head);
101 4027f31a 2017-11-04 stsp const struct got_error *
102 92af5469 2017-11-05 stsp got_repo_open(struct got_repository **ret, const char *path)
104 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
105 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
106 92af5469 2017-11-05 stsp char *abspath = got_path_get_absolute(path);
108 92af5469 2017-11-05 stsp if (abspath == NULL)
109 92af5469 2017-11-05 stsp return got_error(GOT_ERR_BAD_PATH);
111 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
112 92af5469 2017-11-05 stsp if (repo == NULL) {
113 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_NO_MEM);
117 4027f31a 2017-11-04 stsp repo->path = got_path_normalize(abspath);
118 92af5469 2017-11-05 stsp if (repo->path == NULL) {
119 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_BAD_PATH);
123 92af5469 2017-11-05 stsp if (!is_git_repo(repo)) {
124 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
128 4027f31a 2017-11-04 stsp *ret = repo;
131 92af5469 2017-11-05 stsp free(repo);
132 92af5469 2017-11-05 stsp free(abspath);
133 92af5469 2017-11-05 stsp return err;
137 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
139 4027f31a 2017-11-04 stsp free(repo->path);
140 4027f31a 2017-11-04 stsp free(repo);