improve log and clone repo command

This commit is contained in:
Unknwon
2015-12-08 20:05:32 -05:00
parent de77627290
commit 05d41d51a1
3 changed files with 44 additions and 10 deletions
+11 -1
View File
@@ -122,8 +122,18 @@ func (c *Command) RunInDir(dir string) (string, error) {
return string(stdout), nil
}
// RunTimeout executes the command in defualt working directory with given timeout,
// and returns stdout in string and error (combined with stderr).
func (c *Command) RunTimeout(timeout time.Duration) (string, error) {
stdout, err := c.RunInDirTimeout(timeout, "")
if err != nil {
return "", err
}
return string(stdout), nil
}
// Run executes the command in defualt working directory
// and returns stdout in string and error (combined with stderr).
func (c *Command) Run() (string, error) {
return c.RunInDir("")
return c.RunTimeout(-1)
}
+2 -4
View File
@@ -11,14 +11,12 @@ import (
var (
// Debug enables verbose logging on everything.
Debug = true
// This should be false in case Gogs starts in SSH mode.
Debug = false
Prefix = "[git-shell] "
)
func log(format string, args ...interface{}) {
// FIXME: need a better way handle log, such as write to file.
return
if !Debug {
return
}
+31 -5
View File
@@ -11,6 +11,7 @@ import (
"os"
"path"
"path/filepath"
"time"
)
// Repository represents a Git repository.
@@ -65,12 +66,37 @@ func OpenRepository(repoPath string) (*Repository, error) {
return &Repository{Path: repoPath}, nil
}
// Clone clones original repository to target path.
func Clone(from, to string) error {
toDir := path.Dir(to)
os.MkdirAll(toDir, os.ModePerm)
type CloneRepoOptions struct {
Mirror bool
Bare bool
Quiet bool
Timeout time.Duration
}
_, err := NewCommand("clone", from, to).Run()
// Clone clones original repository to target path.
func Clone(from, to string, opts CloneRepoOptions) (err error) {
toDir := path.Dir(to)
if err = os.MkdirAll(toDir, os.ModePerm); err != nil {
return err
}
cmd := NewCommand("clone")
if opts.Mirror {
cmd.AddArguments("--mirror")
}
if opts.Bare {
cmd.AddArguments("--bare")
}
if opts.Quiet {
cmd.AddArguments("--quiet")
}
cmd.AddArguments(from, to)
if opts.Timeout <= 0 {
opts.Timeout = -1
}
_, err = cmd.RunTimeout(opts.Timeout)
return err
}