all repos — cgit @ 927b0ae30c84fbfce877e35415681dce6eba0229

a hyperfast web frontend for git written in c

scan-tree.c (view raw)

  1/* scan-tree.c
  2 *
  3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
  4 *
  5 * Licensed under GNU General Public License v2
  6 *   (see COPYING for full license text)
  7 */
  8
  9#include "cgit.h"
 10#include "scan-tree.h"
 11#include "configfile.h"
 12#include "html.h"
 13
 14/* return 1 if path contains a objects/ directory and a HEAD file */
 15static int is_git_dir(const char *path)
 16{
 17	struct stat st;
 18	struct strbuf pathbuf = STRBUF_INIT;
 19	int result = 0;
 20
 21	strbuf_addf(&pathbuf, "%s/objects", path);
 22	if (stat(pathbuf.buf, &st)) {
 23		if (errno != ENOENT)
 24			fprintf(stderr, "Error checking path %s: %s (%d)\n",
 25				path, strerror(errno), errno);
 26		goto out;
 27	}
 28	if (!S_ISDIR(st.st_mode))
 29		goto out;
 30
 31	strbuf_reset(&pathbuf);
 32	strbuf_addf(&pathbuf, "%s/HEAD", path);
 33	if (stat(pathbuf.buf, &st)) {
 34		if (errno != ENOENT)
 35			fprintf(stderr, "Error checking path %s: %s (%d)\n",
 36				path, strerror(errno), errno);
 37		goto out;
 38	}
 39	if (!S_ISREG(st.st_mode))
 40		goto out;
 41
 42	result = 1;
 43out:
 44	strbuf_release(&pathbuf);
 45	return result;
 46}
 47
 48static struct cgit_repo *repo;
 49static repo_config_fn config_fn;
 50
 51static void repo_config(const char *name, const char *value)
 52{
 53	config_fn(repo, name, value);
 54}
 55
 56static int gitconfig_config(const char *key, const char *value, void *cb)
 57{
 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 (!strcmp(key, "gitweb.homepage"))
 65		config_fn(repo, "homepage", value);
 66	else if (starts_with(key, "cgit."))
 67		config_fn(repo, key + 5, value);
 68
 69	return 0;
 70}
 71
 72static char *xstrrchr(char *s, char *from, int c)
 73{
 74	while (from >= s && *from != c)
 75		from--;
 76	return from < s ? NULL : from;
 77}
 78
 79static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn)
 80{
 81	struct stat st;
 82	struct passwd *pwd;
 83	size_t pathlen;
 84	struct strbuf rel = STRBUF_INIT;
 85	char *p, *slash;
 86	int n;
 87	size_t size;
 88
 89	if (stat(path->buf, &st)) {
 90		fprintf(stderr, "Error accessing %s: %s (%d)\n",
 91			path->buf, strerror(errno), errno);
 92		return;
 93	}
 94
 95	strbuf_addch(path, '/');
 96	pathlen = path->len;
 97
 98	if (ctx.cfg.strict_export) {
 99		strbuf_addstr(path, ctx.cfg.strict_export);
100		if(stat(path->buf, &st))
101			return;
102		strbuf_setlen(path, pathlen);
103	}
104
105	strbuf_addstr(path, "noweb");
106	if (!stat(path->buf, &st))
107		return;
108	strbuf_setlen(path, pathlen);
109
110	if (!starts_with(path->buf, base))
111		strbuf_addbuf(&rel, path);
112	else
113		strbuf_addstr(&rel, path->buf + strlen(base) + 1);
114
115	if (!strcmp(rel.buf + rel.len - 5, "/.git"))
116		strbuf_setlen(&rel, rel.len - 5);
117	else if (rel.len && rel.buf[rel.len - 1] == '/')
118		strbuf_setlen(&rel, rel.len - 1);
119
120	repo = cgit_add_repo(rel.buf);
121	config_fn = fn;
122	if (ctx.cfg.enable_git_config) {
123		strbuf_addstr(path, "config");
124		git_config_from_file(gitconfig_config, path->buf, NULL);
125		strbuf_setlen(path, pathlen);
126	}
127
128	if (ctx.cfg.remove_suffix) {
129		size_t urllen;
130		strip_suffix(repo->url, ".git", &urllen);
131		strip_suffix_mem(repo->url, &urllen, "/");
132		repo->url[urllen] = '\0';
133	}
134	repo->path = xstrdup(path->buf);
135	while (!repo->owner) {
136		if ((pwd = getpwuid(st.st_uid)) == NULL) {
137			fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
138				path->buf, strerror(errno), errno);
139			break;
140		}
141		if (pwd->pw_gecos)
142			if ((p = strchr(pwd->pw_gecos, ',')))
143				*p = '\0';
144		repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
145	}
146
147	if (repo->desc == cgit_default_repo_desc || !repo->desc) {
148		strbuf_addstr(path, "description");
149		if (!stat(path->buf, &st))
150			readfile(path->buf, &repo->desc, &size);
151		strbuf_setlen(path, pathlen);
152	}
153
154	if (ctx.cfg.section_from_path) {
155		n = ctx.cfg.section_from_path;
156		if (n > 0) {
157			slash = rel.buf - 1;
158			while (slash && n && (slash = strchr(slash + 1, '/')))
159				n--;
160		} else {
161			slash = rel.buf + rel.len;
162			while (slash && n && (slash = xstrrchr(rel.buf, slash - 1, '/')))
163				n++;
164		}
165		if (slash && !n) {
166			*slash = '\0';
167			repo->section = xstrdup(rel.buf);
168			*slash = '/';
169			if (starts_with(repo->name, repo->section)) {
170				repo->name += strlen(repo->section);
171				if (*repo->name == '/')
172					repo->name++;
173			}
174		}
175	}
176
177	strbuf_addstr(path, "cgitrc");
178	if (!stat(path->buf, &st))
179		parse_configfile(path->buf, &repo_config);
180
181	strbuf_release(&rel);
182}
183
184static void scan_path(const char *base, const char *path, repo_config_fn fn)
185{
186	DIR *dir = opendir(path);
187	struct dirent *ent;
188	struct strbuf pathbuf = STRBUF_INIT;
189	size_t pathlen = strlen(path);
190	struct stat st;
191
192	if (!dir) {
193		fprintf(stderr, "Error opening directory %s: %s (%d)\n",
194			path, strerror(errno), errno);
195		return;
196	}
197
198	strbuf_add(&pathbuf, path, strlen(path));
199	if (is_git_dir(pathbuf.buf)) {
200		add_repo(base, &pathbuf, fn);
201		goto end;
202	}
203	strbuf_addstr(&pathbuf, "/.git");
204	if (is_git_dir(pathbuf.buf)) {
205		add_repo(base, &pathbuf, fn);
206		goto end;
207	}
208	/*
209	 * Add one because we don't want to lose the trailing '/' when we
210	 * reset the length of pathbuf in the loop below.
211	 */
212	pathlen++;
213	while ((ent = readdir(dir)) != NULL) {
214		if (ent->d_name[0] == '.') {
215			if (ent->d_name[1] == '\0')
216				continue;
217			if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
218				continue;
219			if (!ctx.cfg.scan_hidden_path)
220				continue;
221		}
222		strbuf_setlen(&pathbuf, pathlen);
223		strbuf_addstr(&pathbuf, ent->d_name);
224		if (stat(pathbuf.buf, &st)) {
225			fprintf(stderr, "Error checking path %s: %s (%d)\n",
226				pathbuf.buf, strerror(errno), errno);
227			continue;
228		}
229		if (S_ISDIR(st.st_mode))
230			scan_path(base, pathbuf.buf, fn);
231	}
232end:
233	strbuf_release(&pathbuf);
234	closedir(dir);
235}
236
237void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
238{
239	struct strbuf line = STRBUF_INIT;
240	FILE *projects;
241	int err;
242
243	projects = fopen(projectsfile, "r");
244	if (!projects) {
245		fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
246			projectsfile, strerror(errno), errno);
247		return;
248	}
249	while (strbuf_getline(&line, projects) != EOF) {
250		if (!line.len)
251			continue;
252		strbuf_insert(&line, 0, "/", 1);
253		strbuf_insert(&line, 0, path, strlen(path));
254		scan_path(path, line.buf, fn);
255	}
256	if ((err = ferror(projects))) {
257		fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
258			projectsfile, strerror(err), err);
259	}
260	fclose(projects);
261	strbuf_release(&line);
262}
263
264void scan_tree(const char *path, repo_config_fn fn)
265{
266	scan_path(path, path, fn);
267}