]>
git.cameronkatri.com Git - cgit.git/blob - scan-tree.c
3 * Copyright (C) 2008-2009 Lars Hjemli
4 * Copyright (C) 2010, 2012 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"
17 /* return 1 if path contains a objects/ directory and a HEAD file */
18 static int is_git_dir(const char *path
)
21 static char buf
[MAX_PATH
];
23 if (snprintf(buf
, MAX_PATH
, "%s/objects", path
) >= MAX_PATH
) {
24 fprintf(stderr
, "Insanely long path: %s\n", path
);
29 fprintf(stderr
, "Error checking path %s: %s (%d)\n",
30 path
, strerror(errno
), errno
);
33 if (!S_ISDIR(st
.st_mode
))
36 sprintf(buf
, "%s/HEAD", path
);
39 fprintf(stderr
, "Error checking path %s: %s (%d)\n",
40 path
, strerror(errno
), errno
);
43 if (!S_ISREG(st
.st_mode
))
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
, const char *path
, repo_config_fn fn
)
82 char *rel
, *p
, *slash
;
86 if (stat(path
, &st
)) {
87 fprintf(stderr
, "Error accessing %s: %s (%d)\n",
88 path
, strerror(errno
), errno
);
92 if (ctx
.cfg
.strict_export
&& stat(fmt("%s/%s", path
, ctx
.cfg
.strict_export
), &st
))
95 if (!stat(fmt("%s/noweb", path
), &st
))
99 rel
= xstrdup(fmt("%s", path
));
101 rel
= xstrdup(fmt("%s", path
+ strlen(base
) + 1));
103 if (!strcmp(rel
+ strlen(rel
) - 5, "/.git"))
104 rel
[strlen(rel
) - 5] = '\0';
106 repo
= cgit_add_repo(rel
);
108 if (ctx
.cfg
.enable_git_config
)
109 git_config_from_file(gitconfig_config
, fmt("%s/config", path
), NULL
);
111 if (ctx
.cfg
.remove_suffix
)
112 if ((p
= strrchr(repo
->url
, '.')) && !strcmp(p
, ".git"))
114 repo
->path
= xstrdup(path
);
115 while (!repo
->owner
) {
116 if ((pwd
= getpwuid(st
.st_uid
)) == NULL
) {
117 fprintf(stderr
, "Error reading owner-info for %s: %s (%d)\n",
118 path
, strerror(errno
), errno
);
122 if ((p
= strchr(pwd
->pw_gecos
, ',')))
124 repo
->owner
= xstrdup(pwd
->pw_gecos
? pwd
->pw_gecos
: pwd
->pw_name
);
127 if (repo
->desc
== cgit_default_repo_desc
|| !repo
->desc
) {
128 p
= fmt("%s/description", path
);
130 readfile(p
, &repo
->desc
, &size
);
134 p
= fmt("%s/README.html", path
);
136 repo
->readme
= "README.html";
138 if (ctx
.cfg
.section_from_path
) {
139 n
= ctx
.cfg
.section_from_path
;
142 while (slash
&& n
&& (slash
= strchr(slash
, '/')))
145 slash
= rel
+ strlen(rel
);
146 while (slash
&& n
&& (slash
= xstrrchr(rel
, slash
, '/')))
151 repo
->section
= xstrdup(rel
);
153 if (!prefixcmp(repo
->name
, repo
->section
)) {
154 repo
->name
+= strlen(repo
->section
);
155 if (*repo
->name
== '/')
161 p
= fmt("%s/cgitrc", path
);
163 parse_configfile(xstrdup(p
), &repo_config
);
169 static void scan_path(const char *base
, const char *path
, repo_config_fn fn
)
171 DIR *dir
= opendir(path
);
177 fprintf(stderr
, "Error opening directory %s: %s (%d)\n",
178 path
, strerror(errno
), errno
);
181 if (is_git_dir(path
)) {
182 add_repo(base
, path
, fn
);
185 if (is_git_dir(fmt("%s/.git", path
))) {
186 add_repo(base
, fmt("%s/.git", path
), fn
);
189 while ((ent
= readdir(dir
)) != NULL
) {
190 if (ent
->d_name
[0] == '.') {
191 if (ent
->d_name
[1] == '\0')
193 if (ent
->d_name
[1] == '.' && ent
->d_name
[2] == '\0')
195 if (!ctx
.cfg
.scan_hidden_path
)
198 buf
= malloc(strlen(path
) + strlen(ent
->d_name
) + 2);
200 fprintf(stderr
, "Alloc error on %s: %s (%d)\n",
201 path
, strerror(errno
), errno
);
204 sprintf(buf
, "%s/%s", path
, ent
->d_name
);
205 if (stat(buf
, &st
)) {
206 fprintf(stderr
, "Error checking path %s: %s (%d)\n",
207 buf
, strerror(errno
), errno
);
211 if (S_ISDIR(st
.st_mode
))
212 scan_path(base
, buf
, fn
);
219 #define lastc(s) s[strlen(s) - 1]
221 void scan_projects(const char *path
, const char *projectsfile
, repo_config_fn fn
)
223 char line
[MAX_PATH
* 2], *z
;
227 projects
= fopen(projectsfile
, "r");
229 fprintf(stderr
, "Error opening projectsfile %s: %s (%d)\n",
230 projectsfile
, strerror(errno
), errno
);
233 while (fgets(line
, sizeof(line
), projects
) != NULL
) {
234 for (z
= &lastc(line
);
235 strlen(line
) && strchr("\n\r", *z
);
239 scan_path(path
, fmt("%s/%s", path
, line
), fn
);
241 if ((err
= ferror(projects
))) {
242 fprintf(stderr
, "Error reading from projectsfile %s: %s (%d)\n",
243 projectsfile
, strerror(err
), err
);
248 void scan_tree(const char *path
, repo_config_fn fn
)
250 scan_path(path
, path
, fn
);