]>
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"
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 static struct cgit_repo
*repo
;
50 static repo_config_fn config_fn
;
52 static void scan_tree_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
)
61 if (!strcmp(key
, "gitweb.owner"))
62 config_fn(repo
, "owner", value
);
63 else if (!strcmp(key
, "gitweb.description"))
64 config_fn(repo
, "desc", value
);
65 else if (!strcmp(key
, "gitweb.category"))
66 config_fn(repo
, "section", value
);
67 else if (!strcmp(key
, "gitweb.homepage"))
68 config_fn(repo
, "homepage", value
);
69 else if (skip_prefix(key
, "cgit.", &name
))
70 config_fn(repo
, name
, value
);
75 static char *xstrrchr(char *s
, char *from
, int c
)
77 while (from
>= s
&& *from
!= c
)
79 return from
< s
? NULL
: from
;
82 static void add_repo(const char *base
, struct strbuf
*path
, repo_config_fn fn
)
87 struct strbuf rel
= STRBUF_INIT
;
92 if (stat(path
->buf
, &st
)) {
93 fprintf(stderr
, "Error accessing %s: %s (%d)\n",
94 path
->buf
, strerror(errno
), errno
);
98 strbuf_addch(path
, '/');
101 if (ctx
.cfg
.strict_export
) {
102 strbuf_addstr(path
, ctx
.cfg
.strict_export
);
103 if(stat(path
->buf
, &st
))
105 strbuf_setlen(path
, pathlen
);
108 strbuf_addstr(path
, "noweb");
109 if (!stat(path
->buf
, &st
))
111 strbuf_setlen(path
, pathlen
);
113 if (!starts_with(path
->buf
, base
))
114 strbuf_addbuf(&rel
, path
);
116 strbuf_addstr(&rel
, path
->buf
+ strlen(base
) + 1);
118 if (!strcmp(rel
.buf
+ rel
.len
- 5, "/.git"))
119 strbuf_setlen(&rel
, rel
.len
- 5);
120 else if (rel
.len
&& rel
.buf
[rel
.len
- 1] == '/')
121 strbuf_setlen(&rel
, rel
.len
- 1);
123 repo
= cgit_add_repo(rel
.buf
);
125 if (ctx
.cfg
.enable_git_config
) {
126 strbuf_addstr(path
, "config");
127 git_config_from_file(gitconfig_config
, path
->buf
, NULL
);
128 strbuf_setlen(path
, pathlen
);
131 if (ctx
.cfg
.remove_suffix
) {
133 strip_suffix(repo
->url
, ".git", &urllen
);
134 strip_suffix_mem(repo
->url
, &urllen
, "/");
135 repo
->url
[urllen
] = '\0';
137 repo
->path
= xstrdup(path
->buf
);
138 while (!repo
->owner
) {
139 if ((pwd
= getpwuid(st
.st_uid
)) == NULL
) {
140 fprintf(stderr
, "Error reading owner-info for %s: %s (%d)\n",
141 path
->buf
, strerror(errno
), errno
);
145 if ((p
= strchr(pwd
->pw_gecos
, ',')))
147 repo
->owner
= xstrdup(pwd
->pw_gecos
? pwd
->pw_gecos
: pwd
->pw_name
);
150 if (repo
->desc
== cgit_default_repo_desc
|| !repo
->desc
) {
151 strbuf_addstr(path
, "description");
152 if (!stat(path
->buf
, &st
))
153 readfile(path
->buf
, &repo
->desc
, &size
);
154 strbuf_setlen(path
, pathlen
);
157 if (ctx
.cfg
.section_from_path
) {
158 n
= ctx
.cfg
.section_from_path
;
161 while (slash
&& n
&& (slash
= strchr(slash
+ 1, '/')))
164 slash
= rel
.buf
+ rel
.len
;
165 while (slash
&& n
&& (slash
= xstrrchr(rel
.buf
, slash
- 1, '/')))
170 repo
->section
= xstrdup(rel
.buf
);
172 if (starts_with(repo
->name
, repo
->section
)) {
173 repo
->name
+= strlen(repo
->section
);
174 if (*repo
->name
== '/')
180 strbuf_addstr(path
, "cgitrc");
181 if (!stat(path
->buf
, &st
))
182 parse_configfile(path
->buf
, &scan_tree_repo_config
);
184 strbuf_release(&rel
);
187 static void scan_path(const char *base
, const char *path
, repo_config_fn fn
)
189 DIR *dir
= opendir(path
);
191 struct strbuf pathbuf
= STRBUF_INIT
;
192 size_t pathlen
= strlen(path
);
196 fprintf(stderr
, "Error opening directory %s: %s (%d)\n",
197 path
, strerror(errno
), errno
);
201 strbuf_add(&pathbuf
, path
, strlen(path
));
202 if (is_git_dir(pathbuf
.buf
)) {
203 add_repo(base
, &pathbuf
, fn
);
206 strbuf_addstr(&pathbuf
, "/.git");
207 if (is_git_dir(pathbuf
.buf
)) {
208 add_repo(base
, &pathbuf
, fn
);
212 * Add one because we don't want to lose the trailing '/' when we
213 * reset the length of pathbuf in the loop below.
216 while ((ent
= readdir(dir
)) != NULL
) {
217 if (ent
->d_name
[0] == '.') {
218 if (ent
->d_name
[1] == '\0')
220 if (ent
->d_name
[1] == '.' && ent
->d_name
[2] == '\0')
222 if (!ctx
.cfg
.scan_hidden_path
)
225 strbuf_setlen(&pathbuf
, pathlen
);
226 strbuf_addstr(&pathbuf
, ent
->d_name
);
227 if (stat(pathbuf
.buf
, &st
)) {
228 fprintf(stderr
, "Error checking path %s: %s (%d)\n",
229 pathbuf
.buf
, strerror(errno
), errno
);
232 if (S_ISDIR(st
.st_mode
))
233 scan_path(base
, pathbuf
.buf
, fn
);
236 strbuf_release(&pathbuf
);
240 void scan_projects(const char *path
, const char *projectsfile
, repo_config_fn fn
)
242 struct strbuf line
= STRBUF_INIT
;
246 projects
= fopen(projectsfile
, "r");
248 fprintf(stderr
, "Error opening projectsfile %s: %s (%d)\n",
249 projectsfile
, strerror(errno
), errno
);
252 while (strbuf_getline(&line
, projects
) != EOF
) {
255 strbuf_insert(&line
, 0, "/", 1);
256 strbuf_insert(&line
, 0, path
, strlen(path
));
257 scan_path(path
, line
.buf
, fn
);
259 if ((err
= ferror(projects
))) {
260 fprintf(stderr
, "Error reading from projectsfile %s: %s (%d)\n",
261 projectsfile
, strerror(err
), err
);
264 strbuf_release(&line
);
267 void scan_tree(const char *path
, repo_config_fn fn
)
269 scan_path(path
, path
, fn
);