]> git.cameronkatri.com Git - cgit.git/blob - scan-tree.c
Add support for 'remove-suffix' option
[cgit.git] / scan-tree.c
1 /* scan-tree.c
2 *
3 * Copyright (C) 2008-2009 Lars Hjemli
4 * Copyright (C) 2010 Jason A. Donenfeld <Jason@zx2c4.com>
5 *
6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text)
8 */
9
10 #include "cgit.h"
11 #include "configfile.h"
12 #include "html.h"
13
14 #define MAX_PATH 4096
15
16 /* return 1 if path contains a objects/ directory and a HEAD file */
17 static int is_git_dir(const char *path)
18 {
19 struct stat st;
20 static char buf[MAX_PATH];
21
22 if (snprintf(buf, MAX_PATH, "%s/objects", path) >= MAX_PATH) {
23 fprintf(stderr, "Insanely long path: %s\n", path);
24 return 0;
25 }
26 if (stat(buf, &st)) {
27 if (errno != ENOENT)
28 fprintf(stderr, "Error checking path %s: %s (%d)\n",
29 path, strerror(errno), errno);
30 return 0;
31 }
32 if (!S_ISDIR(st.st_mode))
33 return 0;
34
35 sprintf(buf, "%s/HEAD", path);
36 if (stat(buf, &st)) {
37 if (errno != ENOENT)
38 fprintf(stderr, "Error checking path %s: %s (%d)\n",
39 path, strerror(errno), errno);
40 return 0;
41 }
42 if (!S_ISREG(st.st_mode))
43 return 0;
44
45 return 1;
46 }
47
48 struct cgit_repo *repo;
49 repo_config_fn config_fn;
50
51 static void repo_config(const char *name, const char *value)
52 {
53 config_fn(repo, name, value);
54 }
55
56 static void add_repo(const char *base, const char *path, repo_config_fn fn)
57 {
58 struct stat st;
59 struct passwd *pwd;
60 char *p;
61 size_t size;
62
63 if (stat(path, &st)) {
64 fprintf(stderr, "Error accessing %s: %s (%d)\n",
65 path, strerror(errno), errno);
66 return;
67 }
68 if (!stat(fmt("%s/noweb", path), &st))
69 return;
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);
73 return;
74 }
75 if (base == path)
76 p = fmt("%s", path);
77 else
78 p = fmt("%s", path + strlen(base) + 1);
79
80 if (!strcmp(p + strlen(p) - 5, "/.git"))
81 p[strlen(p) - 5] = '\0';
82
83 repo = cgit_add_repo(xstrdup(p));
84 if (ctx.cfg.remove_suffix)
85 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
86 *p = '\0';
87 repo->name = repo->url;
88 repo->path = xstrdup(path);
89 p = (pwd && pwd->pw_gecos) ? strchr(pwd->pw_gecos, ',') : NULL;
90 if (p)
91 *p = '\0';
92 repo->owner = (pwd ? xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name) : "");
93
94 p = fmt("%s/description", path);
95 if (!stat(p, &st))
96 readfile(p, &repo->desc, &size);
97
98 p = fmt("%s/README.html", path);
99 if (!stat(p, &st))
100 repo->readme = "README.html";
101
102 p = fmt("%s/cgitrc", path);
103 if (!stat(p, &st)) {
104 config_fn = fn;
105 parse_configfile(xstrdup(p), &repo_config);
106 }
107 }
108
109 static void scan_path(const char *base, const char *path, repo_config_fn fn)
110 {
111 DIR *dir;
112 struct dirent *ent;
113 char *buf;
114 struct stat st;
115
116 if (is_git_dir(path)) {
117 add_repo(base, path, fn);
118 return;
119 }
120 if (is_git_dir(fmt("%s/.git", path))) {
121 add_repo(base, fmt("%s/.git", path), fn);
122 return;
123 }
124 dir = opendir(path);
125 if (!dir) {
126 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
127 path, strerror(errno), errno);
128 return;
129 }
130 while((ent = readdir(dir)) != NULL) {
131 if (ent->d_name[0] == '.') {
132 if (ent->d_name[1] == '\0')
133 continue;
134 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
135 continue;
136 }
137 buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
138 if (!buf) {
139 fprintf(stderr, "Alloc error on %s: %s (%d)\n",
140 path, strerror(errno), errno);
141 exit(1);
142 }
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);
147 free(buf);
148 continue;
149 }
150 if (S_ISDIR(st.st_mode))
151 scan_path(base, buf, fn);
152 free(buf);
153 }
154 closedir(dir);
155 }
156
157 #define lastc(s) s[strlen(s) - 1]
158
159 void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
160 {
161 char line[MAX_PATH * 2], *z;
162 FILE *projects;
163 int err;
164
165 projects = fopen(projectsfile, "r");
166 if (!projects) {
167 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
168 projectsfile, strerror(errno), errno);
169 }
170 while (fgets(line, sizeof(line), projects) != NULL) {
171 for (z = &lastc(line);
172 strlen(line) && strchr("\n\r", *z);
173 z = &lastc(line))
174 *z = '\0';
175 if (strlen(line))
176 scan_path(path, fmt("%s/%s", path, line), fn);
177 }
178 if ((err = ferror(projects))) {
179 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
180 projectsfile, strerror(err), err);
181 }
182 fclose(projects);
183 }
184
185 void scan_tree(const char *path, repo_config_fn fn)
186 {
187 scan_path(path, path, fn);
188 }