]>
git.cameronkatri.com Git - cgit.git/blob - scan-tree.c
3 * Copyright (C) 2008-2009 Lars Hjemli
4 * Copyright (C) 2010 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
;
52 static void repo_config(const char *name
, const char *value
)
54 config_fn(repo
, name
, value
);
57 static int git_owner_config(const char *key
, const char *value
, void *cb
)
59 if (!strcmp(key
, "gitweb.owner"))
60 owner
= xstrdup(value
);
64 static char *xstrrchr(char *s
, char *from
, int c
)
66 while (from
>= s
&& *from
!= c
)
68 return from
< s
? NULL
: from
;
71 static void add_repo(const char *base
, const char *path
, repo_config_fn fn
)
75 char *rel
, *p
, *slash
;
79 if (stat(path
, &st
)) {
80 fprintf(stderr
, "Error accessing %s: %s (%d)\n",
81 path
, strerror(errno
), errno
);
85 if (ctx
.cfg
.strict_export
&& stat(fmt("%s/%s", path
, ctx
.cfg
.strict_export
), &st
))
88 if (!stat(fmt("%s/noweb", path
), &st
))
92 if (ctx
.cfg
.enable_gitweb_owner
)
93 git_config_from_file(git_owner_config
, fmt("%s/config", path
), NULL
);
95 rel
= xstrdup(fmt("%s", path
));
97 rel
= xstrdup(fmt("%s", path
+ strlen(base
) + 1));
99 if (!strcmp(rel
+ strlen(rel
) - 5, "/.git"))
100 rel
[strlen(rel
) - 5] = '\0';
102 repo
= cgit_add_repo(rel
);
103 if (ctx
.cfg
.remove_suffix
)
104 if ((p
= strrchr(repo
->url
, '.')) && !strcmp(p
, ".git"))
106 repo
->name
= repo
->url
;
107 repo
->path
= xstrdup(path
);
109 if ((pwd
= getpwuid(st
.st_uid
)) == NULL
) {
110 fprintf(stderr
, "Error reading owner-info for %s: %s (%d)\n",
111 path
, strerror(errno
), errno
);
115 if ((p
= strchr(pwd
->pw_gecos
, ',')))
117 owner
= xstrdup(pwd
->pw_gecos
? pwd
->pw_gecos
: pwd
->pw_name
);
121 p
= fmt("%s/description", path
);
123 readfile(p
, &repo
->desc
, &size
);
126 p
= fmt("%s/README.html", path
);
128 repo
->readme
= "README.html";
130 if (ctx
.cfg
.section_from_path
) {
131 n
= ctx
.cfg
.section_from_path
;
134 while (slash
&& n
&& (slash
= strchr(slash
, '/')))
137 slash
= rel
+ strlen(rel
);
138 while (slash
&& n
&& (slash
= xstrrchr(rel
, slash
, '/')))
143 repo
->section
= xstrdup(rel
);
145 if (!prefixcmp(repo
->name
, repo
->section
)) {
146 repo
->name
+= strlen(repo
->section
);
147 if (*repo
->name
== '/')
153 p
= fmt("%s/cgitrc", path
);
156 parse_configfile(xstrdup(p
), &repo_config
);
160 static void scan_path(const char *base
, const char *path
, repo_config_fn fn
)
162 DIR *dir
= opendir(path
);
168 fprintf(stderr
, "Error opening directory %s: %s (%d)\n",
169 path
, strerror(errno
), errno
);
172 if (is_git_dir(path
)) {
173 add_repo(base
, path
, fn
);
176 if (is_git_dir(fmt("%s/.git", path
))) {
177 add_repo(base
, fmt("%s/.git", path
), fn
);
180 while((ent
= readdir(dir
)) != NULL
) {
181 if (ent
->d_name
[0] == '.') {
182 if (ent
->d_name
[1] == '\0')
184 if (ent
->d_name
[1] == '.' && ent
->d_name
[2] == '\0')
187 buf
= malloc(strlen(path
) + strlen(ent
->d_name
) + 2);
189 fprintf(stderr
, "Alloc error on %s: %s (%d)\n",
190 path
, strerror(errno
), errno
);
193 sprintf(buf
, "%s/%s", path
, ent
->d_name
);
194 if (stat(buf
, &st
)) {
195 fprintf(stderr
, "Error checking path %s: %s (%d)\n",
196 buf
, strerror(errno
), errno
);
200 if (S_ISDIR(st
.st_mode
))
201 scan_path(base
, buf
, fn
);
208 #define lastc(s) s[strlen(s) - 1]
210 void scan_projects(const char *path
, const char *projectsfile
, repo_config_fn fn
)
212 char line
[MAX_PATH
* 2], *z
;
216 projects
= fopen(projectsfile
, "r");
218 fprintf(stderr
, "Error opening projectsfile %s: %s (%d)\n",
219 projectsfile
, strerror(errno
), errno
);
221 while (fgets(line
, sizeof(line
), projects
) != NULL
) {
222 for (z
= &lastc(line
);
223 strlen(line
) && strchr("\n\r", *z
);
227 scan_path(path
, fmt("%s/%s", path
, line
), fn
);
229 if ((err
= ferror(projects
))) {
230 fprintf(stderr
, "Error reading from projectsfile %s: %s (%d)\n",
231 projectsfile
, strerror(err
), err
);
236 void scan_tree(const char *path
, repo_config_fn fn
)
238 scan_path(path
, path
, fn
);