blob: 08f3f1da4b40d4b294f4c453e1b77bdd7d1828df [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
John Keeping812cd492015-03-08 16:32:18 +000048static struct cgit_repo *repo;
49static repo_config_fn config_fn;
Lars Hjemli74061ed2009-08-24 00:04:58 +020050
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{
Lukas Fleischer32c27e82016-10-08 15:45:12 +020058 const char *name;
59
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -040060 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);
Jason A. Donenfeld5f2664f2016-02-22 16:04:15 +010066 else if (!strcmp(key, "gitweb.homepage"))
67 config_fn(repo, "homepage", value);
Lukas Fleischer32c27e82016-10-08 15:45:12 +020068 else if (skip_prefix(key, "cgit.", &name))
69 config_fn(repo, name, value);
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -040070
Jason A. Donenfeld119397b2010-07-29 20:38:01 +020071 return 0;
72}
73
Lars Hjemli797110e2010-08-21 15:44:09 +020074static 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
John Keepingfb3655d2013-04-06 10:28:57 +010081static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn)
Lars Hjemli74061ed2009-08-24 00:04:58 +020082{
Lars Hjemli93397a72008-09-15 00:07:12 +020083 struct stat st;
84 struct passwd *pwd;
John Keepingfb3655d2013-04-06 10:28:57 +010085 size_t pathlen;
86 struct strbuf rel = STRBUF_INIT;
87 char *p, *slash;
Lars Hjemli797110e2010-08-21 15:44:09 +020088 int n;
Lars Hjemlie16f1782009-08-18 17:17:41 +020089 size_t size;
Lars Hjemli93397a72008-09-15 00:07:12 +020090
John Keepingfb3655d2013-04-06 10:28:57 +010091 if (stat(path->buf, &st)) {
Lars Hjemli93397a72008-09-15 00:07:12 +020092 fprintf(stderr, "Error accessing %s: %s (%d)\n",
John Keepingfb3655d2013-04-06 10:28:57 +010093 path->buf, strerror(errno), errno);
Lars Hjemli93397a72008-09-15 00:07:12 +020094 return;
95 }
Felix Hanleye0c6f232010-11-08 19:41:13 +010096
John Keepingfb3655d2013-04-06 10:28:57 +010097 strbuf_addch(path, '/');
98 pathlen = path->len;
Felix Hanleye0c6f232010-11-08 19:41:13 +010099
John Keepingfb3655d2013-04-06 10:28:57 +0100100 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 }
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200106
John Keepingfb3655d2013-04-06 10:28:57 +0100107 strbuf_addstr(path, "noweb");
108 if (!stat(path->buf, &st))
109 return;
110 strbuf_setlen(path, pathlen);
111
Christian Hesse79c985e2014-05-29 17:35:46 +0200112 if (!starts_with(path->buf, base))
John Keepingfb3655d2013-04-06 10:28:57 +0100113 strbuf_addbuf(&rel, path);
Lars Hjemli93397a72008-09-15 00:07:12 +0200114 else
John Keepingfb3655d2013-04-06 10:28:57 +0100115 strbuf_addstr(&rel, path->buf + strlen(base) + 1);
Lars Hjemli93397a72008-09-15 00:07:12 +0200116
John Keepingfb3655d2013-04-06 10:28:57 +0100117 if (!strcmp(rel.buf + rel.len - 5, "/.git"))
118 strbuf_setlen(&rel, rel.len - 5);
John Keeping9a725f42013-04-16 18:39:58 +0100119 else if (rel.len && rel.buf[rel.len - 1] == '/')
120 strbuf_setlen(&rel, rel.len - 1);
Lars Hjemli93397a72008-09-15 00:07:12 +0200121
John Keepingfb3655d2013-04-06 10:28:57 +0100122 repo = cgit_add_repo(rel.buf);
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400123 config_fn = fn;
John Keepingfb3655d2013-04-06 10:28:57 +0100124 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 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100129
Lukas Fleischer485b0992014-12-13 11:40:48 +0100130 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 }
John Keepingfb3655d2013-04-06 10:28:57 +0100136 repo->path = xstrdup(path->buf);
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400137 while (!repo->owner) {
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200138 if ((pwd = getpwuid(st.st_uid)) == NULL) {
139 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
John Keepingfb3655d2013-04-06 10:28:57 +0100140 path->buf, strerror(errno), errno);
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200141 break;
142 }
143 if (pwd->pw_gecos)
144 if ((p = strchr(pwd->pw_gecos, ',')))
145 *p = '\0';
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400146 repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200147 }
Lars Hjemli93397a72008-09-15 00:07:12 +0200148
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400149 if (repo->desc == cgit_default_repo_desc || !repo->desc) {
John Keepingfb3655d2013-04-06 10:28:57 +0100150 strbuf_addstr(path, "description");
151 if (!stat(path->buf, &st))
152 readfile(path->buf, &repo->desc, &size);
153 strbuf_setlen(path, pathlen);
Jason A. Donenfeldb56be4b2012-07-11 04:24:01 +0200154 }
Lars Hjemli93397a72008-09-15 00:07:12 +0200155
Lars Hjemli797110e2010-08-21 15:44:09 +0200156 if (ctx.cfg.section_from_path) {
Lukas Fleischer86e309f2013-06-28 08:58:14 +0000157 n = ctx.cfg.section_from_path;
Lars Hjemli797110e2010-08-21 15:44:09 +0200158 if (n > 0) {
Lukas Fleischer86e309f2013-06-28 08:58:14 +0000159 slash = rel.buf - 1;
160 while (slash && n && (slash = strchr(slash + 1, '/')))
Lars Hjemli797110e2010-08-21 15:44:09 +0200161 n--;
162 } else {
John Keepingfb3655d2013-04-06 10:28:57 +0100163 slash = rel.buf + rel.len;
Lukas Fleischer86e309f2013-06-28 08:58:14 +0000164 while (slash && n && (slash = xstrrchr(rel.buf, slash - 1, '/')))
Lars Hjemli797110e2010-08-21 15:44:09 +0200165 n++;
166 }
167 if (slash && !n) {
168 *slash = '\0';
John Keepingfb3655d2013-04-06 10:28:57 +0100169 repo->section = xstrdup(rel.buf);
Lars Hjemli797110e2010-08-21 15:44:09 +0200170 *slash = '/';
Christian Hesse79c985e2014-05-29 17:35:46 +0200171 if (starts_with(repo->name, repo->section)) {
Lars Hjemli797110e2010-08-21 15:44:09 +0200172 repo->name += strlen(repo->section);
173 if (*repo->name == '/')
174 repo->name++;
175 }
176 }
177 }
Lars Hjemli74061ed2009-08-24 00:04:58 +0200178
John Keepingfb3655d2013-04-06 10:28:57 +0100179 strbuf_addstr(path, "cgitrc");
180 if (!stat(path->buf, &st))
John Keeping687cdf62015-10-08 23:23:57 +0100181 parse_configfile(path->buf, &repo_config);
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400182
John Keepingfb3655d2013-04-06 10:28:57 +0100183 strbuf_release(&rel);
Lars Hjemli93397a72008-09-15 00:07:12 +0200184}
185
Lars Hjemli74061ed2009-08-24 00:04:58 +0200186static void scan_path(const char *base, const char *path, repo_config_fn fn)
Lars Hjemli93397a72008-09-15 00:07:12 +0200187{
Johan Herland682adbc2010-11-15 20:40:43 +0100188 DIR *dir = opendir(path);
Lars Hjemli93397a72008-09-15 00:07:12 +0200189 struct dirent *ent;
John Keepingfb3655d2013-04-06 10:28:57 +0100190 struct strbuf pathbuf = STRBUF_INIT;
191 size_t pathlen = strlen(path);
Lars Hjemli93397a72008-09-15 00:07:12 +0200192 struct stat st;
193
Lars Hjemli93397a72008-09-15 00:07:12 +0200194 if (!dir) {
195 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
196 path, strerror(errno), errno);
197 return;
198 }
John Keepingfb3655d2013-04-06 10:28:57 +0100199
200 strbuf_add(&pathbuf, path, strlen(path));
201 if (is_git_dir(pathbuf.buf)) {
202 add_repo(base, &pathbuf, fn);
Johan Herland682adbc2010-11-15 20:40:43 +0100203 goto end;
204 }
John Keepingfb3655d2013-04-06 10:28:57 +0100205 strbuf_addstr(&pathbuf, "/.git");
206 if (is_git_dir(pathbuf.buf)) {
207 add_repo(base, &pathbuf, fn);
Johan Herland682adbc2010-11-15 20:40:43 +0100208 goto end;
209 }
John Keepingfb3655d2013-04-06 10:28:57 +0100210 /*
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++;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500215 while ((ent = readdir(dir)) != NULL) {
Lars Hjemli93397a72008-09-15 00:07:12 +0200216 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;
Johan Herlanddf522792010-11-15 20:41:00 +0100221 if (!ctx.cfg.scan_hidden_path)
222 continue;
Lars Hjemli93397a72008-09-15 00:07:12 +0200223 }
John Keepingfb3655d2013-04-06 10:28:57 +0100224 strbuf_setlen(&pathbuf, pathlen);
225 strbuf_addstr(&pathbuf, ent->d_name);
226 if (stat(pathbuf.buf, &st)) {
Lars Hjemli93397a72008-09-15 00:07:12 +0200227 fprintf(stderr, "Error checking path %s: %s (%d)\n",
John Keepingfb3655d2013-04-06 10:28:57 +0100228 pathbuf.buf, strerror(errno), errno);
Lars Hjemli93397a72008-09-15 00:07:12 +0200229 continue;
230 }
231 if (S_ISDIR(st.st_mode))
John Keepingfb3655d2013-04-06 10:28:57 +0100232 scan_path(base, pathbuf.buf, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200233 }
Johan Herland682adbc2010-11-15 20:40:43 +0100234end:
John Keepingfb3655d2013-04-06 10:28:57 +0100235 strbuf_release(&pathbuf);
Lars Hjemli93397a72008-09-15 00:07:12 +0200236 closedir(dir);
237}
238
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200239void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
240{
John Keepingfb3655d2013-04-06 10:28:57 +0100241 struct strbuf line = STRBUF_INIT;
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200242 FILE *projects;
243 int err;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100244
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200245 projects = fopen(projectsfile, "r");
246 if (!projects) {
247 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
248 projectsfile, strerror(errno), errno);
Stefan Gehnf15c5832011-03-26 09:51:39 +0100249 return;
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200250 }
Christian Hesse86bf5b42016-04-30 16:57:51 +0200251 while (strbuf_getline(&line, projects) != EOF) {
John Keepingfb3655d2013-04-06 10:28:57 +0100252 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);
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200257 }
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);
John Keepingfb3655d2013-04-06 10:28:57 +0100263 strbuf_release(&line);
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200264}
265
Lars Hjemli74061ed2009-08-24 00:04:58 +0200266void scan_tree(const char *path, repo_config_fn fn)
Lars Hjemli93397a72008-09-15 00:07:12 +0200267{
Lars Hjemli74061ed2009-08-24 00:04:58 +0200268 scan_path(path, path, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200269}