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

How to work with tags ? #430

Closed
se0wtf opened this issue Jun 15, 2017 · 5 comments
Closed

How to work with tags ? #430

se0wtf opened this issue Jun 15, 2017 · 5 comments
Labels

Comments

@se0wtf
Copy link

se0wtf commented Jun 15, 2017

Hello,
I have a question about how to handle tags... i know they are supported but i don't know how to :

  • get the tag list
  • create a new tag and push it

Everything else is working fine (clone/add/commit/push) but i did'nt find an example about the tags.

Can you explain it ?
Thanks guys !

@smola smola added the question label Jun 16, 2017
@avarabyeu
Copy link

+1

@orirawlings
Copy link
Contributor

orirawlings commented Jul 15, 2017

Here is some example code to demonstrate how to list tags:

package main

import (
	"fmt"
	"os"

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

// Basic example of how to list tags.
func main() {
	CheckArgs("<path>")
	path := os.Args[1]

	// We instance a new repository targeting the given path (the .git folder)
	r, err := git.PlainOpen(path)
	CheckIfError(err)

	// List all tag references, both lightweight tags and annotated tags
	Info("git show-ref --tag")

	tagrefs, err := r.Tags()
	CheckIfError(err)
	err = tagrefs.ForEach(func(t *plumbing.Reference) error {
		fmt.Println(t)
		return nil
	})
	CheckIfError(err)

	// Print each annotated tag object (lightweight tags are not included)
	Info("for t in $(git show-ref --tag); do if [ \"$(git cat-file -t $t)\" = \"tag\" ]; then git cat-file -p $t ; fi; done")

	tags, err := r.TagObjects()
	CheckIfError(err)
	err = tags.ForEach(func(t *object.Tag) error {
		fmt.Println(t)
		return nil
	})
	CheckIfError(err)
}

I'm still investigating how to create tags. :)

@orirawlings
Copy link
Contributor

orirawlings commented Jul 16, 2017

Here is an example of how to create lightweight tags.

package main

import (
	"fmt"
	"os"

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

// Basic example of how create a lightweight tag
func main() {
	CheckArgs("<path> <tag> <commit>")
	path := os.Args[1]
	tag := os.Args[2]
	hash := plumbing.NewHash(os.Args[3])

	// We instance a new repository targeting the given path (the .git folder)
	r, err := git.PlainOpen(path)
	CheckIfError(err)

	// We create a lightweight tag
	Info(fmt.Sprintf("git tag %s %s", tag, hash))

	n := plumbing.ReferenceName("refs/tags/" + tag)
	t := plumbing.NewHashReference(n, hash)
	err = r.Storer.SetReference(t)
	CheckIfError(err)
}

Unfortunately you have to create the underlying storage.Storer object to do this. I think it would be a good enhancement to add some methods to the git.Repository for creating branches and/or tags.
EDIT: I overlooked the fact that git.Repository exposes a Storer field. So this is a little easier than I thought.

Creating annotated tags is a little more complicated. I'm still doing some research on that.

@maguro
Copy link
Contributor

maguro commented Jan 12, 2018

To add annotated tags:

tag := object.Tag{
	Name:    tag,
	Message: "Release of " + tag,
	Tagger: object.Signature{
		Name:  "Alan Cabrera",
		Email: "acabrera@foo.com",
		When:  time.Now(),
	},
	PGPSignature: "",
	Target:       hash,
	TargetType:   plumbing.CommitObject,
}

e := r.Storer.NewEncodedObject()
tag.Encode(e)
hash, err := r.Storer.SetEncodedObject(e)
CheckIfError(err)

err = r.Storer.SetReference(plumbing.NewReferenceFromStrings("refs/tags/" + tag, hash.String()))
CheckIfError(err)

@sPooKee
Copy link

sPooKee commented May 16, 2018

thx @maguro ! Works great.

But how do I push newly the created Tag? A repo.Push(&git.PushOptions{RemoteName:"origin"}) pushes my commits, but not the Tag.

Any Ideas? Thanks a lot.

EDIT: apparently no file is written to the ".git" folder .git/refs/tags/.

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

No branches or pull requests

7 participants