Mort
Mort

Reputation: 3549

How to fix "broken" repo with bad remote fetchref

If git has a bad fetchref configured for a remote -- "bad" meaning that the ref does not exist on the remote -- the remote becomes "broken" and no operations with it will work.

Here's a nice little testcase

$> git config --local --add remote.origin.fetch "+refs/heads/foo/bar:refs/remotes/forks/foo/bar"
$> git fetch
fatal: couldn't find remote ref refs/heads/foo/bar
fatal: the remote end hung up unexpectedly

I can solve it like this, but it feels very "hacky" and I'm hoping there's a better way.

    # A bad ref here will really mess up git
    remote=${1:-origin}
    for fetchref in $(git fetch "$remote" 2>&1 | grep "find remote ref" | sed -e "s/.*find remote ref//")
    do
        echo "Removing bad ref: $fetchref"
        git config --local --unset "remote.$remote.fetch" "$fetchref"
    done

This is mildly better but it still feels overly-complicated and fragile.

for fetchref in $(git config --get-all "remote.${remote}.fetch"); do
    ref="${fetchref#:*}" # everything after the colon
    [[ "$ref" =~ \*$ ]] && continue
    if ! git ls-remote --exit-code "${remote}" "$ref" &>/dev/null; then
        echo "Bad ref: $fetchref does not exist on ${remote}. Removing."
        git config --local --unset "remote.${remote}.fetch" "$ref"
    fi
done

Upvotes: 1

Views: 38

Answers (0)

Related Questions