![Demo](https://gowalker.org/public/gogs_demo.gif) | ![Demo](https://gowalker.org/public/gogs_demo.gif) | ||||
##### Current version: 0.5.0 Beta | |||||
##### Current version: 0.5.2 Beta | |||||
### NOTICES | ### NOTICES | ||||
![Demo](https://gowalker.org/public/gogs_demo.gif) | ![Demo](https://gowalker.org/public/gogs_demo.gif) | ||||
##### 当前版本:0.5.1 Beta | |||||
##### 当前版本:0.5.2 Beta | |||||
## 开发目的 | ## 开发目的 | ||||
"github.com/gogits/gogs/modules/setting" | "github.com/gogits/gogs/modules/setting" | ||||
) | ) | ||||
const APP_VER = "0.5.1.0916 Beta" | |||||
const APP_VER = "0.5.2.0916 Beta" | |||||
func init() { | func init() { | ||||
runtime.GOMAXPROCS(runtime.NumCPU()) | runtime.GOMAXPROCS(runtime.NumCPU()) |
if err != nil { | if err != nil { | ||||
log.Fatal(4, "Fail to get Git version: %v", err) | log.Fatal(4, "Fail to get Git version: %v", err) | ||||
} | } | ||||
if ver.Major < 2 && ver.Minor < 8 { | |||||
log.Fatal(4, "Gogs requires Git version greater or equal to 1.8.0") | |||||
reqVer, err := git.ParseVersion("1.7.1") | |||||
if err != nil { | |||||
log.Fatal(4, "Fail to parse required Git version: %v", err) | |||||
} | |||||
if ver.Compare(reqVer) == -1 { | |||||
log.Fatal(4, "Gogs requires Git version greater or equal to 1.7.1") | |||||
} | } | ||||
// Check if server has basic git setting and set if not. | // Check if server has basic git setting and set if not. |
} | } | ||||
func (repo *Repository) commitsCount(id sha1) (int, error) { | func (repo *Repository) commitsCount(id sha1) (int, error) { | ||||
if gitVer.Compare(MustParseVersion("1.8.0")) == -1 { | |||||
stdout, stderr, err := com.ExecCmdDirBytes(repo.Path, "git", "log", "--pretty=format:''", id.String()) | |||||
if err != nil { | |||||
return 0, errors.New(string(stderr)) | |||||
} | |||||
return len(bytes.Split(stdout, []byte("\n"))), nil | |||||
} | |||||
stdout, stderr, err := com.ExecCmdDir(repo.Path, "git", "rev-list", "--count", id.String()) | stdout, stderr, err := com.ExecCmdDir(repo.Path, "git", "rev-list", "--count", id.String()) | ||||
if err != nil { | if err != nil { | ||||
return 0, errors.New(stderr) | return 0, errors.New(stderr) |
"github.com/Unknwon/com" | "github.com/Unknwon/com" | ||||
) | ) | ||||
var ( | |||||
// Cached Git version. | |||||
gitVer *Version | |||||
) | |||||
// Version represents version of Git. | // Version represents version of Git. | ||||
type Version struct { | type Version struct { | ||||
Major, Minor, Patch int | Major, Minor, Patch int | ||||
} | } | ||||
// GetVersion returns current Git version installed. | |||||
func GetVersion() (Version, error) { | |||||
stdout, stderr, err := com.ExecCmd("git", "version") | |||||
if err != nil { | |||||
return Version{}, errors.New(stderr) | |||||
} | |||||
infos := strings.Split(stdout, " ") | |||||
func ParseVersion(verStr string) (*Version, error) { | |||||
infos := strings.Split(verStr, ".") | |||||
if len(infos) < 3 { | if len(infos) < 3 { | ||||
return Version{}, errors.New("not enough output") | |||||
return nil, errors.New("incorrect version input") | |||||
} | } | ||||
v := Version{} | |||||
for i, s := range strings.Split(strings.TrimSpace(infos[2]), ".") { | |||||
v := &Version{} | |||||
for i, s := range infos { | |||||
switch i { | switch i { | ||||
case 0: | case 0: | ||||
v.Major, _ = com.StrTo(s).Int() | v.Major, _ = com.StrTo(s).Int() | ||||
} | } | ||||
return v, nil | return v, nil | ||||
} | } | ||||
func MustParseVersion(verStr string) *Version { | |||||
v, _ := ParseVersion(verStr) | |||||
return v | |||||
} | |||||
// Compare compares two versions, | |||||
// it returns 1 if original is greater, 1 if original is smaller, 0 if equal. | |||||
func (v *Version) Compare(that *Version) int { | |||||
if v.Major > that.Major { | |||||
return 1 | |||||
} else if v.Major < that.Major { | |||||
return -1 | |||||
} | |||||
if v.Minor > that.Minor { | |||||
return 1 | |||||
} else if v.Minor < that.Minor { | |||||
return -1 | |||||
} | |||||
if v.Patch > that.Patch { | |||||
return 1 | |||||
} else if v.Patch < that.Patch { | |||||
return -1 | |||||
} | |||||
return 0 | |||||
} | |||||
// GetVersion returns current Git version installed. | |||||
func GetVersion() (*Version, error) { | |||||
if gitVer != nil { | |||||
return gitVer, nil | |||||
} | |||||
stdout, stderr, err := com.ExecCmd("git", "version") | |||||
if err != nil { | |||||
return nil, errors.New(stderr) | |||||
} | |||||
infos := strings.Split(stdout, " ") | |||||
if len(infos) < 3 { | |||||
return nil, errors.New("not enough output") | |||||
} | |||||
gitVer, err = ParseVersion(infos[2]) | |||||
return gitVer, err | |||||
} |
0.5.1.0916 Beta | |||||
0.5.2.0916 Beta |