|
1 | 1 | #include "clar_libgit2.h" |
2 | 2 | #include "posix.h" |
3 | 3 | #include "fs_path.h" |
| 4 | +#include "futils.h" |
4 | 5 | #include "git2/sys/repository.h" |
5 | 6 |
|
6 | 7 | void cl_git_report_failure( |
@@ -548,33 +549,77 @@ void clar__assert_equal_file( |
548 | 549 | (size_t)expected_bytes, (size_t)total_bytes); |
549 | 550 | } |
550 | 551 |
|
551 | | -static git_buf _cl_restore_home = GIT_BUF_INIT; |
| 552 | +#define FAKE_HOMEDIR_NAME "cl_fake_home" |
552 | 553 |
|
553 | | -void cl_fake_home_cleanup(void *payload) |
| 554 | +static git_buf _cl_restore_homedir = GIT_BUF_INIT; |
| 555 | + |
| 556 | +void cl_fake_homedir_cleanup(void *payload) |
554 | 557 | { |
555 | 558 | GIT_UNUSED(payload); |
556 | 559 |
|
557 | | - if (_cl_restore_home.ptr) { |
558 | | - cl_git_pass(git_libgit2_opts( |
559 | | - GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, _cl_restore_home.ptr)); |
560 | | - git_buf_dispose(&_cl_restore_home); |
| 560 | + if (_cl_restore_homedir.ptr) { |
| 561 | + cl_git_pass(git_futils_rmdir_r(FAKE_HOMEDIR_NAME, NULL, GIT_RMDIR_REMOVE_FILES)); |
| 562 | + |
| 563 | + cl_git_pass(git_libgit2_opts(GIT_OPT_SET_HOMEDIR, _cl_restore_homedir.ptr)); |
| 564 | + git_buf_dispose(&_cl_restore_homedir); |
561 | 565 | } |
562 | 566 | } |
563 | 567 |
|
564 | | -void cl_fake_home(void) |
| 568 | +void cl_fake_homedir(git_str *out) |
565 | 569 | { |
566 | 570 | git_str path = GIT_STR_INIT; |
567 | 571 |
|
568 | 572 | cl_git_pass(git_libgit2_opts( |
569 | | - GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &_cl_restore_home)); |
| 573 | + GIT_OPT_GET_HOMEDIR, &_cl_restore_homedir)); |
| 574 | + |
| 575 | + cl_set_cleanup(cl_fake_homedir_cleanup, NULL); |
| 576 | + |
| 577 | + /* TOC/TOU but merely attempts to prevent accidental cleanup. */ |
| 578 | + cl_assert(!git_fs_path_exists(FAKE_HOMEDIR_NAME)); |
| 579 | + cl_must_pass(p_mkdir(FAKE_HOMEDIR_NAME, 0777)); |
| 580 | + cl_git_pass(git_fs_path_prettify(&path, FAKE_HOMEDIR_NAME, NULL)); |
| 581 | + cl_git_pass(git_libgit2_opts(GIT_OPT_SET_HOMEDIR, path.ptr)); |
570 | 582 |
|
571 | | - cl_set_cleanup(cl_fake_home_cleanup, NULL); |
| 583 | + if (out) |
| 584 | + git_str_swap(out, &path); |
| 585 | + |
| 586 | + git_str_dispose(&path); |
| 587 | +} |
| 588 | + |
| 589 | +#define FAKE_GLOBALCONFIG_NAME "cl_fake_global" |
| 590 | + |
| 591 | +static git_buf _cl_restore_globalconfig = GIT_BUF_INIT; |
| 592 | + |
| 593 | +void cl_fake_globalconfig_cleanup(void *payload) |
| 594 | +{ |
| 595 | + GIT_UNUSED(payload); |
| 596 | + |
| 597 | + if (_cl_restore_globalconfig.ptr) { |
| 598 | + cl_git_pass(git_futils_rmdir_r(FAKE_GLOBALCONFIG_NAME, NULL, GIT_RMDIR_REMOVE_FILES)); |
| 599 | + |
| 600 | + cl_git_pass(git_libgit2_opts(GIT_OPT_SET_HOMEDIR, _cl_restore_globalconfig.ptr)); |
| 601 | + git_buf_dispose(&_cl_restore_globalconfig); |
| 602 | + } |
| 603 | +} |
| 604 | + |
| 605 | +void cl_fake_globalconfig(git_str *out) |
| 606 | +{ |
| 607 | + git_str path = GIT_STR_INIT; |
572 | 608 |
|
573 | | - if (!git_fs_path_exists("home")) |
574 | | - cl_must_pass(p_mkdir("home", 0777)); |
575 | | - cl_git_pass(git_fs_path_prettify(&path, "home", NULL)); |
576 | 609 | cl_git_pass(git_libgit2_opts( |
577 | | - GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); |
| 610 | + GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &_cl_restore_globalconfig)); |
| 611 | + |
| 612 | + cl_set_cleanup(cl_fake_globalconfig_cleanup, NULL); |
| 613 | + |
| 614 | + /* TOC/TOU but merely attempts to prevent accidental cleanup. */ |
| 615 | + cl_assert(!git_fs_path_exists(FAKE_GLOBALCONFIG_NAME)); |
| 616 | + cl_must_pass(p_mkdir(FAKE_GLOBALCONFIG_NAME, 0777)); |
| 617 | + cl_git_pass(git_fs_path_prettify(&path, FAKE_GLOBALCONFIG_NAME, NULL)); |
| 618 | + cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); |
| 619 | + |
| 620 | + if (out) |
| 621 | + git_str_swap(out, &path); |
| 622 | + |
578 | 623 | git_str_dispose(&path); |
579 | 624 | } |
580 | 625 |
|
|
0 commit comments