Blame


1 4027f31a 2017-11-04 stsp #include <assert.h>
2 4027f31a 2017-11-04 stsp #include <limits.h>
3 4027f31a 2017-11-04 stsp #include <stdlib.h>
4 4027f31a 2017-11-04 stsp #include <stdio.h>
5 4027f31a 2017-11-04 stsp #include <sha1.h>
6 4027f31a 2017-11-04 stsp #include <string.h>
7 4027f31a 2017-11-04 stsp
8 4027f31a 2017-11-04 stsp #include "got_path.h"
9 4027f31a 2017-11-04 stsp #include "got_error.h"
10 4027f31a 2017-11-04 stsp #include "got_refs.h"
11 4027f31a 2017-11-04 stsp #include "got_repository.h"
12 4027f31a 2017-11-04 stsp
13 4027f31a 2017-11-04 stsp #define GOT_GIT_DIR ".git"
14 4027f31a 2017-11-04 stsp
15 4027f31a 2017-11-04 stsp /* Mandatory files and directories inside the git directory. */
16 4027f31a 2017-11-04 stsp #define GOT_OBJECTS_DIR "objects"
17 4027f31a 2017-11-04 stsp #define GOT_REFS_DIR "refs"
18 4027f31a 2017-11-04 stsp #define GOT_HEAD_FILE "HEAD"
19 4027f31a 2017-11-04 stsp
20 4027f31a 2017-11-04 stsp static char *
21 4027f31a 2017-11-04 stsp get_path_git_dir(struct got_repository *repo)
22 4027f31a 2017-11-04 stsp {
23 4027f31a 2017-11-04 stsp char *path_git;
24 4027f31a 2017-11-04 stsp
25 4027f31a 2017-11-04 stsp if (asprintf(&path_git, "%s/%s", repo->path, GOT_GIT_DIR) == -1)
26 4027f31a 2017-11-04 stsp return NULL;
27 4027f31a 2017-11-04 stsp
28 4027f31a 2017-11-04 stsp return path_git;
29 4027f31a 2017-11-04 stsp }
30 4027f31a 2017-11-04 stsp
31 4027f31a 2017-11-04 stsp static char *
32 4027f31a 2017-11-04 stsp get_path_git_child(struct got_repository *repo, const char *basename)
33 4027f31a 2017-11-04 stsp {
34 4027f31a 2017-11-04 stsp char *path_child;
35 4027f31a 2017-11-04 stsp
36 4027f31a 2017-11-04 stsp if (asprintf(&path_child, "%s/%s/%s", repo->path, GOT_GIT_DIR,
37 4027f31a 2017-11-04 stsp basename) == -1)
38 4027f31a 2017-11-04 stsp return NULL;
39 4027f31a 2017-11-04 stsp
40 4027f31a 2017-11-04 stsp return path_child;
41 4027f31a 2017-11-04 stsp }
42 4027f31a 2017-11-04 stsp
43 4027f31a 2017-11-04 stsp static char *
44 4027f31a 2017-11-04 stsp get_path_objects(struct got_repository *repo)
45 4027f31a 2017-11-04 stsp {
46 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
47 4027f31a 2017-11-04 stsp }
48 4027f31a 2017-11-04 stsp
49 4027f31a 2017-11-04 stsp static char *
50 4027f31a 2017-11-04 stsp get_path_refs(struct got_repository *repo)
51 4027f31a 2017-11-04 stsp {
52 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
53 4027f31a 2017-11-04 stsp }
54 4027f31a 2017-11-04 stsp
55 4027f31a 2017-11-04 stsp static char *
56 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
57 4027f31a 2017-11-04 stsp {
58 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
59 4027f31a 2017-11-04 stsp }
60 4027f31a 2017-11-04 stsp
61 4027f31a 2017-11-04 stsp static int
62 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
63 4027f31a 2017-11-04 stsp {
64 4027f31a 2017-11-04 stsp char *path_git = get_path_git_dir(repo);
65 4027f31a 2017-11-04 stsp char *path_objects = get_path_objects(repo);
66 4027f31a 2017-11-04 stsp char *path_refs = get_path_refs(repo);
67 4027f31a 2017-11-04 stsp char *path_head = get_path_head(repo);
68 4027f31a 2017-11-04 stsp int ret;
69 4027f31a 2017-11-04 stsp
70 4027f31a 2017-11-04 stsp ret = (path_git != NULL) && (path_objects != NULL) &&
71 4027f31a 2017-11-04 stsp (path_refs != NULL) && (path_head != NULL);
72 4027f31a 2017-11-04 stsp
73 4027f31a 2017-11-04 stsp free(path_git);
74 4027f31a 2017-11-04 stsp free(path_objects);
75 4027f31a 2017-11-04 stsp free(path_refs);
76 4027f31a 2017-11-04 stsp free(path_head);
77 4027f31a 2017-11-04 stsp return ret;
78 4027f31a 2017-11-04 stsp
79 4027f31a 2017-11-04 stsp }
80 4027f31a 2017-11-04 stsp
81 4027f31a 2017-11-04 stsp const struct got_error *
82 4027f31a 2017-11-04 stsp got_repo_open(struct got_repository **ret, const char *abspath)
83 4027f31a 2017-11-04 stsp {
84 4027f31a 2017-11-04 stsp struct got_repository *repo;
85 4027f31a 2017-11-04 stsp
86 4027f31a 2017-11-04 stsp if (!got_path_is_absolute(abspath))
87 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NOT_ABSPATH);
88 4027f31a 2017-11-04 stsp
89 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
90 4027f31a 2017-11-04 stsp if (repo == NULL)
91 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NO_MEM);
92 4027f31a 2017-11-04 stsp
93 4027f31a 2017-11-04 stsp repo->path = got_path_normalize(abspath);
94 4027f31a 2017-11-04 stsp if (repo->path == NULL)
95 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_BAD_PATH);
96 4027f31a 2017-11-04 stsp
97 4027f31a 2017-11-04 stsp if (!is_git_repo(repo))
98 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NOT_GIT_REPO);
99 4027f31a 2017-11-04 stsp
100 4027f31a 2017-11-04 stsp *ret = repo;
101 4027f31a 2017-11-04 stsp return NULL;
102 4027f31a 2017-11-04 stsp }
103 4027f31a 2017-11-04 stsp
104 4027f31a 2017-11-04 stsp void
105 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
106 4027f31a 2017-11-04 stsp {
107 4027f31a 2017-11-04 stsp free(repo->path);
108 4027f31a 2017-11-04 stsp free(repo);
109 4027f31a 2017-11-04 stsp }
110 4027f31a 2017-11-04 stsp
111 4027f31a 2017-11-04 stsp const char *
112 4027f31a 2017-11-04 stsp got_repo_get_path(struct got_repository *repo)
113 4027f31a 2017-11-04 stsp {
114 4027f31a 2017-11-04 stsp return repo->path;
115 4027f31a 2017-11-04 stsp }
116 4027f31a 2017-11-04 stsp
117 4027f31a 2017-11-04 stsp const struct got_error *
118 4027f31a 2017-11-04 stsp got_repo_get_reference(struct got_reference **ref,
119 4027f31a 2017-11-04 stsp struct got_repository *repo, const char *refname)
120 4027f31a 2017-11-04 stsp {
121 4027f31a 2017-11-04 stsp const struct got_error *err = NULL;
122 4027f31a 2017-11-04 stsp char *path_refs;
123 4027f31a 2017-11-04 stsp
124 4027f31a 2017-11-04 stsp /* Some refs live in the .git directory. */
125 4027f31a 2017-11-04 stsp if (strcmp(refname, GOT_REF_HEAD) == 0)
126 4027f31a 2017-11-04 stsp path_refs = get_path_git_dir(repo);
127 4027f31a 2017-11-04 stsp else
128 4027f31a 2017-11-04 stsp path_refs = get_path_refs(repo);
129 4027f31a 2017-11-04 stsp
130 4027f31a 2017-11-04 stsp err = got_ref_open(ref, path_refs, refname);
131 4027f31a 2017-11-04 stsp free(path_refs);
132 4027f31a 2017-11-04 stsp return err;
133 4027f31a 2017-11-04 stsp }