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

Skip to content

Commit 1e9dd60

Browse files
committed
Test submodules with empty index or orphaned head
In both of these cases, the submodule data should still be loaded just (obviously) without the data that comes from either the index or the HEAD. This fixes a bug in the orphaned head case.
1 parent d0c36a0 commit 1e9dd60

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

src/submodule.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,8 +1176,11 @@ static int load_submodule_config_from_head(
11761176
git_iterator *i;
11771177
const git_index_entry *entry;
11781178

1179-
if ((error = git_repository_head_tree(&head, repo)) < 0)
1180-
return error;
1179+
/* if we can't look up current head, then there's no submodule in it */
1180+
if (git_repository_head_tree(&head, repo) < 0) {
1181+
giterr_clear();
1182+
return 0;
1183+
}
11811184

11821185
if ((error = git_iterator_for_tree(&i, head, 0, NULL, NULL)) < 0) {
11831186
git_tree_free(head);

tests-clar/submodule/lookup.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "clar_libgit2.h"
22
#include "submodule_helpers.h"
33
#include "posix.h"
4+
#include "git2/sys/repository.h"
45

56
static git_repository *g_repo = NULL;
67

@@ -112,3 +113,73 @@ void test_submodule_lookup__foreach(void)
112113
cl_git_pass(git_submodule_foreach(g_repo, sm_lookup_cb, &data));
113114
cl_assert_equal_i(8, data.count);
114115
}
116+
117+
void test_submodule_lookup__lookup_even_with_orphaned_head(void)
118+
{
119+
git_reference *orphan;
120+
git_submodule *sm;
121+
122+
/* orphan the head */
123+
cl_git_pass(git_reference_symbolic_create(
124+
&orphan, g_repo, "HEAD", "refs/heads/garbage", 1));
125+
git_reference_free(orphan);
126+
127+
/* lookup existing */
128+
cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged"));
129+
cl_assert(sm);
130+
131+
/* lookup pending change in .gitmodules that is not in HEAD */
132+
cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_added_and_uncommited"));
133+
cl_assert(sm);
134+
135+
/* lookup pending change in .gitmodules that is neither in HEAD nor index */
136+
cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_gitmodules_only"));
137+
cl_assert(sm);
138+
139+
/* lookup git repo subdir that is not added as submodule */
140+
cl_assert_equal_i(GIT_EEXISTS, git_submodule_lookup(&sm, g_repo, "not-submodule"));
141+
142+
/* lookup existing directory that is not a submodule */
143+
cl_assert_equal_i(GIT_ENOTFOUND, git_submodule_lookup(&sm, g_repo, "just_a_dir"));
144+
145+
/* lookup existing file that is not a submodule */
146+
cl_assert_equal_i(GIT_ENOTFOUND, git_submodule_lookup(&sm, g_repo, "just_a_file"));
147+
148+
/* lookup non-existent item */
149+
cl_assert_equal_i(GIT_ENOTFOUND, git_submodule_lookup(&sm, g_repo, "no_such_file"));
150+
}
151+
152+
void test_submodule_lookup__lookup_even_with_missing_index(void)
153+
{
154+
git_index *idx;
155+
git_submodule *sm;
156+
157+
/* give the repo an empty index */
158+
cl_git_pass(git_index_new(&idx));
159+
git_repository_set_index(g_repo, idx);
160+
git_index_free(idx);
161+
162+
/* lookup existing */
163+
cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged"));
164+
cl_assert(sm);
165+
166+
/* lookup pending change in .gitmodules that is not in HEAD */
167+
cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_added_and_uncommited"));
168+
cl_assert(sm);
169+
170+
/* lookup pending change in .gitmodules that is neither in HEAD nor index */
171+
cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_gitmodules_only"));
172+
cl_assert(sm);
173+
174+
/* lookup git repo subdir that is not added as submodule */
175+
cl_assert_equal_i(GIT_EEXISTS, git_submodule_lookup(&sm, g_repo, "not-submodule"));
176+
177+
/* lookup existing directory that is not a submodule */
178+
cl_assert_equal_i(GIT_ENOTFOUND, git_submodule_lookup(&sm, g_repo, "just_a_dir"));
179+
180+
/* lookup existing file that is not a submodule */
181+
cl_assert_equal_i(GIT_ENOTFOUND, git_submodule_lookup(&sm, g_repo, "just_a_file"));
182+
183+
/* lookup non-existent item */
184+
cl_assert_equal_i(GIT_ENOTFOUND, git_submodule_lookup(&sm, g_repo, "no_such_file"));
185+
}

0 commit comments

Comments
 (0)