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 "configfile.h"
16 /* return 1 if path contains a objects/ directory and a HEAD file */
17 static int is_git_dir(const char *path)
20 static char buf[MAX_PATH];
22 if (snprintf(buf, MAX_PATH, "%s/objects", path) >= MAX_PATH) {
23 fprintf(stderr, "Insanely long path: %s\n", path);
28 fprintf(stderr, "Error checking path %s: %s (%d)\n",
29 path, strerror(errno), errno);
32 if (!S_ISDIR(st.st_mode))
35 sprintf(buf, "%s/HEAD", path);
38 fprintf(stderr, "Error checking path %s: %s (%d)\n",
39 path, strerror(errno), errno);
42 if (!S_ISREG(st.st_mode))
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, const char *path, repo_config_fn fn)
81 char *rel, *p, *slash;
85 if (stat(path, &st)) {
86 fprintf(stderr, "Error accessing %s: %s (%d)\n",
87 path, strerror(errno), errno);
91 if (ctx.cfg.strict_export && stat(fmt("%s/%s", path, ctx.cfg.strict_export), &st))
94 if (!stat(fmt("%s/noweb", path), &st))
98 rel = xstrdup(fmt("%s", path));
100 rel = xstrdup(fmt("%s", path + strlen(base) + 1));
102 if (!strcmp(rel + strlen(rel) - 5, "/.git"))
103 rel[strlen(rel) - 5] = '\0';
105 repo = cgit_add_repo(rel);
107 if (ctx.cfg.enable_git_config)
108 git_config_from_file(gitconfig_config, fmt("%s/config", path), NULL);
110 if (ctx.cfg.remove_suffix)
111 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
113 repo->path = xstrdup(path);
114 while (!repo->owner) {
115 if ((pwd = getpwuid(st.st_uid)) == NULL) {
116 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
117 path, strerror(errno), errno);
121 if ((p = strchr(pwd->pw_gecos, ',')))
123 repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
126 if (repo->desc == cgit_default_repo_desc || !repo->desc) {
127 p = fmt("%s/description", path);
129 readfile(p, &repo->desc, &size);
133 p = fmt("%s/README.html", path);
135 repo->readme = "README.html";
137 if (ctx.cfg.section_from_path) {
138 n = ctx.cfg.section_from_path;
141 while (slash && n && (slash = strchr(slash, '/')))
144 slash = rel + strlen(rel);
145 while (slash && n && (slash = xstrrchr(rel, slash, '/')))
150 repo->section = xstrdup(rel);
152 if (!prefixcmp(repo->name, repo->section)) {
153 repo->name += strlen(repo->section);
154 if (*repo->name == '/')
160 p = fmt("%s/cgitrc", path);
162 parse_configfile(xstrdup(p), &repo_config);
168 static void scan_path(const char *base, const char *path, repo_config_fn fn)
170 DIR *dir = opendir(path);
176 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
177 path, strerror(errno), errno);
180 if (is_git_dir(path)) {
181 add_repo(base, path, fn);
184 if (is_git_dir(fmt("%s/.git", path))) {
185 add_repo(base, fmt("%s/.git", path), fn);
188 while ((ent = readdir(dir)) != NULL) {
189 if (ent->d_name[0] == '.') {
190 if (ent->d_name[1] == '\0')
192 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
194 if (!ctx.cfg.scan_hidden_path)
197 buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
199 fprintf(stderr, "Alloc error on %s: %s (%d)\n",
200 path, strerror(errno), errno);
203 sprintf(buf, "%s/%s", path, ent->d_name);
204 if (stat(buf, &st)) {
205 fprintf(stderr, "Error checking path %s: %s (%d)\n",
206 buf, strerror(errno), errno);
210 if (S_ISDIR(st.st_mode))
211 scan_path(base, buf, fn);
218 #define lastc(s) s[strlen(s) - 1]
220 void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
222 char line[MAX_PATH * 2], *z;
226 projects = fopen(projectsfile, "r");
228 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
229 projectsfile, strerror(errno), errno);
232 while (fgets(line, sizeof(line), projects) != NULL) {
233 for (z = &lastc(line);
234 strlen(line) && strchr("\n\r", *z);
238 scan_path(path, fmt("%s/%s", path, line), fn);
240 if ((err = ferror(projects))) {
241 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
242 projectsfile, strerror(err), err);
247 void scan_tree(const char *path, repo_config_fn fn)
249 scan_path(path, path, fn);