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 void add_repo(const char *base, const char *path, repo_config_fn fn)
71 if (stat(path, &st)) {
72 fprintf(stderr, "Error accessing %s: %s (%d)\n",
73 path, strerror(errno), errno);
76 if (!stat(fmt("%s/noweb", path), &st))
80 if (ctx.cfg.enable_gitweb_owner)
81 git_config_from_file(git_owner_config, fmt("%s/config", path), NULL);
85 p = fmt("%s", path + strlen(base) + 1);
87 if (!strcmp(p + strlen(p) - 5, "/.git"))
88 p[strlen(p) - 5] = '\0';
90 repo = cgit_add_repo(xstrdup(p));
91 if (ctx.cfg.remove_suffix)
92 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
94 repo->name = repo->url;
95 repo->path = xstrdup(path);
97 if ((pwd = getpwuid(st.st_uid)) == NULL) {
98 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
99 path, strerror(errno), errno);
103 if ((p = strchr(pwd->pw_gecos, ',')))
105 owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
109 p = fmt("%s/description", path);
111 readfile(p, &repo->desc, &size);
113 p = fmt("%s/README.html", path);
115 repo->readme = "README.html";
117 p = fmt("%s/cgitrc", path);
120 parse_configfile(xstrdup(p), &repo_config);
124 static void scan_path(const char *base, const char *path, repo_config_fn fn)
131 if (is_git_dir(path)) {
132 add_repo(base, path, fn);
135 if (is_git_dir(fmt("%s/.git", path))) {
136 add_repo(base, fmt("%s/.git", path), fn);
141 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
142 path, strerror(errno), errno);
145 while((ent = readdir(dir)) != NULL) {
146 if (ent->d_name[0] == '.') {
147 if (ent->d_name[1] == '\0')
149 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
152 buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
154 fprintf(stderr, "Alloc error on %s: %s (%d)\n",
155 path, strerror(errno), errno);
158 sprintf(buf, "%s/%s", path, ent->d_name);
159 if (stat(buf, &st)) {
160 fprintf(stderr, "Error checking path %s: %s (%d)\n",
161 buf, strerror(errno), errno);
165 if (S_ISDIR(st.st_mode))
166 scan_path(base, buf, fn);
172 #define lastc(s) s[strlen(s) - 1]
174 void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
176 char line[MAX_PATH * 2], *z;
180 projects = fopen(projectsfile, "r");
182 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
183 projectsfile, strerror(errno), errno);
185 while (fgets(line, sizeof(line), projects) != NULL) {
186 for (z = &lastc(line);
187 strlen(line) && strchr("\n\r", *z);
191 scan_path(path, fmt("%s/%s", path, line), fn);
193 if ((err = ferror(projects))) {
194 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
195 projectsfile, strerror(err), err);
200 void scan_tree(const char *path, repo_config_fn fn)
202 scan_path(path, path, fn);