]>
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 "configfile.h"
16 /* return 1 if path contains a objects/ directory and a HEAD file */
17 static int is_git_dir(const char *path
)
20 static char buf
[MAX_PATH
];
22 if (snprintf(buf
, MAX_PATH
, "%s/objects", path
) >= MAX_PATH
) {
23 fprintf(stderr
, "Insanely long path: %s\n", path
);
28 fprintf(stderr
, "Error checking path %s: %s (%d)\n",
29 path
, strerror(errno
), errno
);
32 if (!S_ISDIR(st
.st_mode
))
35 sprintf(buf
, "%s/HEAD", path
);
38 fprintf(stderr
, "Error checking path %s: %s (%d)\n",
39 path
, strerror(errno
), errno
);
42 if (!S_ISREG(st
.st_mode
))
48 struct cgit_repo
*repo
;
49 repo_config_fn config_fn
;
54 static void repo_config(const char *name
, const char *value
)
56 config_fn(repo
, name
, value
);
59 static int gitweb_config(const char *key
, const char *value
, void *cb
)
61 if (ctx
.cfg
.enable_gitweb_owner
&& !strcmp(key
, "gitweb.owner"))
62 owner
= xstrdup(value
);
63 else if (ctx
.cfg
.enable_gitweb_desc
&& !strcmp(key
, "gitweb.description"))
64 desc
= xstrdup(value
);
65 else if (ctx
.cfg
.enable_gitweb_section
&& !strcmp(key
, "gitweb.category"))
66 section
= xstrdup(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
, const char *path
, repo_config_fn fn
)
83 char *rel
, *p
, *slash
;
87 if (stat(path
, &st
)) {
88 fprintf(stderr
, "Error accessing %s: %s (%d)\n",
89 path
, strerror(errno
), errno
);
93 if (ctx
.cfg
.strict_export
&& stat(fmt("%s/%s", path
, ctx
.cfg
.strict_export
), &st
))
96 if (!stat(fmt("%s/noweb", path
), &st
))
102 git_config_from_file(gitweb_config
, fmt("%s/config", path
), NULL
);
105 rel
= xstrdup(fmt("%s", path
));
107 rel
= xstrdup(fmt("%s", path
+ strlen(base
) + 1));
109 if (!strcmp(rel
+ strlen(rel
) - 5, "/.git"))
110 rel
[strlen(rel
) - 5] = '\0';
112 repo
= cgit_add_repo(rel
);
113 if (ctx
.cfg
.remove_suffix
)
114 if ((p
= strrchr(repo
->url
, '.')) && !strcmp(p
, ".git"))
116 repo
->name
= repo
->url
;
117 repo
->path
= xstrdup(path
);
119 if ((pwd
= getpwuid(st
.st_uid
)) == NULL
) {
120 fprintf(stderr
, "Error reading owner-info for %s: %s (%d)\n",
121 path
, strerror(errno
), errno
);
125 if ((p
= strchr(pwd
->pw_gecos
, ',')))
127 owner
= xstrdup(pwd
->pw_gecos
? pwd
->pw_gecos
: pwd
->pw_name
);
134 p
= fmt("%s/description", path
);
136 readfile(p
, &repo
->desc
, &size
);
140 p
= fmt("%s/README.html", path
);
142 repo
->readme
= "README.html";
145 repo
->section
= section
;
146 if (ctx
.cfg
.section_from_path
) {
147 n
= ctx
.cfg
.section_from_path
;
150 while (slash
&& n
&& (slash
= strchr(slash
, '/')))
153 slash
= rel
+ strlen(rel
);
154 while (slash
&& n
&& (slash
= xstrrchr(rel
, slash
, '/')))
159 repo
->section
= xstrdup(rel
);
161 if (!prefixcmp(repo
->name
, repo
->section
)) {
162 repo
->name
+= strlen(repo
->section
);
163 if (*repo
->name
== '/')
169 p
= fmt("%s/cgitrc", path
);
172 parse_configfile(xstrdup(p
), &repo_config
);
178 static void scan_path(const char *base
, const char *path
, repo_config_fn fn
)
180 DIR *dir
= opendir(path
);
186 fprintf(stderr
, "Error opening directory %s: %s (%d)\n",
187 path
, strerror(errno
), errno
);
190 if (is_git_dir(path
)) {
191 add_repo(base
, path
, fn
);
194 if (is_git_dir(fmt("%s/.git", path
))) {
195 add_repo(base
, fmt("%s/.git", path
), fn
);
198 while((ent
= readdir(dir
)) != NULL
) {
199 if (ent
->d_name
[0] == '.') {
200 if (ent
->d_name
[1] == '\0')
202 if (ent
->d_name
[1] == '.' && ent
->d_name
[2] == '\0')
204 if (!ctx
.cfg
.scan_hidden_path
)
207 buf
= malloc(strlen(path
) + strlen(ent
->d_name
) + 2);
209 fprintf(stderr
, "Alloc error on %s: %s (%d)\n",
210 path
, strerror(errno
), errno
);
213 sprintf(buf
, "%s/%s", path
, ent
->d_name
);
214 if (stat(buf
, &st
)) {
215 fprintf(stderr
, "Error checking path %s: %s (%d)\n",
216 buf
, strerror(errno
), errno
);
220 if (S_ISDIR(st
.st_mode
))
221 scan_path(base
, buf
, fn
);
228 #define lastc(s) s[strlen(s) - 1]
230 void scan_projects(const char *path
, const char *projectsfile
, repo_config_fn fn
)
232 char line
[MAX_PATH
* 2], *z
;
236 projects
= fopen(projectsfile
, "r");
238 fprintf(stderr
, "Error opening projectsfile %s: %s (%d)\n",
239 projectsfile
, strerror(errno
), errno
);
242 while (fgets(line
, sizeof(line
), projects
) != NULL
) {
243 for (z
= &lastc(line
);
244 strlen(line
) && strchr("\n\r", *z
);
248 scan_path(path
, fmt("%s/%s", path
, line
), fn
);
250 if ((err
= ferror(projects
))) {
251 fprintf(stderr
, "Error reading from projectsfile %s: %s (%d)\n",
252 projectsfile
, strerror(err
), err
);
257 void scan_tree(const char *path
, repo_config_fn fn
)
259 scan_path(path
, path
, fn
);