forked from gptscript-ai/gptscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.go
54 lines (46 loc) · 1.35 KB
/
cmd.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
package git
import (
"context"
"fmt"
"strings"
"github.com/gptscript-ai/gptscript/pkg/debugcmd"
)
func newGitCommand(ctx context.Context, args ...string) *debugcmd.WrappedCmd {
if log.IsDebug() {
log.Debugf("running git command: %s", strings.Join(args, " "))
}
cmd := debugcmd.New(ctx, "git", args...)
return cmd
}
func LsRemote(ctx context.Context, repo, ref string) (string, error) {
if usePureGo() {
return lsRemotePureGo(ctx, repo, ref)
}
cmd := newGitCommand(ctx, "ls-remote", repo, ref)
if err := cmd.Run(); err != nil {
return "", err
}
for _, line := range strings.Split(cmd.Stdout(), "\n") {
fields := strings.Fields(line)
if len(fields) < 2 {
continue
}
if fields[1] == ref {
return fields[0], nil
}
}
return "", fmt.Errorf("failed to find remote %q as %q", repo, ref)
}
func cloneBare(ctx context.Context, repo, toDir string) error {
cmd := newGitCommand(ctx, "clone", "--bare", "--depth", "1", repo, toDir)
return cmd.Run()
}
func gitWorktreeAdd(ctx context.Context, gitDir, commitDir, commit string) error {
// The double -f is intentional
cmd := newGitCommand(ctx, "--git-dir", gitDir, "worktree", "add", "-f", "-f", commitDir, commit)
return cmd.Run()
}
func fetchCommit(ctx context.Context, gitDir, commit string) error {
cmd := newGitCommand(ctx, "--git-dir", gitDir, "fetch", "origin", commit)
return cmd.Run()
}