Skip to content

Commit

Permalink
Ensure that git 1.7.2 works on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zeripath committed Aug 14, 2019
1 parent d6841c5 commit f501939
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 31 deletions.
3 changes: 3 additions & 0 deletions integrations/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ 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
Expand Down Expand Up @@ -190,6 +191,7 @@ 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())

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)
Expand Down Expand Up @@ -226,6 +228,7 @@ func mediaTest(t *testing.T, ctx *APITestContext, little, big, littleLFS, bigLFS
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)
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 @@ -1054,7 +1054,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
22 changes: 17 additions & 5 deletions modules/git/repo_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
package git

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

"github.com/mcuadros/go-version"
)

func (repo *Repository) getTree(id SHA1) (*Tree, error) {
Expand Down Expand Up @@ -61,6 +64,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 @@ -78,20 +86,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
14 changes: 9 additions & 5 deletions modules/pull/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,21 @@ func Merge(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repositor
return fmt.Errorf("Writing sparse-checkout file to %s: %v", sparseCheckoutListPath, err)
}

gitConfigCommand := func() *git.Command {
gitConfigCommand := func() func() *git.Command {
binVersion, err := git.BinVersion()
if err != nil {
log.Fatal("Error retrieving git version: %v", err)
}

if version.Compare(binVersion, "1.9.2", ">=") {
return git.NewCommand("config", "--local")
if version.Compare(binVersion, "1.8.0", ">=") {
return func() *git.Command {
return git.NewCommand("config", "--local")
}
}
return git.NewCommand("config")
}
return func() *git.Command {
return git.NewCommand("config")
}
}()

// Switch off LFS process (set required, clean and smudge here also)
if err := gitConfigCommand().AddArguments("filter.lfs.process", "").RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
Expand Down
25 changes: 21 additions & 4 deletions modules/repofiles/temp_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/process"
"code.gitea.io/gitea/modules/setting"
"github.com/mcuadros/go-version"
)

// TemporaryUploadRepository is a type to wrap our upload repositories as a shallow clone
Expand Down Expand Up @@ -263,11 +264,15 @@ func (t *TemporaryUploadRepository) CommitTree(author, committer *models.User, t
"GIT_COMMITTER_EMAIL="+committerSig.Email,
"GIT_COMMITTER_DATE="+commitTimeStr,
)
commitHash, stderr, err := process.GetManager().ExecDirEnv(5*time.Minute,
messageBytes := new(bytes.Buffer)
_, _ = messageBytes.WriteString(message)
_, _ = messageBytes.WriteString("\n")
commitHash, stderr, err := process.GetManager().ExecDirEnvStdIn(5*time.Minute,
t.basePath,
fmt.Sprintf("commitTree (git commit-tree): %s", t.basePath),
env,
git.GitExecutable, "commit-tree", treeHash, "-p", "HEAD", "-m", message)
messageBytes,
git.GitExecutable, "commit-tree", treeHash, "-p", "HEAD")
if err != nil {
return "", fmt.Errorf("git commit-tree: %s", stderr)
}
Expand Down Expand Up @@ -327,14 +332,26 @@ func (t *TemporaryUploadRepository) DiffIndex() (diff *models.Diff, err error) {

// CheckAttribute checks the given attribute of the provided files
func (t *TemporaryUploadRepository) CheckAttribute(attribute string, args ...string) (map[string]map[string]string, error) {
binVersion, err := git.BinVersion()
if err != nil {
log.Fatal("Error retrieving git version: %v", err)
}

stdOut := new(bytes.Buffer)
stdErr := new(bytes.Buffer)

timeout := 5 * time.Minute
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

cmdArgs := []string{"check-attr", "-z", attribute, "--cached", "--"}
cmdArgs := []string{"check-attr", "-z", attribute}

// git check-attr --cached first appears in git 1.8.0
if version.Compare(binVersion, "1.8.0", ">=") {
cmdArgs = append(cmdArgs, "--cached")
}
cmdArgs = append(cmdArgs, "--")

for _, arg := range args {
if arg != "" {
cmdArgs = append(cmdArgs, arg)
Expand All @@ -352,7 +369,7 @@ func (t *TemporaryUploadRepository) CheckAttribute(attribute string, args ...str
}

pid := process.GetManager().Add(desc, cmd)
err := cmd.Wait()
err = cmd.Wait()
process.GetManager().Remove(pid)

if err != nil {
Expand Down
25 changes: 13 additions & 12 deletions modules/repofiles/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,6 @@ func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *Up
}
}

// Check there is no way this can return multiple infos
filename2attribute2info, err := t.CheckAttribute("filter", treePath)
if err != nil {
return nil, err
}

content := opts.Content
if bom {
content = string(base.UTF8BOM) + content
Expand All @@ -341,16 +335,23 @@ func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *Up
opts.Content = content
var lfsMetaObject *models.LFSMetaObject

if setting.LFS.StartServer && filename2attribute2info[treePath] != nil && filename2attribute2info[treePath]["filter"] == "lfs" {
// OK so we are supposed to LFS this data!
oid, err := models.GenerateLFSOid(strings.NewReader(opts.Content))
if setting.LFS.StartServer {
// Check there is no way this can return multiple infos
filename2attribute2info, err := t.CheckAttribute("filter", treePath)
if err != nil {
return nil, err
}
lfsMetaObject = &models.LFSMetaObject{Oid: oid, Size: int64(len(opts.Content)), RepositoryID: repo.ID}
content = lfsMetaObject.Pointer()
}

if filename2attribute2info[treePath] != nil && filename2attribute2info[treePath]["filter"] == "lfs" {
// OK so we are supposed to LFS this data!
oid, err := models.GenerateLFSOid(strings.NewReader(opts.Content))
if err != nil {
return nil, err
}
lfsMetaObject = &models.LFSMetaObject{Oid: oid, Size: int64(len(opts.Content)), RepositoryID: repo.ID}
content = lfsMetaObject.Pointer()
}
}
// Add the object to the database
objectHash, err := t.HashObject(strings.NewReader(content))
if err != nil {
Expand Down
11 changes: 7 additions & 4 deletions modules/repofiles/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@ func UploadRepoFiles(repo *models.Repository, doer *models.User, opts *UploadRep
infos[i] = uploadInfo{upload: upload}
}

filename2attribute2info, err := t.CheckAttribute("filter", names...)
if err != nil {
return err
var filename2attribute2info map[string]map[string]string
if setting.LFS.StartServer {
filename2attribute2info, err = t.CheckAttribute("filter", names...)
if err != nil {
return err
}
}

// Copy uploaded files into repository.
Expand All @@ -88,7 +91,7 @@ func UploadRepoFiles(repo *models.Repository, doer *models.User, opts *UploadRep
defer file.Close()

var objectHash string
if filename2attribute2info[uploadInfo.upload.Name] != nil && filename2attribute2info[uploadInfo.upload.Name]["filter"] == "lfs" {
if setting.LFS.StartServer && filename2attribute2info[uploadInfo.upload.Name] != nil && filename2attribute2info[uploadInfo.upload.Name]["filter"] == "lfs" {
// Handle LFS
// FIXME: Inefficient! this should probably happen in models.Upload
oid, err := models.GenerateLFSOid(file)
Expand Down

0 comments on commit f501939

Please sign in to comment.