3 * Copyright (C) 2008-2009 Lars Hjemli
4 * Copyright (C) 2010 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;
52 static void repo_config(const char *name, const char *value)
54 config_fn(repo, name, value);
57 static int git_owner_config(const char *key, const char *value, void *cb)
59 if (!strcmp(key, "gitweb.owner"))
60 owner = xstrdup(value);
64 static char *xstrrchr(char *s, char *from, int c)
66 while (from >= s && *from != c)
68 return from < s ? NULL : from;
71 static void add_repo(const char *base, const char *path, repo_config_fn fn)
75 char *rel, *p, *slash;
79 if (stat(path, &st)) {
80 fprintf(stderr, "Error accessing %s: %s (%d)\n",
81 path, strerror(errno), errno);
84 if (!stat(fmt("%s/noweb", path), &st))
88 if (ctx.cfg.enable_gitweb_owner)
89 git_config_from_file(git_owner_config, fmt("%s/config", path), NULL);
91 rel = xstrdup(fmt("%s", path));
93 rel = xstrdup(fmt("%s", path + strlen(base) + 1));
95 if (!strcmp(rel + strlen(rel) - 5, "/.git"))
96 rel[strlen(rel) - 5] = '\0';
98 repo = cgit_add_repo(rel);
99 if (ctx.cfg.remove_suffix)
100 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
102 repo->name = repo->url;
103 repo->path = xstrdup(path);
105 if ((pwd = getpwuid(st.st_uid)) == NULL) {
106 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
107 path, strerror(errno), errno);
111 if ((p = strchr(pwd->pw_gecos, ',')))
113 owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
117 p = fmt("%s/description", path);
119 readfile(p, &repo->desc, &size);
121 p = fmt("%s/README.html", path);
123 repo->readme = "README.html";
124 if (ctx.cfg.section_from_path) {
125 n = ctx.cfg.section_from_path;
128 while (slash && n && (slash = strchr(slash, '/')))
131 slash = rel + strlen(rel);
132 while (slash && n && (slash = xstrrchr(rel, slash, '/')))
137 repo->section = xstrdup(rel);
139 if (!prefixcmp(repo->name, repo->section)) {
140 repo->name += strlen(repo->section);
141 if (*repo->name == '/')
147 p = fmt("%s/cgitrc", path);
150 parse_configfile(xstrdup(p), &repo_config);
154 static void scan_path(const char *base, const char *path, repo_config_fn fn)
161 if (is_git_dir(path)) {
162 add_repo(base, path, fn);
165 if (is_git_dir(fmt("%s/.git", path))) {
166 add_repo(base, fmt("%s/.git", path), fn);
171 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
172 path, strerror(errno), errno);
175 while((ent = readdir(dir)) != NULL) {
176 if (ent->d_name[0] == '.') {
177 if (ent->d_name[1] == '\0')
179 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
182 buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
184 fprintf(stderr, "Alloc error on %s: %s (%d)\n",
185 path, strerror(errno), errno);
188 sprintf(buf, "%s/%s", path, ent->d_name);
189 if (stat(buf, &st)) {
190 fprintf(stderr, "Error checking path %s: %s (%d)\n",
191 buf, strerror(errno), errno);
195 if (S_ISDIR(st.st_mode))
196 scan_path(base, buf, fn);
202 #define lastc(s) s[strlen(s) - 1]
204 void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
206 char line[MAX_PATH * 2], *z;
210 projects = fopen(projectsfile, "r");
212 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
213 projectsfile, strerror(errno), errno);
215 while (fgets(line, sizeof(line), projects) != NULL) {
216 for (z = &lastc(line);
217 strlen(line) && strchr("\n\r", *z);
221 scan_path(path, fmt("%s/%s", path, line), fn);
223 if ((err = ferror(projects))) {
224 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
225 projectsfile, strerror(err), err);
230 void scan_tree(const char *path, repo_config_fn fn)
232 scan_path(path, path, fn);