]> gitweb.ps.run Git - ps-cgit/blob - scan-tree.c
ui-shared: use placeholder for empty commit subject
[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 "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
51 static void repo_config(const char *name, const char *value)
52 {
53         config_fn(repo, name, value);
54 }
55
56 static int gitconfig_config(const char *key, const char *value, void *cb)
57 {
58         if (!strcmp(key, "gitweb.owner"))
59                 config_fn(repo, "owner", value);
60         else if (!strcmp(key, "gitweb.description"))
61                 config_fn(repo, "desc", value);
62         else if (!strcmp(key, "gitweb.category"))
63                 config_fn(repo, "section", value);
64         else if (!prefixcmp(key, "cgit."))
65                 config_fn(repo, key + 5, value);
66
67         return 0;
68 }
69
70 static char *xstrrchr(char *s, char *from, int c)
71 {
72         while (from >= s && *from != c)
73                 from--;
74         return from < s ? NULL : from;
75 }
76
77 static void add_repo(const char *base, const char *path, repo_config_fn fn)
78 {
79         struct stat st;
80         struct passwd *pwd;
81         char *rel, *p, *slash;
82         int n;
83         size_t size;
84
85         if (stat(path, &st)) {
86                 fprintf(stderr, "Error accessing %s: %s (%d)\n",
87                         path, strerror(errno), errno);
88                 return;
89         }
90
91         if (ctx.cfg.strict_export && stat(fmt("%s/%s", path, ctx.cfg.strict_export), &st))
92                 return;
93
94         if (!stat(fmt("%s/noweb", path), &st))
95                 return;
96
97         if (base == path)
98                 rel = xstrdup(fmt("%s", path));
99         else
100                 rel = xstrdup(fmt("%s", path + strlen(base) + 1));
101
102         if (!strcmp(rel + strlen(rel) - 5, "/.git"))
103                 rel[strlen(rel) - 5] = '\0';
104
105         repo = cgit_add_repo(rel);
106         config_fn = fn;
107         if (ctx.cfg.enable_git_config)
108                 git_config_from_file(gitconfig_config, fmt("%s/config", path), NULL);
109         
110         if (ctx.cfg.remove_suffix)
111                 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
112                         *p = '\0';
113         repo->path = xstrdup(path);
114         while (!repo->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                 repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
124         }
125
126         if (repo->desc == cgit_default_repo_desc || !repo->desc) {
127                 p = fmt("%s/description", path);
128                 if (!stat(p, &st))
129                         readfile(p, &repo->desc, &size);
130         }
131
132         if (!repo->readme) {
133                 p = fmt("%s/README.html", path);
134                 if (!stat(p, &st))
135                         repo->readme = "README.html";
136         }
137         if (ctx.cfg.section_from_path) {
138                 n  = ctx.cfg.section_from_path;
139                 if (n > 0) {
140                         slash = rel;
141                         while (slash && n && (slash = strchr(slash, '/')))
142                                 n--;
143                 } else {
144                         slash = rel + strlen(rel);
145                         while (slash && n && (slash = xstrrchr(rel, slash, '/')))
146                                 n++;
147                 }
148                 if (slash && !n) {
149                         *slash = '\0';
150                         repo->section = xstrdup(rel);
151                         *slash = '/';
152                         if (!prefixcmp(repo->name, repo->section)) {
153                                 repo->name += strlen(repo->section);
154                                 if (*repo->name == '/')
155                                         repo->name++;
156                         }
157                 }
158         }
159
160         p = fmt("%s/cgitrc", path);
161         if (!stat(p, &st))
162                 parse_configfile(xstrdup(p), &repo_config);
163
164
165         free(rel);
166 }
167
168 static void scan_path(const char *base, const char *path, repo_config_fn fn)
169 {
170         DIR *dir = opendir(path);
171         struct dirent *ent;
172         char *buf;
173         struct stat st;
174
175         if (!dir) {
176                 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
177                         path, strerror(errno), errno);
178                 return;
179         }
180         if (is_git_dir(path)) {
181                 add_repo(base, path, fn);
182                 goto end;
183         }
184         if (is_git_dir(fmt("%s/.git", path))) {
185                 add_repo(base, fmt("%s/.git", path), fn);
186                 goto end;
187         }
188         while((ent = readdir(dir)) != NULL) {
189                 if (ent->d_name[0] == '.') {
190                         if (ent->d_name[1] == '\0')
191                                 continue;
192                         if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
193                                 continue;
194                         if (!ctx.cfg.scan_hidden_path)
195                                 continue;
196                 }
197                 buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
198                 if (!buf) {
199                         fprintf(stderr, "Alloc error on %s: %s (%d)\n",
200                                 path, strerror(errno), errno);
201                         exit(1);
202                 }
203                 sprintf(buf, "%s/%s", path, ent->d_name);
204                 if (stat(buf, &st)) {
205                         fprintf(stderr, "Error checking path %s: %s (%d)\n",
206                                 buf, strerror(errno), errno);
207                         free(buf);
208                         continue;
209                 }
210                 if (S_ISDIR(st.st_mode))
211                         scan_path(base, buf, fn);
212                 free(buf);
213         }
214 end:
215         closedir(dir);
216 }
217
218 #define lastc(s) s[strlen(s) - 1]
219
220 void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
221 {
222         char line[MAX_PATH * 2], *z;
223         FILE *projects;
224         int err;
225         
226         projects = fopen(projectsfile, "r");
227         if (!projects) {
228                 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
229                         projectsfile, strerror(errno), errno);
230                 return;
231         }
232         while (fgets(line, sizeof(line), projects) != NULL) {
233                 for (z = &lastc(line);
234                      strlen(line) && strchr("\n\r", *z);
235                      z = &lastc(line))
236                         *z = '\0';
237                 if (strlen(line))
238                         scan_path(path, fmt("%s/%s", path, line), fn);
239         }
240         if ((err = ferror(projects))) {
241                 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
242                         projectsfile, strerror(err), err);
243         }
244         fclose(projects);
245 }
246
247 void scan_tree(const char *path, repo_config_fn fn)
248 {
249         scan_path(path, path, fn);
250 }