3 * Copyright (C) 2008-2009 Lars Hjemli
4 * Copyright (C) 2010, 2012 Jason A. Donenfeld <Jason@zx2c4.com>
6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text)
11 #include "scan-tree.h"
12 #include "configfile.h"
15 /* return 1 if path contains a objects/ directory and a HEAD file */
16 static int is_git_dir(const char *path)
19 struct strbuf pathbuf = STRBUF_INIT;
22 strbuf_addf(&pathbuf, "%s/objects", path);
23 if (stat(pathbuf.buf, &st)) {
25 fprintf(stderr, "Error checking path %s: %s (%d)\n",
26 path, strerror(errno), errno);
29 if (!S_ISDIR(st.st_mode))
32 strbuf_reset(&pathbuf);
33 strbuf_addf(&pathbuf, "%s/HEAD", path);
34 if (stat(pathbuf.buf, &st)) {
36 fprintf(stderr, "Error checking path %s: %s (%d)\n",
37 path, strerror(errno), errno);
40 if (!S_ISREG(st.st_mode))
45 strbuf_release(&pathbuf);
49 struct cgit_repo *repo;
50 repo_config_fn config_fn;
52 static void repo_config(const char *name, const char *value)
54 config_fn(repo, name, value);
57 static int gitconfig_config(const char *key, const char *value, void *cb)
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);
71 static char *xstrrchr(char *s, char *from, int c)
73 while (from >= s && *from != c)
75 return from < s ? NULL : from;
78 static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn)
83 struct strbuf rel = STRBUF_INIT;
88 if (stat(path->buf, &st)) {
89 fprintf(stderr, "Error accessing %s: %s (%d)\n",
90 path->buf, strerror(errno), errno);
94 strbuf_addch(path, '/');
97 if (ctx.cfg.strict_export) {
98 strbuf_addstr(path, ctx.cfg.strict_export);
99 if(stat(path->buf, &st))
101 strbuf_setlen(path, pathlen);
104 strbuf_addstr(path, "noweb");
105 if (!stat(path->buf, &st))
107 strbuf_setlen(path, pathlen);
109 if (strncmp(base, path->buf, strlen(base)))
110 strbuf_addbuf(&rel, path);
112 strbuf_addstr(&rel, path->buf + strlen(base) + 1);
114 if (!strcmp(rel.buf + rel.len - 5, "/.git"))
115 strbuf_setlen(&rel, rel.len - 5);
117 repo = cgit_add_repo(rel.buf);
119 if (ctx.cfg.enable_git_config) {
120 strbuf_addstr(path, "config");
121 git_config_from_file(gitconfig_config, path->buf, NULL);
122 strbuf_setlen(path, pathlen);
125 if (ctx.cfg.remove_suffix)
126 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
128 repo->path = xstrdup(path->buf);
129 while (!repo->owner) {
130 if ((pwd = getpwuid(st.st_uid)) == NULL) {
131 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
132 path->buf, strerror(errno), errno);
136 if ((p = strchr(pwd->pw_gecos, ',')))
138 repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
141 if (repo->desc == cgit_default_repo_desc || !repo->desc) {
142 strbuf_addstr(path, "description");
143 if (!stat(path->buf, &st))
144 readfile(path->buf, &repo->desc, &size);
145 strbuf_setlen(path, pathlen);
149 strbuf_addstr(path, "README.html");
150 if (!stat(path->buf, &st))
151 repo->readme = "README.html";
152 strbuf_setlen(path, pathlen);
154 if (ctx.cfg.section_from_path) {
155 n = ctx.cfg.section_from_path;
158 while (slash && n && (slash = strchr(slash, '/')))
161 slash = rel.buf + rel.len;
162 while (slash && n && (slash = xstrrchr(rel.buf, slash, '/')))
167 repo->section = xstrdup(rel.buf);
169 if (!prefixcmp(repo->name, repo->section)) {
170 repo->name += strlen(repo->section);
171 if (*repo->name == '/')
177 strbuf_addstr(path, "cgitrc");
178 if (!stat(path->buf, &st))
179 parse_configfile(xstrdup(path->buf), &repo_config);
181 strbuf_release(&rel);
184 static void scan_path(const char *base, const char *path, repo_config_fn fn)
186 DIR *dir = opendir(path);
188 struct strbuf pathbuf = STRBUF_INIT;
189 size_t pathlen = strlen(path);
193 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
194 path, strerror(errno), errno);
198 strbuf_add(&pathbuf, path, strlen(path));
199 if (is_git_dir(pathbuf.buf)) {
200 add_repo(base, &pathbuf, fn);
203 strbuf_addstr(&pathbuf, "/.git");
204 if (is_git_dir(pathbuf.buf)) {
205 add_repo(base, &pathbuf, fn);
209 * Add one because we don't want to lose the trailing '/' when we
210 * reset the length of pathbuf in the loop below.
213 while ((ent = readdir(dir)) != NULL) {
214 if (ent->d_name[0] == '.') {
215 if (ent->d_name[1] == '\0')
217 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
219 if (!ctx.cfg.scan_hidden_path)
222 strbuf_setlen(&pathbuf, pathlen);
223 strbuf_addstr(&pathbuf, ent->d_name);
224 if (stat(pathbuf.buf, &st)) {
225 fprintf(stderr, "Error checking path %s: %s (%d)\n",
226 pathbuf.buf, strerror(errno), errno);
229 if (S_ISDIR(st.st_mode))
230 scan_path(base, pathbuf.buf, fn);
233 strbuf_release(&pathbuf);
237 #define lastc(s) s[strlen(s) - 1]
239 void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
241 struct strbuf line = STRBUF_INIT;
245 projects = fopen(projectsfile, "r");
247 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
248 projectsfile, strerror(errno), errno);
251 while (strbuf_getline(&line, projects, '\n') != EOF) {
254 strbuf_insert(&line, 0, "/", 1);
255 strbuf_insert(&line, 0, path, strlen(path));
256 scan_path(path, line.buf, fn);
258 if ((err = ferror(projects))) {
259 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
260 projectsfile, strerror(err), err);
263 strbuf_release(&line);
266 void scan_tree(const char *path, repo_config_fn fn)
268 scan_path(path, path, fn);