blob: 8e3cf52b348f2df6436246103e7c2280926c2c99 [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{
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);
Christian Hesse79c985e2014-05-29 17:35:46 +020064 else if (starts_with(key, "cgit."))
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -040065 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
Christian Hesse79c985e2014-05-29 17:35:46 +0200108 if (!starts_with(path->buf, base))
John Keepingfb3655d2013-04-06 10:28:57 +0100109 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
Lukas Fleischer485b0992014-12-13 11:40:48 +0100126 if (ctx.cfg.remove_suffix) {
127 size_t urllen;
128 strip_suffix(repo->url, ".git", &urllen);
129 strip_suffix_mem(repo->url, &urllen, "/");
130 repo->url[urllen] = '\0';
131 }
John Keepingfb3655d2013-04-06 10:28:57 +0100132 repo->path = xstrdup(path->buf);
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400133 while (!repo->owner) {
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200134 if ((pwd = getpwuid(st.st_uid)) == NULL) {
135 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
John Keepingfb3655d2013-04-06 10:28:57 +0100136 path->buf, strerror(errno), errno);
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200137 break;
138 }
139 if (pwd->pw_gecos)
140 if ((p = strchr(pwd->pw_gecos, ',')))
141 *p = '\0';
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400142 repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200143 }
Lars Hjemli93397a72008-09-15 00:07:12 +0200144
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400145 if (repo->desc == cgit_default_repo_desc || !repo->desc) {
John Keepingfb3655d2013-04-06 10:28:57 +0100146 strbuf_addstr(path, "description");
147 if (!stat(path->buf, &st))
148 readfile(path->buf, &repo->desc, &size);
149 strbuf_setlen(path, pathlen);
Jason A. Donenfeldb56be4b2012-07-11 04:24:01 +0200150 }
Lars Hjemli93397a72008-09-15 00:07:12 +0200151
Lars Hjemli797110e2010-08-21 15:44:09 +0200152 if (ctx.cfg.section_from_path) {
Lukas Fleischer86e309f2013-06-28 08:58:14 +0000153 n = ctx.cfg.section_from_path;
Lars Hjemli797110e2010-08-21 15:44:09 +0200154 if (n > 0) {
Lukas Fleischer86e309f2013-06-28 08:58:14 +0000155 slash = rel.buf - 1;
156 while (slash && n && (slash = strchr(slash + 1, '/')))
Lars Hjemli797110e2010-08-21 15:44:09 +0200157 n--;
158 } else {
John Keepingfb3655d2013-04-06 10:28:57 +0100159 slash = rel.buf + rel.len;
Lukas Fleischer86e309f2013-06-28 08:58:14 +0000160 while (slash && n && (slash = xstrrchr(rel.buf, slash - 1, '/')))
Lars Hjemli797110e2010-08-21 15:44:09 +0200161 n++;
162 }
163 if (slash && !n) {
164 *slash = '\0';
John Keepingfb3655d2013-04-06 10:28:57 +0100165 repo->section = xstrdup(rel.buf);
Lars Hjemli797110e2010-08-21 15:44:09 +0200166 *slash = '/';
Christian Hesse79c985e2014-05-29 17:35:46 +0200167 if (starts_with(repo->name, repo->section)) {
Lars Hjemli797110e2010-08-21 15:44:09 +0200168 repo->name += strlen(repo->section);
169 if (*repo->name == '/')
170 repo->name++;
171 }
172 }
173 }
Lars Hjemli74061ed2009-08-24 00:04:58 +0200174
John Keepingfb3655d2013-04-06 10:28:57 +0100175 strbuf_addstr(path, "cgitrc");
176 if (!stat(path->buf, &st))
177 parse_configfile(xstrdup(path->buf), &repo_config);
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400178
John Keepingfb3655d2013-04-06 10:28:57 +0100179 strbuf_release(&rel);
Lars Hjemli93397a72008-09-15 00:07:12 +0200180}
181
Lars Hjemli74061ed2009-08-24 00:04:58 +0200182static void scan_path(const char *base, const char *path, repo_config_fn fn)
Lars Hjemli93397a72008-09-15 00:07:12 +0200183{
Johan Herland682adbc2010-11-15 20:40:43 +0100184 DIR *dir = opendir(path);
Lars Hjemli93397a72008-09-15 00:07:12 +0200185 struct dirent *ent;
John Keepingfb3655d2013-04-06 10:28:57 +0100186 struct strbuf pathbuf = STRBUF_INIT;
187 size_t pathlen = strlen(path);
Lars Hjemli93397a72008-09-15 00:07:12 +0200188 struct stat st;
189
Lars Hjemli93397a72008-09-15 00:07:12 +0200190 if (!dir) {
191 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
192 path, strerror(errno), errno);
193 return;
194 }
John Keepingfb3655d2013-04-06 10:28:57 +0100195
196 strbuf_add(&pathbuf, path, strlen(path));
197 if (is_git_dir(pathbuf.buf)) {
198 add_repo(base, &pathbuf, fn);
Johan Herland682adbc2010-11-15 20:40:43 +0100199 goto end;
200 }
John Keepingfb3655d2013-04-06 10:28:57 +0100201 strbuf_addstr(&pathbuf, "/.git");
202 if (is_git_dir(pathbuf.buf)) {
203 add_repo(base, &pathbuf, fn);
Johan Herland682adbc2010-11-15 20:40:43 +0100204 goto end;
205 }
John Keepingfb3655d2013-04-06 10:28:57 +0100206 /*
207 * Add one because we don't want to lose the trailing '/' when we
208 * reset the length of pathbuf in the loop below.
209 */
210 pathlen++;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500211 while ((ent = readdir(dir)) != NULL) {
Lars Hjemli93397a72008-09-15 00:07:12 +0200212 if (ent->d_name[0] == '.') {
213 if (ent->d_name[1] == '\0')
214 continue;
215 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
216 continue;
Johan Herlanddf522792010-11-15 20:41:00 +0100217 if (!ctx.cfg.scan_hidden_path)
218 continue;
Lars Hjemli93397a72008-09-15 00:07:12 +0200219 }
John Keepingfb3655d2013-04-06 10:28:57 +0100220 strbuf_setlen(&pathbuf, pathlen);
221 strbuf_addstr(&pathbuf, ent->d_name);
222 if (stat(pathbuf.buf, &st)) {
Lars Hjemli93397a72008-09-15 00:07:12 +0200223 fprintf(stderr, "Error checking path %s: %s (%d)\n",
John Keepingfb3655d2013-04-06 10:28:57 +0100224 pathbuf.buf, strerror(errno), errno);
Lars Hjemli93397a72008-09-15 00:07:12 +0200225 continue;
226 }
227 if (S_ISDIR(st.st_mode))
John Keepingfb3655d2013-04-06 10:28:57 +0100228 scan_path(base, pathbuf.buf, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200229 }
Johan Herland682adbc2010-11-15 20:40:43 +0100230end:
John Keepingfb3655d2013-04-06 10:28:57 +0100231 strbuf_release(&pathbuf);
Lars Hjemli93397a72008-09-15 00:07:12 +0200232 closedir(dir);
233}
234
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200235void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
236{
John Keepingfb3655d2013-04-06 10:28:57 +0100237 struct strbuf line = STRBUF_INIT;
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200238 FILE *projects;
239 int err;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100240
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200241 projects = fopen(projectsfile, "r");
242 if (!projects) {
243 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
244 projectsfile, strerror(errno), errno);
Stefan Gehnf15c5832011-03-26 09:51:39 +0100245 return;
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200246 }
John Keepingfb3655d2013-04-06 10:28:57 +0100247 while (strbuf_getline(&line, projects, '\n') != EOF) {
248 if (!line.len)
249 continue;
250 strbuf_insert(&line, 0, "/", 1);
251 strbuf_insert(&line, 0, path, strlen(path));
252 scan_path(path, line.buf, fn);
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200253 }
254 if ((err = ferror(projects))) {
255 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
256 projectsfile, strerror(err), err);
257 }
258 fclose(projects);
John Keepingfb3655d2013-04-06 10:28:57 +0100259 strbuf_release(&line);
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200260}
261
Lars Hjemli74061ed2009-08-24 00:04:58 +0200262void scan_tree(const char *path, repo_config_fn fn)
Lars Hjemli93397a72008-09-15 00:07:12 +0200263{
Lars Hjemli74061ed2009-08-24 00:04:58 +0200264 scan_path(path, path, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200265}