Skip to content

Commit

Permalink
Restore functionality for early gits (go-gitea#7775) (go-gitea#8476)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
zeripath authored and lunny committed Oct 13, 2019
1 parent 80b50af commit 5c3863c
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 66 deletions.
31 changes: 31 additions & 0 deletions integrations/git_helper_for_declarative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"testing"
"time"

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

err = ioutil.WriteFile(path.Join(tmpDir, "ssh"), []byte("#!/bin/bash\n"+
"ssh -o \"UserKnownHostsFile=/dev/null\" -o \"StrictHostKeyChecking=no\" -o \"IdentitiesOnly=yes\" -i \""+keyFile+"\" \"$@\""), 0700)
assert.NoError(t, err)

//Setup ssh wrapper
os.Setenv("GIT_SSH", path.Join(tmpDir, "ssh"))
os.Setenv("GIT_SSH_COMMAND",
"ssh -o \"UserKnownHostsFile=/dev/null\" -o \"StrictHostKeyChecking=no\" -o \"IdentitiesOnly=yes\" -i \""+keyFile+"\"")
os.Setenv("GIT_SSH_VARIANT", "ssh")
Expand All @@ -53,6 +60,24 @@ func createSSHUrl(gitPath string, u *url.URL) *url.URL {
return &u2
}

func allowLFSFilters() []string {
// Now here we should explicitly allow lfs filters to run
globalArgs := git.GlobalCommandArgs
filteredLFSGlobalArgs := make([]string, len(git.GlobalCommandArgs))
j := 0
for _, arg := range git.GlobalCommandArgs {
if strings.Contains(arg, "lfs") {
j--
} else {
filteredLFSGlobalArgs[j] = arg
j++
}
}
filteredLFSGlobalArgs = filteredLFSGlobalArgs[:j]
git.GlobalCommandArgs = filteredLFSGlobalArgs
return globalArgs
}

func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL)) {
prepareTestEnv(t, 1)
s := http.Server{
Expand All @@ -78,7 +103,9 @@ func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL)) {

func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
return func(t *testing.T) {
oldGlobals := allowLFSFilters()
assert.NoError(t, git.Clone(u.String(), dstLocalPath, git.CloneRepoOptions{}))
git.GlobalCommandArgs = oldGlobals
assert.True(t, com.IsExist(filepath.Join(dstLocalPath, "README.md")))
}
}
Expand Down Expand Up @@ -139,7 +166,9 @@ func doGitCreateBranch(dstPath, branch string) func(*testing.T) {

func doGitCheckoutBranch(dstPath string, args ...string) func(*testing.T) {
return func(t *testing.T) {
oldGlobals := allowLFSFilters()
_, err := git.NewCommand(append([]string{"checkout"}, args...)...).RunInDir(dstPath)
git.GlobalCommandArgs = oldGlobals
assert.NoError(t, err)
}
}
Expand All @@ -153,7 +182,9 @@ func doGitMerge(dstPath string, args ...string) func(*testing.T) {

func doGitPull(dstPath string, args ...string) func(*testing.T) {
return func(t *testing.T) {
oldGlobals := allowLFSFilters()
_, err := git.NewCommand(append([]string{"pull"}, args...)...).RunInDir(dstPath)
git.GlobalCommandArgs = oldGlobals
assert.NoError(t, err)
}
}
62 changes: 48 additions & 14 deletions integrations/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -135,13 +136,33 @@ func standardCommitAndPushTest(t *testing.T, dstPath string) (little, big string
func lfsCommitAndPushTest(t *testing.T, dstPath string) (littleLFS, bigLFS string) {
t.Run("LFS", func(t *testing.T) {
PrintCurrentTest(t)
setting.CheckLFSVersion()
if !setting.LFS.StartServer {
t.Skip()
return
}
prefix := "lfs-data-file-"
_, err := git.NewCommand("lfs").AddArguments("install").RunInDir(dstPath)
assert.NoError(t, err)
_, err = git.NewCommand("lfs").AddArguments("track", prefix+"*").RunInDir(dstPath)
assert.NoError(t, err)
err = git.AddChanges(dstPath, false, ".gitattributes")
assert.NoError(t, err)
oldGlobals := allowLFSFilters()
err = git.CommitChanges(dstPath, git.CommitChangesOptions{
Committer: &git.Signature{
Email: "[email protected]",
Name: "User Two",
When: time.Now(),
},
Author: &git.Signature{
Email: "[email protected]",
Name: "User Two",
When: time.Now(),
},
Message: fmt.Sprintf("Testing commit @ %v", time.Now()),
})
git.GlobalCommandArgs = oldGlobals

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

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

req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", littleLFS))
resp = session.MakeRequest(t, req, http.StatusOK)
assert.NotEqual(t, littleSize, resp.Body.Len())
assert.Contains(t, resp.Body.String(), models.LFSMetaFileIdentifier)
setting.CheckLFSVersion()
if setting.LFS.StartServer {
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", littleLFS))
resp = session.MakeRequest(t, req, http.StatusOK)
assert.NotEqual(t, littleSize, resp.Body.Len())
assert.Contains(t, resp.Body.String(), models.LFSMetaFileIdentifier)
}

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

req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", bigLFS))
resp = session.MakeRequest(t, req, http.StatusOK)
assert.NotEqual(t, bigSize, resp.Body.Len())
assert.Contains(t, resp.Body.String(), models.LFSMetaFileIdentifier)
if setting.LFS.StartServer {
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", bigLFS))
resp = session.MakeRequest(t, req, http.StatusOK)
assert.NotEqual(t, bigSize, resp.Body.Len())
assert.Contains(t, resp.Body.String(), models.LFSMetaFileIdentifier)
}
}
})
}
Expand All @@ -217,18 +243,23 @@ func mediaTest(t *testing.T, ctx *APITestContext, little, big, littleLFS, bigLFS
resp := session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
assert.Equal(t, littleSize, resp.Length)

req = NewRequest(t, "GET", path.Join("/", username, reponame, "/media/branch/master/", littleLFS))
resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
assert.Equal(t, littleSize, resp.Length)
setting.CheckLFSVersion()
if setting.LFS.StartServer {
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/media/branch/master/", littleLFS))
resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
assert.Equal(t, littleSize, resp.Length)
}

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

req = NewRequest(t, "GET", path.Join("/", username, reponame, "/media/branch/master/", bigLFS))
resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
assert.Equal(t, bigSize, resp.Length)
if setting.LFS.StartServer {
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/media/branch/master/", bigLFS))
resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
assert.Equal(t, bigSize, resp.Length)
}
}
})
}
Expand Down Expand Up @@ -274,6 +305,8 @@ func generateCommitWithNewData(size int, repoPath, email, fullName, prefix strin
}

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

