Skip to content

Commit 85eb2cb

Browse files
authored
Merge pull request libgit2#4727 from libgit2/cmn/null-oid-existing-tree
tree: accept null ids in existing trees when updating
2 parents 50186ce + f00db9e commit 85eb2cb

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/tree.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -460,15 +460,16 @@ static int append_entry(
460460
git_treebuilder *bld,
461461
const char *filename,
462462
const git_oid *id,
463-
git_filemode_t filemode)
463+
git_filemode_t filemode,
464+
bool validate)
464465
{
465466
git_tree_entry *entry;
466467
int error = 0;
467468

468-
if (!valid_entry_name(bld->repo, filename))
469+
if (validate && !valid_entry_name(bld->repo, filename))
469470
return tree_error("failed to insert entry: invalid name for a tree entry", filename);
470471

471-
if (git_oid_iszero(id))
472+
if (validate && git_oid_iszero(id))
472473
return tree_error("failed to insert entry: invalid null OID for a tree entry", filename);
473474

474475
entry = alloc_entry(filename, strlen(filename), id);
@@ -566,12 +567,12 @@ static int write_tree(
566567
last_comp = subdir;
567568
}
568569

569-
error = append_entry(bld, last_comp, &sub_oid, S_IFDIR);
570+
error = append_entry(bld, last_comp, &sub_oid, S_IFDIR, true);
570571
git__free(subdir);
571572
if (error < 0)
572573
goto on_error;
573574
} else {
574-
error = append_entry(bld, filename, &entry->id, entry->mode);
575+
error = append_entry(bld, filename, &entry->id, entry->mode, true);
575576
if (error < 0)
576577
goto on_error;
577578
}
@@ -669,7 +670,8 @@ int git_treebuilder_new(
669670
if (append_entry(
670671
bld, entry_src->filename,
671672
entry_src->oid,
672-
entry_src->attr) < 0)
673+
entry_src->attr,
674+
false) < 0)
673675
goto on_error;
674676
}
675677
}

tests/object/tree/update.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,19 @@ void test_object_tree_update__add_conflict2(void)
284284

285285
cl_git_fail(git_tree_create_updated(&tree_updater_id, g_repo, NULL, 2, updates));
286286
}
287+
288+
void test_object_tree_update__remove_invalid_submodule(void)
289+
{
290+
git_tree *baseline;
291+
git_oid updated_tree_id, baseline_id;
292+
git_tree_update updates[] = {
293+
{GIT_TREE_UPDATE_REMOVE, {{0}}, GIT_FILEMODE_BLOB, "submodule"},
294+
};
295+
296+
/* This tree contains a submodule with an all-zero commit for a submodule named 'submodule' */
297+
cl_git_pass(git_oid_fromstr(&baseline_id, "396c7f1adb7925f51ba13a75f48252f44c5a14a2"));
298+
cl_git_pass(git_tree_lookup(&baseline, g_repo, &baseline_id));
299+
cl_git_pass(git_tree_create_updated(&updated_tree_id, g_repo, baseline, 1, updates));
300+
301+
git_tree_free(baseline);
302+
}
Binary file not shown.

0 commit comments

Comments
 (0)