read repository owner name with gitconfig parser instead of a hand-rolled one
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
diff --git a/gotweb/gotweb.c b/gotweb/gotweb.c
index 5a0d7c2..a071ae4 100644
--- a/gotweb/gotweb.c
+++ b/gotweb/gotweb.c
@@ -160,7 +160,7 @@ static struct gw_header *gw_init_header(void);
static const struct got_error *gw_get_repo_description(char **, struct gw_trans *,
char *);
-static char *gw_get_repo_owner(struct gw_trans *,
+static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
char *);
static char *gw_get_time_str(time_t, int);
static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
@@ -539,7 +539,8 @@ gw_index(struct gw_trans *gw_trans)
return got_error_from_errno("asprintf");
if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
- gw_dir->description, gw_dir->owner, gw_dir->age,
+ gw_dir->description, gw_dir->owner ? gw_dir->owner : "",
+ gw_dir->age,
navs) == -1)
return got_error_from_errno("asprintf");
@@ -786,20 +787,18 @@ gw_summary(struct gw_trans *gw_trans)
}
}
- if (gw_trans->gw_conf->got_show_repo_owner) {
- if (gw_trans->gw_dir->owner != NULL &&
- (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
- if (asprintf(&repo_owner_html, repo_owner,
- gw_trans->gw_dir->owner) == -1) {
- error = got_error_from_errno("asprintf");
- goto done;
- }
+ if (gw_trans->gw_conf->got_show_repo_owner &&
+ gw_trans->gw_dir->owner != NULL) {
+ if (asprintf(&repo_owner_html, repo_owner,
+ gw_trans->gw_dir->owner) == -1) {
+ error = got_error_from_errno("asprintf");
+ goto done;
+ }
- kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
- if (kerr != KCGI_OK) {
- error = gw_kcgi_error(kerr);
- goto done;
- }
+ kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
+ if (kerr != KCGI_OK) {
+ error = gw_kcgi_error(kerr);
+ goto done;
}
}
@@ -1047,7 +1046,9 @@ done:
gw_dir->path);
if (error)
goto errored;
- gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
+ error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
+ if (error)
+ goto errored;
error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
"refs/heads", TM_DIFF);
if (error)
@@ -1746,63 +1747,29 @@ done:
return diff_html;
}
-static char *
-gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
+static const struct got_error *
+gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
{
- FILE *f;
- char *owner = NULL, *d_file = NULL;
- char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
- char *comp, *pos, *buf;
- unsigned int i;
-
- if (gw_trans->gw_conf->got_show_repo_owner == 0)
- goto err;
-
- if (asprintf(&d_file, "%s/config", dir) == -1)
- goto err;
+ const struct got_error *error = NULL;
+ struct got_repository *repo;
+ const char *gitconfig_owner;
- if ((f = fopen(d_file, "r")) == NULL)
- goto err;
+ *owner = NULL;
- if ((buf = calloc(128, sizeof(char *))) == NULL)
- goto err;
-
- while ((fgets(buf, 128, f)) != NULL) {
- if (ferror(f))
- goto err;
- if ((pos = strstr(buf, gotweb)) != NULL)
- break;
+ if (gw_trans->gw_conf->got_show_repo_owner == 0)
+ return NULL;
- if ((pos = strstr(buf, gitweb)) != NULL)
- break;
+ error = got_repo_open(&repo, dir, NULL);
+ if (error)
+ return error;
+ gitconfig_owner = got_repo_get_gitconfig_owner(repo);
+ if (gitconfig_owner) {
+ *owner = strdup(gitconfig_owner);
+ if (*owner == NULL)
+ error = got_error_from_errno("strdup");
}
-
- if (pos == NULL)
- goto err;
-
- do {
- fgets(buf, 128, f);
- if (ferror(f))
- goto err;
- } while ((comp = strcasestr(buf, gw_owner)) == NULL);
-
- if (comp == NULL)
- goto err;
-
- if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
- goto err;
-
- for (i = 0; i < 2; i++)
- owner = strsep(&buf, "\"");
-
- if (owner == NULL)
- goto err;
-
- fclose(f);
- free(d_file);
- return owner;
-err:
- return strdup("");
+ got_repo_close(repo);
+ return error;
}
static const struct got_error *
diff --git a/include/got_repository.h b/include/got_repository.h
index f54c896..d3b000f 100644
--- a/include/got_repository.h
+++ b/include/got_repository.h
@@ -44,6 +44,9 @@ const char *got_repo_get_global_gitconfig_author_name(struct got_repository *);
/* Obtain global commit author email parsed ~/.gitconfig, else NULL. */
const char *got_repo_get_global_gitconfig_author_email(struct got_repository *);
+/* Obtain repository owner name if parsed from gitconfig, else NULL. */
+const char *got_repo_get_gitconfig_owner(struct got_repository *);
+
/* Information about one remote repository. */
struct got_remote_repo {
char *name;
diff --git a/lib/got_lib_privsep.h b/lib/got_lib_privsep.h
index 08d4c9d..f73c2bf 100644
--- a/lib/got_lib_privsep.h
+++ b/lib/got_lib_privsep.h
@@ -119,6 +119,8 @@ enum got_imsg_type {
GOT_IMSG_GITCONFIG_STR_VAL,
GOT_IMSG_GITCONFIG_REMOTES,
GOT_IMSG_GITCONFIG_REMOTE,
+ GOT_IMSG_GITCONFIG_OWNER_REQUEST,
+ GOT_IMSG_GITCONFIG_OWNER,
};
/* Structure for GOT_IMSG_ERROR. */
@@ -325,6 +327,7 @@ const struct got_error *got_privsep_send_gitconfig_author_email_req(
struct imsgbuf *);
const struct got_error *got_privsep_send_gitconfig_remotes_req(
struct imsgbuf *);
+const struct got_error *got_privsep_send_gitconfig_owner_req(struct imsgbuf *);
const struct got_error *got_privsep_send_gitconfig_str(struct imsgbuf *,
const char *);
const struct got_error *got_privsep_recv_gitconfig_str(char **,
diff --git a/lib/got_lib_repository.h b/lib/got_lib_repository.h
index fe65562..5b1a405 100644
--- a/lib/got_lib_repository.h
+++ b/lib/got_lib_repository.h
@@ -49,6 +49,7 @@ struct got_repository {
char *global_gitconfig_author_email;
int ngitconfig_remotes;
struct got_remote_repo *gitconfig_remotes;
+ char *gitconfig_owner;
};
const struct got_error*got_repo_cache_object(struct got_repository *,
diff --git a/lib/privsep.c b/lib/privsep.c
index a2566da..8964ec0 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -1305,6 +1305,17 @@ got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
}
const struct got_error *
+got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
+{
+ if (imsg_compose(ibuf,
+ GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
+ return got_error_from_errno("imsg_compose "
+ "GITCONFIG_OWNER_REQUEST");
+
+ return flush_imsg(ibuf);
+}
+
+const struct got_error *
got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
{
size_t len = value ? strlen(value) + 1 : 0;
diff --git a/lib/repository.c b/lib/repository.c
index 1f4bef4..479f6f6 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -113,6 +113,12 @@ got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
return repo->global_gitconfig_author_email;
}
+const char *
+got_repo_get_gitconfig_owner(struct got_repository *repo)
+{
+ return repo->gitconfig_owner;
+}
+
int
got_repo_is_bare(struct got_repository *repo)
{
@@ -374,6 +380,7 @@ static const struct got_error *
parse_gitconfig_file(int *gitconfig_repository_format_version,
char **gitconfig_author_name, char **gitconfig_author_email,
struct got_remote_repo **remotes, int *nremotes,
+ char **gitconfig_owner,
const char *gitconfig_path)
{
const struct got_error *err = NULL, *child_err = NULL;
@@ -462,6 +469,15 @@ parse_gitconfig_file(int *gitconfig_repository_format_version,
goto done;
}
+ if (gitconfig_owner) {
+ err = got_privsep_send_gitconfig_owner_req(ibuf);
+ if (err)
+ goto done;
+ err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
+ if (err)
+ goto done;
+ }
+
imsg_clear(ibuf);
err = got_privsep_send_stop(imsg_fds[0]);
child_err = got_privsep_wait_for_child(pid);
@@ -490,7 +506,7 @@ read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
err = parse_gitconfig_file(&dummy_repo_version,
&repo->global_gitconfig_author_name,
&repo->global_gitconfig_author_email,
- NULL, NULL, global_gitconfig_path);
+ NULL, NULL, NULL, global_gitconfig_path);
if (err)
return err;
}
@@ -503,7 +519,7 @@ read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
&repo->gitconfig_author_name, &repo->gitconfig_author_email,
&repo->gitconfig_remotes, &repo->ngitconfig_remotes,
- repo_gitconfig_path);
+ &repo->gitconfig_owner, repo_gitconfig_path);
if (err)
goto done;
done:
diff --git a/libexec/got-read-gitconfig/got-read-gitconfig.c b/libexec/got-read-gitconfig/got-read-gitconfig.c
index 7c6439d..4d1f4a8 100644
--- a/libexec/got-read-gitconfig/got-read-gitconfig.c
+++ b/libexec/got-read-gitconfig/got-read-gitconfig.c
@@ -142,6 +142,21 @@ done:
return err;
}
+static const struct got_error *
+gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
+{
+ char *value;
+
+ if (gitconfig == NULL)
+ return got_error(GOT_ERR_PRIVSEP_MSG);
+
+ value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
+ if (value)
+ return got_privsep_send_gitconfig_str(ibuf, value);
+ value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
+ return got_privsep_send_gitconfig_str(ibuf, value);
+}
+
int
main(int argc, char *argv[])
{
@@ -220,6 +235,9 @@ main(int argc, char *argv[])
case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
err = gitconfig_remotes_request(&ibuf, gitconfig);
break;
+ case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
+ err = gitconfig_owner_request(&ibuf, gitconfig);
+ break;
default:
err = got_error(GOT_ERR_PRIVSEP_MSG);
break;