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;
51 static void repo_config(const char *name, const char *value)
53 config_fn(repo, name, value);
56 static void add_repo(const char *base, const char *path, repo_config_fn fn)
63 if (stat(path, &st)) {
64 fprintf(stderr, "Error accessing %s: %s (%d)\n",
65 path, strerror(errno), errno);
68 if (!stat(fmt("%s/noweb", path), &st))
70 if ((pwd = getpwuid(st.st_uid)) == NULL) {
71 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
72 path, strerror(errno), errno);
78 p = fmt("%s", path + strlen(base) + 1);
80 if (!strcmp(p + strlen(p) - 5, "/.git"))
81 p[strlen(p) - 5] = '\0';
83 repo = cgit_add_repo(xstrdup(p));
84 if (ctx.cfg.remove_suffix)
85 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
87 repo->name = repo->url;
88 repo->path = xstrdup(path);
89 p = (pwd && pwd->pw_gecos) ? strchr(pwd->pw_gecos, ',') : NULL;
92 repo->owner = (pwd ? xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name) : "");
94 p = fmt("%s/description", path);
96 readfile(p, &repo->desc, &size);
98 p = fmt("%s/README.html", path);
100 repo->readme = "README.html";
102 p = fmt("%s/cgitrc", path);
105 parse_configfile(xstrdup(p), &repo_config);
109 static void scan_path(const char *base, const char *path, repo_config_fn fn)
116 if (is_git_dir(path)) {
117 add_repo(base, path, fn);
120 if (is_git_dir(fmt("%s/.git", path))) {
121 add_repo(base, fmt("%s/.git", path), fn);
126 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
127 path, strerror(errno), errno);
130 while((ent = readdir(dir)) != NULL) {
131 if (ent->d_name[0] == '.') {
132 if (ent->d_name[1] == '\0')
134 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
137 buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
139 fprintf(stderr, "Alloc error on %s: %s (%d)\n",
140 path, strerror(errno), errno);
143 sprintf(buf, "%s/%s", path, ent->d_name);
144 if (stat(buf, &st)) {
145 fprintf(stderr, "Error checking path %s: %s (%d)\n",
146 buf, strerror(errno), errno);
150 if (S_ISDIR(st.st_mode))
151 scan_path(base, buf, fn);
157 #define lastc(s) s[strlen(s) - 1]
159 void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
161 char line[MAX_PATH * 2], *z;
165 projects = fopen(projectsfile, "r");
167 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
168 projectsfile, strerror(errno), errno);
170 while (fgets(line, sizeof(line), projects) != NULL) {
171 for (z = &lastc(line);
172 strlen(line) && strchr("\n\r", *z);
176 scan_path(path, fmt("%s/%s", path, line), fn);
178 if ((err = ferror(projects))) {
179 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
180 projectsfile, strerror(err), err);
185 void scan_tree(const char *path, repo_config_fn fn)
187 scan_path(path, path, fn);