-
Notifications
You must be signed in to change notification settings - Fork 127
/
git.go
75 lines (62 loc) · 1.62 KB
/
git.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// 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"
"io"
"strings"
"sync"
)
var (
// logOutput is the writer to write logs. When not set, no log will be produced.
logOutput io.Writer
// logPrefix is the prefix prepend to each log entry.
logPrefix = "[git-module] "
)
// SetOutput sets the output writer for logs.
func SetOutput(output io.Writer) {
logOutput = output
}
// SetPrefix sets the prefix to be prepended to each log entry.
func SetPrefix(prefix string) {
logPrefix = prefix
}
func log(format string, args ...interface{}) {
if logOutput == nil {
return
}
_, _ = fmt.Fprint(logOutput, logPrefix)
_, _ = fmt.Fprintf(logOutput, format, args...)
_, _ = fmt.Fprintln(logOutput)
}
var (
// gitVersion stores the Git binary version.
// NOTE: To check Git version should call BinVersion not this global variable.
gitVersion string
gitVersionOnce sync.Once
gitVersionErr error
)
// BinVersion returns current Git binary version that is used by this module.
func BinVersion() (string, error) {
gitVersionOnce.Do(func() {
var stdout []byte
stdout, gitVersionErr = NewCommand("version").Run()
if gitVersionErr != nil {
return
}
fields := strings.Fields(string(stdout))
if len(fields) < 3 {
gitVersionErr = fmt.Errorf("not enough output: %s", stdout)
return
}
// Handle special case on Windows.
i := strings.Index(fields[2], "windows")
if i >= 1 {
gitVersion = fields[2][:i-1]
return
}
gitVersion = fields[2]
})
return gitVersion, gitVersionErr
}