-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit.go
More file actions
71 lines (59 loc) · 1.75 KB
/
Copy pathgit.go
File metadata and controls
71 lines (59 loc) · 1.75 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
package getit
import (
"context"
"fmt"
"net/url"
"os/exec"
"strings"
"github.com/kballard/go-shellquote"
)
// The Git [Resolver] uses Git repositories as archive sources, cloning directly.
//
// The URL format supported is:
//
// git://host/path/to/repo
// git+ssh://host/path/to/repo
// git+https://host/path/to/repo
//
// All forms support the following query parameters that control cloning behaviour:
//
// ref=<ref>
// depth=<depth>
type Git struct{}
var _ Resolver = (*Git)(nil)
func NewGit() *Git { return &Git{} }
func (g *Git) Match(source *url.URL) bool {
return source.Scheme == "git+https" || source.Scheme == "git+ssh" || source.Scheme == "git"
}
func (g *Git) Fetch(ctx context.Context, source Source, dest string) error {
args := []string{"clone"}
if depth := source.URL.Query().Get("depth"); depth != "" {
args = append(args, "--depth", depth)
}
if ref := source.URL.Query().Get("ref"); ref != "" {
args = append(args, "--branch", ref)
}
repoURL := convertGitURL(source.URL)
args = append(args, repoURL, dest)
cmd := exec.CommandContext(ctx, "git", args...)
killProcessGroup(cmd)
if output, err := cmd.CombinedOutput(); err != nil {
argsStr := shellquote.Join(args...)
return fmt.Errorf("git clone failed: git %s: %w: %s", argsStr, err, output)
}
return nil
}
// convertGitURL converts a getit git URL to a standard git URL.
// git+https://host/path -> https://host/path
// git+ssh://host/path -> git@host:path (SCP-style)
// git://host/path -> git://host/path
func convertGitURL(u *url.URL) string {
clone := *u
clone.RawQuery = ""
if clone.Scheme == "git+ssh" {
path := strings.TrimPrefix(clone.Path, "/")
return "git@" + clone.Host + ":" + path
}
clone.Scheme = strings.TrimPrefix(clone.Scheme, "git+")
return clone.String()
}