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

Clone can't checkout commit pointed to by tag, but Git works #574

Closed
mitchellh opened this issue Sep 3, 2017 · 5 comments · Fixed by #576
Closed

Clone can't checkout commit pointed to by tag, but Git works #574

mitchellh opened this issue Sep 3, 2017 · 5 comments · Fixed by #576
Labels

Comments

@mitchellh
Copy link

mitchellh commented Sep 3, 2017

If a tag is created pointing to a commit that hasn't been pushed yet, then the checkout will error. However, git works, and the commit is browse-able on GitHub. However, you can see its not in the commit history at all.

The only reason this feels like a bug to me is really because git show <tag> and git checkout <tag> somehow work despite the commit not being easily viewable in any other way.

I created a test repository to reproduce this: https://github.com/mitchellh/go-git-test-failure

Notice the tag v0.1.0 points to a commit (you can click it and see it), but if you go to the commit history it doesn't exist, and there are no other branches either. This is a lightweight tag. But clone the repository manually and you'll be able to use that tag with git.

And some code you can run with go run to reproduce this:

package main

import (
	"fmt"
	"io/ioutil"
	"os"

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

func main() {
	// Create the temporary directory for cloning
	dir, err := ioutil.TempDir("", "test")
	if err != nil {
		panic(err)
	}

	// Clone it into the target directory getting only the tag commit
	repo, err := git.PlainClone(dir, false, &git.CloneOptions{
		URL: "https://github.com/mitchellh/go-git-test-failure.git",
	})
	if err != nil {
		os.RemoveAll(dir)
		panic(err)
	}

	// Get the working tree so we can change refs
	tree, err := repo.Worktree()
	if err != nil {
		panic(err)
	}

	tagName := "v0.1.0"

	// Checkout our tag
	err = tree.Checkout(&git.CheckoutOptions{
		Branch: plumbing.ReferenceName("refs/tags/" + tagName),
	})
	if err != nil {
		panic(err)
	}

	fmt.Println(dir)
}
@mcuadros
Copy link
Contributor

mcuadros commented Sep 4, 2017

The problem is the following:

By default when you clone a repository, only the tags that belong to any of the branches being clone are created in the local repository.

Since this tag points to a commit that is not in the history of the branch master, the tags doesn't exists locally.

I though that this was the behavior of git, but apparently isn't. I will try find this code in git and then I will fix the issue.

@mcuadros
Copy link
Contributor

mcuadros commented Sep 4, 2017

Ok I found the logic on git.

Making a PR.

@mitchellh
Copy link
Author

Thanks! I'll look for the v4 gopkg update and grab it. :)

@mcuadros
Copy link
Contributor

mcuadros commented Sep 4, 2017

@mitchellh
Copy link
Author

Thanks, confirmed it works great.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants