repository: make git_repository_set_workdir() prettify the path it is being passed
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
diff --git a/src/repository.c b/src/repository.c
index 4e0f9d4..ce31328 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -871,13 +871,16 @@ const char *git_repository_workdir(git_repository *repo)
int git_repository_set_workdir(git_repository *repo, const char *workdir)
{
+ git_buf path = GIT_BUF_INIT;
+
assert(repo && workdir);
- free(repo->workdir);
+ if (git_path_prettify_dir(&path, workdir, NULL) < 0)
+ return -1;
- repo->workdir = git__strdup(workdir);
- GITERR_CHECK_ALLOC(repo->workdir);
+ free(repo->workdir);
+ repo->workdir = git_buf_detach(&path);
repo->is_bare = 0;
return 0;
}
diff --git a/tests-clar/repo/setters.c b/tests-clar/repo/setters.c
new file mode 100644
index 0000000..7a65a40
--- /dev/null
+++ b/tests-clar/repo/setters.c
@@ -0,0 +1,34 @@
+#include "clar_libgit2.h"
+#include "buffer.h"
+
+static git_repository *repo;
+
+void test_repo_setters__initialize(void)
+{
+ cl_fixture_sandbox("testrepo.git");
+ cl_git_pass(git_repository_open(&repo, "testrepo.git"));
+}
+
+void test_repo_setters__cleanup(void)
+{
+ git_repository_free(repo);
+ cl_fixture_cleanup("testrepo.git");
+}
+
+void test_repo_setters__setting_a_workdir_turns_a_bare_repository_into_a_standard_one(void)
+{
+ cl_assert(git_repository_is_bare(repo) == 1);
+
+ cl_assert(git_repository_workdir(repo) == NULL);
+ cl_git_pass(git_repository_set_workdir(repo, "./new_workdir"));
+
+ cl_assert(git_repository_workdir(repo) != NULL);
+ cl_assert(git_repository_is_bare(repo) == 0);
+}
+
+void test_repo_setters__setting_a_workdir_prettifies_its_path(void)
+{
+ cl_git_pass(git_repository_set_workdir(repo, "./new_workdir"));
+
+ cl_assert(git__suffixcmp(git_repository_workdir(repo), "/") == 0);
+}