Skip to content

Commit 0a18ab0

Browse files
committed
CommitChanges: allow set both committer and author
1 parent 7b206b5 commit 0a18ab0

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

commit.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,30 @@ func AddChanges(repoPath string, all bool, files ...string) error {
107107
return err
108108
}
109109

110-
// CommitChanges commits local changes with given message and author.
111-
func CommitChanges(repoPath, message string, author *Signature) error {
112-
cmd := NewCommand("commit", "-m", message)
113-
if author != nil {
114-
cmd.AddArguments(fmt.Sprintf("--author='%s <%s>'", author.Name, author.Email))
110+
type CommitChangesOptions struct {
111+
Committer *Signature
112+
Author *Signature
113+
Message string
114+
}
115+
116+
// CommitChanges commits local changes with given committer, author and message.
117+
// If author is nil, it will be the same as committer.
118+
func CommitChanges(repoPath string, opts CommitChangesOptions) error {
119+
cmd := NewCommand()
120+
if opts.Committer != nil {
121+
cmd.AddArguments("-c", "user.name="+opts.Committer.Name, "-c", "user.email="+opts.Committer.Email)
115122
}
116-
_, err := cmd.RunInDir(repoPath)
123+
cmd.AddArguments("commit")
117124

125+
if opts.Author == nil {
126+
opts.Author = opts.Committer
127+
}
128+
if opts.Author != nil {
129+
cmd.AddArguments(fmt.Sprintf("--author='%s <%s>'", opts.Author.Name, opts.Author.Email))
130+
}
131+
cmd.AddArguments("-m", opts.Message)
132+
133+
_, err := cmd.RunInDir(repoPath)
118134
// No stderr but exit status 1 means nothing to commit.
119135
if err != nil && err.Error() == "exit status 1" {
120136
return nil

git.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"time"
1111
)
1212

13-
const _VERSION = "0.3.8"
13+
const _VERSION = "0.4.0"
1414

1515
func Version() string {
1616
return _VERSION

0 commit comments

Comments
 (0)