Skip to content

Commit 3584191

Browse files
committed
Allow setting envs for a command
For the purpose of using envs to set committer for function CommitChanges. Thus, we can get rid of using '-c' option, which was introduced after Git 1.7.1 and breaks the minimum required version.
1 parent a5ca4e1 commit 3584191

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

command.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
"fmt"
1010
"io"
11+
"os"
1112
"os/exec"
1213
"strings"
1314
"time"
@@ -17,6 +18,7 @@ import (
1718
type Command struct {
1819
name string
1920
args []string
21+
envs []string
2022
}
2123

2224
func (c *Command) String() string {
@@ -40,6 +42,12 @@ func (c *Command) AddArguments(args ...string) *Command {
4042
return c
4143
}
4244

45+
// AddEnvs adds new environment variables to the command.
46+
func (c *Command) AddEnvs(envs ...string) *Command {
47+
c.envs = append(c.envs, envs...)
48+
return c
49+
}
50+
4351
const DEFAULT_TIMEOUT = 60 * time.Second
4452

4553
// RunInDirTimeoutPipeline executes the command in given directory with given timeout,
@@ -56,6 +64,9 @@ func (c *Command) RunInDirTimeoutPipeline(timeout time.Duration, dir string, std
5664
}
5765

5866
cmd := exec.Command(c.name, c.args...)
67+
if c.envs != nil {
68+
cmd.Env = append(os.Environ(), c.envs...)
69+
}
5970
cmd.Dir = dir
6071
cmd.Stdout = stdout
6172
cmd.Stderr = stderr

commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ type CommitChangesOptions struct {
116116
func CommitChanges(repoPath string, opts CommitChangesOptions) error {
117117
cmd := NewCommand()
118118
if opts.Committer != nil {
119-
cmd.AddArguments("-c", "user.name="+opts.Committer.Name, "-c", "user.email="+opts.Committer.Email)
119+
cmd.AddEnvs("GIT_COMMITTER_NAME="+opts.Committer.Name, "GIT_COMMITTER_EMAIL="+opts.Committer.Email)
120120
}
121121
cmd.AddArguments("commit")
122122

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.4.2"
13+
const _VERSION = "0.4.3"
1414

1515
func Version() string {
1616
return _VERSION

0 commit comments

Comments
 (0)