git/git.go

92 строки
2.0 KiB
Go
Исходник Обычный вид История

2015-11-27 01:34:02 +03:00
// Copyright 2015 The Gogs Authors. All rights reserved.
// Copyright 2017 The Gitea Authors. All rights reserved.
2015-11-27 01:34:02 +03:00
// 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"
2015-12-09 19:41:48 +03:00
"time"
"github.com/mcuadros/go-version"
2015-11-27 01:34:02 +03:00
)
// Version return this package's current version
2015-12-10 04:45:25 +03:00
func Version() string {
return "0.4.2"
2015-12-10 04:45:25 +03:00
}
2015-11-27 01:34:02 +03:00
var (
// Debug enables verbose logging on everything.
2015-12-09 04:05:32 +03:00
// This should be false in case Gogs starts in SSH mode.
Debug = false
// Prefix the log prefix
Prefix = "[git-module] "
// GitVersionRequired is the minimum Git version required
GitVersionRequired = "1.7.2"
2015-11-27 01:34:02 +03:00
)
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
// BinVersion returns current Git version from shell.
2015-12-10 04:45:25 +03:00
func BinVersion() (string, error) {
2015-11-27 01:34:02 +03:00
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)
}
2015-12-15 02:55:16 +03:00
// Handle special case on Windows.
i := strings.Index(fields[2], "windows")
if i >= 1 {
gitVersion = fields[2][:i-1]
return gitVersion, nil
}
2015-11-27 01:34:02 +03:00
gitVersion = fields[2]
return gitVersion, nil
}
2015-11-27 08:24:02 +03:00
func init() {
gitVersion, err := BinVersion()
if err != nil {
panic(fmt.Sprintf("Git version missing: %v", err))
}
if version.Compare(gitVersion, GitVersionRequired, "<") {
panic(fmt.Sprintf("Git version not supported. Requires version > %v", GitVersionRequired))
}
2015-11-27 08:24:02 +03:00
}
2015-12-09 19:41:48 +03:00
// Fsck verifies the connectivity and validity of the objects in the database
func Fsck(repoPath string, timeout time.Duration, args ...string) error {
2016-01-28 19:07:19 +03:00
// Make sure timeout makes sense.
if timeout <= 0 {
2016-01-28 19:09:33 +03:00
timeout = -1
2016-01-28 19:07:19 +03:00
}
2015-12-09 19:41:48 +03:00
_, err := NewCommand("fsck").AddArguments(args...).RunInDirTimeout(timeout, repoPath)
return err
}