Validate validates the fields and sets the default values.
(r *Repository)
| 582 | |
| 583 | // Validate validates the fields and sets the default values. |
| 584 | func (o *CommitOptions) Validate(r *Repository) error { |
| 585 | if o.All && o.Amend { |
| 586 | return errors.New("all and amend cannot be used together") |
| 587 | } |
| 588 | |
| 589 | if o.Amend && len(o.Parents) > 0 { |
| 590 | return errors.New("parents cannot be used with amend") |
| 591 | } |
| 592 | |
| 593 | if o.Author == nil { |
| 594 | if err := o.loadConfigAuthorAndCommitter(r); err != nil { |
| 595 | return err |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | if o.Committer == nil { |
| 600 | o.Committer = o.Author |
| 601 | } |
| 602 | |
| 603 | if len(o.Parents) == 0 { |
| 604 | head, err := r.Head() |
| 605 | if err != nil && err != plumbing.ErrReferenceNotFound { |
| 606 | return err |
| 607 | } |
| 608 | |
| 609 | if head != nil { |
| 610 | o.Parents = []plumbing.Hash{head.Hash()} |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | return nil |
| 615 | } |
| 616 | |
| 617 | func (o *CommitOptions) loadConfigAuthorAndCommitter(r *Repository) error { |
| 618 | cfg, err := r.ConfigScoped(config.SystemScope) |