aboutsummaryrefslogtreecommitdiffstats
path: root/options.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2020-05-24 16:53:30 +0200
committerGitHub <noreply@github.com>2020-05-24 16:53:30 +0200
commite7f544844d6d736acfd9d75ee0d4a9d37f450103 (patch)
treedc44247231e6603bf10acaee91af5523120b4b84 /options.go
parent6d8103df45ce09ffd5323b4ef46d26440400a54f (diff)
parentbaf8c2761217cd457ef672972d5c1fb4d066e95a (diff)
downloadgo-git-e7f544844d6d736acfd9d75ee0d4a9d37f450103.tar.gz
Merge pull request #75 from mcuadros/scope-config
repository.ConfigScoped and worktree.Commit with empty CommitOptions
Diffstat (limited to 'options.go')
-rw-r--r--options.go44
1 files changed, 42 insertions, 2 deletions
diff --git a/options.go b/options.go
index 2a9727c..5367031 100644
--- a/options.go
+++ b/options.go
@@ -378,7 +378,8 @@ type CommitOptions struct {
// All automatically stage files that have been modified and deleted, but
// new files you have not told Git about are not affected.
All bool
- // Author is the author's signature of the commit.
+ // Author is the author's signature of the commit. If Author is empty the
+ // Name and Email is read from the config, and time.Now it's used as When.
Author *object.Signature
// Committer is the committer's signature of the commit. If Committer is
// nil the Author signature is used.
@@ -395,7 +396,9 @@ type CommitOptions struct {
// Validate validates the fields and sets the default values.
func (o *CommitOptions) Validate(r *Repository) error {
if o.Author == nil {
- return ErrMissingAuthor
+ if err := o.loadConfigAuthorAndCommitter(r); err != nil {
+ return err
+ }
}
if o.Committer == nil {
@@ -416,6 +419,43 @@ func (o *CommitOptions) Validate(r *Repository) error {
return nil
}
+func (o *CommitOptions) loadConfigAuthorAndCommitter(r *Repository) error {
+ cfg, err := r.ConfigScoped(config.SystemScope)
+ if err != nil {
+ return err
+ }
+
+ if o.Author == nil && cfg.Author.Email != "" && cfg.Author.Name != "" {
+ o.Author = &object.Signature{
+ Name: cfg.Author.Name,
+ Email: cfg.Author.Email,
+ When: time.Now(),
+ }
+ }
+
+ if o.Committer == nil && cfg.Committer.Email != "" && cfg.Committer.Name != "" {
+ o.Committer = &object.Signature{
+ Name: cfg.Committer.Name,
+ Email: cfg.Committer.Email,
+ When: time.Now(),
+ }
+ }
+
+ if o.Author == nil && cfg.User.Email != "" && cfg.User.Name != "" {
+ o.Author = &object.Signature{
+ Name: cfg.User.Name,
+ Email: cfg.User.Email,
+ When: time.Now(),
+ }
+ }
+
+ if o.Author == nil {
+ return ErrMissingAuthor
+ }
+
+ return nil
+}
+
var (
ErrMissingName = errors.New("name field is required")
ErrMissingTagger = errors.New("tagger field is required")