Skip to content

Commit 99e40a6

Browse files
authored
Merge pull request libgit2#4263 from libgit2/ethomson/config_for_inmemory_repo
Allow creation of a configuration object in an in-memory repository
2 parents d9914fb + fe9a5dd commit 99e40a6

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

include/git2/repository.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,12 +433,12 @@ typedef enum {
433433
* item. It will thereby honor things like the repository's
434434
* common directory, gitdir, etc. In case a file path cannot
435435
* exist for a given item (e.g. the working directory of a bare
436-
* repository), an error is returned.
436+
* repository), GIT_ENOTFOUND is returned.
437437
*
438438
* @param out Buffer to store the path at
439439
* @param repo Repository to get path for
440440
* @param item The repository item for which to retrieve the path
441-
* @return 0 on success, otherwise a negative value
441+
* @return 0, GIT_ENOTFOUND if the path cannot exist or an error code
442442
*/
443443
GIT_EXTERN(int) git_repository_item_path(git_buf *out, git_repository *repo, git_repository_item_t item);
444444

src/repository.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -943,13 +943,10 @@ static int load_config(
943943
if ((error = git_config_new(&cfg)) < 0)
944944
return error;
945945

946-
error = git_repository_item_path(&config_path, repo, GIT_REPOSITORY_ITEM_CONFIG);
947-
if (error < 0)
948-
goto on_error;
946+
if ((error = git_repository_item_path(&config_path, repo, GIT_REPOSITORY_ITEM_CONFIG)) == 0)
947+
error = git_config_add_file_ondisk(cfg, config_path.ptr, GIT_CONFIG_LEVEL_LOCAL, 0);
949948

950-
if ((error = git_config_add_file_ondisk(
951-
cfg, config_path.ptr, GIT_CONFIG_LEVEL_LOCAL, 0)) < 0 &&
952-
error != GIT_ENOTFOUND)
949+
if (error && error != GIT_ENOTFOUND)
953950
goto on_error;
954951

955952
git_buf_free(&config_path);
@@ -2266,13 +2263,13 @@ int git_repository_item_path(git_buf *out, git_repository *repo, git_repository_
22662263
parent = git_repository_commondir(repo);
22672264
break;
22682265
default:
2269-
giterr_set(GITERR_INVALID, "Invalid item directory");
2266+
giterr_set(GITERR_INVALID, "invalid item directory");
22702267
return -1;
22712268
}
22722269

22732270
if (parent == NULL) {
2274-
giterr_set(GITERR_INVALID, "Path cannot exist in repository");
2275-
return -1;
2271+
giterr_set(GITERR_INVALID, "path cannot exist in repository");
2272+
return GIT_ENOTFOUND;
22762273
}
22772274

22782275
if (git_buf_sets(out, parent) < 0)

tests/network/remote/local.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,19 @@ void test_network_remote_local__push_delete(void)
465465
cl_fixture_cleanup("target.git");
466466
cl_git_sandbox_cleanup();
467467
}
468+
469+
void test_network_remote_local__anonymous_remote_inmemory_repo(void)
470+
{
471+
git_repository *inmemory;
472+
git_remote *remote;
473+
474+
git_buf_sets(&file_path_buf, cl_git_path_url(cl_fixture("testrepo.git")));
475+
476+
cl_git_pass(git_repository_new(&inmemory));
477+
cl_git_pass(git_remote_create_anonymous(&remote, inmemory, git_buf_cstr(&file_path_buf)));
478+
cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
479+
cl_assert(git_remote_connected(remote));
480+
git_remote_disconnect(remote);
481+
git_remote_free(remote);
482+
git_repository_free(inmemory);
483+
}

0 commit comments

Comments
 (0)