@@ -8,10 +8,12 @@ package git
8
8
import (
9
9
"bytes"
10
10
"context"
11
+ "errors"
11
12
"fmt"
12
13
"io"
13
14
"os"
14
15
"os/exec"
16
+ "path/filepath"
15
17
"strings"
16
18
"time"
17
19
"unsafe"
@@ -148,13 +150,21 @@ func (c *Command) Run(opts *RunOpts) error {
148
150
cmd .Env = opts .Env
149
151
}
150
152
153
+ if GlobalConfigFile == "" {
154
+ // TODO: now, some unit test code call the git module directly without initialization, which is incorrect.
155
+ // at the moment, we just use a temp gitconfig to prevent from conflicting with user's gitconfig
156
+ // in future, the git module should be initialized first before use.
157
+ GlobalConfigFile = filepath .Join (os .TempDir (), "/gitea-temp-gitconfig" )
158
+ log .Warn ("GlobalConfigFile is empty, the git module is not initialized correctly, using a temp gitconfig (%s) temporarily" , GlobalConfigFile )
159
+ }
160
+
151
161
cmd .Env = append (
152
162
cmd .Env ,
153
163
fmt .Sprintf ("LC_ALL=%s" , DefaultLocale ),
154
- // avoid prompting for credentials interactively, supported since git v2.3
155
- "GIT_TERMINAL_PROMPT=0" ,
156
- // ignore replace references ( https://git-scm.com/docs/git-replace)
157
- "GIT_NO_REPLACE_OBJECTS=1" ,
164
+ "GIT_TERMINAL_PROMPT=0" , // avoid prompting for credentials interactively, supported since git v2.3
165
+ "GIT_NO_REPLACE_OBJECTS=1" , // ignore replace references (https://git-scm.com/docs/git-replace)
166
+ "GIT_CONFIG_NOSYSTEM=1" , // https://git-scm.com/docs/git-config
167
+ "GIT_CONFIG_GLOBAL=" + GlobalConfigFile , // make Gitea use internal git config only, to prevent conflicts with user's git config
158
168
)
159
169
160
170
cmd .Dir = opts .Dir
@@ -183,7 +193,9 @@ func (c *Command) Run(opts *RunOpts) error {
183
193
184
194
type RunStdError interface {
185
195
error
196
+ Unwrap () error
186
197
Stderr () string
198
+ IsExitCode (code int ) bool
187
199
}
188
200
189
201
type runStdError struct {
@@ -208,6 +220,14 @@ func (r *runStdError) Stderr() string {
208
220
return r .stderr
209
221
}
210
222
223
+ func (r * runStdError ) IsExitCode (code int ) bool {
224
+ var exitError * exec.ExitError
225
+ if errors .As (r .err , & exitError ) {
226
+ return exitError .ExitCode () == code
227
+ }
228
+ return false
229
+ }
230
+
211
231
func bytesToString (b []byte ) string {
212
232
return * (* string )(unsafe .Pointer (& b )) // that's what Golang's strings.Builder.String() does (go/src/strings/builder.go)
213
233
}
0 commit comments