Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Fetching into a bare repo doesn't work #518

Closed
strib opened this issue Jul 28, 2017 · 4 comments
Closed

Fetching into a bare repo doesn't work #518

strib opened this issue Jul 28, 2017 · 4 comments

Comments

@strib
Copy link
Contributor

strib commented Jul 28, 2017

I'm trying to use go-git (v4) to fetch from a remote into a bare repo, and I'm getting back an "already up-to-date" error even though I know for sure that the remote has new commits.

I was writing a test to see what happens when doing a non-fast-forwardable fetch (inspecting the code, I think there's a bug there too as it doesn't appear to check the fast-forward + in the refspec at all), but it seems there's a bigger issue fetching at all.

Here is a shell script to set up a repro (run it in a new test directory):

#!/usr/bin/env bash

# Make bare repo
mkdir repo
cd repo
git init --bare
cd ..

# Make checkout and initialize remote repo
mkdir checkout
cd checkout
echo test > test
git init
git add test
git commit -m "test"
git remote add r ../repo
git push r master:master
cd ..

# Make a 2nd commit that's not pushed yet
cd checkout
echo test2 > test2
git add test2
git commit -m "test2"
cd ..

# Make a remote in the bare repo
cd repo
git remote add c $PWD/../checkout

Then, compile and run this Go program in the same directory:

package main

import (
	"fmt"

	"gopkg.in/src-d/go-billy.v3/osfs"
	gogit "gopkg.in/src-d/go-git.v4"
	"gopkg.in/src-d/go-git.v4/config"
	"gopkg.in/src-d/go-git.v4/storage/filesystem"
)

func main() {
	bare := osfs.New("./repo/")
	s, err := filesystem.NewStorage(bare)
	if err != nil {
		panic(err)
	}

	repo, err := gogit.Open(s, nil)
	if err != nil {
		panic(err)
	}

	o := &gogit.FetchOptions{
		RemoteName: "c",
		RefSpecs:   []config.RefSpec{"master:master"},
	}
	err = repo.Fetch(o)
	if err != nil {
		fmt.Printf("Couldn't fetch from remote (non-force): %+v\n", err)
	}

	o = &gogit.FetchOptions{
		RemoteName: "c",
		RefSpecs:   []config.RefSpec{"+master:master"},
	}
	err = repo.Fetch(o)
	if err != nil {
		fmt.Printf("Couldn't fetch from remote (force): %+v\n", err)
	}
}

I get the output:

$ go build -o fetch main.go && ./fetch 
Couldn't fetch from remote (non-force): already up-to-date
Couldn't fetch from remote (force): already up-to-date

However, if you try it on the command line, this is the behavior:

repo$ git fetch c master:master
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /tmp/gogittest/repo/../checkout
   93e4174..b66c535  master     -> master
 * [new branch]      master     -> c/master

Is go-git intended to be used to access bare repos? Am I doing something wrong? Thanks! And thanks for the great project.

@mcuadros
Copy link
Contributor

The problem is not in the fetch itself, is because you are using, short refs in the refspec like "+master:master". This is not currently supported you must to use a full refspec such as +refs/heads/master:refs/remotes/origin/master.

@strib
Copy link
Contributor Author

strib commented Jul 28, 2017

Oh, thanks!

Off the top of your head, will it reject non-fast-forward fetches without the +, the way it does on the command line? (I can file a separate issue for that if you want.)

@mcuadros
Copy link
Contributor

The current implementation is always force. But is being fixed there #490.

@strib
Copy link
Contributor Author

strib commented Jul 28, 2017

Awesome, I'll watch out for that. Thanks.

@strib strib closed this as completed Jul 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants