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 static struct cgit_repo *repo;
49 static 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)
60 if (!strcmp(key, "gitweb.owner"))
61 config_fn(repo, "owner", value);
62 else if (!strcmp(key, "gitweb.description"))
63 config_fn(repo, "desc", value);
64 else if (!strcmp(key, "gitweb.category"))
65 config_fn(repo, "section", value);
66 else if (!strcmp(key, "gitweb.homepage"))
67 config_fn(repo, "homepage", value);
68 else if (skip_prefix(key, "cgit.", &name))
69 config_fn(repo, name, value);
74 static char *xstrrchr(char *s, char *from, int c)
76 while (from >= s && *from != c)
78 return from < s ? NULL : from;
81 static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn)
86 struct strbuf rel = STRBUF_INIT;
91 if (stat(path->buf, &st)) {
92 fprintf(stderr, "Error accessing %s: %s (%d)\n",
93 path->buf, strerror(errno), errno);
97 strbuf_addch(path, '/');
100 if (ctx.cfg.strict_export) {
101 strbuf_addstr(path, ctx.cfg.strict_export);
102 if(stat(path->buf, &st))
104 strbuf_setlen(path, pathlen);
107 strbuf_addstr(path, "noweb");
108 if (!stat(path->buf, &st))
110 strbuf_setlen(path, pathlen);
112 if (!starts_with(path->buf, base))
113 strbuf_addbuf(&rel, path);
115 strbuf_addstr(&rel, path->buf + strlen(base) + 1);
117 if (!strcmp(rel.buf + rel.len - 5, "/.git"))
118 strbuf_setlen(&rel, rel.len - 5);
119 else if (rel.len && rel.buf[rel.len - 1] == '/')
120 strbuf_setlen(&rel, rel.len - 1);
122 repo = cgit_add_repo(rel.buf);
124 if (ctx.cfg.enable_git_config) {
125 strbuf_addstr(path, "config");
126 git_config_from_file(gitconfig_config, path->buf, NULL);
127 strbuf_setlen(path, pathlen);
130 if (ctx.cfg.remove_suffix) {
132 strip_suffix(repo->url, ".git", &urllen);
133 strip_suffix_mem(repo->url, &urllen, "/");
134 repo->url[urllen] = '\0';
136 repo->path = xstrdup(path->buf);
137 while (!repo->owner) {
138 if ((pwd = getpwuid(st.st_uid)) == NULL) {
139 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
140 path->buf, strerror(errno), errno);
144 if ((p = strchr(pwd->pw_gecos, ',')))
146 repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
149 if (repo->desc == cgit_default_repo_desc || !repo->desc) {
150 strbuf_addstr(path, "description");
151 if (!stat(path->buf, &st))
152 readfile(path->buf, &repo->desc, &size);
153 strbuf_setlen(path, pathlen);
156 if (ctx.cfg.section_from_path) {
157 n = ctx.cfg.section_from_path;
160 while (slash && n && (slash = strchr(slash + 1, '/')))
163 slash = rel.buf + rel.len;
164 while (slash && n && (slash = xstrrchr(rel.buf, slash - 1, '/')))
169 repo->section = xstrdup(rel.buf);
171 if (starts_with(repo->name, repo->section)) {
172 repo->name += strlen(repo->section);
173 if (*repo->name == '/')
179 strbuf_addstr(path, "cgitrc");
180 if (!stat(path->buf, &st))
181 parse_configfile(path->buf, &repo_config);
183 strbuf_release(&rel);
186 static void scan_path(const char *base, const char *path, repo_config_fn fn)
188 DIR *dir = opendir(path);
190 struct strbuf pathbuf = STRBUF_INIT;
191 size_t pathlen = strlen(path);
195 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
196 path, strerror(errno), errno);
200 strbuf_add(&pathbuf, path, strlen(path));
201 if (is_git_dir(pathbuf.buf)) {
202 add_repo(base, &pathbuf, fn);
205 strbuf_addstr(&pathbuf, "/.git");
206 if (is_git_dir(pathbuf.buf)) {
207 add_repo(base, &pathbuf, fn);
211 * Add one because we don't want to lose the trailing '/' when we
212 * reset the length of pathbuf in the loop below.
215 while ((ent = readdir(dir)) != NULL) {
216 if (ent->d_name[0] == '.') {
217 if (ent->d_name[1] == '\0')
219 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
221 if (!ctx.cfg.scan_hidden_path)
224 strbuf_setlen(&pathbuf, pathlen);
225 strbuf_addstr(&pathbuf, ent->d_name);
226 if (stat(pathbuf.buf, &st)) {
227 fprintf(stderr, "Error checking path %s: %s (%d)\n",
228 pathbuf.buf, strerror(errno), errno);
231 if (S_ISDIR(st.st_mode))
232 scan_path(base, pathbuf.buf, fn);
235 strbuf_release(&pathbuf);
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) != 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);