Skip to content

Commit 09e29f4

Browse files
committed
try to fix integration test
1 parent 187a27f commit 09e29f4

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

cmd/hook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ func runHookPostReceive(c *cli.Context) error {
308308
ctx, cancel := installSignals()
309309
defer cancel()
310310

311+
setup("hooks/post-receive.log", c.Bool("debug"))
312+
311313
// First of all run update-server-info no matter what
312314
if _, _, err := git.NewCommand(ctx, "update-server-info").RunStdString(nil); err != nil {
313315
return fmt.Errorf("Failed to call 'git update-server-info': %v", err)
@@ -318,8 +320,6 @@ func runHookPostReceive(c *cli.Context) error {
318320
return nil
319321
}
320322

321-
setup("hooks/post-receive.log", c.Bool("debug"))
322-
323323
if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
324324
if setting.OnlyAllowPushIfGiteaEnvironmentSet {
325325
return fail(`Rejecting changes as Gitea environment not set.

models/migrations/migrations_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ func TestMain(m *testing.M) {
6666

6767
setting.SetCustomPathAndConf("", "", "")
6868
setting.LoadForTest()
69+
if err = git.InitOnceWithSync(context.Background()); err != nil {
70+
fmt.Printf("Unable to InitOnceWithSync: %v\n", err)
71+
os.Exit(1)
72+
}
6973
git.CheckLFSVersion()
7074
setting.InitDBConfig()
7175
setting.NewLogServices(true)

modules/git/git.go

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ package git
77

88
import (
99
"context"
10+
"errors"
1011
"fmt"
1112
"os"
1213
"os/exec"
13-
"path/filepath"
1414
"runtime"
1515
"strings"
1616
"sync"
@@ -30,9 +30,8 @@ var (
3030
// Could be updated to an absolute path while initialization
3131
GitExecutable = "git"
3232

33-
// DefaultContext is the default context to run git commands in
34-
// will be overwritten by InitXxx with HammerContext
35-
DefaultContext = context.Background()
33+
// DefaultContext is the default context to run git commands in, must be initialized by git.InitXxx
34+
DefaultContext context.Context
3635

3736
// SupportProcReceive version >= 2.29.0
3837
SupportProcReceive bool
@@ -125,39 +124,43 @@ func VersionInfo() string {
125124
return fmt.Sprintf(format, args...)
126125
}
127126

127+
func checkInit() error {
128+
if setting.RepoRootPath == "" {
129+
return errors.New("can not init Git's HomeDir (RepoRootPath is empty), the setting and git modules are not initialized correctly")
130+
}
131+
if DefaultContext != nil {
132+
log.Warn("git module has been initialized already, duplicate init should be fixed")
133+
}
134+
return nil
135+
}
136+
128137
// HomeDir is the home dir for git to store the global config file used by Gitea internally
129138
func HomeDir() string {
130139
if setting.RepoRootPath == "" {
131-
// TODO: now, some unit test code call the git module directly without initialization, which is incorrect.
132-
// at the moment, we just use a temp HomeDir to prevent from conflicting with user's git config
133-
// in the future, the git module should be initialized first before use.
134-
tmpHomeDir := filepath.Join(os.TempDir(), "gitea-temp-home")
135-
log.Error("Git's HomeDir is empty (RepoRootPath is empty), the git module is not initialized correctly, using a temp HomeDir (%s) temporarily", tmpHomeDir)
136-
return tmpHomeDir
137-
138-
// the Fatal will cause some CI failures (root reason is still unknown), need to be investigated more in the future
139-
// log.Fatal("Can not get Git's HomeDir (RepoRootPath is empty), the setting and git modules are not initialized correctly")
140+
// strict check, make sure the git module is initialized correctly.
141+
// attention: when the git module is called in gitea sub-command (serv/hook), the log module is not able to show messages to users.
142+
// for example: if there is gitea git hook code calling git.NewCommand before git.InitXxx, the integration test won't show the real failure reasons.
143+
log.Fatal("can not get Git's HomeDir (RepoRootPath is empty), the setting and git modules are not initialized correctly")
144+
return ""
140145
}
141146
return setting.RepoRootPath
142147
}
143148

144149
// InitSimple initializes git module with a very simple step, no config changes, no global command arguments.
145150
// This method doesn't change anything to filesystem. At the moment, it is only used by "git serv" sub-command, no data-race
151+
// However, in integration test, the sub-command function may be called in the current process, so the InitSimple would be called multiple times, too
146152
func InitSimple(ctx context.Context) error {
153+
if err := checkInit(); err != nil {
154+
return err
155+
}
156+
147157
DefaultContext = ctx
148158

149159
if setting.Git.Timeout.Default > 0 {
150160
defaultCommandExecutionTimeout = time.Duration(setting.Git.Timeout.Default) * time.Second
151161
}
152162

153-
if err := SetExecutablePath(setting.Git.Path); err != nil {
154-
return err
155-
}
156-
157-
// force cleanup args
158-
globalCommandArgs = []string{}
159-
160-
return nil
163+
return SetExecutablePath(setting.Git.Path)
161164
}
162165

163166
var initOnce sync.Once
@@ -166,6 +169,10 @@ var initOnce sync.Once
166169
// This method will update the global variables ONLY ONCE (just like git.CheckLFSVersion -- which is not ideal too),
167170
// otherwise there will be data-race problem at the moment.
168171
func InitOnceWithSync(ctx context.Context) (err error) {
172+
if err = checkInit(); err != nil {
173+
return err
174+
}
175+
169176
initOnce.Do(func() {
170177
err = InitSimple(ctx)
171178
if err != nil {

0 commit comments

Comments
 (0)