3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
10 #include "scan-tree.h"
11 #include "configfile.h"
14 /* return 1 if path contains a objects/ directory and a HEAD file */
15 static int is_git_dir(const char *path)
18 struct strbuf pathbuf = STRBUF_INIT;
21 strbuf_addf(&pathbuf, "%s/objects", path);
22 if (stat(pathbuf.buf, &st)) {
24 fprintf(stderr, "Error checking path %s: %s (%d)\n",
25 path, strerror(errno), errno);
28 if (!S_ISDIR(st.st_mode))
31 strbuf_reset(&pathbuf);
32 strbuf_addf(&pathbuf, "%s/HEAD", path);
33 if (stat(pathbuf.buf, &st)) {
35 fprintf(stderr, "Error checking path %s: %s (%d)\n",
36 path, strerror(errno), errno);
39 if (!S_ISREG(st.st_mode))
44 strbuf_release(&pathbuf);
48 struct cgit_repo *repo;
49 repo_config_fn config_fn;
51 static void repo_config(const char *name, const char *value)
53 config_fn(repo, name, value);
56 static int gitconfig_config(const char *key, const char *value, void *cb)
58 if (!strcmp(key, "gitweb.owner"))
59 config_fn(repo, "owner", value);
60 else if (!strcmp(key, "gitweb.description"))
61 config_fn(repo, "desc", value);
62 else if (!strcmp(key, "gitweb.category"))
63 config_fn(repo, "section", value);
64 else if (!prefixcmp(key, "cgit."))
65 config_fn(repo, key + 5, value);
70 static char *xstrrchr(char *s, char *from, int c)
72 while (from >= s && *from != c)
74 return from < s ? NULL : from;
77 static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn)
82 struct strbuf rel = STRBUF_INIT;
87 if (stat(path->buf, &st)) {
88 fprintf(stderr, "Error accessing %s: %s (%d)\n",
89 path->buf, strerror(errno), errno);
93 strbuf_addch(path, '/');
96 if (ctx.cfg.strict_export) {
97 strbuf_addstr(path, ctx.cfg.strict_export);
98 if(stat(path->buf, &st))
100 strbuf_setlen(path, pathlen);
103 strbuf_addstr(path, "noweb");
104 if (!stat(path->buf, &st))
106 strbuf_setlen(path, pathlen);
108 if (strncmp(base, path->buf, strlen(base)))
109 strbuf_addbuf(&rel, path);
111 strbuf_addstr(&rel, path->buf + strlen(base) + 1);
113 if (!strcmp(rel.buf + rel.len - 5, "/.git"))
114 strbuf_setlen(&rel, rel.len - 5);
115 else if (rel.len && rel.buf[rel.len - 1] == '/')
116 strbuf_setlen(&rel, rel.len - 1);
118 repo = cgit_add_repo(rel.buf);
120 if (ctx.cfg.enable_git_config) {
121 strbuf_addstr(path, "config");
122 git_config_from_file(gitconfig_config, path->buf, NULL);
123 strbuf_setlen(path, pathlen);
126 if (ctx.cfg.remove_suffix)
127 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
129 repo->path = xstrdup(path->buf);
130 while (!repo->owner) {
131 if ((pwd = getpwuid(st.st_uid)) == NULL) {
132 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
133 path->buf, strerror(errno), errno);
137 if ((p = strchr(pwd->pw_gecos, ',')))
139 repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
142 if (repo->desc == cgit_default_repo_desc || !repo->desc) {
143 strbuf_addstr(path, "description");
144 if (!stat(path->buf, &st))
145 readfile(path->buf, &repo->desc, &size);
146 strbuf_setlen(path, pathlen);
149 if (ctx.cfg.section_from_path) {
150 n = ctx.cfg.section_from_path;
153 while (slash && n && (slash = strchr(slash + 1, '/')))
156 slash = rel.buf + rel.len;
157 while (slash && n && (slash = xstrrchr(rel.buf, slash - 1, '/')))
162 repo->section = xstrdup(rel.buf);
164 if (!prefixcmp(repo->name, repo->section)) {
165 repo->name += strlen(repo->section);
166 if (*repo->name == '/')
172 strbuf_addstr(path, "cgitrc");
173 if (!stat(path->buf, &st))
174 parse_configfile(xstrdup(path->buf), &repo_config);
176 strbuf_release(&rel);
179 static void scan_path(const char *base, const char *path, repo_config_fn fn)
181 DIR *dir = opendir(path);
183 struct strbuf pathbuf = STRBUF_INIT;
184 size_t pathlen = strlen(path);
188 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
189 path, strerror(errno), errno);
193 strbuf_add(&pathbuf, path, strlen(path));
194 if (is_git_dir(pathbuf.buf)) {
195 add_repo(base, &pathbuf, fn);
198 strbuf_addstr(&pathbuf, "/.git");
199 if (is_git_dir(pathbuf.buf)) {
200 add_repo(base, &pathbuf, fn);
204 * Add one because we don't want to lose the trailing '/' when we
205 * reset the length of pathbuf in the loop below.
208 while ((ent = readdir(dir)) != NULL) {
209 if (ent->d_name[0] == '.') {
210 if (ent->d_name[1] == '\0')
212 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
214 if (!ctx.cfg.scan_hidden_path)
217 strbuf_setlen(&pathbuf, pathlen);
218 strbuf_addstr(&pathbuf, ent->d_name);
219 if (stat(pathbuf.buf, &st)) {
220 fprintf(stderr, "Error checking path %s: %s (%d)\n",
221 pathbuf.buf, strerror(errno), errno);
224 if (S_ISDIR(st.st_mode))
225 scan_path(base, pathbuf.buf, fn);
228 strbuf_release(&pathbuf);
232 void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
234 struct strbuf line = STRBUF_INIT;
238 projects = fopen(projectsfile, "r");
240 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
241 projectsfile, strerror(errno), errno);
244 while (strbuf_getline(&line, projects, '\n') != EOF) {
247 strbuf_insert(&line, 0, "/", 1);
248 strbuf_insert(&line, 0, path, strlen(path));
249 scan_path(path, line.buf, fn);
251 if ((err = ferror(projects))) {
252 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
253 projectsfile, strerror(err), err);
256 strbuf_release(&line);
259 void scan_tree(const char *path, repo_config_fn fn)
261 scan_path(path, path, fn);