aboutsummaryrefslogtreecommitdiffstats
path: root/scan-tree.c
diff options
context:
space:
mode:
Diffstat (limited to 'scan-tree.c')
-rw-r--r--scan-tree.c147
1 files changed, 121 insertions, 26 deletions
diff --git a/scan-tree.c b/scan-tree.c
index ebfd41e..627af1b 100644
--- a/scan-tree.c
+++ b/scan-tree.c
@@ -1,3 +1,12 @@
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
1#include "cgit.h" 10#include "cgit.h"
2#include "configfile.h" 11#include "configfile.h"
3#include "html.h" 12#include "html.h"
@@ -38,17 +47,33 @@ static int is_git_dir(const char *path)
38 47
39struct cgit_repo *repo; 48struct cgit_repo *repo;
40repo_config_fn config_fn; 49repo_config_fn config_fn;
50char *owner;
41 51
42static void repo_config(const char *name, const char *value) 52static void repo_config(const char *name, const char *value)
43{ 53{
44 config_fn(repo, name, value); 54 config_fn(repo, name, value);
45} 55}
46 56
57static 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
64static 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
47static void add_repo(const char *base, const char *path, repo_config_fn fn) 71static void add_repo(const char *base, const char *path, repo_config_fn fn)
48{ 72{
49 struct stat st; 73 struct stat st;
50 struct passwd *pwd; 74 struct passwd *pwd;
51 char *p; 75 char *rel, *p, *slash;
76 int n;
52 size_t size; 77 size_t size;
53 78
54 if (stat(path, &st)) { 79 if (stat(path, &st)) {
@@ -56,34 +81,74 @@ static void add_repo(const char *base, const char *path, repo_config_fn fn)
56 path, strerror(errno), errno); 81 path, strerror(errno), errno);
57 return; 82 return;
58 } 83 }
59 if ((pwd = getpwuid(st.st_uid)) == NULL) { 84
60 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n", 85 if (ctx.cfg.strict_export && stat(fmt("%s/%s", path, ctx.cfg.strict_export), &st))
61 path, strerror(errno), errno);
62 return; 86 return;
63 } 87
88 if (!stat(fmt("%s/noweb", path), &st))
89 return;
90
91 owner = NULL;
92 if (ctx.cfg.enable_gitweb_owner)
93 git_config_from_file(git_owner_config, fmt("%s/config", path), NULL);
64 if (base == path) 94 if (base == path)
65 p = fmt("%s", path); 95 rel = xstrdup(fmt("%s", path));
66 else 96 else
67 p = fmt("%s", path + strlen(base) + 1); 97 rel = xstrdup(fmt("%s", path + strlen(base) + 1));
68 98
69 if (!strcmp(p + strlen(p) - 5, "/.git")) 99 if (!strcmp(rel + strlen(rel) - 5, "/.git"))
70 p[strlen(p) - 5] = '\0'; 100 rel[strlen(rel) - 5] = '\0';
71 101
72 repo = cgit_add_repo(xstrdup(p)); 102 repo = cgit_add_repo(rel);
103 if (ctx.cfg.remove_suffix)
104 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
105 *p = '\0';
73 repo->name = repo->url; 106 repo->name = repo->url;
74 repo->path = xstrdup(path); 107 repo->path = xstrdup(path);
75 p = (pwd && pwd->pw_gecos) ? strchr(pwd->pw_gecos, ',') : NULL; 108 while (!owner) {
76 if (p) 109 if ((pwd = getpwuid(st.st_uid)) == NULL) {
77 *p = '\0'; 110 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
78 repo->owner = (pwd ? xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name) : ""); 111 path, strerror(errno), errno);
112 break;
113 }
114 if (pwd->pw_gecos)
115 if ((p = strchr(pwd->pw_gecos, ',')))
116 *p = '\0';
117 owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
118 }
119 repo->owner = owner;
79 120
80 p = fmt("%s/description", path); 121 p = fmt("%s/description", path);
81 if (!stat(p, &st)) 122 if (!stat(p, &st))
82 readfile(p, &repo->desc, &size); 123 readfile(p, &repo->desc, &size);
83 124
84 p = fmt("%s/README.html", path); 125 if (!repo->readme) {
85 if (!stat(p, &st)) 126 p = fmt("%s/README.html", path);
86 repo->readme = xstrdup(p); 127 if (!stat(p, &st))
128 repo->readme = "README.html";
129 }
130 if (ctx.cfg.section_from_path) {
131 n = ctx.cfg.section_from_path;
132 if (n > 0) {
133 slash = rel;
134 while (slash && n && (slash = strchr(slash, '/')))
135 n--;
136 } else {
137 slash = rel + strlen(rel);
138 while (slash && n && (slash = xstrrchr(rel, slash, '/')))
139 n++;
140 }
141 if (slash && !n) {
142 *slash = '\0';
143 repo->section = xstrdup(rel);
144 *slash = '/';
145 if (!prefixcmp(repo->name, repo->section)) {
146 repo->name += strlen(repo->section);
147 if (*repo->name == '/')
148 repo->name++;
149 }
150 }
151 }
87 152
88 p = fmt("%s/cgitrc", path); 153 p = fmt("%s/cgitrc", path);
89 if (!stat(p, &st)) { 154 if (!stat(p, &st)) {
@@ -94,24 +159,23 @@ static void add_repo(const char *base, const char *path, repo_config_fn fn)
94 159
95static void scan_path(const char *base, const char *path, repo_config_fn fn) 160static void scan_path(const char *base, const char *path, repo_config_fn fn)
96{ 161{
97 DIR *dir; 162 DIR *dir = opendir(path);
98 struct dirent *ent; 163 struct dirent *ent;
99 char *buf; 164 char *buf;
100 struct stat st; 165 struct stat st;
101 166
167 if (!dir) {
168 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
169 path, strerror(errno), errno);
170 return;
171 }
102 if (is_git_dir(path)) { 172 if (is_git_dir(path)) {
103 add_repo(base, path, fn); 173 add_repo(base, path, fn);
104 return; 174 goto end;
105 } 175 }
106 if (is_git_dir(fmt("%s/.git", path))) { 176 if (is_git_dir(fmt("%s/.git", path))) {
107 add_repo(base, fmt("%s/.git", path), fn); 177 add_repo(base, fmt("%s/.git", path), fn);
108 return; 178 goto end;
109 }
110 dir = opendir(path);
111 if (!dir) {
112 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
113 path, strerror(errno), errno);
114 return;
115 } 179 }
116 while((ent = readdir(dir)) != NULL) { 180 while((ent = readdir(dir)) != NULL) {
117 if (ent->d_name[0] == '.') { 181 if (ent->d_name[0] == '.') {
@@ -119,6 +183,8 @@ static void scan_path(const char *base, const char *path, repo_config_fn fn)
119 continue; 183 continue;
120 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0') 184 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
121 continue; 185 continue;
186 if (!ctx.cfg.scan_hidden_path)
187 continue;
122 } 188 }
123 buf = malloc(strlen(path) + strlen(ent->d_name) + 2); 189 buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
124 if (!buf) { 190 if (!buf) {
@@ -137,9 +203,38 @@ static void scan_path(const char *base, const char *path, repo_config_fn fn)
137 scan_path(base, buf, fn); 203 scan_path(base, buf, fn);
138 free(buf); 204 free(buf);
139 } 205 }
206end:
140 closedir(dir); 207 closedir(dir);
141} 208}
142 209
210#define lastc(s) s[strlen(s) - 1]
211
212void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
213{
214 char line[MAX_PATH * 2], *z;
215 FILE *projects;
216 int err;
217
218 projects = fopen(projectsfile, "r");
219 if (!projects) {
220 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
221 projectsfile, strerror(errno), errno);
222 }
223 while (fgets(line, sizeof(line), projects) != NULL) {
224 for (z = &lastc(line);
225 strlen(line) && strchr("\n\r", *z);
226 z = &lastc(line))
227 *z = '\0';
228 if (strlen(line))
229 scan_path(path, fmt("%s/%s", path, line), fn);
230 }
231 if ((err = ferror(projects))) {
232 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
233 projectsfile, strerror(err), err);
234 }
235 fclose(projects);
236}
237
143void scan_tree(const char *path, repo_config_fn fn) 238void scan_tree(const char *path, repo_config_fn fn)
144{ 239{
145 scan_path(path, path, fn); 240 scan_path(path, path, fn);