Skip to content

Commit d20dcc6

Browse files
committed
fix(conf): ignore git rev-parse error
1 parent 7921272 commit d20dcc6

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

internal/conf/conf.go

+23-19
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99
"strings"
10+
"sync"
1011

1112
"github.com/pkg/errors"
1213
"github.com/yeqown/gitlab-flow/internal/types"
@@ -103,32 +104,35 @@ func DefaultConfPath() string {
103104
return configDirectory
104105
}
105106

107+
var (
108+
_defaultCWD string
109+
_defaultCwdOnce sync.Once
110+
)
111+
106112
// DefaultCWD returns the working directory of current project, default cwd is from
107113
// git rev-parse --show-toplevel command, but if the command could not execute successfully,
108114
// `pwd` command will be used instead.
109-
func DefaultCWD() (cwd string) {
110-
w := bytes.NewBuffer(nil)
111-
if err := pkg.RunOutput("git rev-parse --show-toplevel", w); err != nil {
112-
log.
113-
WithFields(log.Fields{
114-
"error": err,
115-
"command": "git rev-parse --show-toplevel",
116-
}).
117-
Warn("conf.DefaultCWD failed")
118-
}
115+
func DefaultCWD() string {
116+
_defaultCwdOnce.Do(func() {
117+
w := bytes.NewBuffer(nil)
118+
if err := pkg.RunOutput("git rev-parse --show-toplevel", w); err != nil {
119+
log.Debug("pre-exec 'git rev-parse --show-toplevel' failed:")
120+
log.Debugf("%s\n", err)
121+
}
119122

120-
if s := w.String(); s != "" {
121-
cwd = s
122-
}
123+
if s := w.String(); s != "" {
124+
_defaultCWD = s
125+
}
123126

124-
if cwd == "" {
125-
cwd, _ = os.Getwd()
126-
}
127+
if _defaultCWD == "" {
128+
_defaultCWD, _ = os.Getwd()
129+
}
127130

128-
cwd = strings.Trim(cwd, "\n")
129-
cwd = strings.Trim(cwd, "\t")
131+
_defaultCWD = strings.Trim(_defaultCWD, "\n")
132+
_defaultCWD = strings.Trim(_defaultCWD, "\t")
133+
})
130134

131-
return cwd
135+
return _defaultCWD
132136
}
133137

134138
const (

0 commit comments

Comments
 (0)