]> gitweb.ps.run Git - ps-cgit/blob - scan-tree.c
scan-tree: Support gitweb.description.
[ps-cgit] / scan-tree.c
1 /* scan-tree.c
2  * 
3  * Copyright (C) 2008-2009 Lars Hjemli
4  * Copyright (C) 2010 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 "configfile.h"
12 #include "html.h"
13
14 #define MAX_PATH 4096
15
16 /* return 1 if path contains a objects/ directory and a HEAD file */
17 static int is_git_dir(const char *path)
18 {
19         struct stat st;
20         static char buf[MAX_PATH];
21
22         if (snprintf(buf, MAX_PATH, "%s/objects", path) >= MAX_PATH) {
23                 fprintf(stderr, "Insanely long path: %s\n", path);
24                 return 0;
25         }
26         if (stat(buf, &st)) {
27                 if (errno != ENOENT)
28                         fprintf(stderr, "Error checking path %s: %s (%d)\n",
29                                 path, strerror(errno), errno);
30                 return 0;
31         }
32         if (!S_ISDIR(st.st_mode))
33                 return 0;
34
35         sprintf(buf, "%s/HEAD", path);
36         if (stat(buf, &st)) {
37                 if (errno != ENOENT)
38                         fprintf(stderr, "Error checking path %s: %s (%d)\n",
39                                 path, strerror(errno), errno);
40                 return 0;
41         }
42         if (!S_ISREG(st.st_mode))
43                 return 0;
44
45         return 1;
46 }
47
48 struct cgit_repo *repo;
49 repo_config_fn config_fn;
50 char *owner;
51 char *desc;
52
53 static void repo_config(const char *name, const char *value)
54 {
55         config_fn(repo, name, value);
56 }
57
58 static int gitweb_config(const char *key, const char *value, void *cb)
59 {
60         if (ctx.cfg.enable_gitweb_owner && !strcmp(key, "gitweb.owner"))
61                 owner = xstrdup(value);
62         else if (ctx.cfg.enable_gitweb_desc && !strcmp(key, "gitweb.description"))
63                 desc = xstrdup(value);
64         return 0;
65 }
66
67
68
69 static char *xstrrchr(char *s, char *from, int c)
70 {
71         while (from >= s && *from != c)
72                 from--;
73         return from < s ? NULL : from;
74 }
75
76 static void add_repo(const char *base, const char *path, repo_config_fn fn)
77 {
78         struct stat st;
79         struct passwd *pwd;
80         char *rel, *p, *slash;
81         int n;
82         size_t size;
83
84         if (stat(path, &st)) {
85                 fprintf(stderr, "Error accessing %s: %s (%d)\n",
86                         path, strerror(errno), errno);
87                 return;
88         }
89
90         if (ctx.cfg.strict_export && stat(fmt("%s/%s", path, ctx.cfg.strict_export), &st))
91                 return;
92
93         if (!stat(fmt("%s/noweb", path), &st))
94                 return;
95
96         owner = NULL;
97         desc = NULL;
98         git_config_from_file(gitweb_config, fmt("%s/config", path), NULL);
99         
100         if (base == path)
101                 rel = xstrdup(fmt("%s", path));
102         else
103                 rel = xstrdup(fmt("%s", path + strlen(base) + 1));
104
105         if (!strcmp(rel + strlen(rel) - 5, "/.git"))
106                 rel[strlen(rel) - 5] = '\0';
107
108         repo = cgit_add_repo(rel);
109         if (ctx.cfg.remove_suffix)
110                 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
111                         *p = '\0';
112         repo->name = repo->url;
113         repo->path = xstrdup(path);
114         while (!owner) {
115                 if ((pwd = getpwuid(st.st_uid)) == NULL) {
116                         fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
117                                 path, strerror(errno), errno);
118                         break;
119                 }
120                 if (pwd->pw_gecos)
121                         if ((p = strchr(pwd->pw_gecos, ',')))
122                                 *p = '\0';
123                 owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
124         }
125         repo->owner = owner;
126
127         if (desc)
128                 repo->desc = desc;
129         else {
130                 p = fmt("%s/description", path);
131                 if (!stat(p, &st))
132                         readfile(p, &repo->desc, &size);
133         }
134
135         if (!repo->readme) {
136                 p = fmt("%s/README.html", path);
137                 if (!stat(p, &st))
138                         repo->readme = "README.html";
139         }
140         if (ctx.cfg.section_from_path) {
141                 n  = ctx.cfg.section_from_path;
142                 if (n > 0) {
143                         slash = rel;
144                         while (slash && n && (slash = strchr(slash, '/')))
145                                 n--;
146                 } else {
147                         slash = rel + strlen(rel);
148                         while (slash && n && (slash = xstrrchr(rel, slash, '/')))
149                                 n++;
150                 }
151                 if (slash && !n) {
152                         *slash = '\0';
153                         repo->section = xstrdup(rel);
154                         *slash = '/';
155                         if (!prefixcmp(repo->name, repo->section)) {
156                                 repo->name += strlen(repo->section);
157                                 if (*repo->name == '/')
158                                         repo->name++;
159                         }
160                 }
161         }
162
163         p = fmt("%s/cgitrc", path);
164         if (!stat(p, &st)) {
165                 config_fn = fn;
166                 parse_configfile(xstrdup(p), &repo_config);
167         }
168
169         free(rel);
170 }
171
172 static void scan_path(const char *base, const char *path, repo_config_fn fn)
173 {
174         DIR *dir = opendir(path);
175         struct dirent *ent;
176         char *buf;
177         struct stat st;
178
179         if (!dir) {
180                 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
181                         path, strerror(errno), errno);
182                 return;
183         }
184         if (is_git_dir(path)) {
185                 add_repo(base, path, fn);
186                 goto end;
187         }
188         if (is_git_dir(fmt("%s/.git", path))) {
189                 add_repo(base, fmt("%s/.git", path), fn);
190                 goto end;
191         }
192         while((ent = readdir(dir)) != NULL) {
193                 if (ent->d_name[0] == '.') {
194                         if (ent->d_name[1] == '\0')
195                                 continue;
196                         if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
197                                 continue;
198                         if (!ctx.cfg.scan_hidden_path)
199                                 continue;
200                 }
201                 buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
202                 if (!buf) {
203                         fprintf(stderr, "Alloc error on %s: %s (%d)\n",
204                                 path, strerror(errno), errno);
205                         exit(1);
206                 }
207                 sprintf(buf, "%s/%s", path, ent->d_name);
208                 if (stat(buf, &st)) {
209                         fprintf(stderr, "Error checking path %s: %s (%d)\n",
210                                 buf, strerror(errno), errno);
211                         free(buf);
212                         continue;
213                 }
214                 if (S_ISDIR(st.st_mode))
215                         scan_path(base, buf, fn);
216                 free(buf);
217         }
218 end:
219         closedir(dir);
220 }
221
222 #define lastc(s) s[strlen(s) - 1]
223
224 void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
225 {
226         char line[MAX_PATH * 2], *z;
227         FILE *projects;
228         int err;
229         
230         projects = fopen(projectsfile, "r");
231         if (!projects) {
232                 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
233                         projectsfile, strerror(errno), errno);
234                 return;
235         }
236         while (fgets(line, sizeof(line), projects) != NULL) {
237                 for (z = &lastc(line);
238                      strlen(line) && strchr("\n\r", *z);
239                      z = &lastc(line))
240                         *z = '\0';
241                 if (strlen(line))
242                         scan_path(path, fmt("%s/%s", path, line), fn);
243         }
244         if ((err = ferror(projects))) {
245                 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
246                         projectsfile, strerror(err), err);
247         }
248         fclose(projects);
249 }
250
251 void scan_tree(const char *path, repo_config_fn fn)
252 {
253         scan_path(path, path, fn);
254 }