Validate validates the fields and sets the default values.
(r *Repository)
| 550 | |
| 551 | // Validate validates the fields and sets the default values. |
| 552 | func (o *CommitOptions) Validate(r *Repository) error { |
| 553 | if o.All && o.Amend { |
| 554 | return errors.New("all and amend cannot be used together") |
| 555 | } |
| 556 | |
| 557 | if o.Amend && len(o.Parents) > 0 { |
| 558 | return errors.New("parents cannot be used with amend") |
| 559 | } |
| 560 | |
| 561 | if o.Author == nil { |
| 562 | if err := o.loadConfigAuthorAndCommitter(r); err != nil { |
| 563 | return err |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | if o.Committer == nil { |
| 568 | o.Committer = o.Author |
| 569 | } |
| 570 | |
| 571 | if len(o.Parents) == 0 { |
| 572 | head, err := r.Head() |
| 573 | if err != nil && err != plumbing.ErrReferenceNotFound { |
| 574 | return err |
| 575 | } |
| 576 | |
| 577 | if head != nil { |
| 578 | o.Parents = []plumbing.Hash{head.Hash()} |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | return nil |
| 583 | } |
| 584 | |
| 585 | func (o *CommitOptions) loadConfigAuthorAndCommitter(r *Repository) error { |
| 586 | cfg, err := r.ConfigScoped(config.SystemScope) |