s
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package gitcmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
gitExecPath string
|
||||
gitOnce sync.Once
|
||||
gitErr error
|
||||
)
|
||||
|
||||
func SetExecutable(path string) {
|
||||
gitExecPath = path
|
||||
}
|
||||
|
||||
func Executable() (string, error) {
|
||||
gitOnce.Do(func() {
|
||||
if gitExecPath == "" {
|
||||
gitExecPath, gitErr = exec.LookPath("git")
|
||||
}
|
||||
})
|
||||
return gitExecPath, gitErr
|
||||
}
|
||||
|
||||
func HomeDir() string {
|
||||
if dir := os.Getenv("GIT_HOME"); dir != "" {
|
||||
return dir
|
||||
}
|
||||
return os.TempDir()
|
||||
}
|
||||
|
||||
func CommonEnvs() []string {
|
||||
return []string{
|
||||
"HOME=" + HomeDir(),
|
||||
"GIT_CONFIG_NOSYSTEM=1",
|
||||
"GIT_NO_REPLACE_OBJECTS=1",
|
||||
"GIT_TERMINAL_PROMPT=0",
|
||||
"LC_ALL=C",
|
||||
}
|
||||
}
|
||||
|
||||
func Version(ctx context.Context) (string, error) {
|
||||
stdout, _, err := NewCommand("version").RunStdString(ctx)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("git version: %w", err)
|
||||
}
|
||||
fields := strings.Fields(stdout)
|
||||
if len(fields) >= 3 {
|
||||
return fields[2], nil
|
||||
}
|
||||
return stdout, nil
|
||||
}
|
||||
Reference in New Issue
Block a user