Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aafaa49

Browse files
committed
blob: fail to create a blob from a dir with EDIRECTORY
This also affects `git_index_add_bypath()` by providing a better error message and a specific error code when a directory is passed.
1 parent 7624b91 commit aafaa49

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

src/blob.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ int git_blob__create_from_paths(
185185
(error = git_repository_odb(&odb, repo)) < 0)
186186
goto done;
187187

188+
if (S_ISDIR(st.st_mode)) {
189+
giterr_set(GITERR_ODB, "cannot create blob from '%s'; it is a directory", content_path);
190+
error = GIT_EDIRECTORY;
191+
goto done;
192+
}
193+
188194
if (out_st)
189195
memcpy(out_st, &st, sizeof(st));
190196

tests/index/bypath.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "clar_libgit2.h"
2+
#include "repository.h"
3+
#include "../submodule/submodule_helpers.h"
4+
5+
static git_repository *g_repo;
6+
static git_index *g_idx;
7+
8+
void test_index_bypath__initialize(void)
9+
{
10+
g_repo = setup_fixture_submod2();
11+
cl_git_pass(git_repository_index__weakptr(&g_idx, g_repo));
12+
}
13+
14+
void test_index_bypath__cleanup(void)
15+
{
16+
g_repo = NULL;
17+
g_idx = NULL;
18+
}
19+
20+
void test_index_bypath__add_directory(void)
21+
{
22+
cl_git_fail_with(GIT_EDIRECTORY, git_index_add_bypath(g_idx, "just_a_dir"));
23+
}

tests/submodule/add.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "submodule_helpers.h"
55
#include "config/config_helpers.h"
66
#include "fileops.h"
7+
#include "repository.h"
78

89
static git_repository *g_repo = NULL;
910

0 commit comments

Comments
 (0)