1
0
mirror of https://github.com/go-gitea/git.git synced 2026-02-05 15:45:40 +01:00

initial command

This commit is contained in:
Unknwon
2015-11-26 17:34:02 -05:00
commit 6c189876cb
5 changed files with 180 additions and 0 deletions

76
command.go Normal file
View File

@@ -0,0 +1,76 @@
// Copyright 2015 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package git
import (
"bytes"
"fmt"
"os/exec"
"strings"
)
// Command represents a command with its subcommands or arguments.
type Command struct {
name string
args []string
}
func (c *Command) String() string {
if len(c.args) == 0 {
return c.name
}
return fmt.Sprintf("%s %s", c.name, strings.Join(c.args, " "))
}
// NewCommand creates and returns a new Git Command based on given command and arguments.
func NewCommand(args ...string) *Command {
return &Command{
name: "git",
args: args,
}
}
// AddArguments adds new argument(s) to the command.
func (c *Command) AddArguments(args ...string) *Command {
c.args = append(c.args, args...)
return c
}
func (c *Command) run(dir string) (string, error) {
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
cmd := exec.Command(c.name, c.args...)
cmd.Dir = dir
cmd.Stdout = stdout
cmd.Stderr = stderr
if len(dir) == 0 {
log(c.String())
} else {
log("%s: %v", dir, c)
}
if err := cmd.Run(); err != nil {
return stdout.String(), concatenateError(err, stderr.String())
}
if stdout.Len() > 0 {
log("stdout:\n%s", stdout)
}
return stdout.String(), nil
}
// Run executes the command in defualt working directory
// and returns stdout and error (combined with stderr).
func (c *Command) Run() (string, error) {
return c.run("")
}
// RunInDir executes the command in given directory
// and returns stdout and error (combined with stderr).
func (c *Command) RunInDir(dir string) (string, error) {
return c.run(dir)
}

16
error.go Normal file
View File

@@ -0,0 +1,16 @@
// Copyright 2015 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package git
import (
"fmt"
)
func concatenateError(err error, stderr string) error {
if len(stderr) == 0 {
return err
}
return fmt.Errorf("%v - %s", err, stderr)
}

51
git.go Normal file
View File

@@ -0,0 +1,51 @@
// Copyright 2015 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package git
import (
"fmt"
"strings"
)
var (
// Debug enables verbose logging on everything.
Debug = true
Prefix = "[git-shell] "
)
func log(format string, args ...interface{}) {
if !Debug {
return
}
fmt.Print(Prefix)
if len(args) == 0 {
fmt.Println(format)
} else {
fmt.Printf(format+"\n", args...)
}
}
var gitVersion string
// Version returns current Git version from shell.
func Version() (string, error) {
if len(gitVersion) > 0 {
return gitVersion, nil
}
stdout, err := NewCommand("version").Run()
if err != nil {
return "", err
}
fields := strings.Fields(stdout)
if len(fields) < 3 {
return "", fmt.Errorf("not enough output: %s", stdout)
}
gitVersion = fields[2]
return gitVersion, nil
}

21
repo.go Normal file
View File

@@ -0,0 +1,21 @@
// Copyright 2015 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package git
import (
"os"
)
// InitRepository initializes a new Git repository in choice of bare or not.
func InitRepository(repoPath string, bare bool) error {
os.MkdirAll(repoPath, os.ModePerm)
cmd := NewCommand("init")
if bare {
cmd.AddArguments("--bare")
}
_, err := cmd.RunInDir(repoPath)
return err
}

16
signature.go Normal file
View File

@@ -0,0 +1,16 @@
// Copyright 2015 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package git
import (
"time"
)
// Signature represents the Author or Committer information.
type Signature struct {
Email string
Name string
When time.Time
}