Skip to content

Commit

Permalink
git: Add tagging support
Browse files Browse the repository at this point in the history
This adds a few methods:

* CreateTag, which can be used to create both lightweight and annotated
tags with a supplied TagObjectOptions struct. PGP signing is possible as
well.
* Tag, to fetch a single tag ref. As opposed to Tags or TagObjects, this
will also fetch the tag object if it exists and return it along with the
output. Lightweight tags just return the object as nil.
* DeleteTag, to delete a tag. This simply deletes the ref. The object is
left orphaned to be GCed later.

I'm not 100% sure if DeleteTag is the correct behavior - looking for
details on exactly *what* happens to a tag object if you delete the ref
and not the tag were sparse, and groking the Git source did not really
produce much insight to the untrained eye. This may be something that
comes up in review. If deletion of the object is necessary, the
in-memory storer may require some updates to allow DeleteLooseObject to
be supported.

Signed-off-by: Chris Marchesi <[email protected]>
  • Loading branch information
vancluever committed Aug 22, 2018
1 parent 7b6c126 commit b9f5efe
Show file tree
Hide file tree
Showing 3 changed files with 443 additions and 3 deletions.
47 changes: 45 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,9 @@ type CommitOptions struct {
// Parents are the parents commits for the new commit, by default when
// len(Parents) is zero, the hash of HEAD reference is used.
Parents []plumbing.Hash
// A key to sign the commit with. A nil value here means the commit will not
// be signed. The private key must be present and already decrypted.
// SignKey denotes a key to sign the commit with. A nil value here means the
// commit will not be signed. The private key must be present and already
// decrypted.
SignKey *openpgp.Entity
}

Expand Down Expand Up @@ -377,6 +378,48 @@ func (o *CommitOptions) Validate(r *Repository) error {
return nil
}

var (
ErrMissingName = errors.New("name field is required")
ErrMissingTagger = errors.New("tagger field is required")
ErrMissingMessage = errors.New("message field is required")
ErrBadObjectType = errors.New("bad object type for tagging")
)

// TagObjectOptions describes how a tag object should be created.
type TagObjectOptions struct {
// Tagger defines the signature of the tag creator.
Tagger *object.Signature
// Message defines the annotation of the tag.
Message string
// TargetType is the object type of the target. The object specified by
// Target must be of this type.
TargetType plumbing.ObjectType
// SignKey denotes a key to sign the tag with. A nil value here means the tag
// will not be signed. The private key must be present and already decrypted.
SignKey *openpgp.Entity
}

// Validate validates the fields and sets the default values.
func (o *TagObjectOptions) Validate(r *Repository, hash plumbing.Hash) error {
if o.Tagger == nil {
return ErrMissingTagger
}

if o.Message == "" {
return ErrMissingMessage
}

if o.TargetType == plumbing.InvalidObject || o.TargetType == plumbing.AnyObject {
return ErrBadObjectType
}

if _, err := r.Object(o.TargetType, hash); err != nil {
return err
}

return nil
}

