blob: 6a2f65a86b2cda7826df470836f9757ba86abf98 [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"
Jeff Smith86a6d352017-08-09 19:02:56 -050013#include <config.h>
Lars Hjemli93397a72008-09-15 00:07:12 +020014
Lars Hjemli93397a72008-09-15 00:07:12 +020015/* return 1 if path contains a objects/ directory and a HEAD file */
16static int is_git_dir(const char *path)
17{
18 struct stat st;
John Keepingfb3655d2013-04-06 10:28:57 +010019 struct strbuf pathbuf = STRBUF_INIT;
20 int result = 0;
Lars Hjemli93397a72008-09-15 00:07:12 +020021
John Keepingfb3655d2013-04-06 10:28:57 +010022 strbuf_addf(&pathbuf, "%s/objects", path);
23 if (stat(pathbuf.buf, &st)) {
Lars Hjemli93397a72008-09-15 00:07:12 +020024 if (errno != ENOENT)
25 fprintf(stderr, "Error checking path %s: %s (%d)\n",
26 path, strerror(errno), errno);
John Keepingfb3655d2013-04-06 10:28:57 +010027 goto out;
Lars Hjemli93397a72008-09-15 00:07:12 +020028 }
29 if (!S_ISDIR(st.st_mode))
John Keepingfb3655d2013-04-06 10:28:57 +010030 goto out;
Lars Hjemli93397a72008-09-15 00:07:12 +020031
John Keepingfb3655d2013-04-06 10:28:57 +010032 strbuf_reset(&pathbuf);
33 strbuf_addf(&pathbuf, "%s/HEAD", path);
34 if (stat(pathbuf.buf, &st)) {
Lars Hjemli93397a72008-09-15 00:07:12 +020035 if (errno != ENOENT)
36 fprintf(stderr, "Error checking path %s: %s (%d)\n",
37 path, strerror(errno), errno);
John Keepingfb3655d2013-04-06 10:28:57 +010038 goto out;
Lars Hjemli93397a72008-09-15 00:07:12 +020039 }
40 if (!S_ISREG(st.st_mode))
John Keepingfb3655d2013-04-06 10:28:57 +010041 goto out;
Lars Hjemli93397a72008-09-15 00:07:12 +020042
John Keepingfb3655d2013-04-06 10:28:57 +010043 result = 1;
44out:
45 strbuf_release(&pathbuf);
46 return result;
Lars Hjemli93397a72008-09-15 00:07:12 +020047}
48
John Keeping812cd492015-03-08 16:32:18 +000049static struct cgit_repo *repo;
50static repo_config_fn config_fn;
Lars Hjemli74061ed2009-08-24 00:04:58 +020051
Jeff Smith86a6d352017-08-09 19:02:56 -050052static void scan_tree_repo_config(const char *name, const char *value)
Lars Hjemli93397a72008-09-15 00:07:12 +020053{
Lars Hjemli74061ed2009-08-24 00:04:58 +020054 config_fn(repo, name, value);
55}
56
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -040057static int gitconfig_config(const char *key, const char *value, void *cb)
Jason A. Donenfeld119397b2010-07-29 20:38:01 +020058{
Lukas Fleischer32c27e82016-10-08 15:45:12 +020059 const char *name;
60
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -040061 if (!strcmp(key, "gitweb.owner"))
62 config_fn(repo, "owner", value);
63 else if (!strcmp(key, "gitweb.description"))
64 config_fn(repo, "desc", value);
65 else if (!strcmp(key, "gitweb.category"))
66 config_fn(repo, "section", value);
Jason A. Donenfeld5f2664f2016-02-22 16:04:15 +010067 else if (!strcmp(key, "gitweb.homepage"))
68 config_fn(repo, "homepage", value);
Lukas Fleischer32c27e82016-10-08 15:45:12 +020069 else if (skip_prefix(key, "cgit.", &name))
70 config_fn(repo, name, value);
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -040071
Jason A. Donenfeld119397b2010-07-29 20:38:01 +020072 return 0;
73}
74
Lars Hjemli797110e2010-08-21 15:44:09 +020075static char *xstrrchr(char *s, char *from, int c)
76{
77 while (from >= s && *from != c)
78 from--;
79 return from < s ? NULL : from;
80}
81
John Keepingfb3655d2013-04-06 10:28:57 +010082static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn)
Lars Hjemli74061ed2009-08-24 00:04:58 +020083{
Lars Hjemli93397a72008-09-15 00:07:12 +020084 struct stat st;
85 struct passwd *pwd;
John Keepingfb3655d2013-04-06 10:28:57 +010086 size_t pathlen;
87 struct strbuf rel = STRBUF_INIT;
88 char *p, *slash;
Lars Hjemli797110e2010-08-21 15:44:09 +020089 int n;
Lars Hjemlie16f1782009-08-18 17:17:41 +020090 size_t size;
Lars Hjemli93397a72008-09-15 00:07:12 +020091
John Keepingfb3655d2013-04-06 10:28:57 +010092 if (stat(path->buf, &st)) {
Lars Hjemli93397a72008-09-15 00:07:12 +020093 fprintf(stderr, "Error accessing %s: %s (%d)\n",
John Keepingfb3655d2013-04-06 10:28:57 +010094 path->buf, strerror(errno), errno);
Lars Hjemli93397a72008-09-15 00:07:12 +020095 return;
96 }
Felix Hanleye0c6f232010-11-08 19:41:13 +010097
John Keepingfb3655d2013-04-06 10:28:57 +010098 strbuf_addch(path, '/');
99 pathlen = path->len;
Felix Hanleye0c6f232010-11-08 19:41:13 +0100100
John Keepingfb3655d2013-04-06 10:28:57 +0100101 if (ctx.cfg.strict_export) {
102 strbuf_addstr(path, ctx.cfg.strict_export);
103 if(stat(path->buf, &st))
104 return;
105 strbuf_setlen(path, pathlen);
106 }
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200107
John Keepingfb3655d2013-04-06 10:28:57 +0100108 strbuf_addstr(path, "noweb");
109 if (!stat(path->buf, &st))
110 return;
111 strbuf_setlen(path, pathlen);
112
Christian Hesse79c985e2014-05-29 17:35:46 +0200113 if (!starts_with(path->buf, base))
John Keepingfb3655d2013-04-06 10:28:57 +0100114 strbuf_addbuf(&rel, path);
Lars Hjemli93397a72008-09-15 00:07:12 +0200115 else
John Keepingfb3655d2013-04-06 10:28:57 +0100116 strbuf_addstr(&rel, path->buf + strlen(base) + 1);
Lars Hjemli93397a72008-09-15 00:07:12 +0200117
John Keepingfb3655d2013-04-06 10:28:57 +0100118 if (!strcmp(rel.buf + rel.len - 5, "/.git"))
119 strbuf_setlen(&rel, rel.len - 5);
John Keeping9a725f42013-04-16 18:39:58 +0100120 else if (rel.len && rel.buf[rel.len - 1] == '/')
121 strbuf_setlen(&rel, rel.len - 1);
Lars Hjemli93397a72008-09-15 00:07:12 +0200122
John Keepingfb3655d2013-04-06 10:28:57 +0100123 repo = cgit_add_repo(rel.buf);
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400124 config_fn = fn;
John Keepingfb3655d2013-04-06 10:28:57 +0100125 if (ctx.cfg.enable_git_config) {
126 strbuf_addstr(path, "config");
127 git_config_from_file(gitconfig_config, path->buf, NULL);
128 strbuf_setlen(path, pathlen);
129 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100130
Lukas Fleischer485b0992014-12-13 11:40:48 +0100131 if (ctx.cfg.remove_suffix) {
132 size_t urllen;
133 strip_suffix(repo->url, ".git", &urllen);
134 strip_suffix_mem(repo->url, &urllen, "/");
135 repo->url[urllen] = '\0';
136 }
John Keepingfb3655d2013-04-06 10:28:57 +0100137 repo->path = xstrdup(path->buf);
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400138 while (!repo->owner) {
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200139 if ((pwd = getpwuid(st.st_uid)) == NULL) {
140 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
John Keepingfb3655d2013-04-06 10:28:57 +0100141 path->buf, strerror(errno), errno);
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200142 break;
143 }
144 if (pwd->pw_gecos)
145 if ((p = strchr(pwd->pw_gecos, ',')))
146 *p = '\0';
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400147 repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200148 }
Lars Hjemli93397a72008-09-15 00:07:12 +0200149
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400150 if (repo->desc == cgit_default_repo_desc || !repo->desc) {
John Keepingfb3655d2013-04-06 10:28:57 +0100151 strbuf_addstr(path, "description");
152 if (!stat(path->buf, &st))
153 readfile(path->buf, &repo->desc, &size);
154 strbuf_setlen(path, pathlen);
Jason A. Donenfeldb56be4b2012-07-11 04:24:01 +0200155 }
Lars Hjemli93397a72008-09-15 00:07:12 +0200156
Lars Hjemli797110e2010-08-21 15:44:09 +0200157 if (ctx.cfg.section_from_path) {
Lukas Fleischer86e309f2013-06-28 08:58:14 +0000158 n = ctx.cfg.section_from_path;
Lars Hjemli797110e2010-08-21 15:44:09 +0200159 if (n > 0) {
Lukas Fleischer86e309f2013-06-28 08:58:14 +0000160 slash = rel.buf - 1;
161 while (slash && n && (slash = strchr(slash + 1, '/')))
Lars Hjemli797110e2010-08-21 15:44:09 +0200162 n--;
163 } else {
John Keepingfb3655d2013-04-06 10:28:57 +0100164 slash = rel.buf + rel.len;
Lukas Fleischer86e309f2013-06-28 08:58:14 +0000165 while (slash && n && (slash = xstrrchr(rel.buf, slash - 1, '/')))
Lars Hjemli797110e2010-08-21 15:44:09 +0200166 n++;
167 }
168 if (slash && !n) {
169 *slash = '\0';
John Keepingfb3655d2013-04-06 10:28:57 +0100170 repo->section = xstrdup(rel.buf);
Lars Hjemli797110e2010-08-21 15:44:09 +0200171 *slash = '/';
Christian Hesse79c985e2014-05-29 17:35:46 +0200172 if (starts_with(repo->name, repo->section)) {
Lars Hjemli797110e2010-08-21 15:44:09 +0200173 repo->name += strlen(repo->section);
174 if (*repo->name == '/')
175 repo->name++;
176 }
177 }
178 }
Lars Hjemli74061ed2009-08-24 00:04:58 +0200179
John Keepingfb3655d2013-04-06 10:28:57 +0100180 strbuf_addstr(path, "cgitrc");
181 if (!stat(path->buf, &st))
Jeff Smith86a6d352017-08-09 19:02:56 -0500182 parse_configfile(path->buf, &scan_tree_repo_config);
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400183
John Keepingfb3655d2013-04-06 10:28:57 +0100184 strbuf_release(&rel);
Lars Hjemli93397a72008-09-15 00:07:12 +0200185}
186
Lars Hjemli74061ed2009-08-24 00:04:58 +0200187static void scan_path(const char *base, const char *path, repo_config_fn fn)
Lars Hjemli93397a72008-09-15 00:07:12 +0200188{
Johan Herland682adbc2010-11-15 20:40:43 +0100189 DIR *dir = opendir(path);
Lars Hjemli93397a72008-09-15 00:07:12 +0200190 struct dirent *ent;
John Keepingfb3655d2013-04-06 10:28:57 +0100191 struct strbuf pathbuf = STRBUF_INIT;
192 size_t pathlen = strlen(path);
Lars Hjemli93397a72008-09-15 00:07:12 +0200193 struct stat st;
194
Lars Hjemli93397a72008-09-15 00:07:12 +0200195 if (!dir) {
196 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
197 path, strerror(errno), errno);
198 return;
199 }
John Keepingfb3655d2013-04-06 10:28:57 +0100200
201 strbuf_add(&pathbuf, path, strlen(path));
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 strbuf_addstr(&pathbuf, "/.git");
207 if (is_git_dir(pathbuf.buf)) {
208 add_repo(base, &pathbuf, fn);
Johan Herland682adbc2010-11-15 20:40:43 +0100209 goto end;
210 }
John Keepingfb3655d2013-04-06 10:28:57 +0100211 /*
212 * Add one because we don't want to lose the trailing '/' when we
213 * reset the length of pathbuf in the loop below.
214 */
215 pathlen++;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500216 while ((ent = readdir(dir)) != NULL) {
Lars Hjemli93397a72008-09-15 00:07:12 +0200217 if (ent->d_name[0] == '.') {
218 if (ent->d_name[1] == '\0')
219 continue;
220 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
221 continue;
Johan Herlanddf522792010-11-15 20:41:00 +0100222 if (!ctx.cfg.scan_hidden_path)
223 continue;
Lars Hjemli93397a72008-09-15 00:07:12 +0200224 }
John Keepingfb3655d2013-04-06 10:28:57 +0100225 strbuf_setlen(&pathbuf, pathlen);
226 strbuf_addstr(&pathbuf, ent->d_name);
227 if (stat(pathbuf.buf, &st)) {
Lars Hjemli93397a72008-09-15 00:07:12 +0200228 fprintf(stderr, "Error checking path %s: %s (%d)\n",
John Keepingfb3655d2013-04-06 10:28:57 +0100229 pathbuf.buf, strerror(errno), errno);
Lars Hjemli93397a72008-09-15 00:07:12 +0200230 continue;
231 }
232 if (S_ISDIR(st.st_mode))
John Keepingfb3655d2013-04-06 10:28:57 +0100233 scan_path(base, pathbuf.buf, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200234 }
Johan Herland682adbc2010-11-15 20:40:43 +0100235end:
John Keepingfb3655d2013-04-06 10:28:57 +0100236 strbuf_release(&pathbuf);
Lars Hjemli93397a72008-09-15 00:07:12 +0200237 closedir(dir);
238}
239
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200240void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
241{
John Keepingfb3655d2013-04-06 10:28:57 +0100242 struct strbuf line = STRBUF_INIT;
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200243 FILE *projects;
244 int err;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100245
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200246 projects = fopen(projectsfile, "r");
247 if (!projects) {
248 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
249 projectsfile, strerror(errno), errno);
Stefan Gehnf15c5832011-03-26 09:51:39 +0100250 return;
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200251 }
Christian Hesse86bf5b42016-04-30 16:57:51 +0200252 while (strbuf_getline(&line, projects) != EOF) {
John Keepingfb3655d2013-04-06 10:28:57 +0100253 if (!line.len)
254 continue;
255 strbuf_insert(&line, 0, "/", 1);
256 strbuf_insert(&line, 0, path, strlen(path));
257 scan_path(path, line.buf, fn);
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200258 }
259 if ((err = ferror(projects))) {
260 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
261 projectsfile, strerror(err), err);
262 }
263 fclose(projects);
John Keepingfb3655d2013-04-06 10:28:57 +0100264 strbuf_release(&line);
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200265}
266
Lars Hjemli74061ed2009-08-24 00:04:58 +0200267void scan_tree(const char *path, repo_config_fn fn)
Lars Hjemli93397a72008-09-15 00:07:12 +0200268{
Lars Hjemli74061ed2009-08-24 00:04:58 +0200269 scan_path(path, path, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200270}