Skip to content

Commit 5e759b6

Browse files
zeripathlunny
authored andcommitted
Restore functionality for early gits (#7775)
* Change tests to make it possible to run TestGit with 1.7.2 * Make merge run on 1.7.2 * Fix tracking and staging branch name problem * Ensure that git 1.7.2 works on tests * ensure that there is no chance for conflicts * Fix-up missing merge issues * Final rm * Ensure LFS filters run on the tests * Do not sign commits from temp repo * Restore tracking fetch change * Apply suggestions from code review * Update modules/repofiles/temp_repo.go
1 parent ac3613b commit 5e759b6

File tree

12 files changed

+226
-66
lines changed

12 files changed

+226
-66
lines changed

integrations/git_helper_for_declarative_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import (
1212
"net/http"
1313
"net/url"
1414
"os"
15+
"path"
1516
"path/filepath"
17+
"strings"
1618
"testing"
1719
"time"
1820

@@ -37,7 +39,12 @@ func withKeyFile(t *testing.T, keyname string, callback func(string)) {
3739
err = ssh.GenKeyPair(keyFile)
3840
assert.NoError(t, err)
3941

42+
err = ioutil.WriteFile(path.Join(tmpDir, "ssh"), []byte("#!/bin/bash\n"+
43+
"ssh -o \"UserKnownHostsFile=/dev/null\" -o \"StrictHostKeyChecking=no\" -o \"IdentitiesOnly=yes\" -i \""+keyFile+"\" \"$@\""), 0700)
44+
assert.NoError(t, err)
45+
4046
//Setup ssh wrapper
47+
os.Setenv("GIT_SSH", path.Join(tmpDir, "ssh"))
4148
os.Setenv("GIT_SSH_COMMAND",
4249
"ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -i \""+keyFile+"\"")
4350
os.Setenv("GIT_SSH_VARIANT", "ssh")
@@ -54,6 +61,24 @@ func createSSHUrl(gitPath string, u *url.URL) *url.URL {
5461
return &u2
5562
}
5663

64+
func allowLFSFilters() []string {
65+
// Now here we should explicitly allow lfs filters to run
66+
globalArgs := git.GlobalCommandArgs
67+
filteredLFSGlobalArgs := make([]string, len(git.GlobalCommandArgs))
68+
j := 0
69+
for _, arg := range git.GlobalCommandArgs {
70+
if strings.Contains(arg, "lfs") {
71+
j--
72+
} else {
73+
filteredLFSGlobalArgs[j] = arg
74+
j++
75+
}
76+
}
77+
filteredLFSGlobalArgs = filteredLFSGlobalArgs[:j]
78+
git.GlobalCommandArgs = filteredLFSGlobalArgs
79+
return globalArgs
80+
}
81+
5782
func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL)) {
5883
prepareTestEnv(t, 1)
5984
s := http.Server{
@@ -79,7 +104,9 @@ func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL)) {
79104

80105
func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
81106
return func(t *testing.T) {
107+
oldGlobals := allowLFSFilters()
82108
assert.NoError(t, git.Clone(u.String(), dstLocalPath, git.CloneRepoOptions{}))
109+
git.GlobalCommandArgs = oldGlobals
83110
assert.True(t, com.IsExist(filepath.Join(dstLocalPath, "README.md")))
84111
}
85112
}
@@ -140,7 +167,9 @@ func doGitCreateBranch(dstPath, branch string) func(*testing.T) {
140167

141168
func doGitCheckoutBranch(dstPath string, args ...string) func(*testing.T) {
142169
return func(t *testing.T) {
170+
oldGlobals := allowLFSFilters()
143171
_, err := git.NewCommand(append([]string{"checkout"}, args...)...).RunInDir(dstPath)
172+
git.GlobalCommandArgs = oldGlobals
144173
assert.NoError(t, err)
145174
}
146175
}
@@ -154,7 +183,9 @@ func doGitMerge(dstPath string, args ...string) func(*testing.T) {
154183

155184
func doGitPull(dstPath string, args ...string) func(*testing.T) {
156185
return func(t *testing.T) {
186+
oldGlobals := allowLFSFilters()
157187
_, err := git.NewCommand(append([]string{"pull"}, args...)...).RunInDir(dstPath)
188+
git.GlobalCommandArgs = oldGlobals
158189
assert.NoError(t, err)
159190
}
160191
}

integrations/git_test.go

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"code.gitea.io/gitea/models"
2121
"code.gitea.io/gitea/modules/git"
22+
"code.gitea.io/gitea/modules/setting"
2223
api "code.gitea.io/gitea/modules/structs"
2324

2425
"github.com/stretchr/testify/assert"
@@ -135,13 +136,33 @@ func standardCommitAndPushTest(t *testing.T, dstPath string) (little, big string
135136
func lfsCommitAndPushTest(t *testing.T, dstPath string) (littleLFS, bigLFS string) {
136137
t.Run("LFS", func(t *testing.T) {
137138
PrintCurrentTest(t)
139+
setting.CheckLFSVersion()
140+
if !setting.LFS.StartServer {
141+
t.Skip()
142+
return
143+
}
138144
prefix := "lfs-data-file-"
139145
_, err := git.NewCommand("lfs").AddArguments("install").RunInDir(dstPath)
140146
assert.NoError(t, err)
141147
_, err = git.NewCommand("lfs").AddArguments("track", prefix+"*").RunInDir(dstPath)
142148
assert.NoError(t, err)
143149
err = git.AddChanges(dstPath, false, ".gitattributes")
144150
assert.NoError(t, err)
151+
oldGlobals := allowLFSFilters()
152+
err = git.CommitChanges(dstPath, git.CommitChangesOptions{
153+
Committer: &git.Signature{
154+
155+
Name: "User Two",
156+
When: time.Now(),
157+
},
158+
Author: &git.Signature{
159+
160+
Name: "User Two",
161+
When: time.Now(),
162+
},
163+
Message: fmt.Sprintf("Testing commit @ %v", time.Now()),
164+
})
165+
git.GlobalCommandArgs = oldGlobals
145166

146167
littleLFS, bigLFS = commitAndPushTest(t, dstPath, prefix)
147168

@@ -185,20 +206,25 @@ func rawTest(t *testing.T, ctx *APITestContext, little, big, littleLFS, bigLFS s
185206
resp := session.MakeRequest(t, req, http.StatusOK)
186207
assert.Equal(t, littleSize, resp.Body.Len())
187208

188-
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", littleLFS))
189-
resp = session.MakeRequest(t, req, http.StatusOK)
190-
assert.NotEqual(t, littleSize, resp.Body.Len())
191-
assert.Contains(t, resp.Body.String(), models.LFSMetaFileIdentifier)
209+
setting.CheckLFSVersion()
210+
if setting.LFS.StartServer {
211+
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", littleLFS))
212+
resp = session.MakeRequest(t, req, http.StatusOK)
213+
assert.NotEqual(t, littleSize, resp.Body.Len())
214+
assert.Contains(t, resp.Body.String(), models.LFSMetaFileIdentifier)
215+
}
192216

193217
if !testing.Short() {
194218
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", big))
195219
resp = session.MakeRequest(t, req, http.StatusOK)
196220
assert.Equal(t, bigSize, resp.Body.Len())
197221

198-
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", bigLFS))
199-
resp = session.MakeRequest(t, req, http.StatusOK)
200-
assert.NotEqual(t, bigSize, resp.Body.Len())
201-
assert.Contains(t, resp.Body.String(), models.LFSMetaFileIdentifier)
222+
if setting.LFS.StartServer {
223+
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", bigLFS))
224+
resp = session.MakeRequest(t, req, http.StatusOK)
225+
assert.NotEqual(t, bigSize, resp.Body.Len())
226+
assert.Contains(t, resp.Body.String(), models.LFSMetaFileIdentifier)
227+
}
202228
}
203229
})
204230
}
@@ -217,18 +243,23 @@ func mediaTest(t *testing.T, ctx *APITestContext, little, big, littleLFS, bigLFS
217243
resp := session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
218244
assert.Equal(t, littleSize, resp.Length)
219245

220-
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/media/branch/master/", littleLFS))
221-
resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
222-
assert.Equal(t, littleSize, resp.Length)
246+
setting.CheckLFSVersion()
247+
if setting.LFS.StartServer {
248+
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/media/branch/master/", littleLFS))
249+
resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
250+
assert.Equal(t, littleSize, resp.Length)
251+
}
223252

224253
if !testing.Short() {
225254
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/media/branch/master/", big))
226255
resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
227256
assert.Equal(t, bigSize, resp.Length)
228257

229-
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/media/branch/master/", bigLFS))
230-
resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
231-
assert.Equal(t, bigSize, resp.Length)
258+
if setting.LFS.StartServer {
259+
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/media/branch/master/", bigLFS))
260+
resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
261+
assert.Equal(t, bigSize, resp.Length)
262+
}
232263
}
233264
})
234265
}
@@ -274,6 +305,8 @@ func generateCommitWithNewData(size int, repoPath, email, fullName, prefix strin
274305
}
275306

276307
//Commit
308+
// Now here we should explicitly allow lfs filters to run
309+
oldGlobals := allowLFSFilters()
277310
err = git.AddChanges(repoPath, false, filepath.Base(tmpFile.Name()))
278311
if err != nil {
279312
return "", err
@@ -291,6 +324,7 @@ func generateCommitWithNewData(size int, repoPath, email, fullName, prefix strin
291324
},
292325
Message: fmt.Sprintf("Testing commit @ %v", time.Now()),
293326
})
327+
git.GlobalCommandArgs = oldGlobals
294328
return filepath.Base(tmpFile.Name()), err
295329
}
296330

integrations/lfs_getobject_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ func storeObjectInRepo(t *testing.T, repositoryID int64, content *[]byte) string
5858

5959
func doLfs(t *testing.T, content *[]byte, expectGzip bool) {
6060
prepareTestEnv(t)
61+
setting.CheckLFSVersion()
62+
if !setting.LFS.StartServer {
63+
t.Skip()
64+
return
65+
}
6166
repo, err := models.GetRepositoryByOwnerAndName("user2", "repo1")
6267
assert.NoError(t, err)
6368
oid := storeObjectInRepo(t, repo.ID, content)

models/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ func CleanUpMigrateInfo(repo *Repository) (*Repository, error) {
10931093
}
10941094
}
10951095

1096-
_, err := git.NewCommand("remote", "remove", "origin").RunInDir(repoPath)
1096+
_, err := git.NewCommand("remote", "rm", "origin").RunInDir(repoPath)
10971097
if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
10981098
return repo, fmt.Errorf("CleanUpMigrateInfo: %v", err)
10991099
}

