Skip to content

Commit 5c3863c

Browse files
zeripathlunny
authored andcommitted
Restore functionality for early gits (#7775) (#8476)
* 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 * Apply suggestions from code review * Update modules/repofiles/temp_repo.go
1 parent 80b50af commit 5c3863c

12 files changed

+223
-66
lines changed

Diff for: integrations/git_helper_for_declarative_test.go

+31
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

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

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

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

79104
func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
80105
return func(t *testing.T) {
106+
oldGlobals := allowLFSFilters()
81107
assert.NoError(t, git.Clone(u.String(), dstLocalPath, git.CloneRepoOptions{}))
108+
git.GlobalCommandArgs = oldGlobals
82109
assert.True(t, com.IsExist(filepath.Join(dstLocalPath, "README.md")))
83110
}
84111
}
@@ -139,7 +166,9 @@ func doGitCreateBranch(dstPath, branch string) func(*testing.T) {
139166

140167
func doGitCheckoutBranch(dstPath string, args ...string) func(*testing.T) {
141168
return func(t *testing.T) {
169+
oldGlobals := allowLFSFilters()
142170
_, err := git.NewCommand(append([]string{"checkout"}, args...)...).RunInDir(dstPath)
171+
git.GlobalCommandArgs = oldGlobals
143172
assert.NoError(t, err)
144173
}
145174
}
@@ -153,7 +182,9 @@ func doGitMerge(dstPath string, args ...string) func(*testing.T) {
153182

154183
func doGitPull(dstPath string, args ...string) func(*testing.T) {
155184
return func(t *testing.T) {
185+
oldGlobals := allowLFSFilters()
156186
_, err := git.NewCommand(append([]string{"pull"}, args...)...).RunInDir(dstPath)
187+
git.GlobalCommandArgs = oldGlobals
157188
assert.NoError(t, err)
158189
}
159190
}

Diff for: integrations/git_test.go

+48-14
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+
Email: "user2@example.com",
155+
Name: "User Two",
156+
When: time.Now(),
157+
},
158+
Author: &git.Signature{
159+
Email: "user2@example.com",
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

Diff for: integrations/lfs_getobject_test.go

+5
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)

Diff for: models/repo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ func CleanUpMigrateInfo(repo *Repository) (*Repository, error) {
10631063
}
10641064
}
10651065

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

Diff for: models/repo_mirror.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (m *Mirror) FullAddress() string {
134134
func (m *Mirror) SaveAddress(addr string) error {
135135
repoPath := m.Repo.RepoPath()
136136
// Remove old origin
137-
_, err := git.NewCommand("remote", "remove", "origin").RunInDir(repoPath)
137+
_, err := git.NewCommand("remote", "rm", "origin").RunInDir(repoPath)
138138
if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
139139
return err
140140
}

Diff for: modules/git/repo_branch.go

+1-1
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

Diff for: modules/git/repo_tree.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
package git
77

88
import (
9+
"bytes"
910
"fmt"
1011
"os"
1112
"strings"
1213
"time"
1314

15+
"github.com/mcuadros/go-version"
1416
"gopkg.in/src-d/go-git.v4/plumbing"
1517
)
1618

@@ -63,6 +65,11 @@ type CommitTreeOpts struct {
6365

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

6875
// Because this may call hooks we should pass in the environment
@@ -80,20 +87,24 @@ func (repo *Repository) CommitTree(sig *Signature, tree *Tree, opts CommitTreeOp
8087
cmd.AddArguments("-p", parent)
8188
}
8289

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

8594
if opts.KeyID != "" {
8695
cmd.AddArguments(fmt.Sprintf("-S%s", opts.KeyID))
8796
}
8897

89-
if opts.NoGPGSign {
98+
if version.Compare(binVersion, "2.0.0", ">=") && opts.NoGPGSign {
9099
cmd.AddArguments("--no-gpg-sign")
91100
}
92101

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

95106
if err != nil {
96-
return SHA1{}, err
107+
return SHA1{}, concatenateError(err, stderr.String())
97108
}
98-
return NewIDFromString(strings.TrimSpace(res))
109+
return NewIDFromString(strings.TrimSpace(stdout.String()))
99110
}

Diff for: modules/process/manager.go

+14
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)