Git Product home page Git Product logo

Comments (4)

carlosmn avatar carlosmn commented on August 20, 2024

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().

from git2go.

milosgajdos avatar milosgajdos commented on August 20, 2024

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!

from git2go.

carlosmn avatar carlosmn commented on August 20, 2024

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.

from git2go.

milosgajdos avatar milosgajdos commented on August 20, 2024

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 👍

from git2go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.