blob: 29c8263a0f6feeea85545999adfeef41a8ab03ae [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. Donenfelddcbc0432013-05-26 15:20:02 +02004 * Copyright (C) 2010-2013 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 Hjemli797110e2010-08-21 15:44:09 +0200150 if (ctx.cfg.section_from_path) {
Lukas Fleischer86e309f2013-06-28 08:58:14 +0000151 n = ctx.cfg.section_from_path;
Lars Hjemli797110e2010-08-21 15:44:09 +0200152 if (n > 0) {
Lukas Fleischer86e309f2013-06-28 08:58:14 +0000153 slash = rel.buf - 1;
154 while (slash && n && (slash = strchr(slash + 1, '/')))
Lars Hjemli797110e2010-08-21 15:44:09 +0200155 n--;
156 } else {
John Keepingfb3655d2013-04-06 10:28:57 +0100157 slash = rel.buf + rel.len;
Lukas Fleischer86e309f2013-06-28 08:58:14 +0000158 while (slash && n && (slash = xstrrchr(rel.buf, slash - 1, '/')))
Lars Hjemli797110e2010-08-21 15:44:09 +0200159 n++;
160 }
161 if (slash && !n) {
162 *slash = '\0';
John Keepingfb3655d2013-04-06 10:28:57 +0100163 repo->section = xstrdup(rel.buf);
Lars Hjemli797110e2010-08-21 15:44:09 +0200164 *slash = '/';
165 if (!prefixcmp(repo->name, repo->section)) {
166 repo->name += strlen(repo->section);
167 if (*repo->name == '/')
168 repo->name++;
169 }
170 }
171 }
Lars Hjemli74061ed2009-08-24 00:04:58 +0200172
John Keepingfb3655d2013-04-06 10:28:57 +0100173 strbuf_addstr(path, "cgitrc");
174 if (!stat(path->buf, &st))
175 parse_configfile(xstrdup(path->buf), &repo_config);
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400176
John Keepingfb3655d2013-04-06 10:28:57 +0100177 strbuf_release(&rel);
Lars Hjemli93397a72008-09-15 00:07:12 +0200178}
179
Lars Hjemli74061ed2009-08-24 00:04:58 +0200180static void scan_path(const char *base, const char *path, repo_config_fn fn)
Lars Hjemli93397a72008-09-15 00:07:12 +0200181{
Johan Herland682adbc2010-11-15 20:40:43 +0100182 DIR *dir = opendir(path);
Lars Hjemli93397a72008-09-15 00:07:12 +0200183 struct dirent *ent;
John Keepingfb3655d2013-04-06 10:28:57 +0100184 struct strbuf pathbuf = STRBUF_INIT;
185 size_t pathlen = strlen(path);
Lars Hjemli93397a72008-09-15 00:07:12 +0200186 struct stat st;
187
Lars Hjemli93397a72008-09-15 00:07:12 +0200188 if (!dir) {
189 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
190 path, strerror(errno), errno);
191 return;
192 }
John Keepingfb3655d2013-04-06 10:28:57 +0100193
194 strbuf_add(&pathbuf, path, strlen(path));
195 if (is_git_dir(pathbuf.buf)) {
196 add_repo(base, &pathbuf, fn);
Johan Herland682adbc2010-11-15 20:40:43 +0100197 goto end;
198 }
John Keepingfb3655d2013-04-06 10:28:57 +0100199 strbuf_addstr(&pathbuf, "/.git");
200 if (is_git_dir(pathbuf.buf)) {
201 add_repo(base, &pathbuf, fn);
Johan Herland682adbc2010-11-15 20:40:43 +0100202 goto end;
203 }
John Keepingfb3655d2013-04-06 10:28:57 +0100204 /*
205 * Add one because we don't want to lose the trailing '/' when we
206 * reset the length of pathbuf in the loop below.
207 */
208 pathlen++;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500209 while ((ent = readdir(dir)) != NULL) {
Lars Hjemli93397a72008-09-15 00:07:12 +0200210 if (ent->d_name[0] == '.') {
211 if (ent->d_name[1] == '\0')
212 continue;
213 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
214 continue;
Johan Herlanddf522792010-11-15 20:41:00 +0100215 if (!ctx.cfg.scan_hidden_path)
216 continue;
Lars Hjemli93397a72008-09-15 00:07:12 +0200217 }
John Keepingfb3655d2013-04-06 10:28:57 +0100218 strbuf_setlen(&pathbuf, pathlen);
219 strbuf_addstr(&pathbuf, ent->d_name);
220 if (stat(pathbuf.buf, &st)) {
Lars Hjemli93397a72008-09-15 00:07:12 +0200221 fprintf(stderr, "Error checking path %s: %s (%d)\n",
John Keepingfb3655d2013-04-06 10:28:57 +0100222 pathbuf.buf, strerror(errno), errno);
Lars Hjemli93397a72008-09-15 00:07:12 +0200223 continue;
224 }
225 if (S_ISDIR(st.st_mode))
John Keepingfb3655d2013-04-06 10:28:57 +0100226 scan_path(base, pathbuf.buf, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200227 }
Johan Herland682adbc2010-11-15 20:40:43 +0100228end:
John Keepingfb3655d2013-04-06 10:28:57 +0100229 strbuf_release(&pathbuf);
Lars Hjemli93397a72008-09-15 00:07:12 +0200230 closedir(dir);
231}
232
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200233void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
234{
John Keepingfb3655d2013-04-06 10:28:57 +0100235 struct strbuf line = STRBUF_INIT;
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200236 FILE *projects;
237 int err;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100238
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200239 projects = fopen(projectsfile, "r");
240 if (!projects) {
241 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
242 projectsfile, strerror(errno), errno);
Stefan Gehnf15c5832011-03-26 09:51:39 +0100243 return;
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200244 }
John Keepingfb3655d2013-04-06 10:28:57 +0100245 while (strbuf_getline(&line, projects, '\n') != EOF) {
246 if (!line.len)
247 continue;
248 strbuf_insert(&line, 0, "/", 1);
249 strbuf_insert(&line, 0, path, strlen(path));
250 scan_path(path, line.buf, fn);
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200251 }
252 if ((err = ferror(projects))) {
253 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
254 projectsfile, strerror(err), err);
255 }
256 fclose(projects);
John Keepingfb3655d2013-04-06 10:28:57 +0100257 strbuf_release(&line);
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200258}
259
Lars Hjemli74061ed2009-08-24 00:04:58 +0200260void scan_tree(const char *path, repo_config_fn fn)
Lars Hjemli93397a72008-09-15 00:07:12 +0200261{
Lars Hjemli74061ed2009-08-24 00:04:58 +0200262 scan_path(path, path, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200263}