Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git clone specific tag #126

Closed
milosgajdos opened this issue Oct 24, 2014 · 4 comments
Closed

git clone specific tag #126

milosgajdos opened this issue Oct 24, 2014 · 4 comments

Comments

@milosgajdos
Copy link

When I try to clone a specific tag, I run something like this:

git clone --branch v1.0.0 https://github.com/ministryofjustice/supervisor-formula.git vendor/supervisor

The above command succeeds without any hiccups.
I figured I could write a go func like below.

func CloneRepo(url string, tag string, path string) error {
    opts := git.CloneOptions{
        CheckoutBranch: tag,
    }

    repo, err := git.Clone(url, path, &opts)
    if err != nil {
        return err
    }

    fmt.Println(repo)
    return nil
}

However, I'm getting the following error:

Failed to clone https://github.com/ministryofjustice/supervisor-formula.git Reference 'refs/remotes/origin/v1.0.0' not found

I'm guessing this is a lack of knowledge on my side, but I'm wondering whether I can accomplish something like this using the git package ?

@carlosmn
Copy link
Member

The thing here is that the option is called "--branch" so that's what we implemented, checking out a particular branch. But as it happens, what git means here is more like "meh, I'll take whatever", which is a behaviour which we haven't implemented.

Until we do accept any string to DWIM, you'd have to perform the switch to the commit in that tag via CheckoutTree() and SetHeadDetached().

@milosgajdos
Copy link
Author

Thanks for the hints! I've eventually done something like below:

ref, err := iter.Next()
    for err == nil {
        if ref.IsTag() {
            tagName := strings.TrimPrefix(ref.Name(), "refs/tags/")
            if tagName == "my_tag" {
                fmt.Println("Found the tag", "my_tag")
                obj, err := ref.Peel(git.ObjectTag)
                if err != nil {
                    return fmt.Errorf("Unable to peel the ref: %s\n", err)
                }

                if err := repo.SetHeadDetached(obj.Id(), nil, ""); err != nil {
                    return fmt.Errorf("Checking out tag %s failed: %s", tag, err)
                }

                return nil
            }
        }

        ref, err = iter.Next()
    }

However, I don't understand why I can't Peel the tag reference ? Every time I call ref.Peel(git.ObjectTag) I get the following error:

The git_object of id 'abe8b968b0607f92ddeac31b062208b7ee5fd475' can not be successfully peeled into a tag (git_otype=4).

I had the look at the actual tag hash and it's the same as reported by the error above:

$ cat repo/.git/refs/tags/my_tag
abe8b968b0607f92ddeac31b062208b7ee5fd475

So the tag actually exists. Am I missing something important here ?

Thanks!

@carlosmn
Copy link
Member

The tag reference exists, that doesn't mean there's a tag object underneath. If you look at that ref with git show or git cat-file -t, it should show that there is no tag object there.

But you're not interested in a tag object being there to perform be branch switch. What you need is a commit, so that's what you should be looking for.

You also might be interested in Repository.DwimReference() which will perform this shortname-to-reference conversion that you're doing manually in this code.

@milosgajdos
Copy link
Author

Thanks @carlosmn Eventually I went with something like this:

    ref, err := repo.DwimReference(tag)
    if err != nil {
        return fmt.Errorf("Could not find the %s ref: %s\n", ref.Name(), err)
    }

    if err := repo.SetHeadDetached(ref.Target(), nil, ""); err != nil {
        return fmt.Errorf("Checking out tag %s failed: %s", tag, err)
    }

I'm not sure if that's the best way to approach this, but it does seem to do the trick 👍

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

No branches or pull requests

2 participants