]> git.cameronkatri.com Git - cgit.git/blob - scan-tree.c
git: update to 1.8.5
[cgit.git] / scan-tree.c
1 /* scan-tree.c
2 *
3 * Copyright (C) 2008-2009 Lars Hjemli
4 * Copyright (C) 2010-2013 Jason A. Donenfeld <Jason@zx2c4.com>
5 *
6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text)
8 */
9
10 #include "cgit.h"
11 #include "scan-tree.h"
12 #include "configfile.h"
13 #include "html.h"
14
15 /* return 1 if path contains a objects/ directory and a HEAD file */
16 static int is_git_dir(const char *path)
17 {
18 struct stat st;
19 struct strbuf pathbuf = STRBUF_INIT;
20 int result = 0;
21
22 strbuf_addf(&pathbuf, "%s/objects", path);
23 if (stat(pathbuf.buf, &st)) {
24 if (errno != ENOENT)
25 fprintf(stderr, "Error checking path %s: %s (%d)\n",
26 path, strerror(errno), errno);
27 goto out;
28 }
29 if (!S_ISDIR(st.st_mode))
30 goto out;
31
32 strbuf_reset(&pathbuf);
33 strbuf_addf(&pathbuf, "%s/HEAD", path);
34 if (stat(pathbuf.buf, &st)) {
35 if (errno != ENOENT)
36 fprintf(stderr, "Error checking path %s: %s (%d)\n",
37 path, strerror(errno), errno);
38 goto out;
39 }
40 if (!S_ISREG(st.st_mode))
41 goto out;
42
43 result = 1;
44 out:
45 strbuf_release(&pathbuf);
46 return result;
47 }
48
49 struct cgit_repo *repo;
50 repo_config_fn config_fn;
51
52 static void repo_config(const char *name, const char *value)
53 {
54 config_fn(repo, name, value);
55 }
56
57 static int gitconfig_config(const char *key, const char *value, void *cb)
58 {
59 if (!strcmp(key, "gitweb.owner"))
60 config_fn(repo, "owner", value);
61 else if (!strcmp(key, "gitweb.description"))
62 config_fn(repo, "desc", value);
63 else if (!strcmp(key, "gitweb.category"))
64 config_fn(repo, "section", value);
65 else if (!prefixcmp(key, "cgit."))
66 config_fn(repo, key + 5, value);
67
68 return 0;
69 }
70
71 static char *xstrrchr(char *s, char *from, int c)
72 {
73 while (from >= s && *from != c)
74 from--;
75 return from < s ? NULL : from;
76 }
77
78 static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn)
79 {
80 struct stat st;
81 struct passwd *pwd;
82 size_t pathlen;
83 struct strbuf rel = STRBUF_INIT;
84 char *p, *slash;
85 int n;
86 size_t size;
87
88 if (stat(path->buf, &st)) {
89 fprintf(stderr, "Error accessing %s: %s (%d)\n",
90 path->buf, strerror(errno), errno);
91 return;
92 }
93
94 strbuf_addch(path, '/');
95 pathlen = path->len;
96
97 if (ctx.cfg.strict_export) {
98 strbuf_addstr(path, ctx.cfg.strict_export);
99 if(stat(path->buf, &st))
100 return;
101 strbuf_setlen(path, pathlen);
102 }
103
104 strbuf_addstr(path, "noweb");
105 if (!stat(path->buf, &st))
106 return;
107 strbuf_setlen(path, pathlen);
108
109 if (strncmp(base, path->buf, strlen(base)))
110 strbuf_addbuf(&rel, path);
111 else
112 strbuf_addstr(&rel, path->buf + strlen(base) + 1);
113
114 if (!strcmp(rel.buf + rel.len - 5, "/.git"))
115 strbuf_setlen(&rel, rel.len - 5);
116 else if (rel.len && rel.buf[rel.len - 1] == '/')
117 strbuf_setlen(&rel, rel.len - 1);
118
119 repo = cgit_add_repo(rel.buf);
120 config_fn = fn;
121 if (ctx.cfg.enable_git_config) {
122 strbuf_addstr(path, "config");
123 git_config_from_file(gitconfig_config, path->buf, NULL);
124 strbuf_setlen(path, pathlen);
125 }
126
127 if (ctx.cfg.remove_suffix)
128 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
129 *p = '\0';
130 repo->path = xstrdup(path->buf);
131 while (!repo->owner) {
132 if ((pwd = getpwuid(st.st_uid)) == NULL) {
133 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
134 path->buf, strerror(errno), errno);
135 break;
136 }
137 if (pwd->pw_gecos)
138 if ((p = strchr(pwd->pw_gecos, ',')))
139 *p = '\0';
140 repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
141 }
142
143 if (repo->desc == cgit_default_repo_desc || !repo->desc) {
144 strbuf_addstr(path, "description");
145 if (!stat(path->buf, &st))
146 readfile(path->buf, &repo->desc, &size);
147 strbuf_setlen(path, pathlen);
148 }
149
150 if (ctx.cfg.section_from_path) {
151 n = ctx.cfg.section_from_path;
152 if (n > 0) {
153 slash = rel.buf - 1;
154 while (slash && n && (slash = strchr(slash + 1, '/')))
155 n--;
156 } else {
157 slash = rel.buf + rel.len;
158 while (slash && n && (slash = xstrrchr(rel.buf, slash - 1, '/')))
159 n++;
160 }
161 if (slash && !n) {
162 *slash = '\0';
163 repo->section = xstrdup(rel.buf);
164 *slash = '/';
165 if (!prefixcmp(repo->name, repo->section)) {
166 repo->name += strlen(repo->section);
167 if (*repo->name == '/')
168 repo->name++;
169 }
170 }
171 }
172
173 strbuf_addstr(path, "cgitrc");
174 if (!stat(path->buf, &st))
175 parse_configfile(xstrdup(path->buf), &repo_config);
176
177 strbuf_release(&rel);
178 }
179
180 static void scan_path(const char *base, const char *path, repo_config_fn fn)
181 {
182 DIR *dir = opendir(path);
183 struct dirent *ent;
184 struct strbuf pathbuf = STRBUF_INIT;
185 size_t pathlen = strlen(path);
186 struct stat st;
187
188 if (!dir) {
189 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
190 path, strerror(errno), errno);
191 return;
192 }
193
194 strbuf_add(&pathbuf, path, strlen(path));
195 if (is_git_dir(pathbuf.buf)) {
196 add_repo(base, &pathbuf, fn);
197 goto end;
198 }
199 strbuf_addstr(&pathbuf, "/.git");
200 if (is_git_dir(pathbuf.buf)) {
201 add_repo(base, &pathbuf, fn);
202 goto end;
203 }
204 /*
205 * Add one because we don't want to lose the trailing '/' when we
206 * reset the length of pathbuf in the loop below.
207 */
208 pathlen++;
209 while ((ent = readdir(dir)) != NULL) {
210 if (ent->d_name[0] == '.') {
211 if (ent->d_name[1] == '\0')
212 continue;
213 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
214 continue;
215 if (!ctx.cfg.scan_hidden_path)
216 continue;
217 }
218 strbuf_setlen(&pathbuf, pathlen);
219 strbuf_addstr(&pathbuf, ent->d_name);
220 if (stat(pathbuf.buf, &st)) {
221 fprintf(stderr, "Error checking path %s: %s (%d)\n",
222 pathbuf.buf, strerror(errno), errno);
223 continue;
224 }
225 if (S_ISDIR(st.st_mode))
226 scan_path(base, pathbuf.buf, fn);
227 }
228 end:
229 strbuf_release(&pathbuf);
230 closedir(dir);
231 }
232
233 void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
234 {
235 struct strbuf line = STRBUF_INIT;
236 FILE *projects;
237 int err;
238
239 projects = fopen(projectsfile, "r");
240 if (!projects) {
241 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
242 projectsfile, strerror(errno), errno);
243 return;
244 }
245 while (strbuf_getline(&line, projects, '\n') != EOF) {
246 if (!line.len)
247 continue;
248 strbuf_insert(&line, 0, "/", 1);
249 strbuf_insert(&line, 0, path, strlen(path));
250 scan_path(path, line.buf, fn);
251 }
252 if ((err = ferror(projects))) {
253 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
254 projectsfile, strerror(err), err);
255 }
256 fclose(projects);
257 strbuf_release(&line);
258 }
259
260 void scan_tree(const char *path, repo_config_fn fn)
261 {
262 scan_path(path, path, fn);
263 }