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 char *guess_defbranch(const char *repo_path)
78 head = fmt("%s/HEAD", repo_path);
79 fd = open(head, O_RDONLY);
81 return xstrdup("master");
83 memset(buffer, 0, sizeof(buffer));
84 len = read_in_full(fd, buffer, sizeof(buffer)-1);
87 if(!memcmp(buffer, "ref: refs/heads/", 16))
88 return xstrndup(buffer+16, len-17);
90 if(strlen(buffer) == 41) {
91 /* probably contains a SHA1 sum */
92 memset(buffer, 0, sizeof(buffer));
93 if(readlink(head, buffer, sizeof(buffer)-1)) {
94 ref_start = memmem(buffer, sizeof(buffer)-1, "refs/heads/", 11);
96 return xstrdup(ref_start+11);
100 return xstrdup("master");
104 static void add_repo(const char *base, const char *path, repo_config_fn fn)
108 char *rel, *p, *slash;
112 if (stat(path, &st)) {
113 fprintf(stderr, "Error accessing %s: %s (%d)\n",
114 path, strerror(errno), errno);
118 if (ctx.cfg.strict_export && stat(fmt("%s/%s", path, ctx.cfg.strict_export), &st))
121 if (!stat(fmt("%s/noweb", path), &st))
125 if (ctx.cfg.enable_gitweb_owner)
126 git_config_from_file(git_owner_config, fmt("%s/config", path), NULL);
128 rel = xstrdup(fmt("%s", path));
130 rel = xstrdup(fmt("%s", path + strlen(base) + 1));
132 if (!strcmp(rel + strlen(rel) - 5, "/.git"))
133 rel[strlen(rel) - 5] = '\0';
135 repo = cgit_add_repo(rel);
136 if (ctx.cfg.remove_suffix)
137 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
139 repo->name = repo->url;
140 repo->path = xstrdup(path);
142 repo->defbranch = guess_defbranch(repo->path);
145 if ((pwd = getpwuid(st.st_uid)) == NULL) {
146 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
147 path, strerror(errno), errno);
151 if ((p = strchr(pwd->pw_gecos, ',')))
153 owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
157 p = fmt("%s/description", path);
159 readfile(p, &repo->desc, &size);
162 p = fmt("%s/README.html", path);
164 repo->readme = "README.html";
166 if (ctx.cfg.section_from_path) {
167 n = ctx.cfg.section_from_path;
170 while (slash && n && (slash = strchr(slash, '/')))
173 slash = rel + strlen(rel);
174 while (slash && n && (slash = xstrrchr(rel, slash, '/')))
179 repo->section = xstrdup(rel);
181 if (!prefixcmp(repo->name, repo->section)) {
182 repo->name += strlen(repo->section);
183 if (*repo->name == '/')
189 p = fmt("%s/cgitrc", path);
192 parse_configfile(xstrdup(p), &repo_config);
198 static void scan_path(const char *base, const char *path, repo_config_fn fn)
200 DIR *dir = opendir(path);
206 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
207 path, strerror(errno), errno);
210 if (is_git_dir(path)) {
211 add_repo(base, path, fn);
214 if (is_git_dir(fmt("%s/.git", path))) {
215 add_repo(base, fmt("%s/.git", path), fn);
218 while((ent = readdir(dir)) != NULL) {
219 if (ent->d_name[0] == '.') {
220 if (ent->d_name[1] == '\0')
222 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
224 if (!ctx.cfg.scan_hidden_path)
227 buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
229 fprintf(stderr, "Alloc error on %s: %s (%d)\n",
230 path, strerror(errno), errno);
233 sprintf(buf, "%s/%s", path, ent->d_name);
234 if (stat(buf, &st)) {
235 fprintf(stderr, "Error checking path %s: %s (%d)\n",
236 buf, strerror(errno), errno);
240 if (S_ISDIR(st.st_mode))
241 scan_path(base, buf, fn);
248 #define lastc(s) s[strlen(s) - 1]
250 void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
252 char line[MAX_PATH * 2], *z;
256 projects = fopen(projectsfile, "r");
258 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
259 projectsfile, strerror(errno), errno);
262 while (fgets(line, sizeof(line), projects) != NULL) {
263 for (z = &lastc(line);
264 strlen(line) && strchr("\n\r", *z);
268 scan_path(path, fmt("%s/%s", path, line), fn);
270 if ((err = ferror(projects))) {
271 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
272 projectsfile, strerror(err), err);
277 void scan_tree(const char *path, repo_config_fn fn)
279 scan_path(path, path, fn);