]> git.cameronkatri.com Git - cgit.git/blob - scan-tree.c
Add support for "readme" option
[cgit.git] / 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 void add_repo(const char *base, const char *path, repo_config_fn fn)
65 {
66 struct stat st;
67 struct passwd *pwd;
68 char *p;
69 size_t size;
70
71 if (stat(path, &st)) {
72 fprintf(stderr, "Error accessing %s: %s (%d)\n",
73 path, strerror(errno), errno);
74 return;
75 }
76 if (!stat(fmt("%s/noweb", path), &st))
77 return;
78
79 owner = NULL;
80 if (ctx.cfg.enable_gitweb_owner)
81 git_config_from_file(git_owner_config, fmt("%s/config", path), NULL);
82 if (base == path)
83 p = fmt("%s", path);
84 else
85 p = fmt("%s", path + strlen(base) + 1);
86
87 if (!strcmp(p + strlen(p) - 5, "/.git"))
88 p[strlen(p) - 5] = '\0';
89
90 repo = cgit_add_repo(xstrdup(p));
91 if (ctx.cfg.remove_suffix)
92 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
93 *p = '\0';
94 repo->name = repo->url;
95 repo->path = xstrdup(path);
96 while (!owner) {
97 if ((pwd = getpwuid(st.st_uid)) == NULL) {
98 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
99 path, strerror(errno), errno);
100 break;
101 }
102 if (pwd->pw_gecos)
103 if ((p = strchr(pwd->pw_gecos, ',')))
104 *p = '\0';
105 owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
106 }
107 repo->owner = owner;
108
109 p = fmt("%s/description", path);
110 if (!stat(p, &st))
111 readfile(p, &repo->desc, &size);
112
113 if (!repo->readme) {
114 p = fmt("%s/README.html", path);
115 if (!stat(p, &st))
116 repo->readme = "README.html";
117 }
118
119 p = fmt("%s/cgitrc", path);
120 if (!stat(p, &st)) {
121 config_fn = fn;
122 parse_configfile(xstrdup(p), &repo_config);
123 }
124 }
125
126 static void scan_path(const char *base, const char *path, repo_config_fn fn)
127 {
128 DIR *dir;
129 struct dirent *ent;
130 char *buf;
131 struct stat st;
132
133 if (is_git_dir(path)) {
134 add_repo(base, path, fn);
135 return;
136 }
137 if (is_git_dir(fmt("%s/.git", path))) {
138 add_repo(base, fmt("%s/.git", path), fn);
139 return;
140 }
141 dir = opendir(path);
142 if (!dir) {
143 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
144 path, strerror(errno), errno);
145 return;
146 }
147 while((ent = readdir(dir)) != NULL) {
148 if (ent->d_name[0] == '.') {
149 if (ent->d_name[1] == '\0')
150 continue;
151 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
152 continue;
153 }
154 buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
155 if (!buf) {
156 fprintf(stderr, "Alloc error on %s: %s (%d)\n",
157 path, strerror(errno), errno);
158 exit(1);
159 }
160 sprintf(buf, "%s/%s", path, ent->d_name);
161 if (stat(buf, &st)) {
162 fprintf(stderr, "Error checking path %s: %s (%d)\n",
163 buf, strerror(errno), errno);
164 free(buf);
165 continue;
166 }
167 if (S_ISDIR(st.st_mode))
168 scan_path(base, buf, fn);
169 free(buf);
170 }
171 closedir(dir);
172 }
173
174 #define lastc(s) s[strlen(s) - 1]
175
176 void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
177 {
178 char line[MAX_PATH * 2], *z;
179 FILE *projects;
180 int err;
181
182 projects = fopen(projectsfile, "r");
183 if (!projects) {
184 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
185 projectsfile, strerror(errno), errno);
186 }
187 while (fgets(line, sizeof(line), projects) != NULL) {
188 for (z = &lastc(line);
189 strlen(line) && strchr("\n\r", *z);
190 z = &lastc(line))
191 *z = '\0';
192 if (strlen(line))
193 scan_path(path, fmt("%s/%s", path, line), fn);
194 }
195 if ((err = ferror(projects))) {
196 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
197 projectsfile, strerror(err), err);
198 }
199 fclose(projects);
200 }
201
202 void scan_tree(const char *path, repo_config_fn fn)
203 {
204 scan_path(path, path, fn);
205 }