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

Reference IsTag() is broken #461

Closed
andrewrynhard opened this issue Jul 4, 2017 · 2 comments
Closed

Reference IsTag() is broken #461

andrewrynhard opened this issue Jul 4, 2017 · 2 comments

Comments

@andrewrynhard
Copy link

Checkout a tag:

git checkout v0.1.0

Then run a go program that uses go-git like so:

ref, _ := repo.Head()
println(ref.Name())
println(ref.IsTag())

The output shows that Name() returns HEAD and IsTag() returns false.

@andrewrynhard
Copy link
Author

Here is the workaround I am using:

func Tag(repo *git.Repository) (tag string, isTag bool, err error) {
	ref, err := repo.Head()
	if err != nil {
		return
	}
	tags, err := repo.Tags()
	if err != nil {
		return
	}
	err = tags.ForEach(func(_ref *plumbing.Reference) error {
		if _ref.Hash().String() == ref.Hash().String() {
			isTag = true
			tag = _ref.Name().Short()
			return nil
		}
		return nil
	})
	if err != nil {
		return
	}

	return
}

@mcuadros
Copy link
Contributor

mcuadros commented Jul 5, 2017

The HEAD when a tag is checkout is not a symbolic reference, is a reference pointing to a commit, this is called detached HEAD.

So your solution is the right approach to know witch tag was checkout.

> git clone [email protected]:src-d/go-git.git -b v4.0.0-rc11     
Cloning into 'go-git'...
remote: Counting objects: 6745, done.
remote: Total 6745 (delta 0), reused 0 (delta 0), pack-reused 6745
Receiving objects: 100% (6745/6745), 14.85 MiB | 191.00 KiB/s, done.
Resolving deltas: 100% (4028/4028), done.
Note: checking out 'ad02bf020460c210660db4fffda7f926b6aae95a'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

>  git show-ref --head HEAD                                                                                                                                                                                mcuadros@mcuadros-xps-arch
ad02bf020460c210660db4fffda7f926b6aae95a HEAD
102d4b5aeb9b3cbd544c59706a1b0dd9300ddcc8 refs/remotes/origin/HEAD

> git status -b                                                                                                                                                                                           
Not currently on any branch.
nothing to commit, working tree clean

> git branch                                
* (no branch)

@mcuadros mcuadros closed this as completed Jul 5, 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