新增git 命令运行方法

This commit is contained in:
zyj
2026-03-06 18:22:58 +08:00
parent 9b923ba301
commit d8851e7698
4 changed files with 65 additions and 1 deletions

View File

@@ -1,5 +1,11 @@
package main
func main() {
import "github.com/zhuy1228/GitPilot/internal"
func main() {
proxyInfo, err := internal.GetCurrentProxy()
if err != nil {
panic(err)
}
println(proxyInfo.String())
}

5
go.mod
View File

@@ -1,3 +1,8 @@
module github.com/zhuy1228/GitPilot
go 1.25.5
require (
golang.org/x/sys v0.41.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

5
go.sum Normal file
View File

@@ -0,0 +1,5 @@
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

48
internal/git/client.go Normal file
View File

@@ -0,0 +1,48 @@
package git
import (
"bytes"
"context"
"fmt"
"os/exec"
"time"
"github.com/zhuy1228/GitPilot/internal"
)
type GitClient struct {
Enabled bool
Proxy string
Timeout time.Duration
}
func NewGitClient() *GitClient {
proxy, _ := internal.GetCurrentProxy()
return &GitClient{
Enabled: proxy.Enabled,
Proxy: proxy.Protocol + "://" + proxy.Server,
}
}
// Run 执行 git 命令,支持超时和代理设置
func (g *GitClient) Run(path string, args ...string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), g.Timeout)
defer cancel()
header := []string{"-C", path, "git"}
if g.Enabled {
header = append(header, "-c", "http.proxy="+g.Proxy, "-c", "https.proxy="+g.Proxy)
}
argsArr := append(header, args...)
cmd := exec.CommandContext(ctx, "git", argsArr...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if ctx.Err() == context.DeadlineExceeded {
return "", fmt.Errorf("git command timeout: git %v", args)
}
if err != nil {
return "", fmt.Errorf("git command error: %v, stderr: %s", err, stderr.String())
}
return stdout.String(), nil
}