// ListOptions describes how a remote list should be performed.
type ListOptions struct {
// Auth credentials, if required, to use with the remote repository.
Expand Down
134 changes: 133 additions & 1 deletion repository.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package git

import (
"bytes"
"context"
"errors"
"fmt"
stdioutil "io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"time"

"golang.org/x/crypto/openpgp"
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/internal/revision"
"gopkg.in/src-d/go-git.v4/plumbing"
Expand All @@ -31,7 +34,12 @@ var (
// ErrBranchExists an error stating the specified branch already exists
ErrBranchExists = errors.New("branch already exists")
// ErrBranchNotFound an error stating the specified branch does not exist
ErrBranchNotFound = errors.New("branch not found")
ErrBranchNotFound = errors.New("branch not found")
// ErrTagExists an error stating the specified tag already exists
ErrTagExists = errors.New("tag already exists")
// ErrTagNotFound an error stating the specified tag does not exist
ErrTagNotFound = errors.New("tag not found")

ErrInvalidReference = errors.New("invalid reference, should be a tag or a branch")
ErrRepositoryNotExists = errors.New("repository does not exist")
ErrRepositoryAlreadyExists = errors.New("repository already exists")
Expand Down Expand Up @@ -484,6 +492,130 @@ func (r *Repository) DeleteBranch(name string) error {
return r.Storer.SetConfig(cfg)
}

// CreateTag creates a tag. If opts is included, the tag is an annotated tag,
// otherwise a lightweight tag is created.
func (r *Repository) CreateTag(name string, hash plumbing.Hash, opts *TagObjectOptions) (*plumbing.Reference, error) {
rname := plumbing.ReferenceName(path.Join("refs", "tags", name))

_, err := r.Storer.Reference(rname)
switch err {
case nil:
// Tag exists, this is an error
return nil, ErrTagExists
case plumbing.ErrReferenceNotFound:
// Tag missing, available for creation, pass this
default:
// Some other error
return nil, err
}

var target plumbing.Hash
if opts != nil {
target, err = r.createTagObject(name, hash, opts)
if err != nil {
return nil, err
}
} else {
target = hash
}

ref := plumbing.NewHashReference(rname, target)
if err = r.Storer.SetReference(ref); err != nil {
return nil, err
}

return ref, nil
}

func (r *Repository) createTagObject(name string, hash plumbing.Hash, opts *TagObjectOptions) (plumbing.Hash, error) {
if err := opts.Validate(r, hash); err != nil {
return plumbing.ZeroHash, err
}

tag := &object.Tag{
Name: name,
Tagger: *opts.Tagger,
Message: opts.Message,
TargetType: opts.TargetType,
Target: hash,
}

if opts.SignKey != nil {
sig, err := r.buildTagSignature(tag, opts.SignKey)
if err != nil {
return plumbing.ZeroHash, err
}

tag.PGPSignature = sig
}

obj := r.Storer.NewEncodedObject()
if err := tag.Encode(obj); err != nil {
return plumbing.ZeroHash, err
}

return r.Storer.SetEncodedObject(obj)
}

func (r *Repository) buildTagSignature(tag *object.Tag, signKey *openpgp.Entity) (string, error) {
encoded := &plumbing.MemoryObject{}
if err := tag.Encode(encoded); err != nil {
return "", err
}

rdr, err := encoded.Reader()
if err != nil {
return "", err
}

var b bytes.Buffer
if err := openpgp.ArmoredDetachSign(&b, signKey, rdr, nil); err != nil {
return "", err
}

return b.String(), nil
}

// Tag fetches a tag from the repository. The tag is returned as a raw
// reference. If the tag is annotated, a non-nil tag object is returned.
func (r *Repository) Tag(name string) (*plumbing.Reference, *object.Tag, error) {
ref, err := r.Reference(plumbing.ReferenceName(path.Join("refs", "tags", name)), false)
if err != nil {
if err == plumbing.ErrReferenceNotFound {
// Return a friendly error for this one, versus just ReferenceNotFound.
return nil, nil, ErrTagNotFound
}

return nil, nil, err
}

obj, err := r.TagObject(ref.Hash())
if err != nil && err != plumbing.ErrObjectNotFound {
return nil, nil, err
}

return ref, obj, nil
}

// DeleteTag deletes a tag from the repository.
func (r *Repository) DeleteTag(name string) error {
_, obj, err := r.Tag(name)
if err != nil {
return err
}

if err = r.Storer.RemoveReference(plumbing.ReferenceName(path.Join("refs", "tags", name))); err != nil {
return err
}

// Delete the tag object if this was an annotated tag.
if obj != nil {
return r.DeleteObject(obj.Hash)
}

return nil
}

func (r *Repository) resolveToCommitHash(h plumbing.Hash) (plumbing.Hash, error) {
obj, err := r.Storer.EncodedObject(plumbing.AnyObject, h)
if err != nil {
Expand Down
Loading

0 comments on commit b9f5efe

Please sign in to comment.