blob: 1a2ea87e5008afe396df231114fabb59df8ce1b2 [file] [log] [blame]
Jason A. Donenfeld35165022010-07-29 17:52:29 +02001/* scan-tree.c
Lukas Fleischerf7f26f82014-01-08 15:10:49 +01002 *
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
Jason A. Donenfeld35165022010-07-29 17:52:29 +02004 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
Lars Hjemli93397a72008-09-15 00:07:12 +02009#include "cgit.h"
John Keeping8f208792013-04-06 11:37:59 +010010#include "scan-tree.h"
Lars Hjemli74061ed2009-08-24 00:04:58 +020011#include "configfile.h"
Lars Hjemli93397a72008-09-15 00:07:12 +020012#include "html.h"
13
Lars Hjemli93397a72008-09-15 00:07:12 +020014/* 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;
John Keepingfb3655d2013-04-06 10:28:57 +010018 struct strbuf pathbuf = STRBUF_INIT;
19 int result = 0;
Lars Hjemli93397a72008-09-15 00:07:12 +020020
John Keepingfb3655d2013-04-06 10:28:57 +010021 strbuf_addf(&pathbuf, "%s/objects", path);
22 if (stat(pathbuf.buf, &st)) {
Lars Hjemli93397a72008-09-15 00:07:12 +020023 if (errno != ENOENT)
24 fprintf(stderr, "Error checking path %s: %s (%d)\n",
25 path, strerror(errno), errno);
John Keepingfb3655d2013-04-06 10:28:57 +010026 goto out;
Lars Hjemli93397a72008-09-15 00:07:12 +020027 }
28 if (!S_ISDIR(st.st_mode))
John Keepingfb3655d2013-04-06 10:28:57 +010029 goto out;
Lars Hjemli93397a72008-09-15 00:07:12 +020030
John Keepingfb3655d2013-04-06 10:28:57 +010031 strbuf_reset(&pathbuf);
32 strbuf_addf(&pathbuf, "%s/HEAD", path);
33 if (stat(pathbuf.buf, &st)) {
Lars Hjemli93397a72008-09-15 00:07:12 +020034 if (errno != ENOENT)
35 fprintf(stderr, "Error checking path %s: %s (%d)\n",
36 path, strerror(errno), errno);
John Keepingfb3655d2013-04-06 10:28:57 +010037 goto out;
Lars Hjemli93397a72008-09-15 00:07:12 +020038 }
39 if (!S_ISREG(st.st_mode))
John Keepingfb3655d2013-04-06 10:28:57 +010040 goto out;
Lars Hjemli93397a72008-09-15 00:07:12 +020041
John Keepingfb3655d2013-04-06 10:28:57 +010042 result = 1;
43out:
44 strbuf_release(&pathbuf);
45 return result;
Lars Hjemli93397a72008-09-15 00:07:12 +020046}
47
Lars Hjemli74061ed2009-08-24 00:04:58 +020048struct cgit_repo *repo;
49repo_config_fn config_fn;
50
51static void repo_config(const char *name, const char *value)
Lars Hjemli93397a72008-09-15 00:07:12 +020052{
Lars Hjemli74061ed2009-08-24 00:04:58 +020053 config_fn(repo, name, value);
54}
55
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -040056static int gitconfig_config(const char *key, const char *value, void *cb)
Jason A. Donenfeld119397b2010-07-29 20:38:01 +020057{
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -040058 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 (!prefixcmp(key, "cgit."))
65 config_fn(repo, key + 5, value);
66
Jason A. Donenfeld119397b2010-07-29 20:38:01 +020067 return 0;
68}
69
Lars Hjemli797110e2010-08-21 15:44:09 +020070static 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
John Keepingfb3655d2013-04-06 10:28:57 +010077static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn)
Lars Hjemli74061ed2009-08-24 00:04:58 +020078{
Lars Hjemli93397a72008-09-15 00:07:12 +020079 struct stat st;
80 struct passwd *pwd;
John Keepingfb3655d2013-04-06 10:28:57 +010081 size_t pathlen;
82 struct strbuf rel = STRBUF_INIT;
83 char *p, *slash;
Lars Hjemli797110e2010-08-21 15:44:09 +020084 int n;
Lars Hjemlie16f1782009-08-18 17:17:41 +020085 size_t size;
Lars Hjemli93397a72008-09-15 00:07:12 +020086
John Keepingfb3655d2013-04-06 10:28:57 +010087 if (stat(path->buf, &st)) {
Lars Hjemli93397a72008-09-15 00:07:12 +020088 fprintf(stderr, "Error accessing %s: %s (%d)\n",
John Keepingfb3655d2013-04-06 10:28:57 +010089 path->buf, strerror(errno), errno);
Lars Hjemli93397a72008-09-15 00:07:12 +020090 return;
91 }
Felix Hanleye0c6f232010-11-08 19:41:13 +010092
John Keepingfb3655d2013-04-06 10:28:57 +010093 strbuf_addch(path, '/');
94 pathlen = path->len;
Felix Hanleye0c6f232010-11-08 19:41:13 +010095
John Keepingfb3655d2013-04-06 10:28:57 +010096 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 }
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200102
John Keepingfb3655d2013-04-06 10:28:57 +0100103 strbuf_addstr(path, "noweb");
104 if (!stat(path->buf, &st))
105 return;
106 strbuf_setlen(path, pathlen);
107
108 if (strncmp(base, path->buf, strlen(base)))
109 strbuf_addbuf(&rel, path);
Lars Hjemli93397a72008-09-15 00:07:12 +0200110 else
John Keepingfb3655d2013-04-06 10:28:57 +0100111 strbuf_addstr(&rel, path->buf + strlen(base) + 1);
Lars Hjemli93397a72008-09-15 00:07:12 +0200112
John Keepingfb3655d2013-04-06 10:28:57 +0100113 if (!strcmp(rel.buf + rel.len - 5, "/.git"))
114 strbuf_setlen(&rel, rel.len - 5);
John Keeping9a725f42013-04-16 18:39:58 +0100115 else if (rel.len && rel.buf[rel.len - 1] == '/')
116 strbuf_setlen(&rel, rel.len - 1);
Lars Hjemli93397a72008-09-15 00:07:12 +0200117
John Keepingfb3655d2013-04-06 10:28:57 +0100118 repo = cgit_add_repo(rel.buf);
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400119 config_fn = fn;
John Keepingfb3655d2013-04-06 10:28:57 +0100120 if (ctx.cfg.enable_git_config) {
121 strbuf_addstr(path, "config");
122 git_config_from_file(gitconfig_config, path->buf, NULL);
123 strbuf_setlen(path, pathlen);
124 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100125
Jason A. Donenfeld2e4a9412010-07-29 19:47:50 +0200126 if (ctx.cfg.remove_suffix)
127 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
128 *p = '\0';
John Keepingfb3655d2013-04-06 10:28:57 +0100129 repo->path = xstrdup(path->buf);
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400130 while (!repo->owner) {
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200131 if ((pwd = getpwuid(st.st_uid)) == NULL) {
132 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
John Keepingfb3655d2013-04-06 10:28:57 +0100133 path->buf, strerror(errno), errno);
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200134 break;
135 }
136 if (pwd->pw_gecos)
137 if ((p = strchr(pwd->pw_gecos, ',')))
138 *p = '\0';
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400139 repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200140 }
Lars Hjemli93397a72008-09-15 00:07:12 +0200141
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400142 if (repo->desc == cgit_default_repo_desc || !repo->desc) {
John Keepingfb3655d2013-04-06 10:28:57 +0100143 strbuf_addstr(path, "description");
144 if (!stat(path->buf, &st))
145 readfile(path->buf, &repo->desc, &size);
146 strbuf_setlen(path, pathlen);
Jason A. Donenfeldb56be4b2012-07-11 04:24:01 +0200147 }
Lars Hjemli93397a72008-09-15 00:07:12 +0200148
Lars Hjemli797110e2010-08-21 15:44:09 +0200149 if (ctx.cfg.section_from_path) {
Lukas Fleischer86e309f2013-06-28 08:58:14 +0000150 n = ctx.cfg.section_from_path;
Lars Hjemli797110e2010-08-21 15:44:09 +0200151 if (n > 0) {
Lukas Fleischer86e309f2013-06-28 08:58:14 +0000152 slash = rel.buf - 1;
153 while (slash && n && (slash = strchr(slash + 1, '/')))
Lars Hjemli797110e2010-08-21 15:44:09 +0200154 n--;
155 } else {
John Keepingfb3655d2013-04-06 10:28:57 +0100156 slash = rel.buf + rel.len;
Lukas Fleischer86e309f2013-06-28 08:58:14 +0000157 while (slash && n && (slash = xstrrchr(rel.buf, slash - 1, '/')))
Lars Hjemli797110e2010-08-21 15:44:09 +0200158 n++;
159 }
160 if (slash && !n) {
161 *slash = '\0';
John Keepingfb3655d2013-04-06 10:28:57 +0100162 repo->section = xstrdup(rel.buf);
Lars Hjemli797110e2010-08-21 15:44:09 +0200163 *slash = '/';
164 if (!prefixcmp(repo->name, repo->section)) {
165 repo->name += strlen(repo->section);
166 if (*repo->name == '/')
167 repo->name++;
168 }
169 }
170 }
Lars Hjemli74061ed2009-08-24 00:04:58 +0200171
John Keepingfb3655d2013-04-06 10:28:57 +0100172 strbuf_addstr(path, "cgitrc");
173 if (!stat(path->buf, &st))
174 parse_configfile(xstrdup(path->buf), &repo_config);
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400175
John Keepingfb3655d2013-04-06 10:28:57 +0100176 strbuf_release(&rel);
Lars Hjemli93397a72008-09-15 00:07:12 +0200177}
178
Lars Hjemli74061ed2009-08-24 00:04:58 +0200179static void scan_path(const char *base, const char *path, repo_config_fn fn)
Lars Hjemli93397a72008-09-15 00:07:12 +0200180{
Johan Herland682adbc2010-11-15 20:40:43 +0100181 DIR *dir = opendir(path);
Lars Hjemli93397a72008-09-15 00:07:12 +0200182 struct dirent *ent;
John Keepingfb3655d2013-04-06 10:28:57 +0100183 struct strbuf pathbuf = STRBUF_INIT;
184 size_t pathlen = strlen(path);
Lars Hjemli93397a72008-09-15 00:07:12 +0200185 struct stat st;
186
Lars Hjemli93397a72008-09-15 00:07:12 +0200187 if (!dir) {
188 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
189 path, strerror(errno), errno);
190 return;
191 }
John Keepingfb3655d2013-04-06 10:28:57 +0100192
193 strbuf_add(&pathbuf, path, strlen(path));
194 if (is_git_dir(pathbuf.buf)) {
195 add_repo(base, &pathbuf, fn);
Johan Herland682adbc2010-11-15 20:40:43 +0100196 goto end;
197 }
John Keepingfb3655d2013-04-06 10:28:57 +0100198 strbuf_addstr(&pathbuf, "/.git");
199 if (is_git_dir(pathbuf.buf)) {
200 add_repo(base, &pathbuf, fn);
Johan Herland682adbc2010-11-15 20:40:43 +0100201 goto end;
202 }
John Keepingfb3655d2013-04-06 10:28:57 +0100203 /*
204 * Add one because we don't want to lose the trailing '/' when we
205 * reset the length of pathbuf in the loop below.
206 */
207 pathlen++;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500208 while ((ent = readdir(dir)) != NULL) {
Lars Hjemli93397a72008-09-15 00:07:12 +0200209 if (ent->d_name[0] == '.') {
210 if (ent->d_name[1] == '\0')
211 continue;
212 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
213 continue;
Johan Herlanddf522792010-11-15 20:41:00 +0100214 if (!ctx.cfg.scan_hidden_path)
215 continue;
Lars Hjemli93397a72008-09-15 00:07:12 +0200216 }
John Keepingfb3655d2013-04-06 10:28:57 +0100217 strbuf_setlen(&pathbuf, pathlen);
218 strbuf_addstr(&pathbuf, ent->d_name);
219 if (stat(pathbuf.buf, &st)) {
Lars Hjemli93397a72008-09-15 00:07:12 +0200220 fprintf(stderr, "Error checking path %s: %s (%d)\n",
John Keepingfb3655d2013-04-06 10:28:57 +0100221 pathbuf.buf, strerror(errno), errno);
Lars Hjemli93397a72008-09-15 00:07:12 +0200222 continue;
223 }
224 if (S_ISDIR(st.st_mode))
John Keepingfb3655d2013-04-06 10:28:57 +0100225 scan_path(base, pathbuf.buf, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200226 }
Johan Herland682adbc2010-11-15 20:40:43 +0100227end:
John Keepingfb3655d2013-04-06 10:28:57 +0100228 strbuf_release(&pathbuf);
Lars Hjemli93397a72008-09-15 00:07:12 +0200229 closedir(dir);
230}
231
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200232void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
233{
John Keepingfb3655d2013-04-06 10:28:57 +0100234 struct strbuf line = STRBUF_INIT;
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200235 FILE *projects;
236 int err;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100237
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200238 projects = fopen(projectsfile, "r");
239 if (!projects) {
240 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
241 projectsfile, strerror(errno), errno);
Stefan Gehnf15c5832011-03-26 09:51:39 +0100242 return;
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200243 }
John Keepingfb3655d2013-04-06 10:28:57 +0100244 while (strbuf_getline(&line, projects, '\n') != EOF) {
245 if (!line.len)
246 continue;
247 strbuf_insert(&line, 0, "/", 1);
248 strbuf_insert(&line, 0, path, strlen(path));
249 scan_path(path, line.buf, fn);
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200250 }
251 if ((err = ferror(projects))) {
252 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
253 projectsfile, strerror(err), err);
254 }
255 fclose(projects);
John Keepingfb3655d2013-04-06 10:28:57 +0100256 strbuf_release(&line);
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200257}
258
Lars Hjemli74061ed2009-08-24 00:04:58 +0200259void scan_tree(const char *path, repo_config_fn fn)
Lars Hjemli93397a72008-09-15 00:07:12 +0200260{
Lars Hjemli74061ed2009-08-24 00:04:58 +0200261 scan_path(path, path, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200262}