go-git icon indicating copy to clipboard operation
go-git copied to clipboard

Fetch on remote with changed commit fails

Open Kriechi opened this issue 6 years ago • 1 comments

Calling Fetch on a remote that has a changed / overwritten commit will cause a object not found error.

Reproduction steps:

git clone --depth 1 --no-single-branch --no-checkout https://github.com/python-hyper/wsproto /tmp/local-repo
git clone https://github.com/Kriechi/wsproto.git /tmp/remote-repo
cd /tmp/local-repo
git remote add remote-repo /tmp/remote-repo
git remote show origin
git remote show remote-repo 
git fetch --all --verbose

Now run this code:

package main

import (
	"fmt"

	"gopkg.in/src-d/go-git.v4"
)

func main() {
	r, err := git.PlainOpen("/tmp/local-repo")
	if err != nil {
		fmt.Println(err)
		return
	}

	remotes, err := r.Remotes()
	if err != nil {
		fmt.Println(err)
		return
	}
	for _, remote := range remotes {
		fmt.Println("Fetching: ", remote.Config().Name, "via", remote.Config().URLs)
		err := remote.Fetch(&git.FetchOptions{
			RemoteName: remote.Config().Name,
			Force:      true,
		})
		if err != nil && err != git.NoErrAlreadyUpToDate {
			fmt.Println("Error:", remote.Config().Name, err)
			continue
		}
	}

	fmt.Println("done")
}

Output:

$ go run test.go
Fetching:  origin via [https://github.com/python-hyper/wsproto]
Fetching:  remote-repo via [/tmp/remote-repo]
done

So far so good. Now lets change the top commit of the remote-repo: cd /tmp/remote-repo && git commit --amend --reset-author -m "foobar"

And run the go code again, and this time it will fail:

$ go run test.go
Fetching:  origin via [https://github.com/python-hyper/wsproto]
Fetching:  remote-repo via [/tmp/remote-repo]
Error: remote-repo object not found
done

But the git CLI has no problems fetching the updates:

$ cd /tmp/local-repo
$ git fetch --all --verbose
Fetching origin
From https://github.com/python-hyper/wsproto
 = [up to date]      master             -> origin/master
 = [up to date]      requires-io-master -> origin/requires-io-master
Fetching remote-repo
remote: Enumerating objects: 364, done.
remote: Counting objects: 100% (347/347), done.
remote: Compressing objects: 100% (95/95), done.
remote: Total 242 (delta 174), reused 214 (delta 147)
Receiving objects: 100% (242/242), 42.28 KiB | 14.09 MiB/s, done.
Resolving deltas: 100% (174/174), completed with 41 local objects.
From /tmp/remote-repo
 + 7ef9200...0e31dbc master     -> remote-repo/master  (forced update)

Kriechi avatar Dec 23 '18 18:12 Kriechi

I have a similar issue with Pull. When the top commit has changed or there's a new commit, Pull fails with a non-fast forward error.

jigneshdarji91 avatar Dec 26 '19 20:12 jigneshdarji91