]>
git.cameronkatri.com Git - cgit.git/blob - scan-tree.c
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
10 #include "scan-tree.h"
11 #include "configfile.h"
14 /* return 1 if path contains a objects/ directory and a HEAD file */
15 static int is_git_dir(const char *path
)
18 struct strbuf pathbuf
= STRBUF_INIT
;
21 strbuf_addf(&pathbuf
, "%s/objects", path
);
22 if (stat(pathbuf
.buf
, &st
)) {
24 fprintf(stderr
, "Error checking path %s: %s (%d)\n",
25 path
, strerror(errno
), errno
);
28 if (!S_ISDIR(st
.st_mode
))
31 strbuf_reset(&pathbuf
);
32 strbuf_addf(&pathbuf
, "%s/HEAD", path
);
33 if (stat(pathbuf
.buf
, &st
)) {
35 fprintf(stderr
, "Error checking path %s: %s (%d)\n",
36 path
, strerror(errno
), errno
);
39 if (!S_ISREG(st
.st_mode
))
44 strbuf_release(&pathbuf
);
48 static struct cgit_repo
*repo
;
49 static repo_config_fn config_fn
;
51 static void repo_config(const char *name
, const char *value
)
53 config_fn(repo
, name
, value
);
56 static int gitconfig_config(const char *key
, const char *value
, void *cb
)
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 (!strcmp(key
, "gitweb.homepage"))
65 config_fn(repo
, "homepage", value
);
66 else if (starts_with(key
, "cgit."))
67 config_fn(repo
, key
+ 5, value
);
72 static char *xstrrchr(char *s
, char *from
, int c
)
74 while (from
>= s
&& *from
!= c
)
76 return from
< s
? NULL
: from
;
79 static void add_repo(const char *base
, struct strbuf
*path
, repo_config_fn fn
)
84 struct strbuf rel
= STRBUF_INIT
;
89 if (stat(path
->buf
, &st
)) {
90 fprintf(stderr
, "Error accessing %s: %s (%d)\n",
91 path
->buf
, strerror(errno
), errno
);
95 strbuf_addch(path
, '/');
98 if (ctx
.cfg
.strict_export
) {
99 strbuf_addstr(path
, ctx
.cfg
.strict_export
);
100 if(stat(path
->buf
, &st
))
102 strbuf_setlen(path
, pathlen
);
105 strbuf_addstr(path
, "noweb");
106 if (!stat(path
->buf
, &st
))
108 strbuf_setlen(path
, pathlen
);
110 if (!starts_with(path
->buf
, base
))
111 strbuf_addbuf(&rel
, path
);
113 strbuf_addstr(&rel
, path
->buf
+ strlen(base
) + 1);
115 if (!strcmp(rel
.buf
+ rel
.len
- 5, "/.git"))
116 strbuf_setlen(&rel
, rel
.len
- 5);
117 else if (rel
.len
&& rel
.buf
[rel
.len
- 1] == '/')
118 strbuf_setlen(&rel
, rel
.len
- 1);
120 repo
= cgit_add_repo(rel
.buf
);
122 if (ctx
.cfg
.enable_git_config
) {
123 strbuf_addstr(path
, "config");
124 git_config_from_file(gitconfig_config
, path
->buf
, NULL
);
125 strbuf_setlen(path
, pathlen
);
128 if (ctx
.cfg
.remove_suffix
) {
130 strip_suffix(repo
->url
, ".git", &urllen
);
131 strip_suffix_mem(repo
->url
, &urllen
, "/");
132 repo
->url
[urllen
] = '\0';
134 repo
->path
= xstrdup(path
->buf
);
135 while (!repo
->owner
) {
136 if ((pwd
= getpwuid(st
.st_uid
)) == NULL
) {
137 fprintf(stderr
, "Error reading owner-info for %s: %s (%d)\n",
138 path
->buf
, strerror(errno
), errno
);
142 if ((p
= strchr(pwd
->pw_gecos
, ',')))
144 repo
->owner
= xstrdup(pwd
->pw_gecos
? pwd
->pw_gecos
: pwd
->pw_name
);
147 if (repo
->desc
== cgit_default_repo_desc
|| !repo
->desc
) {
148 strbuf_addstr(path
, "description");
149 if (!stat(path
->buf
, &st
))
150 readfile(path
->buf
, &repo
->desc
, &size
);
151 strbuf_setlen(path
, pathlen
);
154 if (ctx
.cfg
.section_from_path
) {
155 n
= ctx
.cfg
.section_from_path
;
158 while (slash
&& n
&& (slash
= strchr(slash
+ 1, '/')))
161 slash
= rel
.buf
+ rel
.len
;
162 while (slash
&& n
&& (slash
= xstrrchr(rel
.buf
, slash
- 1, '/')))
167 repo
->section
= xstrdup(rel
.buf
);
169 if (starts_with(repo
->name
, repo
->section
)) {
170 repo
->name
+= strlen(repo
->section
);
171 if (*repo
->name
== '/')
177 strbuf_addstr(path
, "cgitrc");
178 if (!stat(path
->buf
, &st
))
179 parse_configfile(path
->buf
, &repo_config
);
181 strbuf_release(&rel
);
184 static void scan_path(const char *base
, const char *path
, repo_config_fn fn
)
186 DIR *dir
= opendir(path
);
188 struct strbuf pathbuf
= STRBUF_INIT
;
189 size_t pathlen
= strlen(path
);
193 fprintf(stderr
, "Error opening directory %s: %s (%d)\n",
194 path
, strerror(errno
), errno
);
198 strbuf_add(&pathbuf
, path
, strlen(path
));
199 if (is_git_dir(pathbuf
.buf
)) {
200 add_repo(base
, &pathbuf
, fn
);
203 strbuf_addstr(&pathbuf
, "/.git");
204 if (is_git_dir(pathbuf
.buf
)) {
205 add_repo(base
, &pathbuf
, fn
);
209 * Add one because we don't want to lose the trailing '/' when we
210 * reset the length of pathbuf in the loop below.
213 while ((ent
= readdir(dir
)) != NULL
) {
214 if (ent
->d_name
[0] == '.') {
215 if (ent
->d_name
[1] == '\0')
217 if (ent
->d_name
[1] == '.' && ent
->d_name
[2] == '\0')
219 if (!ctx
.cfg
.scan_hidden_path
)
222 strbuf_setlen(&pathbuf
, pathlen
);
223 strbuf_addstr(&pathbuf
, ent
->d_name
);
224 if (stat(pathbuf
.buf
, &st
)) {
225 fprintf(stderr
, "Error checking path %s: %s (%d)\n",
226 pathbuf
.buf
, strerror(errno
), errno
);
229 if (S_ISDIR(st
.st_mode
))
230 scan_path(base
, pathbuf
.buf
, fn
);
233 strbuf_release(&pathbuf
);
237 void scan_projects(const char *path
, const char *projectsfile
, repo_config_fn fn
)
239 struct strbuf line
= STRBUF_INIT
;
243 projects
= fopen(projectsfile
, "r");
245 fprintf(stderr
, "Error opening projectsfile %s: %s (%d)\n",
246 projectsfile
, strerror(errno
), errno
);
249 while (strbuf_getline(&line
, projects
) != EOF
) {
252 strbuf_insert(&line
, 0, "/", 1);
253 strbuf_insert(&line
, 0, path
, strlen(path
));
254 scan_path(path
, line
.buf
, fn
);
256 if ((err
= ferror(projects
))) {
257 fprintf(stderr
, "Error reading from projectsfile %s: %s (%d)\n",
258 projectsfile
, strerror(err
), err
);
261 strbuf_release(&line
);
264 void scan_tree(const char *path
, repo_config_fn fn
)
266 scan_path(path
, path
, fn
);