-rw-r--r-- | scan-tree.c | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/scan-tree.c b/scan-tree.c index e987824..6ba9193 100644 --- a/scan-tree.c +++ b/scan-tree.c | |||
@@ -52,51 +52,59 @@ char *owner; | |||
52 | static void repo_config(const char *name, const char *value) | 52 | static void repo_config(const char *name, const char *value) |
53 | { | 53 | { |
54 | config_fn(repo, name, value); | 54 | config_fn(repo, name, value); |
55 | } | 55 | } |
56 | 56 | ||
57 | static int git_owner_config(const char *key, const char *value, void *cb) | 57 | static int git_owner_config(const char *key, const char *value, void *cb) |
58 | { | 58 | { |
59 | if (!strcmp(key, "gitweb.owner")) | 59 | if (!strcmp(key, "gitweb.owner")) |
60 | owner = xstrdup(value); | 60 | owner = xstrdup(value); |
61 | return 0; | 61 | return 0; |
62 | } | 62 | } |
63 | 63 | ||
64 | static char *xstrrchr(char *s, char *from, int c) | ||
65 | { | ||
66 | while (from >= s && *from != c) | ||
67 | from--; | ||
68 | return from < s ? NULL : from; | ||
69 | } | ||
70 | |||
64 | static void add_repo(const char *base, const char *path, repo_config_fn fn) | 71 | static void add_repo(const char *base, const char *path, repo_config_fn fn) |
65 | { | 72 | { |
66 | struct stat st; | 73 | struct stat st; |
67 | struct passwd *pwd; | 74 | struct passwd *pwd; |
68 | char *p; | 75 | char *rel, *p, *slash; |
76 | int n; | ||
69 | size_t size; | 77 | size_t size; |
70 | 78 | ||
71 | if (stat(path, &st)) { | 79 | if (stat(path, &st)) { |
72 | fprintf(stderr, "Error accessing %s: %s (%d)\n", | 80 | fprintf(stderr, "Error accessing %s: %s (%d)\n", |
73 | path, strerror(errno), errno); | 81 | path, strerror(errno), errno); |
74 | return; | 82 | return; |
75 | } | 83 | } |
76 | if (!stat(fmt("%s/noweb", path), &st)) | 84 | if (!stat(fmt("%s/noweb", path), &st)) |
77 | return; | 85 | return; |
78 | 86 | ||
79 | owner = NULL; | 87 | owner = NULL; |
80 | if (ctx.cfg.enable_gitweb_owner) | 88 | if (ctx.cfg.enable_gitweb_owner) |
81 | git_config_from_file(git_owner_config, fmt("%s/config", path), NULL); | 89 | git_config_from_file(git_owner_config, fmt("%s/config", path), NULL); |
82 | if (base == path) | 90 | if (base == path) |
83 | p = fmt("%s", path); | 91 | rel = xstrdup(fmt("%s", path)); |
84 | else | 92 | else |
85 | p = fmt("%s", path + strlen(base) + 1); | 93 | rel = xstrdup(fmt("%s", path + strlen(base) + 1)); |
86 | 94 | ||
87 | if (!strcmp(p + strlen(p) - 5, "/.git")) | 95 | if (!strcmp(rel + strlen(rel) - 5, "/.git")) |
88 | p[strlen(p) - 5] = '\0'; | 96 | rel[strlen(rel) - 5] = '\0'; |
89 | 97 | ||
90 | repo = cgit_add_repo(xstrdup(p)); | 98 | repo = cgit_add_repo(rel); |
91 | if (ctx.cfg.remove_suffix) | 99 | if (ctx.cfg.remove_suffix) |
92 | if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git")) | 100 | if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git")) |
93 | *p = '\0'; | 101 | *p = '\0'; |
94 | repo->name = repo->url; | 102 | repo->name = repo->url; |
95 | repo->path = xstrdup(path); | 103 | repo->path = xstrdup(path); |
96 | while (!owner) { | 104 | while (!owner) { |
97 | if ((pwd = getpwuid(st.st_uid)) == NULL) { | 105 | if ((pwd = getpwuid(st.st_uid)) == NULL) { |
98 | fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n", | 106 | fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n", |
99 | path, strerror(errno), errno); | 107 | path, strerror(errno), errno); |
100 | break; | 108 | break; |
101 | } | 109 | } |
102 | if (pwd->pw_gecos) | 110 | if (pwd->pw_gecos) |
@@ -104,24 +112,46 @@ static void add_repo(const char *base, const char *path, repo_config_fn fn) | |||
104 | *p = '\0'; | 112 | *p = '\0'; |
105 | owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name); | 113 | owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name); |
106 | } | 114 | } |
107 | repo->owner = owner; | 115 | repo->owner = owner; |
108 | 116 | ||
109 | p = fmt("%s/description", path); | 117 | p = fmt("%s/description", path); |
110 | if (!stat(p, &st)) | 118 | if (!stat(p, &st)) |
111 | readfile(p, &repo->desc, &size); | 119 | readfile(p, &repo->desc, &size); |
112 | 120 | ||
113 | p = fmt("%s/README.html", path); | 121 | p = fmt("%s/README.html", path); |
114 | if (!stat(p, &st)) | 122 | if (!stat(p, &st)) |
115 | repo->readme = "README.html"; | 123 | repo->readme = "README.html"; |
124 | if (ctx.cfg.section_from_path) { | ||
125 | n = ctx.cfg.section_from_path; | ||
126 | if (n > 0) { | ||
127 | slash = rel; | ||
128 | while (slash && n && (slash = strchr(slash, '/'))) | ||
129 | n--; | ||
130 | } else { | ||
131 | slash = rel + strlen(rel); | ||
132 | while (slash && n && (slash = xstrrchr(rel, slash, '/'))) | ||
133 | n++; | ||
134 | } | ||
135 | if (slash && !n) { | ||
136 | *slash = '\0'; | ||
137 | repo->section = xstrdup(rel); | ||
138 | *slash = '/'; | ||
139 | if (!prefixcmp(repo->name, repo->section)) { | ||
140 | repo->name += strlen(repo->section); | ||
141 | if (*repo->name == '/') | ||
142 | repo->name++; | ||
143 | } | ||
144 | } | ||
145 | } | ||
116 | 146 | ||
117 | p = fmt("%s/cgitrc", path); | 147 | p = fmt("%s/cgitrc", path); |
118 | if (!stat(p, &st)) { | 148 | if (!stat(p, &st)) { |
119 | config_fn = fn; | 149 | config_fn = fn; |
120 | parse_configfile(xstrdup(p), &repo_config); | 150 | parse_configfile(xstrdup(p), &repo_config); |
121 | } | 151 | } |
122 | } | 152 | } |
123 | 153 | ||
124 | static void scan_path(const char *base, const char *path, repo_config_fn fn) | 154 | static void scan_path(const char *base, const char *path, repo_config_fn fn) |
125 | { | 155 | { |
126 | DIR *dir; | 156 | DIR *dir; |
127 | struct dirent *ent; | 157 | struct dirent *ent; |