Commit 9a1cc63fe0022b929860dfe206daf674f83a7d9a

Stefan Sperling 2020-02-03T09:18:40

read repository owner name with gitconfig parser instead of a hand-rolled one

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;