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

Skip to content

Commit 8a9419a

Browse files
committed
remote: build up the list of refs to remove
When removing the remote-tracking branches, build up the list and remove in two steps, working around an issue with the iterator. Removing while we're iterating over the refs can cause us to miss references.
1 parent 9bc2813 commit 8a9419a

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

src/remote.c

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,24 +1809,50 @@ static int remove_branch_config_related_entries(
18091809
return error;
18101810
}
18111811

1812-
static int remove_refs(git_repository *repo, const char *glob)
1812+
static int remove_refs(git_repository *repo, const git_refspec *spec)
18131813
{
1814-
git_reference_iterator *iter;
1814+
git_reference_iterator *iter = NULL;
1815+
git_vector refs;
18151816
const char *name;
1817+
char *dup;
18161818
int error;
1819+
size_t i;
18171820

1818-
if ((error = git_reference_iterator_glob_new(&iter, repo, glob)) < 0)
1821+
if ((error = git_vector_init(&refs, 8, NULL)) < 0)
18191822
return error;
18201823

1824+
if ((error = git_reference_iterator_new(&iter, repo)) < 0)
1825+
goto cleanup;
1826+
18211827
while ((error = git_reference_next_name(&name, iter)) == 0) {
1822-
if ((error = git_reference_remove(repo, name)) < 0)
1823-
break;
1824-
}
1825-
git_reference_iterator_free(iter);
1828+
if (!git_refspec_dst_matches(spec, name))
1829+
continue;
1830+
1831+
dup = git__strdup(name);
1832+
if (!dup) {
1833+
error = -1;
1834+
goto cleanup;
1835+
}
18261836

1837+
if ((error = git_vector_insert(&refs, dup)) < 0)
1838+
goto cleanup;
1839+
}
18271840
if (error == GIT_ITEROVER)
18281841
error = 0;
1842+
if (error < 0)
1843+
goto cleanup;
1844+
1845+
git_vector_foreach(&refs, i, name) {
1846+
if ((error = git_reference_remove(repo, name)) < 0)
1847+
break;
1848+
}
18291849

1850+
cleanup:
1851+
git_reference_iterator_free(iter);
1852+
git_vector_foreach(&refs, i, dup) {
1853+
git__free(dup);
1854+
}
1855+
git_vector_free(&refs);
18301856
return error;
18311857
}
18321858

@@ -1848,7 +1874,7 @@ static int remove_remote_tracking(git_repository *repo, const char *remote_name)
18481874
if (refspec == NULL)
18491875
continue;
18501876

1851-
if ((error = remove_refs(repo, git_refspec_dst(refspec))) < 0)
1877+
if ((error = remove_refs(repo, refspec)) < 0)
18521878
break;
18531879
}
18541880

0 commit comments

Comments
 (0)