···
8
-
"github.com/go-git/go-git/v5/plumbing"
"tangled.sh/tangled.sh/core/api/tangled"
"tangled.sh/tangled.sh/core/workflow"
···
22
-
// checkoutStep checks out the specified ref in the cloned repository.
23
-
func checkoutStep(twf tangled.Pipeline_Workflow, tr tangled.Pipeline_TriggerMetadata) Step {
31
-
ref = tr.Push.NewSha
32
-
case "pull_request":
33
-
ref = tr.PullRequest.TargetBranch
35
-
// TODO: this needs to be specified in lexicon
37
-
ref = tr.Repo.DefaultBranch
40
-
checkoutCmd := fmt.Sprintf("git config advice.detachedHead false; git checkout --progress --force %s", ref)
43
-
Command: checkoutCmd,
44
-
Name: "Checkout ref " + ref,
// cloneOptsAsSteps processes clone options and adds corresponding steps
// to the beginning of the workflow's step list if cloning is not skipped.
24
+
// the steps to do here are:
26
+
// - git remote add origin <url>
27
+
// - git fetch --depth=<d> --recurse-submodules=<yes|no> <sha>
28
+
// - git checkout FETCH_HEAD
func cloneStep(twf tangled.Pipeline_Workflow, tr tangled.Pipeline_TriggerMetadata, dev bool) Step {
34
+
var commands []string
36
+
// initialize git repo in workspace
37
+
commands = append(commands, "git init")
39
+
// add repo as git remote
40
+
scheme := "https://"
tr.Repo.Knot = strings.ReplaceAll(tr.Repo.Knot, "localhost", "host.docker.internal")
45
+
url := scheme + path.Join(tr.Repo.Knot, tr.Repo.Did, tr.Repo.Repo)
46
+
commands = append(commands, fmt.Sprintf("git remote add origin %s", url))
61
-
cloneUrl := uri + path.Join(tr.Repo.Knot, tr.Repo.Did, tr.Repo.Repo)
62
-
cloneCmd := []string{"git", "clone", cloneUrl, "."}
50
+
var fetchArgs []string
64
-
// default clone depth is 1
66
-
if twf.Clone.Depth > 1 {
67
-
cloneDepth = int(twf.Clone.Depth)
69
-
cloneCmd = append(cloneCmd, fmt.Sprintf("--depth=%d", cloneDepth))
71
-
// select the clone branch
74
-
case workflow.TriggerKindManual:
75
-
// TODO: unimplemented
76
-
case workflow.TriggerKindPush:
78
-
refName := plumbing.ReferenceName(ref)
79
-
cloneBranch = refName.Short()
80
-
case workflow.TriggerKindPullRequest:
81
-
cloneBranch = tr.PullRequest.SourceBranch
52
+
// default clone depth is 1
54
+
if twf.Clone.Depth > 1 {
55
+
depth = int(twf.Clone.Depth)
57
+
fetchArgs = append(fetchArgs, fmt.Sprintf("--depth=%d", depth))
84
-
if cloneBranch != "" {
85
-
cloneCmd = append(cloneCmd, fmt.Sprintf("--branch=%s", cloneBranch))
59
+
// optionally recurse submodules
60
+
if twf.Clone.Submodules {
61
+
fetchArgs = append(fetchArgs, "--recurse-submodules=yes")
64
+
// set remote to fetch from
65
+
fetchArgs = append(fetchArgs, "origin")
67
+
// set revision to checkout
68
+
switch workflow.TriggerKind(tr.Kind) {
69
+
case workflow.TriggerKindManual:
70
+
// TODO: unimplemented
71
+
case workflow.TriggerKindPush:
72
+
fetchArgs = append(fetchArgs, tr.Push.NewSha)
73
+
case workflow.TriggerKindPullRequest:
74
+
fetchArgs = append(fetchArgs, tr.PullRequest.SourceSha)
88
-
if twf.Clone.Submodules {
89
-
cloneCmd = append(cloneCmd, "--recursive")
77
+
commands = append(commands, fmt.Sprintf("git fetch %s", strings.Join(fetchArgs, " ")))
92
-
fmt.Println(strings.Join(cloneCmd, " "))
81
+
commands = append(commands, "git checkout FETCH_HEAD")
95
-
Command: strings.Join(cloneCmd, " "),
84
+
Command: strings.Join(commands, "\n"),
Name: "Clone repository into workspace",