]>
git.cameronkatri.com Git - cgit.git/blob - scan-tree.c
3 * Copyright (C) 2008-2009 Lars Hjemli
4 * Copyright (C) 2010-2013 Jason A. Donenfeld <Jason@zx2c4.com>
6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text)
11 #include "scan-tree.h"
12 #include "configfile.h"
15 /* return 1 if path contains a objects/ directory and a HEAD file */
16 static int is_git_dir(const char *path
)
19 struct strbuf pathbuf
= STRBUF_INIT
;
22 strbuf_addf(&pathbuf
, "%s/objects", path
);
23 if (stat(pathbuf
.buf
, &st
)) {
25 fprintf(stderr
, "Error checking path %s: %s (%d)\n",
26 path
, strerror(errno
), errno
);
29 if (!S_ISDIR(st
.st_mode
))
32 strbuf_reset(&pathbuf
);
33 strbuf_addf(&pathbuf
, "%s/HEAD", path
);
34 if (stat(pathbuf
.buf
, &st
)) {
36 fprintf(stderr
, "Error checking path %s: %s (%d)\n",
37 path
, strerror(errno
), errno
);
40 if (!S_ISREG(st
.st_mode
))
45 strbuf_release(&pathbuf
);
49 struct cgit_repo
*repo
;
50 repo_config_fn config_fn
;
52 static void repo_config(const char *name
, const char *value
)
54 config_fn(repo
, name
, value
);
57 static int gitconfig_config(const char *key
, const char *value
, void *cb
)
59 if (!strcmp(key
, "gitweb.owner"))
60 config_fn(repo
, "owner", value
);
61 else if (!strcmp(key
, "gitweb.description"))
62 config_fn(repo
, "desc", value
);
63 else if (!strcmp(key
, "gitweb.category"))
64 config_fn(repo
, "section", value
);
65 else if (!prefixcmp(key
, "cgit."))
66 config_fn(repo
, key
+ 5, value
);
71 static char *xstrrchr(char *s
, char *from
, int c
)
73 while (from
>= s
&& *from
!= c
)
75 return from
< s
? NULL
: from
;
78 static void add_repo(const char *base
, struct strbuf
*path
, repo_config_fn fn
)
83 struct strbuf rel
= STRBUF_INIT
;
88 if (stat(path
->buf
, &st
)) {
89 fprintf(stderr
, "Error accessing %s: %s (%d)\n",
90 path
->buf
, strerror(errno
), errno
);
94 strbuf_addch(path
, '/');
97 if (ctx
.cfg
.strict_export
) {
98 strbuf_addstr(path
, ctx
.cfg
.strict_export
);
99 if(stat(path
->buf
, &st
))
101 strbuf_setlen(path
, pathlen
);
104 strbuf_addstr(path
, "noweb");
105 if (!stat(path
->buf
, &st
))
107 strbuf_setlen(path
, pathlen
);
109 if (strncmp(base
, path
->buf
, strlen(base
)))
110 strbuf_addbuf(&rel
, path
);
112 strbuf_addstr(&rel
, path
->buf
+ strlen(base
) + 1);
114 if (!strcmp(rel
.buf
+ rel
.len
- 5, "/.git"))
115 strbuf_setlen(&rel
, rel
.len
- 5);
116 else if (rel
.len
&& rel
.buf
[rel
.len
- 1] == '/')
117 strbuf_setlen(&rel
, rel
.len
- 1);
119 repo
= cgit_add_repo(rel
.buf
);
121 if (ctx
.cfg
.enable_git_config
) {
122 strbuf_addstr(path
, "config");
123 git_config_from_file(gitconfig_config
, path
->buf
, NULL
);
124 strbuf_setlen(path
, pathlen
);
127 if (ctx
.cfg
.remove_suffix
)
128 if ((p
= strrchr(repo
->url
, '.')) && !strcmp(p
, ".git"))
130 repo
->path
= xstrdup(path
->buf
);
131 while (!repo
->owner
) {
132 if ((pwd
= getpwuid(st
.st_uid
)) == NULL
) {
133 fprintf(stderr
, "Error reading owner-info for %s: %s (%d)\n",
134 path
->buf
, strerror(errno
), errno
);
138 if ((p
= strchr(pwd
->pw_gecos
, ',')))
140 repo
->owner
= xstrdup(pwd
->pw_gecos
? pwd
->pw_gecos
: pwd
->pw_name
);
143 if (repo
->desc
== cgit_default_repo_desc
|| !repo
->desc
) {
144 strbuf_addstr(path
, "description");
145 if (!stat(path
->buf
, &st
))
146 readfile(path
->buf
, &repo
->desc
, &size
);
147 strbuf_setlen(path
, pathlen
);
150 if (ctx
.cfg
.section_from_path
) {
151 n
= ctx
.cfg
.section_from_path
;
154 while (slash
&& n
&& (slash
= strchr(slash
+ 1, '/')))
157 slash
= rel
.buf
+ rel
.len
;
158 while (slash
&& n
&& (slash
= xstrrchr(rel
.buf
, slash
- 1, '/')))
163 repo
->section
= xstrdup(rel
.buf
);
165 if (!prefixcmp(repo
->name
, repo
->section
)) {
166 repo
->name
+= strlen(repo
->section
);
167 if (*repo
->name
== '/')
173 strbuf_addstr(path
, "cgitrc");
174 if (!stat(path
->buf
, &st
))
175 parse_configfile(xstrdup(path
->buf
), &repo_config
);
177 strbuf_release(&rel
);
180 static void scan_path(const char *base
, const char *path
, repo_config_fn fn
)
182 DIR *dir
= opendir(path
);
184 struct strbuf pathbuf
= STRBUF_INIT
;
185 size_t pathlen
= strlen(path
);
189 fprintf(stderr
, "Error opening directory %s: %s (%d)\n",
190 path
, strerror(errno
), errno
);
194 strbuf_add(&pathbuf
, path
, strlen(path
));
195 if (is_git_dir(pathbuf
.buf
)) {
196 add_repo(base
, &pathbuf
, fn
);
199 strbuf_addstr(&pathbuf
, "/.git");
200 if (is_git_dir(pathbuf
.buf
)) {
201 add_repo(base
, &pathbuf
, fn
);
205 * Add one because we don't want to lose the trailing '/' when we
206 * reset the length of pathbuf in the loop below.
209 while ((ent
= readdir(dir
)) != NULL
) {
210 if (ent
->d_name
[0] == '.') {
211 if (ent
->d_name
[1] == '\0')
213 if (ent
->d_name
[1] == '.' && ent
->d_name
[2] == '\0')
215 if (!ctx
.cfg
.scan_hidden_path
)
218 strbuf_setlen(&pathbuf
, pathlen
);
219 strbuf_addstr(&pathbuf
, ent
->d_name
);
220 if (stat(pathbuf
.buf
, &st
)) {
221 fprintf(stderr
, "Error checking path %s: %s (%d)\n",
222 pathbuf
.buf
, strerror(errno
), errno
);
225 if (S_ISDIR(st
.st_mode
))
226 scan_path(base
, pathbuf
.buf
, fn
);
229 strbuf_release(&pathbuf
);
233 void scan_projects(const char *path
, const char *projectsfile
, repo_config_fn fn
)
235 struct strbuf line
= STRBUF_INIT
;
239 projects
= fopen(projectsfile
, "r");
241 fprintf(stderr
, "Error opening projectsfile %s: %s (%d)\n",
242 projectsfile
, strerror(errno
), errno
);
245 while (strbuf_getline(&line
, projects
, '\n') != EOF
) {
248 strbuf_insert(&line
, 0, "/", 1);
249 strbuf_insert(&line
, 0, path
, strlen(path
));
250 scan_path(path
, line
.buf
, fn
);
252 if ((err
= ferror(projects
))) {
253 fprintf(stderr
, "Error reading from projectsfile %s: %s (%d)\n",
254 projectsfile
, strerror(err
), err
);
257 strbuf_release(&line
);
260 void scan_tree(const char *path
, repo_config_fn fn
)
262 scan_path(path
, path
, fn
);