all repos — cgit @ v1.1

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	const char *name;
 59
 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);
 70
 71	return 0;
 72}
 73
 74static char *xstrrchr(char *s, char *from, int c)
 75{
 76	while (from >= s && *from != c)
 77		from--;
 78	return from < s ? NULL : from;
 79}
 80
 81static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn)
 82{
 83	struct stat st;
 84	struct passwd *pwd;
 85	size_t pathlen;
 86	struct strbuf rel = STRBUF_INIT;
 87	char *p, *slash;
 88	int n;
 89	size_t size;
 90
 91	if (stat(path->buf, &st)) {
 92		fprintf(stderr, "Error accessing %s: %s (%d)\n",
 93			path->buf, strerror(errno), errno);
 94		return;
 95	}
 96
 97	strbuf_addch(path, '/');
 98	pathlen = path->len;
 99
100	if (ctx.cfg.strict_export) {
101		strbuf_addstr(path, ctx.cfg.strict_export);
102		if(stat(path->buf, &st))
103			return;
104		strbuf_setlen(path, pathlen);
105	}
106
107	strbuf_addstr(path, "noweb");
108	if (!stat(path->buf, &st))
109		return;
110	strbuf_setlen(path, pathlen);
111
112	if (!starts_with(path->buf, base))
113		strbuf_addbuf(&rel, path);
114	else
115		strbuf_addstr(&rel, path->buf + strlen(base) + 1);
116
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);
121
122	repo = cgit_add_repo(rel.buf);
123	config_fn = fn;
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);
128	}
129
130	if (ctx.cfg.remove_suffix) {
131		size_t urllen;
132		strip_suffix(repo->url, ".git", &urllen);
133		strip_suffix_mem(repo->url, &urllen, "/");
134		repo->url[urllen] = '\0';
135	}
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);
141			break;
142		}
143		if (pwd->pw_gecos)
144			if ((p = strchr(pwd->pw_gecos, ',')))
145				*p = '\0';
146		repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
147	}
148
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);
154	}
155
156	if (ctx.cfg.section_from_path) {
157		n = ctx.cfg.section_from_path;
158		if (n > 0) {
159			slash = rel.buf - 1;
160			while (slash && n && (slash = strchr(slash + 1, '/')))
161				n--;
162		} else {
163			slash = rel.buf + rel.len;
164			while (slash && n && (slash = xstrrchr(rel.buf, slash - 1, '/')))
165				n++;
166		}
167		if (slash && !n) {
168			*slash = '\0';
169			repo->section = xstrdup(rel.buf);
170			*slash = '/';
171			if (starts_with(repo->name, repo->section)) {
172				repo->name += strlen(repo->section);
173				if (*repo->name == '/')
174					repo->name++;
175			}
176		}
177	}
178
179	strbuf_addstr(path, "cgitrc");
180	if (!stat(path->buf, &st))
181		parse_configfile(path->buf, &repo_config);
182
183	strbuf_release(&rel);
184}
185
186static void scan_path(const char *base, const char *path, repo_config_fn fn)
187{
188	DIR *dir = opendir(path);
189	struct dirent *ent;
190	struct strbuf pathbuf = STRBUF_INIT;
191	size_t pathlen = strlen(path);
192	struct stat st;
193
194	if (!dir) {
195		fprintf(stderr, "Error opening directory %s: %s (%d)\n",
196			path, strerror(errno), errno);
197		return;
198	}
199
200	strbuf_add(&pathbuf, path, strlen(path));
201	if (is_git_dir(pathbuf.buf)) {
202		add_repo(base, &pathbuf, fn);
203		goto end;
204	}
205	strbuf_addstr(&pathbuf, "/.git");
206	if (is_git_dir(pathbuf.buf)) {
207		add_repo(base, &pathbuf, fn);
208		goto end;
209	}
210	/*
211	 * Add one because we don't want to lose the trailing '/' when we
212	 * reset the length of pathbuf in the loop below.
213	 */
214	pathlen++;
215	while ((ent = readdir(dir)) != NULL) {
216		if (ent->d_name[0] == '.') {
217			if (ent->d_name[1] == '\0')
218				continue;
219			if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
220				continue;
221			if (!ctx.cfg.scan_hidden_path)
222				continue;
223		}
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);
229			continue;
230		}
231		if (S_ISDIR(st.st_mode))
232			scan_path(base, pathbuf.buf, fn);
233	}
234end:
235	strbuf_release(&pathbuf);
236	closedir(dir);
237}
238
239void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
240{
241	struct strbuf line = STRBUF_INIT;
242	FILE *projects;
243	int err;
244
245	projects = fopen(projectsfile, "r");
246	if (!projects) {
247		fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
248			projectsfile, strerror(errno), errno);
249		return;
250	}
251	while (strbuf_getline(&line, projects) != EOF) {
252		if (!line.len)
253			continue;
254		strbuf_insert(&line, 0, "/", 1);
255		strbuf_insert(&line, 0, path, strlen(path));
256		scan_path(path, line.buf, fn);
257	}
258	if ((err = ferror(projects))) {
259		fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
260			projectsfile, strerror(err), err);
261	}
262	fclose(projects);
263	strbuf_release(&line);
264}
265
266void scan_tree(const char *path, repo_config_fn fn)
267{
268	scan_path(path, path, fn);
269}