|
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" |
|
32 | 35 | // ErrBranchExists an error stating the specified branch already exists |
33 | 36 | ErrBranchExists = errors.New("branch already exists") |
34 | 37 | // ErrBranchNotFound an error stating the specified branch does not exist |
35 | | - ErrBranchNotFound = errors.New("branch not found") |
| 38 | + ErrBranchNotFound = errors.New("branch not found") |
| 39 | + // ErrTagExists an error stating the specified tag already exists |
| 40 | + ErrTagExists = errors.New("tag already exists") |
| 41 | + // ErrTagNotFound an error stating the specified tag does not exist |
| 42 | + ErrTagNotFound = errors.New("tag not found") |
| 43 | + |
36 | 44 | ErrInvalidReference = errors.New("invalid reference, should be a tag or a branch") |
37 | 45 | ErrRepositoryNotExists = errors.New("repository does not exist") |
38 | 46 | ErrRepositoryAlreadyExists = errors.New("repository already exists") |
@@ -478,6 +486,139 @@ func (r *Repository) DeleteBranch(name string) error { |
478 | 486 | return r.Storer.SetConfig(cfg) |
479 | 487 | } |
480 | 488 |
|
| 489 | +// CreateTag creates a tag. If opts is included, the tag is an annotated tag, |
| 490 | +// otherwise a lightweight tag is created. |
| 491 | +func (r *Repository) CreateTag(name string, hash plumbing.Hash, opts *CreateTagOptions) (*plumbing.Reference, error) { |
| 492 | + rname := plumbing.ReferenceName(path.Join("refs", "tags", name)) |
| 493 | + |
| 494 | + _, err := r.Storer.Reference(rname) |
| 495 | + switch err { |
| 496 | + case nil: |
| 497 | + // Tag exists, this is an error |
| 498 | + return nil, ErrTagExists |
| 499 | + case plumbing.ErrReferenceNotFound: |
| 500 | + // Tag missing, available for creation, pass this |
| 501 | + default: |
| 502 | + // Some other error |
| 503 | + return nil, err |
| 504 | + } |
| 505 | + |
| 506 | + var target plumbing.Hash |
| 507 | + if opts != nil { |
| 508 | + target, err = r.createTagObject(name, hash, opts) |
| 509 | + if err != nil { |
| 510 | + return nil, err |
| 511 | + } |
| 512 | + } else { |
| 513 | + target = hash |
| 514 | + } |
| 515 | + |
| 516 | + ref := plumbing.NewHashReference(rname, target) |
| 517 | + if err = r.Storer.SetReference(ref); err != nil { |
| 518 | + return nil, err |
| 519 | + } |
| 520 | + |
| 521 | + return ref, nil |
| 522 | +} |
| 523 | + |
| 524 | +func (r *Repository) createTagObject(name string, hash plumbing.Hash, opts *CreateTagOptions) (plumbing.Hash, error) { |
| 525 | + if err := opts.Validate(r, hash); err != nil { |
| 526 | + return plumbing.ZeroHash, err |
| 527 | + } |
| 528 | + |
| 529 | + rawobj, err := object.GetObject(r.Storer, hash) |
| 530 | + if err != nil { |
| 531 | + return plumbing.ZeroHash, err |
| 532 | + } |
| 533 | + |
| 534 | + tag := &object.Tag{ |
| 535 | + Name: name, |
| 536 | + Tagger: *opts.Tagger, |
| 537 | + Message: opts.Message, |
| 538 | + TargetType: rawobj.Type(), |
| 539 | + Target: hash, |
| 540 | + } |
| 541 | + |
| 542 | + if opts.SignKey != nil { |
| 543 | + sig, err := r.buildTagSignature(tag, opts.SignKey) |
| 544 | + if err != nil { |
| 545 | + return plumbing.ZeroHash, err |
| 546 | + } |
| 547 | + |
| 548 | + tag.PGPSignature = sig |
| 549 | + } |
| 550 | + |
| 551 | + obj := r.Storer.NewEncodedObject() |
| 552 | + if err := tag.Encode(obj); err != nil { |
| 553 | + return plumbing.ZeroHash, err |
| 554 | + } |
| 555 | + |
| 556 | + return r.Storer.SetEncodedObject(obj) |
| 557 | +} |
| 558 | + |
| 559 | +func (r *Repository) buildTagSignature(tag *object.Tag, signKey *openpgp.Entity) (string, error) { |
| 560 | + encoded := &plumbing.MemoryObject{} |
| 561 | + if err := tag.Encode(encoded); err != nil { |
| 562 | + return "", err |
| 563 | + } |
| 564 | + |
| 565 | + rdr, err := encoded.Reader() |
| 566 | + if err != nil { |
| 567 | + return "", err |
| 568 | + } |
| 569 | + |
| 570 | + var b bytes.Buffer |
| 571 | + if err := openpgp.ArmoredDetachSign(&b, signKey, rdr, nil); err != nil { |
| 572 | + return "", err |
| 573 | + } |
| 574 | + |
| 575 | + return b.String(), nil |
| 576 | +} |
| 577 | + |
| 578 | +// Tag returns a tag from the repository. |
| 579 | +// |
| 580 | +// If you want to check to see if the tag is an annotated tag, you can call |
| 581 | +// TagObject on the hash of the reference in ForEach: |
| 582 | +// |
| 583 | +// ref, err := r.Tag("v0.1.0") |
| 584 | +// if err != nil { |
| 585 | +// // Handle error |
| 586 | +// } |
| 587 | +// |
| 588 | +// obj, err := r.TagObject(ref.Hash()) |
| 589 | +// switch err { |
| 590 | +// case nil: |
| 591 | +// // Tag object present |
| 592 | +// case plumbing.ErrObjectNotFound: |
| 593 | +// // Not a tag object |
| 594 | +// default: |
| 595 | +// // Some other error |
| 596 | +// } |
| 597 | +// |
| 598 | +func (r *Repository) Tag(name string) (*plumbing.Reference, error) { |
| 599 | + ref, err := r.Reference(plumbing.ReferenceName(path.Join("refs", "tags", name)), false) |
| 600 | + if err != nil { |
| 601 | + if err == plumbing.ErrReferenceNotFound { |
| 602 | + // Return a friendly error for this one, versus just ReferenceNotFound. |
| 603 | + return nil, ErrTagNotFound |
| 604 | + } |
| 605 | + |
| 606 | + return nil, err |
| 607 | + } |
| 608 | + |
| 609 | + return ref, nil |
| 610 | +} |
| 611 | + |
| 612 | +// DeleteTag deletes a tag from the repository. |
| 613 | +func (r *Repository) DeleteTag(name string) error { |
| 614 | + _, err := r.Tag(name) |
| 615 | + if err != nil { |
| 616 | + return err |
| 617 | + } |
| 618 | + |
| 619 | + return r.Storer.RemoveReference(plumbing.ReferenceName(path.Join("refs", "tags", name))) |
| 620 | +} |
| 621 | + |
481 | 622 | func (r *Repository) resolveToCommitHash(h plumbing.Hash) (plumbing.Hash, error) { |
482 | 623 | obj, err := r.Storer.EncodedObject(plumbing.AnyObject, h) |
483 | 624 | if err != nil { |
@@ -839,9 +980,31 @@ func (r *Repository) Log(o *LogOptions) (object.CommitIter, error) { |
839 | 980 | return nil, fmt.Errorf("invalid Order=%v", o.Order) |
840 | 981 | } |
841 | 982 |
|
842 | | -// Tags returns all the References from Tags. This method returns only lightweight |
843 | | -// tags. Note that not all the tags are lightweight ones. To return annotated tags |
844 | | -// too, you need to call TagObjects() method. |
| 983 | +// Tags returns all the tag References in a repository. |
| 984 | +// |
| 985 | +// If you want to check to see if the tag is an annotated tag, you can call |
| 986 | +// TagObject on the hash Reference passed in through ForEach: |
| 987 | +// |
| 988 | +// iter, err := r.Tags() |
| 989 | +// if err != nil { |
| 990 | +// // Handle error |
| 991 | +// } |
| 992 | +// |
| 993 | +// if err := iter.ForEach(func (ref *plumbing.Reference) error { |
| 994 | +// obj, err := r.TagObject(ref.Hash()) |
| 995 | +// switch err { |
| 996 | +// case nil: |
| 997 | +// // Tag object present |
| 998 | +// case plumbing.ErrObjectNotFound: |
| 999 | +// // Not a tag object |
| 1000 | +// default: |
| 1001 | +// // Some other error |
| 1002 | +// return err |
| 1003 | +// } |
| 1004 | +// }); err != nil { |
| 1005 | +// // Handle outer iterator error |
| 1006 | +// } |
| 1007 | +// |
845 | 1008 | func (r *Repository) Tags() (storer.ReferenceIter, error) { |
846 | 1009 | refIter, err := r.Storer.IterReferences() |
847 | 1010 | if err != nil { |
|
0 commit comments