blob: a1ec8fb7ac73cbc4416245087390849d199f822c [file] [log] [blame]
Jason A. Donenfeld35165022010-07-29 17:52:29 +02001/* scan-tree.c
2 *
3 * Copyright (C) 2008-2009 Lars Hjemli
Jason A. Donenfeld7f08e032012-07-12 20:00:40 +02004 * Copyright (C) 2010, 2012 Jason A. Donenfeld <Jason@zx2c4.com>
Jason A. Donenfeld35165022010-07-29 17:52:29 +02005 *
6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text)
8 */
9
Lars Hjemli93397a72008-09-15 00:07:12 +020010#include "cgit.h"
John Keeping8f208792013-04-06 11:37:59 +010011#include "scan-tree.h"
Lars Hjemli74061ed2009-08-24 00:04:58 +020012#include "configfile.h"
Lars Hjemli93397a72008-09-15 00:07:12 +020013#include "html.h"
14
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
Lars Hjemli74061ed2009-08-24 00:04:58 +020049struct cgit_repo *repo;
50repo_config_fn config_fn;
51
52static void 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{
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -040059 if (!strcmp(key, "gitweb.owner"))
60 config_fn(repo, "owner", value);
61 else if (!strcmp(key, "gitweb.description"))
62 config_fn(repo, "desc", value);
63 else if (!strcmp(key, "gitweb.category"))
64 config_fn(repo, "section", value);
65 else if (!prefixcmp(key, "cgit."))
66 config_fn(repo, key + 5, value);
67
Jason A. Donenfeld119397b2010-07-29 20:38:01 +020068 return 0;
69}
70
Lars Hjemli797110e2010-08-21 15:44:09 +020071static char *xstrrchr(char *s, char *from, int c)
72{
73 while (from >= s && *from != c)
74 from--;
75 return from < s ? NULL : from;
76}
77
John Keepingfb3655d2013-04-06 10:28:57 +010078static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn)
Lars Hjemli74061ed2009-08-24 00:04:58 +020079{
Lars Hjemli93397a72008-09-15 00:07:12 +020080 struct stat st;
81 struct passwd *pwd;
John Keepingfb3655d2013-04-06 10:28:57 +010082 size_t pathlen;
83 struct strbuf rel = STRBUF_INIT;
84 char *p, *slash;
Lars Hjemli797110e2010-08-21 15:44:09 +020085 int n;
Lars Hjemlie16f1782009-08-18 17:17:41 +020086 size_t size;
Lars Hjemli93397a72008-09-15 00:07:12 +020087
John Keepingfb3655d2013-04-06 10:28:57 +010088 if (stat(path->buf, &st)) {
Lars Hjemli93397a72008-09-15 00:07:12 +020089 fprintf(stderr, "Error accessing %s: %s (%d)\n",
John Keepingfb3655d2013-04-06 10:28:57 +010090 path->buf, strerror(errno), errno);
Lars Hjemli93397a72008-09-15 00:07:12 +020091 return;
92 }
Felix Hanleye0c6f232010-11-08 19:41:13 +010093
John Keepingfb3655d2013-04-06 10:28:57 +010094 strbuf_addch(path, '/');
95 pathlen = path->len;
Felix Hanleye0c6f232010-11-08 19:41:13 +010096
John Keepingfb3655d2013-04-06 10:28:57 +010097 if (ctx.cfg.strict_export) {
98 strbuf_addstr(path, ctx.cfg.strict_export);
99 if(stat(path->buf, &st))
100 return;
101 strbuf_setlen(path, pathlen);
102 }
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200103
John Keepingfb3655d2013-04-06 10:28:57 +0100104 strbuf_addstr(path, "noweb");
105 if (!stat(path->buf, &st))
106 return;
107 strbuf_setlen(path, pathlen);
108
109 if (strncmp(base, path->buf, strlen(base)))
110 strbuf_addbuf(&rel, path);
Lars Hjemli93397a72008-09-15 00:07:12 +0200111 else
John Keepingfb3655d2013-04-06 10:28:57 +0100112 strbuf_addstr(&rel, path->buf + strlen(base) + 1);
Lars Hjemli93397a72008-09-15 00:07:12 +0200113
John Keepingfb3655d2013-04-06 10:28:57 +0100114 if (!strcmp(rel.buf + rel.len - 5, "/.git"))
115 strbuf_setlen(&rel, rel.len - 5);
John Keeping9a725f42013-04-16 18:39:58 +0100116 else if (rel.len && rel.buf[rel.len - 1] == '/')
117 strbuf_setlen(&rel, rel.len - 1);
Lars Hjemli93397a72008-09-15 00:07:12 +0200118
John Keepingfb3655d2013-04-06 10:28:57 +0100119 repo = cgit_add_repo(rel.buf);
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400120 config_fn = fn;
John Keepingfb3655d2013-04-06 10:28:57 +0100121 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 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100126
Jason A. Donenfeld2e4a9412010-07-29 19:47:50 +0200127 if (ctx.cfg.remove_suffix)
128 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
129 *p = '\0';
John Keepingfb3655d2013-04-06 10:28:57 +0100130 repo->path = xstrdup(path->buf);
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400131 while (!repo->owner) {
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200132 if ((pwd = getpwuid(st.st_uid)) == NULL) {
133 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
John Keepingfb3655d2013-04-06 10:28:57 +0100134 path->buf, strerror(errno), errno);
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200135 break;
136 }
137 if (pwd->pw_gecos)
138 if ((p = strchr(pwd->pw_gecos, ',')))
139 *p = '\0';
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400140 repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200141 }
Lars Hjemli93397a72008-09-15 00:07:12 +0200142
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400143 if (repo->desc == cgit_default_repo_desc || !repo->desc) {
John Keepingfb3655d2013-04-06 10:28:57 +0100144 strbuf_addstr(path, "description");
145 if (!stat(path->buf, &st))
146 readfile(path->buf, &repo->desc, &size);
147 strbuf_setlen(path, pathlen);
Jason A. Donenfeldb56be4b2012-07-11 04:24:01 +0200148 }
Lars Hjemli93397a72008-09-15 00:07:12 +0200149
Lars Hjemli515edb02010-08-21 15:08:01 +0200150 if (!repo->readme) {
John Keepingfb3655d2013-04-06 10:28:57 +0100151 strbuf_addstr(path, "README.html");
152 if (!stat(path->buf, &st))
Lars Hjemli515edb02010-08-21 15:08:01 +0200153 repo->readme = "README.html";
John Keepingfb3655d2013-04-06 10:28:57 +0100154 strbuf_setlen(path, pathlen);
Lars Hjemli515edb02010-08-21 15:08:01 +0200155 }
Lars Hjemli797110e2010-08-21 15:44:09 +0200156 if (ctx.cfg.section_from_path) {
157 n = ctx.cfg.section_from_path;
158 if (n > 0) {
John Keepingfb3655d2013-04-06 10:28:57 +0100159 slash = rel.buf;
Lars Hjemli797110e2010-08-21 15:44:09 +0200160 while (slash && n && (slash = strchr(slash, '/')))
161 n--;
162 } else {
John Keepingfb3655d2013-04-06 10:28:57 +0100163 slash = rel.buf + rel.len;
164 while (slash && n && (slash = xstrrchr(rel.buf, slash, '/')))
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 = '/';
171 if (!prefixcmp(repo->name, repo->section)) {
172 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))
181 parse_configfile(xstrdup(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 +0200239#define lastc(s) s[strlen(s) - 1]
240
241void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
242{
John Keepingfb3655d2013-04-06 10:28:57 +0100243 struct strbuf line = STRBUF_INIT;
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200244 FILE *projects;
245 int err;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100246
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200247 projects = fopen(projectsfile, "r");
248 if (!projects) {
249 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
250 projectsfile, strerror(errno), errno);
Stefan Gehnf15c5832011-03-26 09:51:39 +0100251 return;
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200252 }
John Keepingfb3655d2013-04-06 10:28:57 +0100253 while (strbuf_getline(&line, projects, '\n') != EOF) {
254 if (!line.len)
255 continue;
256 strbuf_insert(&line, 0, "/", 1);
257 strbuf_insert(&line, 0, path, strlen(path));
258 scan_path(path, line.buf, fn);
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200259 }
260 if ((err = ferror(projects))) {
261 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
262 projectsfile, strerror(err), err);
263 }
264 fclose(projects);
John Keepingfb3655d2013-04-06 10:28:57 +0100265 strbuf_release(&line);
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200266}
267
Lars Hjemli74061ed2009-08-24 00:04:58 +0200268void scan_tree(const char *path, repo_config_fn fn)
Lars Hjemli93397a72008-09-15 00:07:12 +0200269{
Lars Hjemli74061ed2009-08-24 00:04:58 +0200270 scan_path(path, path, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200271}