diff --git a/command.go b/command.go index 18e4021..0b2fdcb 100644 --- a/command.go +++ b/command.go @@ -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) } diff --git a/git.go b/git.go index 446e110..264f27d 100644 --- a/git.go +++ b/git.go @@ -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 } diff --git a/repo.go b/repo.go index 7dd6a0f..0f51fef 100644 --- a/repo.go +++ b/repo.go @@ -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 }