Expand Down
5 changes: 5 additions & 0 deletions integrations/lfs_getobject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ func storeObjectInRepo(t *testing.T, repositoryID int64, content *[]byte) string

func doLfs(t *testing.T, content *[]byte, expectGzip bool) {
prepareTestEnv(t)
setting.CheckLFSVersion()
if !setting.LFS.StartServer {
t.Skip()
return
}
repo, err := models.GetRepositoryByOwnerAndName("user2", "repo1")
assert.NoError(t, err)
oid := storeObjectInRepo(t, repo.ID, content)
Expand Down
2 changes: 1 addition & 1 deletion models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ func CleanUpMigrateInfo(repo *Repository) (*Repository, error) {
}
}

_, err := git.NewCommand("remote", "remove", "origin").RunInDir(repoPath)
_, err := git.NewCommand("remote", "rm", "origin").RunInDir(repoPath)
if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
return repo, fmt.Errorf("CleanUpMigrateInfo: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion models/repo_mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (m *Mirror) FullAddress() string {
func (m *Mirror) SaveAddress(addr string) error {
repoPath := m.Repo.RepoPath()
// Remove old origin
_, err := git.NewCommand("remote", "remove", "origin").RunInDir(repoPath)
_, err := git.NewCommand("remote", "rm", "origin").RunInDir(repoPath)
if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion modules/git/repo_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (repo *Repository) AddRemote(name, url string, fetch bool) error {

// RemoveRemote removes a remote from repository.
func (repo *Repository) RemoveRemote(name string) error {
_, err := NewCommand("remote", "remove", name).RunInDir(repo.Path)
_, err := NewCommand("remote", "rm", name).RunInDir(repo.Path)
return err
}

Expand Down
21 changes: 16 additions & 5 deletions modules/git/repo_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
package git

import (
"bytes"
"fmt"
"os"
"strings"
"time"

"github.com/mcuadros/go-version"
"gopkg.in/src-d/go-git.v4/plumbing"
)

Expand Down Expand Up @@ -63,6 +65,11 @@ type CommitTreeOpts struct {

// CommitTree creates a commit from a given tree id for the user with provided message
func (repo *Repository) CommitTree(sig *Signature, tree *Tree, opts CommitTreeOpts) (SHA1, error) {
binVersion, err := BinVersion()
if err != nil {
return SHA1{}, err
}

commitTimeStr := time.Now().Format(time.RFC3339)

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

cmd.AddArguments("-m", opts.Message)
messageBytes := new(bytes.Buffer)
_, _ = messageBytes.WriteString(opts.Message)
_, _ = messageBytes.WriteString("\n")

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

if opts.NoGPGSign {
if version.Compare(binVersion, "2.0.0", ">=") && opts.NoGPGSign {
cmd.AddArguments("--no-gpg-sign")
}

res, err := cmd.RunInDirWithEnv(repo.Path, env)
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
err = cmd.RunInDirTimeoutEnvFullPipeline(env, -1, repo.Path, stdout, stderr, messageBytes)

if err != nil {
return SHA1{}, err
return SHA1{}, concatenateError(err, stderr.String())
}
return NewIDFromString(strings.TrimSpace(res))
return NewIDFromString(strings.TrimSpace(stdout.String()))
}
14 changes: 14 additions & 0 deletions modules/process/manager.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

Expand All @@ -9,6 +10,7 @@ import (
"context"
"errors"
"fmt"
"io"
"os/exec"
"sync"
"time"
Expand Down Expand Up @@ -93,6 +95,14 @@ func (pm *Manager) ExecDir(timeout time.Duration, dir, desc, cmdName string, arg
// Returns its complete stdout and stderr
// outputs and an error, if any (including timeout)
func (pm *Manager) ExecDirEnv(timeout time.Duration, dir, desc string, env []string, cmdName string, args ...string) (string, string, error) {
return pm.ExecDirEnvStdIn(timeout, dir, desc, env, nil, cmdName, args...)
}

// ExecDirEnvStdIn runs a command in given path and environment variables with provided stdIN, and waits for its completion
// up to the given timeout (or DefaultTimeout if -1 is given).
// Returns its complete stdout and stderr
// outputs and an error, if any (including timeout)
func (pm *Manager) ExecDirEnvStdIn(timeout time.Duration, dir, desc string, env []string, stdIn io.Reader, cmdName string, args ...string) (string, string, error) {
if timeout == -1 {
timeout = 60 * time.Second
}
Expand All @@ -108,6 +118,10 @@ func (pm *Manager) ExecDirEnv(timeout time.Duration, dir, desc string, env []str
cmd.Env = env
cmd.Stdout = stdOut
cmd.Stderr = stdErr
if stdIn != nil {
cmd.Stdin = stdIn
}

if err := cmd.Start(); err != nil {
return "", "", err
}
Expand Down
Loading

0 comments on commit 5c3863c

Please sign in to comment.