|
1 | 1 | package git |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "context" |
5 | 6 | "errors" |
6 | 7 | "fmt" |
7 | 8 | stdioutil "io/ioutil" |
8 | 9 | "os" |
| 10 | + "path" |
9 | 11 | "path/filepath" |
10 | 12 | "strings" |
11 | 13 | "time" |
12 | 14 |
|
| 15 | + "golang.org/x/crypto/openpgp" |
13 | 16 | "gopkg.in/src-d/go-git.v4/config" |
14 | 17 | "gopkg.in/src-d/go-git.v4/internal/revision" |
15 | 18 | "gopkg.in/src-d/go-git.v4/plumbing" |
|
31 | 34 | // ErrBranchExists an error stating the specified branch already exists |
32 | 35 | ErrBranchExists = errors.New("branch already exists") |
33 | 36 | // ErrBranchNotFound an error stating the specified branch does not exist |
34 | | - ErrBranchNotFound = errors.New("branch not found") |
| 37 | + ErrBranchNotFound = errors.New("branch not found") |
| 38 | + // ErrTagExists an error stating the specified tag already exists |
| 39 | + ErrTagExists = errors.New("tag already exists") |
| 40 | + // ErrTagNotFound an error stating the specified tag does not exist |
| 41 | + ErrTagNotFound = errors.New("tag not found") |
| 42 | + |
35 | 43 | ErrInvalidReference = errors.New("invalid reference, should be a tag or a branch") |
36 | 44 | ErrRepositoryNotExists = errors.New("repository does not exist") |
37 | 45 | ErrRepositoryAlreadyExists = errors.New("repository already exists") |
@@ -484,6 +492,130 @@ func (r *Repository) DeleteBranch(name string) error { |
484 | 492 | return r.Storer.SetConfig(cfg) |
485 | 493 | } |
486 | 494 |
|
| 495 | +// CreateTag creates a tag. If opts is included, the tag is an annotated tag, |
| 496 | +// otherwise a lightweight tag is created. |
| 497 | +func (r *Repository) CreateTag(name string, hash plumbing.Hash, opts *TagObjectOptions) (*plumbing.Reference, error) { |
| 498 | + rname := plumbing.ReferenceName(path.Join("refs", "tags", name)) |
| 499 | + |
| 500 | + _, err := r.Storer.Reference(rname) |
| 501 | + switch err { |
| 502 | + case nil: |
| 503 | + // Tag exists, this is an error |
| 504 | + return nil, ErrTagExists |
| 505 | + case plumbing.ErrReferenceNotFound: |
| 506 | + // Tag missing, available for creation, pass this |
| 507 | + default: |
| 508 | + // Some other error |
| 509 | + return nil, err |
| 510 | + } |
| 511 | + |
| 512 | + var target plumbing.Hash |
| 513 | + if opts != nil { |
| 514 | + target, err = r.createTagObject(name, hash, opts) |
| 515 | + if err != nil { |
| 516 | + return nil, err |
| 517 | + } |
| 518 | + } else { |
| 519 | + target = hash |
| 520 | + } |
| 521 | + |
| 522 | + ref := plumbing.NewHashReference(rname, target) |
| 523 | + if err = r.Storer.SetReference(ref); err != nil { |
| 524 | + return nil, err |
| 525 | + } |
| 526 | + |
| 527 | + return ref, nil |
| 528 | +} |
| 529 | + |
| 530 | +func (r *Repository) createTagObject(name string, hash plumbing.Hash, opts *TagObjectOptions) (plumbing.Hash, error) { |
| 531 | + if err := opts.Validate(r, hash); err != nil { |
| 532 | + return plumbing.ZeroHash, err |
| 533 | + } |
| 534 | + |
| 535 | + tag := &object.Tag{ |
| 536 | + Name: name, |
| 537 | + Tagger: *opts.Tagger, |
| 538 | + Message: opts.Message, |
| 539 | + TargetType: opts.TargetType, |
| 540 | + Target: hash, |
| 541 | + } |
| 542 | + |
| 543 | + if opts.SignKey != nil { |
| 544 | + sig, err := r.buildTagSignature(tag, opts.SignKey) |
| 545 | + if err != nil { |
| 546 | + return plumbing.ZeroHash, err |
| 547 | + } |
| 548 | + |
| 549 | + tag.PGPSignature = sig |
| 550 | + } |
| 551 | + |
| 552 | + obj := r.Storer.NewEncodedObject() |
| 553 | + if err := tag.Encode(obj); err != nil { |
| 554 | + return plumbing.ZeroHash, err |
| 555 | + } |
| 556 | + |
| 557 | + return r.Storer.SetEncodedObject(obj) |
| 558 | +} |
| 559 | + |
| 560 | +func (r *Repository) buildTagSignature(tag *object.Tag, signKey *openpgp.Entity) (string, error) { |
| 561 | + encoded := &plumbing.MemoryObject{} |
| 562 | + if err := tag.Encode(encoded); err != nil { |
| 563 | + return "", err |
| 564 | + } |
| 565 | + |
| 566 | + rdr, err := encoded.Reader() |
| 567 | + if err != nil { |
| 568 | + return "", err |
| 569 | + } |
| 570 | + |
| 571 | + var b bytes.Buffer |
| 572 | + if err := openpgp.ArmoredDetachSign(&b, signKey, rdr, nil); err != nil { |
| 573 | + return "", err |
| 574 | + } |
| 575 | + |
| 576 | + return b.String(), nil |
| 577 | +} |
| 578 | + |
| 579 | +// Tag fetches a tag from the repository. The tag is returned as a raw |
| 580 | +// reference. If the tag is annotated, a non-nil tag object is returned. |
| 581 | +func (r *Repository) Tag(name string) (*plumbing.Reference, *object.Tag, error) { |
| 582 | + ref, err := r.Reference(plumbing.ReferenceName(path.Join("refs", "tags", name)), false) |
| 583 | + if err != nil { |
| 584 | + if err == plumbing.ErrReferenceNotFound { |
| 585 | + // Return a friendly error for this one, versus just ReferenceNotFound. |
| 586 | + return nil, nil, ErrTagNotFound |
| 587 | + } |
| 588 | + |
| 589 | + return nil, nil, err |
| 590 | + } |
| 591 | + |
| 592 | + obj, err := r.TagObject(ref.Hash()) |
| 593 | + if err != nil && err != plumbing.ErrObjectNotFound { |
| 594 | + return nil, nil, err |
| 595 | + } |
| 596 | + |
| 597 | + return ref, obj, nil |
| 598 | +} |
| 599 | + |
| 600 | +// DeleteTag deletes a tag from the repository. |
| 601 | +func (r *Repository) DeleteTag(name string) error { |
| 602 | + _, obj, err := r.Tag(name) |
| 603 | + if err != nil { |
| 604 | + return err |
| 605 | + } |
| 606 | + |
| 607 | + if err = r.Storer.RemoveReference(plumbing.ReferenceName(path.Join("refs", "tags", name))); err != nil { |
| 608 | + return err |
| 609 | + } |
| 610 | + |
| 611 | + // Delete the tag object if this was an annotated tag. |
| 612 | + if obj != nil { |
| 613 | + return r.DeleteObject(obj.Hash) |
| 614 | + } |
| 615 | + |
| 616 | + return nil |
| 617 | +} |
| 618 | + |
487 | 619 | func (r *Repository) resolveToCommitHash(h plumbing.Hash) (plumbing.Hash, error) { |
488 | 620 | obj, err := r.Storer.EncodedObject(plumbing.AnyObject, h) |
489 | 621 | if err != nil { |
|
0 commit comments