forked from iwarapter/gpg-commit-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush.go
More file actions
85 lines (70 loc) · 2.46 KB
/
Copy pathpush.go
File metadata and controls
85 lines (70 loc) · 2.46 KB
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
76
77
78
79
80
81
82
83
84
85
package main
import (
"context"
"fmt"
"strings"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
type GitPushArgs struct {
RepoDir string `json:"repo_dir,omitempty" jsonschema:"Path to the git repository (defaults to current directory)"`
Remote string `json:"remote,omitempty" jsonschema:"Remote name (defaults to 'origin')"`
Branch string `json:"branch,omitempty" jsonschema:"Branch to push (defaults to the current branch)"`
}
func gitPushHandler(ctx context.Context, _ *mcp.CallToolRequest, args GitPushArgs) (*mcp.CallToolResult, any, error) {
repoDir, err := resolveRepo(ctx, args.RepoDir)
if err != nil {
return nil, nil, err
}
remote := args.Remote
if remote == "" {
remote = "origin"
}
gitArgs := []string{"-C", repoDir, "push", remote}
if args.Branch != "" {
gitArgs = append(gitArgs, args.Branch)
}
stdout, stderr, err := runGit(ctx, gitArgs...)
if err != nil {
return nil, nil, fmt.Errorf("git push failed: %s\n%s", stderr, stdout)
}
output := strings.TrimSpace(stdout + "\n" + stderr)
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: fmt.Sprintf("Push successful:\n%s", output)},
},
}, nil, nil
}
type GitForcePushArgs struct {
RepoDir string `json:"repo_dir,omitempty" jsonschema:"Path to the git repository (defaults to current directory)"`
Remote string `json:"remote,omitempty" jsonschema:"Remote name (defaults to 'origin')"`
Branch string `json:"branch,omitempty" jsonschema:"Branch to push (defaults to the current branch)"`
Force bool `json:"force,omitempty" jsonschema:"Use --force instead of --force-with-lease. Dangerous: overwrites remote unconditionally."`
}
func gitForcePushHandler(ctx context.Context, _ *mcp.CallToolRequest, args GitForcePushArgs) (*mcp.CallToolResult, any, error) {
repoDir, err := resolveRepo(ctx, args.RepoDir)
if err != nil {
return nil, nil, err
}
remote := args.Remote
if remote == "" {
remote = "origin"
}
forceFlag := "--force-with-lease"
if args.Force {
forceFlag = "--force"
}
gitArgs := []string{"-C", repoDir, "push", forceFlag, remote}
if args.Branch != "" {
gitArgs = append(gitArgs, args.Branch)
}
stdout, stderr, err := runGit(ctx, gitArgs...)
if err != nil {
return nil, nil, fmt.Errorf("git force push failed: %s\n%s", stderr, stdout)
}
output := strings.TrimSpace(stdout + "\n" + stderr)
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: fmt.Sprintf("Force push successful:\n%s", output)},
},
}, nil, nil
}