From d8851e7698f7af6c97856aa9b6635f7c984c5bdb Mon Sep 17 00:00:00 2001 From: zyj Date: Fri, 6 Mar 2026 18:22:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Egit=20=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/main.go | 8 ++++++- go.mod | 5 +++++ go.sum | 5 +++++ internal/git/client.go | 48 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 go.sum create mode 100644 internal/git/client.go diff --git a/cmd/main.go b/cmd/main.go index 7905807..7b76b36 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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()) } diff --git a/go.mod b/go.mod index 5c2e1e9..2cfc9b0 100644 --- a/go.mod +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2e09267 --- /dev/null +++ b/go.sum @@ -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= diff --git a/internal/git/client.go b/internal/git/client.go new file mode 100644 index 0000000..f9e81f1 --- /dev/null +++ b/internal/git/client.go @@ -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 +}