]> gitweb.ps.run Git - ps-cgit/blob - scan-tree.c
guess default branch from HEAD
[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
52 static void repo_config(const char *name, const char *value)
53 {
54         config_fn(repo, name, value);
55 }
56
57 static int git_owner_config(const char *key, const char *value, void *cb)
58 {
59         if (!strcmp(key, "gitweb.owner"))
60                 owner = xstrdup(value);
61         return 0;
62 }
63
64 static char *xstrrchr(char *s, char *from, int c)
65 {
66         while (from >= s && *from != c)
67                 from--;
68         return from < s ? NULL : from;
69 }
70
71 static char *guess_defbranch(const char *repo_path)
72 {
73         int fd, len;
74         char buffer[256];
75         char *ref_start;
76         char *head;
77
78         head = fmt("%s/HEAD", repo_path);
79         fd = open(head, O_RDONLY);
80         if (fd == -1)
81                 return xstrdup("master");
82
83         memset(buffer, 0, sizeof(buffer));
84         len = read_in_full(fd, buffer, sizeof(buffer)-1);
85         close(fd);
86
87         if(!memcmp(buffer, "ref: refs/heads/", 16))
88                 return xstrndup(buffer+16, len-17);
89
90         if(strlen(buffer) == 41) {
91                 /* probably contains a SHA1 sum */
92                 memset(buffer, 0, sizeof(buffer));
93                 if(readlink(head, buffer, sizeof(buffer)-1)) {
94                         ref_start = memmem(buffer, sizeof(buffer)-1, "refs/heads/", 11);
95                         if(ref_start)
96                                 return xstrdup(ref_start+11);
97                 }
98         }
99
100         return xstrdup("master");
101 }
102
103
104 static void add_repo(const char *base, const char *path, repo_config_fn fn)
105 {
106         struct stat st;
107         struct passwd *pwd;
108         char *rel, *p, *slash;
109         int n;
110         size_t size;
111
112         if (stat(path, &st)) {
113                 fprintf(stderr, "Error accessing %s: %s (%d)\n",
114                         path, strerror(errno), errno);
115                 return;
116         }
117
118         if (ctx.cfg.strict_export && stat(fmt("%s/%s", path, ctx.cfg.strict_export), &st))
119                 return;
120
121         if (!stat(fmt("%s/noweb", path), &st))
122                 return;
123
124         owner = NULL;
125         if (ctx.cfg.enable_gitweb_owner)
126                 git_config_from_file(git_owner_config, fmt("%s/config", path), NULL);
127         if (base == path)
128                 rel = xstrdup(fmt("%s", path));
129         else
130                 rel = xstrdup(fmt("%s", path + strlen(base) + 1));
131
132         if (!strcmp(rel + strlen(rel) - 5, "/.git"))
133                 rel[strlen(rel) - 5] = '\0';
134
135         repo = cgit_add_repo(rel);
136         if (ctx.cfg.remove_suffix)
137                 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
138                         *p = '\0';
139         repo->name = repo->url;
140         repo->path = xstrdup(path);
141
142         repo->defbranch = guess_defbranch(repo->path);
143
144         while (!owner) {
145                 if ((pwd = getpwuid(st.st_uid)) == NULL) {
146                         fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
147                                 path, strerror(errno), errno);
148                         break;
149                 }
150                 if (pwd->pw_gecos)
151                         if ((p = strchr(pwd->pw_gecos, ',')))
152                                 *p = '\0';
153                 owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
154         }
155         repo->owner = owner;
156
157         p = fmt("%s/description", path);
158         if (!stat(p, &st))
159                 readfile(p, &repo->desc, &size);
160
161         if (!repo->readme) {
162                 p = fmt("%s/README.html", path);
163                 if (!stat(p, &st))
164                         repo->readme = "README.html";
165         }
166         if (ctx.cfg.section_from_path) {
167                 n  = ctx.cfg.section_from_path;
168                 if (n > 0) {
169                         slash = rel;
170                         while (slash && n && (slash = strchr(slash, '/')))
171                                 n--;
172                 } else {
173                         slash = rel + strlen(rel);
174                         while (slash && n && (slash = xstrrchr(rel, slash, '/')))
175                                 n++;
176                 }
177                 if (slash && !n) {
178                         *slash = '\0';
179                         repo->section = xstrdup(rel);
180                         *slash = '/';
181                         if (!prefixcmp(repo->name, repo->section)) {
182                                 repo->name += strlen(repo->section);
183                                 if (*repo->name == '/')
184                                         repo->name++;
185                         }
186                 }
187         }
188
189         p = fmt("%s/cgitrc", path);
190         if (!stat(p, &st)) {
191                 config_fn = fn;
192                 parse_configfile(xstrdup(p), &repo_config);
193         }
194
195         free(rel);
196 }
197
198 static void scan_path(const char *base, const char *path, repo_config_fn fn)
199 {
200         DIR *dir = opendir(path);
201         struct dirent *ent;
202         char *buf;
203         struct stat st;
204
205         if (!dir) {
206                 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
207                         path, strerror(errno), errno);
208                 return;
209         }
210         if (is_git_dir(path)) {
211                 add_repo(base, path, fn);
212                 goto end;
213         }
214         if (is_git_dir(fmt("%s/.git", path))) {
215                 add_repo(base, fmt("%s/.git", path), fn);
216                 goto end;
217         }
218         while((ent = readdir(dir)) != NULL) {
219                 if (ent->d_name[0] == '.') {
220                         if (ent->d_name[1] == '\0')
221                                 continue;
222                         if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
223                                 continue;
224                         if (!ctx.cfg.scan_hidden_path)
225                                 continue;
226                 }
227                 buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
228                 if (!buf) {
229                         fprintf(stderr, "Alloc error on %s: %s (%d)\n",
230                                 path, strerror(errno), errno);
231                         exit(1);
232                 }
233                 sprintf(buf, "%s/%s", path, ent->d_name);
234                 if (stat(buf, &st)) {
235                         fprintf(stderr, "Error checking path %s: %s (%d)\n",
236                                 buf, strerror(errno), errno);
237                         free(buf);
238                         continue;
239                 }
240                 if (S_ISDIR(st.st_mode))
241                         scan_path(base, buf, fn);
242                 free(buf);
243         }
244 end:
245         closedir(dir);
246 }
247
248 #define lastc(s) s[strlen(s) - 1]
249
250 void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
251 {
252         char line[MAX_PATH * 2], *z;
253         FILE *projects;
254         int err;
255         
256         projects = fopen(projectsfile, "r");
257         if (!projects) {
258                 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
259                         projectsfile, strerror(errno), errno);
260                 return;
261         }
262         while (fgets(line, sizeof(line), projects) != NULL) {
263                 for (z = &lastc(line);
264                      strlen(line) && strchr("\n\r", *z);
265                      z = &lastc(line))
266                         *z = '\0';
267                 if (strlen(line))
268                         scan_path(path, fmt("%s/%s", path, line), fn);
269         }
270         if ((err = ferror(projects))) {
271                 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
272                         projectsfile, strerror(err), err);
273         }
274         fclose(projects);
275 }
276
277 void scan_tree(const char *path, repo_config_fn fn)
278 {
279         scan_path(path, path, fn);
280 }