modules/git/repo_branch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func (repo *Repository) AddRemote(name, url string, fetch bool) error {
165165

166166
// RemoveRemote removes a remote from repository.
167167
func (repo *Repository) RemoveRemote(name string) error {
168-
_, err := NewCommand("remote", "remove", name).RunInDir(repo.Path)
168+
_, err := NewCommand("remote", "rm", name).RunInDir(repo.Path)
169169
return err
170170
}
171171

modules/git/repo_tree.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
package git
77

88
import (
9+
"bytes"
910
"fmt"
1011
"os"
1112
"strings"
1213
"time"
14+
15+
"github.com/mcuadros/go-version"
1316
)
1417

1518
func (repo *Repository) getTree(id SHA1) (*Tree, error) {
@@ -61,6 +64,11 @@ type CommitTreeOpts struct {
6164

6265
// CommitTree creates a commit from a given tree id for the user with provided message
6366
func (repo *Repository) CommitTree(sig *Signature, tree *Tree, opts CommitTreeOpts) (SHA1, error) {
67+
binVersion, err := BinVersion()
68+
if err != nil {
69+
return SHA1{}, err
70+
}
71+
6472
commitTimeStr := time.Now().Format(time.RFC3339)
6573

6674
// Because this may call hooks we should pass in the environment
@@ -78,20 +86,24 @@ func (repo *Repository) CommitTree(sig *Signature, tree *Tree, opts CommitTreeOp
7886
cmd.AddArguments("-p", parent)
7987
}
8088

81-
cmd.AddArguments("-m", opts.Message)
89+
messageBytes := new(bytes.Buffer)
90+
_, _ = messageBytes.WriteString(opts.Message)
91+
_, _ = messageBytes.WriteString("\n")
8292

8393
if opts.KeyID != "" {
8494
cmd.AddArguments(fmt.Sprintf("-S%s", opts.KeyID))
8595
}
8696

87-
if opts.NoGPGSign {
97+
if version.Compare(binVersion, "2.0.0", ">=") && opts.NoGPGSign {
8898
cmd.AddArguments("--no-gpg-sign")
8999
}
90100

91-
res, err := cmd.RunInDirWithEnv(repo.Path, env)
101+
stdout := new(bytes.Buffer)
102+
stderr := new(bytes.Buffer)
103+
err = cmd.RunInDirTimeoutEnvFullPipeline(env, -1, repo.Path, stdout, stderr, messageBytes)
92104

93105
if err != nil {
94-
return SHA1{}, err
106+
return SHA1{}, concatenateError(err, stderr.String())
95107
}
96-
return NewIDFromString(strings.TrimSpace(res))
108+
return NewIDFromString(strings.TrimSpace(stdout.String()))
97109
}

modules/process/manager.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2019 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

@@ -9,6 +10,7 @@ import (
910
"context"
1011
"errors"
1112
"fmt"
13+
"io"
1214
"os/exec"
1315
"sync"
1416
"time"
@@ -93,6 +95,14 @@ func (pm *Manager) ExecDir(timeout time.Duration, dir, desc, cmdName string, arg
9395
// Returns its complete stdout and stderr
9496
// outputs and an error, if any (including timeout)
9597
func (pm *Manager) ExecDirEnv(timeout time.Duration, dir, desc string, env []string, cmdName string, args ...string) (string, string, error) {
98+
return pm.ExecDirEnvStdIn(timeout, dir, desc, env, nil, cmdName, args...)
99+
}
100+
101+
// ExecDirEnvStdIn runs a command in given path and environment variables with provided stdIN, and waits for its completion
102+
// up to the given timeout (or DefaultTimeout if -1 is given).
103+
// Returns its complete stdout and stderr
104+
// outputs and an error, if any (including timeout)
105+
func (pm *Manager) ExecDirEnvStdIn(timeout time.Duration, dir, desc string, env []string, stdIn io.Reader, cmdName string, args ...string) (string, string, error) {
96106
if timeout == -1 {
97107
timeout = 60 * time.Second
98108
}
@@ -108,6 +118,10 @@ func (pm *Manager) ExecDirEnv(timeout time.Duration, dir, desc string, env []str
108118
cmd.Env = env
109119
cmd.Stdout = stdOut
110120
cmd.Stderr = stdErr
121+
if stdIn != nil {
122+
cmd.Stdin = stdIn
123+
}
124+
111125
if err := cmd.Start(); err != nil {
112126
return "", "", err
113127
}

0 commit comments

Comments
 (0)