parse remotes from gitconfig
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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
diff --git a/include/got_error.h b/include/got_error.h
index e2e96e4..d0df125 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -127,6 +127,7 @@
#define GOT_ERR_REBASE_REQUIRED 111
#define GOT_ERR_REGEX 112
#define GOT_ERR_REF_NAME_MINUS 113
+#define GOT_ERR_GITCONFIG_SYNTAX 114
static const struct got_error {
int code;
@@ -260,6 +261,7 @@ static const struct got_error {
{ GOT_ERR_REBASE_REQUIRED,"specified branch must be rebased first" },
{ GOT_ERR_REGEX, "regular expression error" },
{ GOT_ERR_REF_NAME_MINUS, "reference name may not start with '-'" },
+ { GOT_ERR_GITCONFIG_SYNTAX, "gitconfig syntax error" },
};
/*
diff --git a/include/got_repository.h b/include/got_repository.h
index 7ed477b..6ef2a57 100644
--- a/include/got_repository.h
+++ b/include/got_repository.h
@@ -44,6 +44,16 @@ 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 *);
+/* Information about one remote repository. */
+struct got_remote_repo {
+ char *name;
+ char *url;
+};
+
+/* Obtain the list of remote repositories parsed from gitconfig. */
+void got_repo_get_gitconfig_remotes(int *, struct got_remote_repo **,
+ struct got_repository *);
+
/*
* Obtain paths to various directories within a repository.
* The caller must dispose of a path with free(3).
diff --git a/lib/gitconfig.c b/lib/gitconfig.c
index 1ddfb97..5188ecb 100644
--- a/lib/gitconfig.c
+++ b/lib/gitconfig.c
@@ -494,6 +494,51 @@ got_gitconfig_get_str(struct got_gitconfig *conf, char *section, char *tag)
return 0;
}
+const struct got_error *
+got_gitconfig_get_section_list(struct got_gitconfig_list **sections,
+ struct got_gitconfig *conf)
+{
+ const struct got_error *err = NULL;
+ struct got_gitconfig_list *list = NULL;
+ struct got_gitconfig_list_node *node = 0;
+ struct got_gitconfig_binding *cb;
+ int i;
+
+ *sections = NULL;
+
+ list = malloc(sizeof *list);
+ if (!list)
+ return got_error_from_errno("malloc");
+ TAILQ_INIT(&list->fields);
+ list->cnt = 0;
+ for (i = 0; i < nitems(conf->bindings); i++) {
+ for (cb = LIST_FIRST(&conf->bindings[i]); cb;
+ cb = LIST_NEXT(cb, link)) {
+ list->cnt++;
+ node = calloc(1, sizeof *node);
+ if (!node) {
+ err = got_error_from_errno("calloc");
+ goto cleanup;
+ }
+ node->field = strdup(cb->section);
+ if (!node->field) {
+ err = got_error_from_errno("strdup");
+ goto cleanup;
+ }
+ TAILQ_INSERT_TAIL(&list->fields, node, link);
+ }
+ }
+
+ *sections = list;
+ return NULL;
+
+cleanup:
+ free(node);
+ if (list)
+ got_gitconfig_free_list(list);
+ return err;
+}
+
/*
* Build a list of string values out of the comma separated value denoted by
* TAG in SECTION.
diff --git a/lib/got_lib_gitconfig.h b/lib/got_lib_gitconfig.h
index 4d61ab3..615d269 100644
--- a/lib/got_lib_gitconfig.h
+++ b/lib/got_lib_gitconfig.h
@@ -39,6 +39,8 @@ struct got_gitconfig_list {
struct got_gitconfig;
void got_gitconfig_free_list(struct got_gitconfig_list *);
+const struct got_error *got_gitconfig_get_section_list(
+ struct got_gitconfig_list **, struct got_gitconfig *);
struct got_gitconfig_list *got_gitconfig_get_list(struct got_gitconfig *,
char *, char *);
struct got_gitconfig_list *got_gitconfig_get_tag_list(struct got_gitconfig *, char *);
diff --git a/lib/got_lib_privsep.h b/lib/got_lib_privsep.h
index a37a450..b27325d 100644
--- a/lib/got_lib_privsep.h
+++ b/lib/got_lib_privsep.h
@@ -111,8 +111,11 @@ enum got_imsg_type {
GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST,
GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST,
GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST,
+ GOT_IMSG_GITCONFIG_REMOTES_REQUEST,
GOT_IMSG_GITCONFIG_INT_VAL,
GOT_IMSG_GITCONFIG_STR_VAL,
+ GOT_IMSG_GITCONFIG_REMOTES,
+ GOT_IMSG_GITCONFIG_REMOTE,
};
/* Structure for GOT_IMSG_ERROR. */
@@ -229,6 +232,24 @@ struct got_imsg_packed_object {
int idx;
} __attribute__((__packed__));
+/*
+ * Structure for GOT_IMSG_GITCONFIG_REMOTE data.
+ */
+struct got_imsg_remote {
+ size_t name_len;
+ size_t url_len;
+
+ /* Followed by name_len + url_len data bytes. */
+};
+
+/*
+ * Structure for GOT_IMSG_GITCONFIG_REMOTES data.
+ */
+struct got_imsg_remotes {
+ int nremotes; /* This many GOT_IMSG_GITCONFIG_REMOTE messages follow. */
+};
+
+struct got_remote_repo;
struct got_pack;
struct got_packidx;
struct got_pathlist_head;
@@ -285,11 +306,17 @@ const struct got_error *got_privsep_send_gitconfig_author_name_req(
struct imsgbuf *);
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_str(struct imsgbuf *,
const char *);
const struct got_error *got_privsep_recv_gitconfig_str(char **,
struct imsgbuf *);
const struct got_error *got_privsep_send_gitconfig_int(struct imsgbuf *, int);
const struct got_error *got_privsep_recv_gitconfig_int(int *, struct imsgbuf *);
+const struct got_error *got_privsep_send_gitconfig_remotes(struct imsgbuf *,
+ struct got_remote_repo *, int);
+const struct got_error *got_privsep_recv_gitconfig_remotes(
+ struct got_remote_repo **, int *, struct imsgbuf *);
void got_privsep_exec_child(int[2], const char *, const char *);
diff --git a/lib/got_lib_repository.h b/lib/got_lib_repository.h
index c304faf..93f1734 100644
--- a/lib/got_lib_repository.h
+++ b/lib/got_lib_repository.h
@@ -47,6 +47,8 @@ struct got_repository {
char *gitconfig_author_email;
char *global_gitconfig_author_name;
char *global_gitconfig_author_email;
+ int ngitconfig_remotes;
+ struct got_remote_repo *gitconfig_remotes;
};
const struct got_error*got_repo_cache_object(struct got_repository *,
diff --git a/lib/privsep.c b/lib/privsep.c
index 58bdc01..f31cafb 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -34,6 +34,7 @@
#include "got_object.h"
#include "got_error.h"
#include "got_path.h"
+#include "got_repository.h"
#include "got_lib_sha1.h"
#include "got_lib_delta.h"
@@ -1279,6 +1280,17 @@ got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
}
const struct got_error *
+got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
+{
+ if (imsg_compose(ibuf,
+ GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
+ return got_error_from_errno("imsg_compose "
+ "GITCONFIG_REMOTE_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;
@@ -1370,6 +1382,170 @@ got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
}
const struct got_error *
+got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
+ struct got_remote_repo *remotes, int nremotes)
+{
+ const struct got_error *err = NULL;
+ struct got_imsg_remotes iremotes;
+ int i;
+
+ iremotes.nremotes = nremotes;
+ if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
+ &iremotes, sizeof(iremotes)) == -1)
+ return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
+
+ err = flush_imsg(ibuf);
+ imsg_clear(ibuf);
+ if (err)
+ return err;
+
+ for (i = 0; i < nremotes; i++) {
+ struct got_imsg_remote iremote;
+ size_t len = sizeof(iremote);
+ struct ibuf *wbuf;
+
+ iremote.name_len = strlen(remotes[i].name);
+ len += iremote.name_len;
+ iremote.url_len = strlen(remotes[i].url);
+ len += iremote.url_len;
+
+ wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
+ if (wbuf == NULL)
+ return got_error_from_errno(
+ "imsg_create GITCONFIG_REMOTE");
+
+ if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
+ err = got_error_from_errno(
+ "imsg_add GIITCONFIG_REMOTE");
+ ibuf_free(wbuf);
+ return err;
+ }
+
+ if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
+ err = got_error_from_errno(
+ "imsg_add GIITCONFIG_REMOTE");
+ ibuf_free(wbuf);
+ return err;
+ }
+ if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
+ err = got_error_from_errno(
+ "imsg_add GIITCONFIG_REMOTE");
+ ibuf_free(wbuf);
+ return err;
+ }
+
+ wbuf->fd = -1;
+ imsg_close(ibuf, wbuf);
+ err = flush_imsg(ibuf);
+ if (err)
+ return err;
+ }
+
+ return NULL;
+}
+
+const struct got_error *
+got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
+ int *nremotes, struct imsgbuf *ibuf)
+{
+ const struct got_error *err = NULL;
+ struct imsg imsg;
+ size_t datalen;
+ struct got_imsg_remotes iremotes;
+ struct got_imsg_remote iremote;
+
+ *remotes = NULL;
+ *nremotes = 0;
+
+ err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
+ if (err)
+ return err;
+ datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
+
+ switch (imsg.hdr.type) {
+ case GOT_IMSG_GITCONFIG_REMOTES:
+ if (datalen != sizeof(iremotes)) {
+ err = got_error(GOT_ERR_PRIVSEP_LEN);
+ break;
+ }
+ memcpy(&iremotes, imsg.data, sizeof(iremotes));
+ if (iremotes.nremotes == 0) {
+ imsg_free(&imsg);
+ return NULL;
+ }
+ break;
+ default:
+ err = got_error(GOT_ERR_PRIVSEP_MSG);
+ break;
+ }
+
+ imsg_free(&imsg);
+
+ *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
+ if (*remotes == NULL)
+ return got_error_from_errno("recallocarray");
+
+ while (*nremotes < iremotes.nremotes) {
+ struct got_remote_repo *remote;
+
+ err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
+ if (err)
+ break;
+ datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
+
+ switch (imsg.hdr.type) {
+ case GOT_IMSG_GITCONFIG_REMOTE:
+ remote = &(*remotes)[*nremotes];
+ if (datalen < sizeof(iremote)) {
+ err = got_error(GOT_ERR_PRIVSEP_LEN);
+ break;
+ }
+ memcpy(&iremote, imsg.data, sizeof(iremote));
+ if (iremote.name_len == 0 || iremote.url_len == 0 ||
+ (sizeof(iremote) + iremote.name_len +
+ iremote.url_len) > datalen) {
+ err = got_error(GOT_ERR_PRIVSEP_LEN);
+ break;
+ }
+ remote->name = strndup(imsg.data + sizeof(iremote),
+ iremote.name_len);
+ if (remote->name == NULL) {
+ err = got_error_from_errno("strndup");
+ break;
+ }
+ remote->url = strndup(imsg.data + sizeof(iremote) +
+ iremote.name_len, iremote.url_len);
+ if (remote->url == NULL) {
+ err = got_error_from_errno("strndup");
+ free(remote->name);
+ break;
+ }
+ (*nremotes)++;
+ break;
+ default:
+ err = got_error(GOT_ERR_PRIVSEP_MSG);
+ break;
+ }
+
+ imsg_free(&imsg);
+ if (err)
+ break;
+ }
+
+ if (err) {
+ int i;
+ for (i = 0; i < *nremotes; i++) {
+ free((*remotes)[i].name);
+ free((*remotes)[i].url);
+ }
+ free(*remotes);
+ *remotes = NULL;
+ *nremotes = 0;
+ }
+ return err;
+}
+
+const struct got_error *
got_privsep_unveil_exec_helpers(void)
{
const char *helpers[] = {
diff --git a/lib/repository.c b/lib/repository.c
index a5b0f8f..e702255 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -170,6 +170,14 @@ get_path_gitconfig(char **p, struct got_repository *repo)
return NULL;
}
+void
+got_repo_get_gitconfig_remotes(int *nremotes, struct got_remote_repo **remotes,
+ struct got_repository *repo)
+{
+ *nremotes = repo->ngitconfig_remotes;
+ *remotes = repo->gitconfig_remotes;
+}
+
static int
is_git_repo(struct got_repository *repo)
{
@@ -365,6 +373,7 @@ done:
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,
const char *gitconfig_path)
{
const struct got_error *err = NULL, *child_err = NULL;
@@ -442,6 +451,17 @@ parse_gitconfig_file(int *gitconfig_repository_format_version,
if (err)
goto done;
+ if (remotes && nremotes) {
+ err = got_privsep_send_gitconfig_remotes_req(ibuf);
+ if (err)
+ goto done;
+
+ err = got_privsep_recv_gitconfig_remotes(remotes,
+ nremotes, ibuf);
+ if (err)
+ goto done;
+ }
+
imsg_clear(ibuf);
err = got_privsep_send_stop(imsg_fds[0]);
child_err = got_privsep_wait_for_child(pid);
@@ -470,7 +490,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,
- global_gitconfig_path);
+ NULL, NULL, global_gitconfig_path);
if (err)
return err;
}
@@ -482,6 +502,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);
if (err)
goto done;
@@ -620,6 +641,11 @@ got_repo_close(struct got_repository *repo)
free(repo->gitconfig_author_name);
free(repo->gitconfig_author_email);
+ for (i = 0; i < repo->ngitconfig_remotes; i++) {
+ free(repo->gitconfig_remotes[i].name);
+ free(repo->gitconfig_remotes[i].url);
+ }
+ free(repo->gitconfig_remotes);
free(repo);
return err;
diff --git a/libexec/got-read-gitconfig/got-read-gitconfig.c b/libexec/got-read-gitconfig/got-read-gitconfig.c
index c14f37d..7c6439d 100644
--- a/libexec/got-read-gitconfig/got-read-gitconfig.c
+++ b/libexec/got-read-gitconfig/got-read-gitconfig.c
@@ -32,6 +32,7 @@
#include "got_error.h"
#include "got_object.h"
+#include "got_repository.h"
#include "got_lib_delta.h"
#include "got_lib_object.h"
@@ -72,6 +73,75 @@ gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
return got_privsep_send_gitconfig_str(ibuf, value);
}
+static const struct got_error *
+gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
+{
+ const struct got_error *err = NULL;
+ struct got_gitconfig_list *sections;
+ struct got_gitconfig_list_node *node;
+ struct got_remote_repo *remotes = NULL;
+ int nremotes = 0, i;
+
+ if (gitconfig == NULL)
+ return got_error(GOT_ERR_PRIVSEP_MSG);
+
+ err = got_gitconfig_get_section_list(§ions, gitconfig);
+ if (err)
+ return err;
+
+ TAILQ_FOREACH(node, §ions->fields, link) {
+ if (strncasecmp("remote \"", node->field, 8) != 0)
+ continue;
+ nremotes++;
+ }
+
+ if (nremotes == 0) {
+ err = got_privsep_send_gitconfig_remotes(ibuf, NULL, 0);
+ goto done;
+ }
+
+ remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
+ if (remotes == NULL) {
+ err = got_error_from_errno("recallocarray");
+ goto done;
+ }
+
+ i = 0;
+ TAILQ_FOREACH(node, §ions->fields, link) {
+ char *name, *end;
+
+ if (strncasecmp("remote \"", node->field, 8) != 0)
+ continue;
+
+ name = strdup(node->field + 8);
+ if (name == NULL) {
+ err = got_error_from_errno("strdup");
+ goto done;
+ }
+ end = strrchr(name, '"');
+ if (end)
+ *end = '\0';
+ remotes[i].name = name;
+
+ remotes[i].url = got_gitconfig_get_str(gitconfig,
+ node->field, "url");
+ if (remotes[i].url == NULL) {
+ err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
+ goto done;
+ }
+
+ i++;
+ }
+
+ err = got_privsep_send_gitconfig_remotes(ibuf, remotes, nremotes);
+done:
+ for (i = 0; i < nremotes; i++)
+ free(remotes[i].name);
+ free(remotes);
+ got_gitconfig_free_list(sections);
+ return err;
+}
+
int
main(int argc, char *argv[])
{
@@ -147,6 +217,9 @@ main(int argc, char *argv[])
err = gitconfig_str_request(&ibuf, gitconfig, "user",
"email");
break;
+ case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
+ err = gitconfig_remotes_request(&ibuf, gitconfig);
+ break;
default:
err = got_error(GOT_ERR_PRIVSEP_MSG);
break;