blob: 0d3e0ade7d0be88e7b7b612fbf7ee1ba69f9f023 [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
15#define MAX_PATH 4096
16
17/* return 1 if path contains a objects/ directory and a HEAD file */
18static int is_git_dir(const char *path)
19{
20 struct stat st;
21 static char buf[MAX_PATH];
22
23 if (snprintf(buf, MAX_PATH, "%s/objects", path) >= MAX_PATH) {
24 fprintf(stderr, "Insanely long path: %s\n", path);
25 return 0;
26 }
27 if (stat(buf, &st)) {
28 if (errno != ENOENT)
29 fprintf(stderr, "Error checking path %s: %s (%d)\n",
30 path, strerror(errno), errno);
31 return 0;
32 }
33 if (!S_ISDIR(st.st_mode))
34 return 0;
35
36 sprintf(buf, "%s/HEAD", path);
37 if (stat(buf, &st)) {
38 if (errno != ENOENT)
39 fprintf(stderr, "Error checking path %s: %s (%d)\n",
40 path, strerror(errno), errno);
41 return 0;
42 }
43 if (!S_ISREG(st.st_mode))
44 return 0;
45
46 return 1;
47}
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
Lars Hjemli74061ed2009-08-24 00:04:58 +020078static void add_repo(const char *base, const char *path, repo_config_fn fn)
79{
Lars Hjemli93397a72008-09-15 00:07:12 +020080 struct stat st;
81 struct passwd *pwd;
Lars Hjemli797110e2010-08-21 15:44:09 +020082 char *rel, *p, *slash;
83 int n;
Lars Hjemlie16f1782009-08-18 17:17:41 +020084 size_t size;
Lars Hjemli93397a72008-09-15 00:07:12 +020085
86 if (stat(path, &st)) {
87 fprintf(stderr, "Error accessing %s: %s (%d)\n",
88 path, strerror(errno), errno);
89 return;
90 }
Felix Hanleye0c6f232010-11-08 19:41:13 +010091
92 if (ctx.cfg.strict_export && stat(fmt("%s/%s", path, ctx.cfg.strict_export), &st))
93 return;
94
Lars Hjemli31ba37c2010-02-28 18:40:02 +010095 if (!stat(fmt("%s/noweb", path), &st))
96 return;
Jason A. Donenfeld119397b2010-07-29 20:38:01 +020097
Lars Hjemli93397a72008-09-15 00:07:12 +020098 if (base == path)
Lars Hjemli797110e2010-08-21 15:44:09 +020099 rel = xstrdup(fmt("%s", path));
Lars Hjemli93397a72008-09-15 00:07:12 +0200100 else
Lars Hjemli797110e2010-08-21 15:44:09 +0200101 rel = xstrdup(fmt("%s", path + strlen(base) + 1));
Lars Hjemli93397a72008-09-15 00:07:12 +0200102
Lars Hjemli797110e2010-08-21 15:44:09 +0200103 if (!strcmp(rel + strlen(rel) - 5, "/.git"))
104 rel[strlen(rel) - 5] = '\0';
Lars Hjemli93397a72008-09-15 00:07:12 +0200105
Lars Hjemli797110e2010-08-21 15:44:09 +0200106 repo = cgit_add_repo(rel);
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400107 config_fn = fn;
108 if (ctx.cfg.enable_git_config)
109 git_config_from_file(gitconfig_config, fmt("%s/config", path), NULL);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100110
Jason A. Donenfeld2e4a9412010-07-29 19:47:50 +0200111 if (ctx.cfg.remove_suffix)
112 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
113 *p = '\0';
Lars Hjemli93397a72008-09-15 00:07:12 +0200114 repo->path = xstrdup(path);
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400115 while (!repo->owner) {
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200116 if ((pwd = getpwuid(st.st_uid)) == NULL) {
117 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
118 path, strerror(errno), errno);
119 break;
120 }
121 if (pwd->pw_gecos)
122 if ((p = strchr(pwd->pw_gecos, ',')))
123 *p = '\0';
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400124 repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200125 }
Lars Hjemli93397a72008-09-15 00:07:12 +0200126
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400127 if (repo->desc == cgit_default_repo_desc || !repo->desc) {
Jason A. Donenfeldb56be4b2012-07-11 04:24:01 +0200128 p = fmt("%s/description", path);
129 if (!stat(p, &st))
130 readfile(p, &repo->desc, &size);
131 }
Lars Hjemli93397a72008-09-15 00:07:12 +0200132
Lars Hjemli515edb02010-08-21 15:08:01 +0200133 if (!repo->readme) {
134 p = fmt("%s/README.html", path);
135 if (!stat(p, &st))
136 repo->readme = "README.html";
137 }
Lars Hjemli797110e2010-08-21 15:44:09 +0200138 if (ctx.cfg.section_from_path) {
139 n = ctx.cfg.section_from_path;
140 if (n > 0) {
141 slash = rel;
142 while (slash && n && (slash = strchr(slash, '/')))
143 n--;
144 } else {
145 slash = rel + strlen(rel);
146 while (slash && n && (slash = xstrrchr(rel, slash, '/')))
147 n++;
148 }
149 if (slash && !n) {
150 *slash = '\0';
151 repo->section = xstrdup(rel);
152 *slash = '/';
153 if (!prefixcmp(repo->name, repo->section)) {
154 repo->name += strlen(repo->section);
155 if (*repo->name == '/')
156 repo->name++;
157 }
158 }
159 }
Lars Hjemli74061ed2009-08-24 00:04:58 +0200160
161 p = fmt("%s/cgitrc", path);
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400162 if (!stat(p, &st))
Lars Hjemli74061ed2009-08-24 00:04:58 +0200163 parse_configfile(xstrdup(p), &repo_config);
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -0400164
Jamie Couture2a8f5532011-06-03 19:21:01 -0400165
166 free(rel);
Lars Hjemli93397a72008-09-15 00:07:12 +0200167}
168
Lars Hjemli74061ed2009-08-24 00:04:58 +0200169static void scan_path(const char *base, const char *path, repo_config_fn fn)
Lars Hjemli93397a72008-09-15 00:07:12 +0200170{
Johan Herland682adbc2010-11-15 20:40:43 +0100171 DIR *dir = opendir(path);
Lars Hjemli93397a72008-09-15 00:07:12 +0200172 struct dirent *ent;
173 char *buf;
174 struct stat st;
175
Lars Hjemli93397a72008-09-15 00:07:12 +0200176 if (!dir) {
177 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
178 path, strerror(errno), errno);
179 return;
180 }
Johan Herland682adbc2010-11-15 20:40:43 +0100181 if (is_git_dir(path)) {
182 add_repo(base, path, fn);
183 goto end;
184 }
185 if (is_git_dir(fmt("%s/.git", path))) {
186 add_repo(base, fmt("%s/.git", path), fn);
187 goto end;
188 }
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500189 while ((ent = readdir(dir)) != NULL) {
Lars Hjemli93397a72008-09-15 00:07:12 +0200190 if (ent->d_name[0] == '.') {
191 if (ent->d_name[1] == '\0')
192 continue;
193 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
194 continue;
Johan Herlanddf522792010-11-15 20:41:00 +0100195 if (!ctx.cfg.scan_hidden_path)
196 continue;
Lars Hjemli93397a72008-09-15 00:07:12 +0200197 }
198 buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
199 if (!buf) {
200 fprintf(stderr, "Alloc error on %s: %s (%d)\n",
201 path, strerror(errno), errno);
202 exit(1);
203 }
204 sprintf(buf, "%s/%s", path, ent->d_name);
205 if (stat(buf, &st)) {
206 fprintf(stderr, "Error checking path %s: %s (%d)\n",
207 buf, strerror(errno), errno);
208 free(buf);
209 continue;
210 }
211 if (S_ISDIR(st.st_mode))
Lars Hjemli74061ed2009-08-24 00:04:58 +0200212 scan_path(base, buf, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200213 free(buf);
214 }
Johan Herland682adbc2010-11-15 20:40:43 +0100215end:
Lars Hjemli93397a72008-09-15 00:07:12 +0200216 closedir(dir);
217}
218
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200219#define lastc(s) s[strlen(s) - 1]
220
221void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
222{
223 char line[MAX_PATH * 2], *z;
224 FILE *projects;
225 int err;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100226
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200227 projects = fopen(projectsfile, "r");
228 if (!projects) {
229 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
230 projectsfile, strerror(errno), errno);
Stefan Gehnf15c5832011-03-26 09:51:39 +0100231 return;
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200232 }
233 while (fgets(line, sizeof(line), projects) != NULL) {
234 for (z = &lastc(line);
235 strlen(line) && strchr("\n\r", *z);
236 z = &lastc(line))
237 *z = '\0';
238 if (strlen(line))
239 scan_path(path, fmt("%s/%s", path, line), fn);
240 }
241 if ((err = ferror(projects))) {
242 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
243 projectsfile, strerror(err), err);
244 }
245 fclose(projects);
246}
247
Lars Hjemli74061ed2009-08-24 00:04:58 +0200248void scan_tree(const char *path, repo_config_fn fn)
Lars Hjemli93397a72008-09-15 00:07:12 +0200249{
Lars Hjemli74061ed2009-08-24 00:04:58 +0200250 scan_path(path, path, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200251}