Fix race when running commands with timeout (#1465)

Update vendored module code.gitea.io/git
This commit is contained in:
Mura Li 2017-04-08 10:23:39 +08:00 committed by Lunny Xiao
parent 5c0bee9b20
commit edbb9eefd6
4 changed files with 73 additions and 25 deletions

27
vendor/code.gitea.io/git/command.go generated vendored
View file

@ -6,6 +6,7 @@ package git
import ( import (
"bytes" "bytes"
"context"
"fmt" "fmt"
"io" "io"
"os/exec" "os/exec"
@ -58,7 +59,10 @@ func (c *Command) RunInDirTimeoutPipeline(timeout time.Duration, dir string, std
log("%s: %v", dir, c) log("%s: %v", dir, c)
} }
cmd := exec.Command(c.name, c.args...) ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
cmd := exec.CommandContext(ctx, c.name, c.args...)
cmd.Dir = dir cmd.Dir = dir
cmd.Stdout = stdout cmd.Stdout = stdout
cmd.Stderr = stderr cmd.Stderr = stderr
@ -66,26 +70,7 @@ func (c *Command) RunInDirTimeoutPipeline(timeout time.Duration, dir string, std
return err return err
} }
done := make(chan error) return cmd.Wait()
go func() {
done <- cmd.Wait()
}()
var err error
select {
case <-time.After(timeout):
if cmd.Process != nil && cmd.ProcessState != nil && !cmd.ProcessState.Exited() {
if err := cmd.Process.Kill(); err != nil {
return fmt.Errorf("fail to kill process: %v", err)
}
}
<-done
return ErrExecTimeout{timeout}
case err = <-done:
}
return err
} }
// RunInDirTimeout executes the command in given directory with given timeout, // RunInDirTimeout executes the command in given directory with given timeout,

63
vendor/code.gitea.io/git/repo.go generated vendored
View file

@ -11,7 +11,10 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"strings"
"time" "time"
"github.com/Unknwon/com"
) )
// Repository represents a Git repository. // Repository represents a Git repository.
@ -198,3 +201,63 @@ func MoveFile(repoPath, oldTreeName, newTreeName string) error {
_, err := NewCommand("mv").AddArguments(oldTreeName, newTreeName).RunInDir(repoPath) _, err := NewCommand("mv").AddArguments(oldTreeName, newTreeName).RunInDir(repoPath)
return err return err
} }
// CountObject represents repository count objects report
type CountObject struct {
Count int64
Size int64
InPack int64
Packs int64
SizePack int64
PrunePack int64
Garbage int64
SizeGarbage int64
}
const (
statCount = "count: "
statSize = "size: "
statInpack = "in-pack: "
statPacks = "packs: "
statSizePack = "size-pack: "
statPrunePackage = "prune-package: "
statGarbage = "garbage: "
statSizeGarbage = "size-garbage: "
)
// GetRepoSize returns disk consumption for repo in path
func GetRepoSize(repoPath string) (*CountObject, error) {
cmd := NewCommand("count-objects", "-v")
stdout, err := cmd.RunInDir(repoPath)
if err != nil {
return nil, err
}
return parseSize(stdout), nil
}
// parseSize parses the output from count-objects and return a CountObject
func parseSize(objects string) *CountObject {
repoSize := new(CountObject)
for _, line := range strings.Split(objects, "\n") {
switch {
case strings.HasPrefix(line, statCount):
repoSize.Count = com.StrTo(line[7:]).MustInt64()
case strings.HasPrefix(line, statSize):
repoSize.Size = com.StrTo(line[6:]).MustInt64() * 1024
case strings.HasPrefix(line, statInpack):
repoSize.InPack = com.StrTo(line[9:]).MustInt64()
case strings.HasPrefix(line, statPacks):
repoSize.Packs = com.StrTo(line[7:]).MustInt64()
case strings.HasPrefix(line, statSizePack):
repoSize.SizePack = com.StrTo(line[11:]).MustInt64() * 1024
case strings.HasPrefix(line, statPrunePackage):
repoSize.PrunePack = com.StrTo(line[16:]).MustInt64()
case strings.HasPrefix(line, statGarbage):
repoSize.Garbage = com.StrTo(line[9:]).MustInt64()
case strings.HasPrefix(line, statSizeGarbage):
repoSize.SizeGarbage = com.StrTo(line[14:]).MustInt64() * 1024
}
}
return repoSize
}

View file

@ -229,7 +229,7 @@ func (repo *Repository) FileCommitsCount(revision, file string) (int64, error) {
// CommitsByFileAndRange return the commits accroding revison file and the page // CommitsByFileAndRange return the commits accroding revison file and the page
func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (*list.List, error) { func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (*list.List, error) {
stdout, err := NewCommand("log", revision, "--skip="+strconv.Itoa((page-1)*50), stdout, err := NewCommand("log", revision, "--follow", "--skip="+strconv.Itoa((page-1)*50),
"--max-count="+strconv.Itoa(CommitsRangeSize), prettyLogFormat, "--", file).RunInDirBytes(repo.Path) "--max-count="+strconv.Itoa(CommitsRangeSize), prettyLogFormat, "--", file).RunInDirBytes(repo.Path)
if err != nil { if err != nil {
return nil, err return nil, err

6
vendor/vendor.json vendored
View file

@ -3,10 +3,10 @@
"ignore": "test", "ignore": "test",
"package": [ "package": [
{ {
"checksumSHA1": "bKoCvndU5ZVC5vqtwYjuU3YPJ6k=", "checksumSHA1": "vPnpECwoEpT/TTJn8CINm2cxV8s=",
"path": "code.gitea.io/git", "path": "code.gitea.io/git",
"revision": "337468881d5961d36de8e950a607d6033e73dcf0", "revision": "135704d70ee8dddec363e80f3235092493fea2c2",
"revisionTime": "2017-03-13T15:07:03Z" "revisionTime": "2017-04-07T07:44:04Z"
}, },
{ {
"checksumSHA1": "32qRX47gRmdBW4l4hCKGRZbuIJk=", "checksumSHA1": "32qRX47gRmdBW4l4hCKGRZbuIJk=",