Merge pull request #4263 from libgit2/ethomson/config_for_inmemory_repo Allow creation of a configuration object in an in-memory repository
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
diff --git a/include/git2/repository.h b/include/git2/repository.h
index a396a54..8aac0b3 100644
--- a/include/git2/repository.h
+++ b/include/git2/repository.h
@@ -433,12 +433,12 @@ typedef enum {
* item. It will thereby honor things like the repository's
* common directory, gitdir, etc. In case a file path cannot
* exist for a given item (e.g. the working directory of a bare
- * repository), an error is returned.
+ * repository), GIT_ENOTFOUND is returned.
*
* @param out Buffer to store the path at
* @param repo Repository to get path for
* @param item The repository item for which to retrieve the path
- * @return 0 on success, otherwise a negative value
+ * @return 0, GIT_ENOTFOUND if the path cannot exist or an error code
*/
GIT_EXTERN(int) git_repository_item_path(git_buf *out, git_repository *repo, git_repository_item_t item);
diff --git a/src/repository.c b/src/repository.c
index c7b40fd..7ecb00e 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -943,13 +943,10 @@ static int load_config(
if ((error = git_config_new(&cfg)) < 0)
return error;
- error = git_repository_item_path(&config_path, repo, GIT_REPOSITORY_ITEM_CONFIG);
- if (error < 0)
- goto on_error;
+ if ((error = git_repository_item_path(&config_path, repo, GIT_REPOSITORY_ITEM_CONFIG)) == 0)
+ error = git_config_add_file_ondisk(cfg, config_path.ptr, GIT_CONFIG_LEVEL_LOCAL, 0);
- if ((error = git_config_add_file_ondisk(
- cfg, config_path.ptr, GIT_CONFIG_LEVEL_LOCAL, 0)) < 0 &&
- error != GIT_ENOTFOUND)
+ if (error && error != GIT_ENOTFOUND)
goto on_error;
git_buf_free(&config_path);
@@ -2266,13 +2263,13 @@ int git_repository_item_path(git_buf *out, git_repository *repo, git_repository_
parent = git_repository_commondir(repo);
break;
default:
- giterr_set(GITERR_INVALID, "Invalid item directory");
+ giterr_set(GITERR_INVALID, "invalid item directory");
return -1;
}
if (parent == NULL) {
- giterr_set(GITERR_INVALID, "Path cannot exist in repository");
- return -1;
+ giterr_set(GITERR_INVALID, "path cannot exist in repository");
+ return GIT_ENOTFOUND;
}
if (git_buf_sets(out, parent) < 0)
diff --git a/tests/network/remote/local.c b/tests/network/remote/local.c
index 6194802..7bae038 100644
--- a/tests/network/remote/local.c
+++ b/tests/network/remote/local.c
@@ -465,3 +465,19 @@ void test_network_remote_local__push_delete(void)
cl_fixture_cleanup("target.git");
cl_git_sandbox_cleanup();
}
+
+void test_network_remote_local__anonymous_remote_inmemory_repo(void)
+{
+ git_repository *inmemory;
+ git_remote *remote;
+
+ git_buf_sets(&file_path_buf, cl_git_path_url(cl_fixture("testrepo.git")));
+
+ cl_git_pass(git_repository_new(&inmemory));
+ cl_git_pass(git_remote_create_anonymous(&remote, inmemory, git_buf_cstr(&file_path_buf)));
+ cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
+ cl_assert(git_remote_connected(remote));
+ git_remote_disconnect(remote);
+ git_remote_free(remote);
+ git_repository_free(inmemory);
+}