]> gitweb.ps.run Git - ps-cgit/blob - scan-tree.c
ui-summary: Use default branch for readme if : prefix
[ps-cgit] / scan-tree.c
1 /* scan-tree.c
2  * 
3  * Copyright (C) 2008-2009 Lars Hjemli
4  * Copyright (C) 2010, 2012 Jason A. Donenfeld <Jason@zx2c4.com>
5  *
6  * Licensed under GNU General Public License v2
7  *   (see COPYING for full license text)
8  */
9
10 #include "cgit.h"
11 #include "scan-tree.h"
12 #include "configfile.h"
13 #include "html.h"
14
15 /* return 1 if path contains a objects/ directory and a HEAD file */
16 static int is_git_dir(const char *path)
17 {
18         struct stat st;
19         struct strbuf pathbuf = STRBUF_INIT;
20         int result = 0;
21
22         strbuf_addf(&pathbuf, "%s/objects", path);
23         if (stat(pathbuf.buf, &st)) {
24                 if (errno != ENOENT)
25                         fprintf(stderr, "Error checking path %s: %s (%d)\n",
26                                 path, strerror(errno), errno);
27                 goto out;
28         }
29         if (!S_ISDIR(st.st_mode))
30                 goto out;
31
32         strbuf_reset(&pathbuf);
33         strbuf_addf(&pathbuf, "%s/HEAD", path);
34         if (stat(pathbuf.buf, &st)) {
35                 if (errno != ENOENT)
36                         fprintf(stderr, "Error checking path %s: %s (%d)\n",
37                                 path, strerror(errno), errno);
38                 goto out;
39         }
40         if (!S_ISREG(st.st_mode))
41                 goto out;
42
43         result = 1;
44 out:
45         strbuf_release(&pathbuf);
46         return result;
47 }
48
49 struct cgit_repo *repo;
50 repo_config_fn config_fn;
51
52 static void repo_config(const char *name, const char *value)
53 {
54         config_fn(repo, name, value);
55 }
56
57 static int gitconfig_config(const char *key, const char *value, void *cb)
58 {
59         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
68         return 0;
69 }
70
71 static 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
78 static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn)
79 {
80         struct stat st;
81         struct passwd *pwd;
82         size_t pathlen;
83         struct strbuf rel = STRBUF_INIT;
84         char *p, *slash;
85         int n;
86         size_t size;
87
88         if (stat(path->buf, &st)) {
89                 fprintf(stderr, "Error accessing %s: %s (%d)\n",
90                         path->buf, strerror(errno), errno);
91                 return;
92         }
93
94         strbuf_addch(path, '/');
95         pathlen = path->len;
96
97         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         }
103
104         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);
111         else
112                 strbuf_addstr(&rel, path->buf + strlen(base) + 1);
113
114         if (!strcmp(rel.buf + rel.len - 5, "/.git"))
115                 strbuf_setlen(&rel, rel.len - 5);
116         else if (rel.len && rel.buf[rel.len - 1] == '/')
117                 strbuf_setlen(&rel, rel.len - 1);
118
119         repo = cgit_add_repo(rel.buf);
120         config_fn = fn;
121         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         }
126
127         if (ctx.cfg.remove_suffix)
128                 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
129                         *p = '\0';
130         repo->path = xstrdup(path->buf);
131         while (!repo->owner) {
132                 if ((pwd = getpwuid(st.st_uid)) == NULL) {
133                         fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
134                                 path->buf, strerror(errno), errno);
135                         break;
136                 }
137                 if (pwd->pw_gecos)
138                         if ((p = strchr(pwd->pw_gecos, ',')))
139                                 *p = '\0';
140                 repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
141         }
142
143         if (repo->desc == cgit_default_repo_desc || !repo->desc) {
144                 strbuf_addstr(path, "description");
145                 if (!stat(path->buf, &st))
146                         readfile(path->buf, &repo->desc, &size);
147                 strbuf_setlen(path, pathlen);
148         }
149
150         if (!repo->readme) {
151                 strbuf_addstr(path, "README.html");
152                 if (!stat(path->buf, &st))
153                         repo->readme = "README.html";
154                 strbuf_setlen(path, pathlen);
155         }
156         if (ctx.cfg.section_from_path) {
157                 n  = ctx.cfg.section_from_path;
158                 if (n > 0) {
159                         slash = rel.buf;
160                         while (slash && n && (slash = strchr(slash, '/')))
161                                 n--;
162                 } else {
163                         slash = rel.buf + rel.len;
164                         while (slash && n && (slash = xstrrchr(rel.buf, slash, '/')))
165                                 n++;
166                 }
167                 if (slash && !n) {
168                         *slash = '\0';
169                         repo->section = xstrdup(rel.buf);
170                         *slash = '/';
171                         if (!prefixcmp(repo->name, repo->section)) {
172                                 repo->name += strlen(repo->section);
173                                 if (*repo->name == '/')
174                                         repo->name++;
175                         }
176                 }
177         }
178
179         strbuf_addstr(path, "cgitrc");
180         if (!stat(path->buf, &st))
181                 parse_configfile(xstrdup(path->buf), &repo_config);
182
183         strbuf_release(&rel);
184 }
185
186 static void scan_path(const char *base, const char *path, repo_config_fn fn)
187 {
188         DIR *dir = opendir(path);
189         struct dirent *ent;
190         struct strbuf pathbuf = STRBUF_INIT;
191         size_t pathlen = strlen(path);
192         struct stat st;
193
194         if (!dir) {
195                 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
196                         path, strerror(errno), errno);
197                 return;
198         }
199
200         strbuf_add(&pathbuf, path, strlen(path));
201         if (is_git_dir(pathbuf.buf)) {
202                 add_repo(base, &pathbuf, fn);
203                 goto end;
204         }
205         strbuf_addstr(&pathbuf, "/.git");
206         if (is_git_dir(pathbuf.buf)) {
207                 add_repo(base, &pathbuf, fn);
208                 goto end;
209         }
210         /*
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++;
215         while ((ent = readdir(dir)) != NULL) {
216                 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;
221                         if (!ctx.cfg.scan_hidden_path)
222                                 continue;
223                 }
224                 strbuf_setlen(&pathbuf, pathlen);
225                 strbuf_addstr(&pathbuf, ent->d_name);
226                 if (stat(pathbuf.buf, &st)) {
227                         fprintf(stderr, "Error checking path %s: %s (%d)\n",
228                                 pathbuf.buf, strerror(errno), errno);
229                         continue;
230                 }
231                 if (S_ISDIR(st.st_mode))
232                         scan_path(base, pathbuf.buf, fn);
233         }
234 end:
235         strbuf_release(&pathbuf);
236         closedir(dir);
237 }
238
239 #define lastc(s) s[strlen(s) - 1]
240
241 void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
242 {
243         struct strbuf line = STRBUF_INIT;
244         FILE *projects;
245         int err;
246
247         projects = fopen(projectsfile, "r");
248         if (!projects) {
249                 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
250                         projectsfile, strerror(errno), errno);
251                 return;
252         }
253         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);
259         }
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);
265         strbuf_release(&line);
266 }
267
268 void scan_tree(const char *path, repo_config_fn fn)
269 {
270         scan_path(path, path, fn);
271 }