]>
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 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
);
114 p
= fmt("%s/README.html", path
);
116 repo
->readme
= "README.html";
119 p
= fmt("%s/cgitrc", path
);
122 parse_configfile(xstrdup(p
), &repo_config
);
126 static void scan_path(const char *base
, const char *path
, repo_config_fn fn
)
133 if (is_git_dir(path
)) {
134 add_repo(base
, path
, fn
);
137 if (is_git_dir(fmt("%s/.git", path
))) {
138 add_repo(base
, fmt("%s/.git", path
), fn
);
143 fprintf(stderr
, "Error opening directory %s: %s (%d)\n",
144 path
, strerror(errno
), errno
);
147 while((ent
= readdir(dir
)) != NULL
) {
148 if (ent
->d_name
[0] == '.') {
149 if (ent
->d_name
[1] == '\0')
151 if (ent
->d_name
[1] == '.' && ent
->d_name
[2] == '\0')
154 buf
= malloc(strlen(path
) + strlen(ent
->d_name
) + 2);
156 fprintf(stderr
, "Alloc error on %s: %s (%d)\n",
157 path
, strerror(errno
), errno
);
160 sprintf(buf
, "%s/%s", path
, ent
->d_name
);
161 if (stat(buf
, &st
)) {
162 fprintf(stderr
, "Error checking path %s: %s (%d)\n",
163 buf
, strerror(errno
), errno
);
167 if (S_ISDIR(st
.st_mode
))
168 scan_path(base
, buf
, fn
);
174 #define lastc(s) s[strlen(s) - 1]
176 void scan_projects(const char *path
, const char *projectsfile
, repo_config_fn fn
)
178 char line
[MAX_PATH
* 2], *z
;
182 projects
= fopen(projectsfile
, "r");
184 fprintf(stderr
, "Error opening projectsfile %s: %s (%d)\n",
185 projectsfile
, strerror(errno
), errno
);
187 while (fgets(line
, sizeof(line
), projects
) != NULL
) {
188 for (z
= &lastc(line
);
189 strlen(line
) && strchr("\n\r", *z
);
193 scan_path(path
, fmt("%s/%s", path
, line
), fn
);
195 if ((err
= ferror(projects
))) {
196 fprintf(stderr
, "Error reading from projectsfile %s: %s (%d)\n",
197 projectsfile
, strerror(err
), err
);
202 void scan_tree(const char *path
, repo_config_fn fn
)
204 scan_path(path
, path
, fn
);