use author name/email from ~/.gitconfig if GOT_AUTHOR is not set
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 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
diff --git a/got/got.1 b/got/got.1
index ba35bfd..f8920a6 100644
--- a/got/got.1
+++ b/got/got.1
@@ -97,7 +97,9 @@ and
.Dv user.email configuration settings can be
obtained from the repository's
.Pa .git/config
-file.
+file or from Git's global
+.Pa ~/.gitconfig
+configuration file.
.Pp
The options for
.Cm got import
@@ -704,7 +706,9 @@ and
.Dv user.email configuration settings can be
obtained from the repository's
.Pa .git/config
-file.
+file or from Git's global
+.Pa ~/.gitconfig
+configuration file.
.Pp
The options for
.Cm got commit
@@ -1226,13 +1230,25 @@ may fail to parse commits without an email address in author data,
attempts to reject
.Ev GOT_AUTHOR
environment variables with a missing email address.
-Git's
+.Pp
+If present, Git's
.Dv user.name
and
.Dv user.email configuration settings in the repository's
.Pa .git/config
file will override the value of
.Ev GOT_AUTHOR .
+However, the
+.Dv user.name
+and
+.Dv user.email
+configuration settings contained in Git's global
+.Pa ~/.gitconfig
+configuration file will be used only if the
+.Ev GOT_AUTHOR
+environment variable is
+.Em not
+set.
.It Ev VISUAL , EDITOR
The editor spawned by
.Cm got commit ,
diff --git a/got/got.c b/got/got.c
index 5f37798..6f3b569 100644
--- a/got/got.c
+++ b/got/got.c
@@ -495,21 +495,27 @@ static const struct got_error *
get_author(char **author, struct got_repository *repo)
{
const struct got_error *err = NULL;
- const char *got_author, *gitconfig_name, *gitconfig_email;
+ const char *got_author, *name, *email;
*author = NULL;
- gitconfig_name = got_repo_get_gitconfig_author_name(repo);
- gitconfig_email = got_repo_get_gitconfig_author_email(repo);
- if (gitconfig_name && gitconfig_email) {
- if (asprintf(author, "%s <%s>",
- gitconfig_name, gitconfig_email) == -1)
+ name = got_repo_get_gitconfig_author_name(repo);
+ email = got_repo_get_gitconfig_author_email(repo);
+ if (name && email) {
+ if (asprintf(author, "%s <%s>", name, email) == -1)
return got_error_from_errno("asprintf");
return NULL;
}
got_author = getenv("GOT_AUTHOR");
if (got_author == NULL) {
+ name = got_repo_get_global_gitconfig_author_name(repo);
+ email = got_repo_get_global_gitconfig_author_email(repo);
+ if (name && email) {
+ if (asprintf(author, "%s <%s>", name, email) == -1)
+ return got_error_from_errno("asprintf");
+ return NULL;
+ }
/* TODO: Look up user in password database? */
return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
}
@@ -547,11 +553,25 @@ done:
}
static const struct got_error *
+get_gitconfig_path(char **gitconfig_path)
+{
+ const char *homedir = getenv("HOME");
+
+ *gitconfig_path = NULL;
+ if (homedir) {
+ if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
+ return got_error_from_errno("asprintf");
+
+ }
+ return NULL;
+}
+
+static const struct got_error *
cmd_import(int argc, char *argv[])
{
const struct got_error *error = NULL;
char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
- char *editor = NULL, *author = NULL;
+ char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
const char *branch_name = "master";
char *refname = NULL, *id_str = NULL;
struct got_repository *repo = NULL;
@@ -614,7 +634,10 @@ cmd_import(int argc, char *argv[])
return got_error_from_errno("getcwd");
}
got_path_strip_trailing_slashes(repo_path);
- error = got_repo_open(&repo, repo_path);
+ error = get_gitconfig_path(&gitconfig_path);
+ if (error)
+ goto done;
+ error = got_repo_open(&repo, repo_path, gitconfig_path);
if (error)
goto done;
@@ -705,6 +728,7 @@ done:
free(new_commit_id);
free(id_str);
free(author);
+ free(gitconfig_path);
if (branch_ref)
got_ref_close(branch_ref);
if (head_ref)
@@ -986,7 +1010,7 @@ cmd_checkout(int argc, char *argv[])
got_path_strip_trailing_slashes(repo_path);
got_path_strip_trailing_slashes(worktree_path);
- error = got_repo_open(&repo, repo_path);
+ error = got_repo_open(&repo, repo_path, NULL);
if (error != NULL)
goto done;
@@ -1239,7 +1263,8 @@ cmd_update(int argc, char *argv[])
if (error)
goto done;
- error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
+ error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
+ NULL);
if (error != NULL)
goto done;
@@ -1795,7 +1820,7 @@ cmd_log(int argc, char *argv[])
goto done;
}
- error = got_repo_open(&repo, repo_path);
+ error = got_repo_open(&repo, repo_path, NULL);
if (error != NULL)
goto done;
@@ -2194,7 +2219,7 @@ cmd_diff(int argc, char *argv[])
return got_error_from_errno("getcwd");
}
- error = got_repo_open(&repo, repo_path);
+ error = got_repo_open(&repo, repo_path, NULL);
free(repo_path);
if (error != NULL)
goto done;
@@ -2491,7 +2516,7 @@ cmd_blame(int argc, char *argv[])
}
}
- error = got_repo_open(&repo, repo_path);
+ error = got_repo_open(&repo, repo_path, NULL);
if (error != NULL)
goto done;
@@ -2790,7 +2815,7 @@ cmd_tree(int argc, char *argv[])
}
}
- error = got_repo_open(&repo, repo_path);
+ error = got_repo_open(&repo, repo_path, NULL);
if (error != NULL)
goto done;
@@ -2911,7 +2936,8 @@ cmd_status(int argc, char *argv[])
if (error != NULL)
goto done;
- error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
+ error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
+ NULL);
if (error != NULL)
goto done;
@@ -3146,7 +3172,7 @@ cmd_ref(int argc, char *argv[])
}
}
- error = got_repo_open(&repo, repo_path);
+ error = got_repo_open(&repo, repo_path, NULL);
if (error != NULL)
goto done;
@@ -3386,7 +3412,7 @@ cmd_branch(int argc, char *argv[])
}
}
- error = got_repo_open(&repo, repo_path);
+ error = got_repo_open(&repo, repo_path, NULL);
if (error != NULL)
goto done;
@@ -3764,6 +3790,7 @@ cmd_tag(int argc, char *argv[])
struct got_repository *repo = NULL;
struct got_worktree *worktree = NULL;
char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
+ char *gitconfig_path = NULL;
const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
int ch, do_list = 0;
@@ -3840,17 +3867,22 @@ cmd_tag(int argc, char *argv[])
}
}
- error = got_repo_open(&repo, repo_path);
- if (error != NULL)
- goto done;
-
-
if (do_list) {
+ error = got_repo_open(&repo, repo_path, NULL);
+ if (error != NULL)
+ goto done;
error = apply_unveil(got_repo_get_path(repo), 1, NULL);
if (error)
goto done;
error = list_tags(repo, worktree);
} else {
+ error = get_gitconfig_path(&gitconfig_path);
+ if (error)
+ goto done;
+ error = got_repo_open(&repo, repo_path, gitconfig_path);
+ if (error != NULL)
+ goto done;
+
if (tagmsg) {
error = apply_unveil(got_repo_get_path(repo), 1, NULL);
if (error)
@@ -3885,6 +3917,7 @@ done:
got_worktree_close(worktree);
free(cwd);
free(repo_path);
+ free(gitconfig_path);
free(commit_id_str);
return error;
}
@@ -3938,7 +3971,8 @@ cmd_add(int argc, char *argv[])
if (error)
goto done;
- error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
+ error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
+ NULL);
if (error != NULL)
goto done;
@@ -4030,7 +4064,8 @@ cmd_remove(int argc, char *argv[])
if (error)
goto done;
- error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
+ error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
+ NULL);
if (error)
goto done;
@@ -4244,7 +4279,8 @@ cmd_revert(int argc, char *argv[])
if (error)
goto done;
- error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
+ error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
+ NULL);
if (error != NULL)
goto done;
@@ -4406,7 +4442,7 @@ cmd_commit(int argc, char *argv[])
struct got_object_id *id = NULL;
const char *logmsg = NULL;
struct collect_commit_logmsg_arg cl_arg;
- char *editor = NULL, *author = NULL;
+ char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
int ch, rebase_in_progress, histedit_in_progress;
struct got_pathlist_head paths;
@@ -4454,7 +4490,11 @@ cmd_commit(int argc, char *argv[])
if (error)
goto done;
- error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
+ error = get_gitconfig_path(&gitconfig_path);
+ if (error)
+ goto done;
+ error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
+ gitconfig_path);
if (error != NULL)
goto done;
@@ -4514,6 +4554,7 @@ done:
got_worktree_close(worktree);
free(cwd);
free(id_str);
+ free(gitconfig_path);
free(editor);
free(author);
return error;
@@ -4567,7 +4608,8 @@ cmd_cherrypick(int argc, char *argv[])
if (error)
goto done;
- error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
+ error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
+ NULL);
if (error != NULL)
goto done;
@@ -4682,7 +4724,8 @@ cmd_backout(int argc, char *argv[])
if (error)
goto done;
- error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
+ error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
+ NULL);
if (error != NULL)
goto done;
@@ -5073,7 +5116,8 @@ cmd_rebase(int argc, char *argv[])
if (error)
goto done;
- error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
+ error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
+ NULL);
if (error != NULL)
goto done;
@@ -6067,7 +6111,8 @@ cmd_histedit(int argc, char *argv[])
if (error)
goto done;
- error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
+ error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
+ NULL);
if (error != NULL)
goto done;
@@ -6425,7 +6470,8 @@ cmd_stage(int argc, char *argv[])
if (error)
goto done;
- error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
+ error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
+ NULL);
if (error != NULL)
goto done;
@@ -6532,7 +6578,8 @@ cmd_unstage(int argc, char *argv[])
if (error)
goto done;
- error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
+ error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
+ NULL);
if (error != NULL)
goto done;
@@ -6793,7 +6840,7 @@ cmd_cat(int argc, char *argv[])
return got_error_from_errno("getcwd");
}
- error = got_repo_open(&repo, repo_path);
+ error = got_repo_open(&repo, repo_path, NULL);
free(repo_path);
if (error != NULL)
goto done;
diff --git a/include/got_repository.h b/include/got_repository.h
index f2ecc74..7ed477b 100644
--- a/include/got_repository.h
+++ b/include/got_repository.h
@@ -19,7 +19,8 @@ struct got_pathlist_head;
struct got_tag_object;
/* Open and close repositories. */
-const struct got_error *got_repo_open(struct got_repository**, const char *);
+const struct got_error *got_repo_open(struct got_repository**, const char *,
+ const char *);
const struct got_error *got_repo_close(struct got_repository*);
/* Obtain the on-disk path to the repository. */
@@ -37,6 +38,12 @@ const char *got_repo_get_gitconfig_author_name(struct got_repository *);
/* Obtain the commit author email if parsed from gitconfig, else NULL. */
const char *got_repo_get_gitconfig_author_email(struct got_repository *);
+/* Obtain global commit author name parsed ~/.gitconfig, else NULL. */
+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 paths to various directories within a repository.
* The caller must dispose of a path with free(3).
diff --git a/lib/got_lib_repository.h b/lib/got_lib_repository.h
index 71d6c05..c304faf 100644
--- a/lib/got_lib_repository.h
+++ b/lib/got_lib_repository.h
@@ -45,6 +45,8 @@ struct got_repository {
int gitconfig_repository_format_version;
char *gitconfig_author_name;
char *gitconfig_author_email;
+ char *global_gitconfig_author_name;
+ char *global_gitconfig_author_email;
};
const struct got_error*got_repo_cache_object(struct got_repository *,
diff --git a/lib/repository.c b/lib/repository.c
index 454319a..2882ec4 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -94,13 +94,24 @@ got_repo_get_gitconfig_author_name(struct got_repository *repo)
return repo->gitconfig_author_name;
}
-/* Obtain the commit author email address parsed from gitconfig. */
const char *
got_repo_get_gitconfig_author_email(struct got_repository *repo)
{
return repo->gitconfig_author_email;
}
+const char *
+got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
+{
+ return repo->global_gitconfig_author_name;
+}
+
+const char *
+got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
+{
+ return repo->global_gitconfig_author_email;
+}
+
int
got_repo_is_bare(struct got_repository *repo)
{
@@ -351,31 +362,25 @@ done:
}
static const struct got_error *
-read_gitconfig(struct got_repository *repo)
+parse_gitconfig_file(int *gitconfig_repository_format_version,
+ char **gitconfig_author_name, char **gitconfig_author_email,
+ const char *gitconfig_path)
{
const struct got_error *err = NULL, *child_err = NULL;
- char *gitconfig_path = NULL;
int fd = -1;
int imsg_fds[2] = { -1, -1 };
pid_t pid;
struct imsgbuf *ibuf;
- /* TODO: Read settings from ~/.gitconfig as well? */
-
- /* Read repository's .git/config file. */
- err = get_path_gitconfig(&gitconfig_path, repo);
- if (err)
- return err;
+ *gitconfig_repository_format_version = 0;
+ *gitconfig_author_name = NULL;
+ *gitconfig_author_email = NULL;
fd = open(gitconfig_path, O_RDONLY);
if (fd == -1) {
- if (errno == ENOENT) {
- free(gitconfig_path);
+ if (errno == ENOENT)
return NULL;
- }
- err = got_error_from_errno2("open", gitconfig_path);
- free(gitconfig_path);
- return err;
+ return got_error_from_errno2("open", gitconfig_path);
}
ibuf = calloc(1, sizeof(*ibuf));
@@ -395,7 +400,7 @@ read_gitconfig(struct got_repository *repo)
goto done;
} else if (pid == 0) {
got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
- repo->path);
+ gitconfig_path);
/* not reached */
}
@@ -416,7 +421,7 @@ read_gitconfig(struct got_repository *repo)
goto done;
err = got_privsep_recv_gitconfig_int(
- &repo->gitconfig_repository_format_version, ibuf);
+ gitconfig_repository_format_version, ibuf);
if (err)
goto done;
@@ -424,8 +429,7 @@ read_gitconfig(struct got_repository *repo)
if (err)
goto done;
- err = got_privsep_recv_gitconfig_str(&repo->gitconfig_author_name,
- ibuf);
+ err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
if (err)
goto done;
@@ -433,8 +437,7 @@ read_gitconfig(struct got_repository *repo)
if (err)
goto done;
- err = got_privsep_recv_gitconfig_str(&repo->gitconfig_author_email,
- ibuf);
+ err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
if (err)
goto done;
@@ -450,13 +453,45 @@ done:
err = got_error_from_errno("close");
if (fd != -1 && close(fd) == -1 && err == NULL)
err = got_error_from_errno2("close", gitconfig_path);
- free(gitconfig_path);
free(ibuf);
return err;
}
+static const struct got_error *
+read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
+{
+ const struct got_error *err = NULL;
+ char *repo_gitconfig_path = NULL;
+
+ if (global_gitconfig_path) {
+ /* Read settings from ~/.gitconfig. */
+ int dummy_repo_version;
+ err = parse_gitconfig_file(&dummy_repo_version,
+ &repo->global_gitconfig_author_name,
+ &repo->global_gitconfig_author_email,
+ global_gitconfig_path);
+ if (err)
+ return err;
+ }
+
+ /* Read repository's .git/config file. */
+ err = get_path_gitconfig(&repo_gitconfig_path, repo);
+ if (err)
+ return err;
+
+ err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
+ &repo->gitconfig_author_name, &repo->gitconfig_author_email,
+ repo_gitconfig_path);
+ if (err)
+ goto done;
+done:
+ free(repo_gitconfig_path);
+ return err;
+}
+
const struct got_error *
-got_repo_open(struct got_repository **repop, const char *path)
+got_repo_open(struct got_repository **repop, const char *path,
+ const char *global_gitconfig_path)
{
struct got_repository *repo = NULL;
const struct got_error *err = NULL;
@@ -527,7 +562,7 @@ got_repo_open(struct got_repository **repop, const char *path)
}
} while (path);
- err = read_gitconfig(repo);
+ err = read_gitconfig(repo, global_gitconfig_path);
if (err)
goto done;
if (repo->gitconfig_repository_format_version != 0)
diff --git a/lib/worktree.c b/lib/worktree.c
index 789f858..9ccc0f4 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -401,7 +401,7 @@ open_worktree(struct got_worktree **worktree, const char *path)
goto done;
}
- err = got_repo_open(&repo, (*worktree)->repo_path);
+ err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
if (err)
goto done;
diff --git a/tog/tog.c b/tog/tog.c
index 287db60..a0deace 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -1905,7 +1905,7 @@ open_log_view(struct tog_view *view, struct got_object_id *start_id,
view->search_start = search_start_log_view;
view->search_next = search_next_log_view;
- err = got_repo_open(&thread_repo, got_repo_get_path(repo));
+ err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
if (err)
goto done;
err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
@@ -2278,7 +2278,7 @@ cmd_log(int argc, char *argv[])
init_curses();
- error = got_repo_open(&repo, repo_path);
+ error = got_repo_open(&repo, repo_path, NULL);
if (error != NULL)
goto done;
@@ -2924,7 +2924,7 @@ cmd_diff(int argc, char *argv[])
init_curses();
- error = got_repo_open(&repo, repo_path);
+ error = got_repo_open(&repo, repo_path, NULL);
if (error)
goto done;
@@ -3307,7 +3307,7 @@ run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
goto done;
}
- err = got_repo_open(&thread_repo, got_repo_get_path(repo));
+ err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
if (err)
goto done;
@@ -3800,7 +3800,7 @@ cmd_blame(int argc, char *argv[])
init_curses();
- error = got_repo_open(&repo, repo_path);
+ error = got_repo_open(&repo, repo_path, NULL);
if (error != NULL)
goto done;
@@ -4579,7 +4579,7 @@ cmd_tree(int argc, char *argv[])
init_curses();
- error = got_repo_open(&repo, repo_path);
+ error = got_repo_open(&repo, repo_path, NULL);
if (error != NULL)
goto done;