|
7 | 7 | "os"
|
8 | 8 | "path/filepath"
|
9 | 9 | "strings"
|
| 10 | + "sync" |
10 | 11 |
|
11 | 12 | "github.com/pkg/errors"
|
12 | 13 | "github.com/yeqown/gitlab-flow/internal/types"
|
@@ -103,32 +104,35 @@ func DefaultConfPath() string {
|
103 | 104 | return configDirectory
|
104 | 105 | }
|
105 | 106 |
|
| 107 | +var ( |
| 108 | + _defaultCWD string |
| 109 | + _defaultCwdOnce sync.Once |
| 110 | +) |
| 111 | + |
106 | 112 | // DefaultCWD returns the working directory of current project, default cwd is from
|
107 | 113 | // git rev-parse --show-toplevel command, but if the command could not execute successfully,
|
108 | 114 | // `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 | + } |
119 | 122 |
|
120 |
| - if s := w.String(); s != "" { |
121 |
| - cwd = s |
122 |
| - } |
| 123 | + if s := w.String(); s != "" { |
| 124 | + _defaultCWD = s |
| 125 | + } |
123 | 126 |
|
124 |
| - if cwd == "" { |
125 |
| - cwd, _ = os.Getwd() |
126 |
| - } |
| 127 | + if _defaultCWD == "" { |
| 128 | + _defaultCWD, _ = os.Getwd() |
| 129 | + } |
127 | 130 |
|
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 | + }) |
130 | 134 |
|
131 |
| - return cwd |
| 135 | + return _defaultCWD |
132 | 136 | }
|
133 | 137 |
|
134 | 138 | const (
|
|
0 commit comments