all repos — cgit @ 390ffad022336b02dca6dec23504cefeb12adada

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