# feat(repo): add `entire repo clone /gh/<owner>/<repo>`

`ca0886a`→[main](/content/gh/entireio/cli/commits/main/index.html)·

toothbrush·2w ago·3 files·+388 added/-0 removed

Looks up where a repo is mirrored: clones directly when on one cluster,
prompts (or --cluster) when on more than one. Optional [target-dir] is
passed through to git clone.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

## Sessions

245ec47a154fView transcript

## Changes

3

- cmd/entire/cli

- Mrepo.go+1

- Arepo_clone.go+218

- Arepo_clone_test.go+169

```
26 unmodified lines

27
28
29
30
31
32
33

26 unmodified lines

cmd.AddCommand(newRepoListCmd())
	cmd.AddCommand(newRepoGetCmd())
	cmd.AddCommand(newRepoDeleteCmd())
	cmd.AddCommand(newRepoCloneCmd())
	cmd.AddCommand(newRepoMirrorCmd())
	return cmd
}
```

Mcmd/entire/cli/repo.go+1

```
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

package cli

import (
	"context"
	"errors"
	"fmt"
	"os/exec"
	"regexp"
	"sort"
	"strings"

"charm.land/huh/v2"
	"github.com/spf13/cobra"

"github.com/entireio/cli/cmd/entire/cli/interactive"
	"github.com/entireio/cli/internal/coreapi"
)

// mirrorCloneRefRe parses the clone-ref shape `entire repo clone` accepts:
// the `/gh/<owner>/<repo>` path of a mirror's clone URL, with or without the
// leading slash. owner/repo reuse the GitHub identifier charsets from
// parseGitHubURL so the same metacharacter vectors are closed at the boundary
// (owner/repo flow unescaped into the synthesised entire:// clone URL).
var mirrorCloneRefRe = regexp.MustCompile(`^/?gh/` + gitHubOwnerPat + `/` + gitHubRepoPat + `$`)

// parseMirrorCloneRef turns a clone ref like `/gh/entirehq/entire-api` into the
// API provider ("github") and the lowercased owner/repo. The `gh` token is the
// path provider used in entire:// clone URLs; it maps to the "github" upstream
// provider the control plane records.
func parseMirrorCloneRef(ref string) (provider, owner, repo string, err error) {
	m := mirrorCloneRefRe.FindStringSubmatch(strings.TrimSpace(ref))
	if m == nil {
		return "", "", "", fmt.Errorf("expected /gh/<owner>/<repo>, got %q", ref)
	}
	owner, repo = strings.ToLower(m[1]), strings.ToLower(m[2])
	if gitHubDotOnlyRe.MatchString(repo) {
		return "", "", "", fmt.Errorf("repo cannot be dot-only: %s", ref)
	}
	return checkpointProviderGitHub, owner, repo, nil
}

func newRepoCloneCmd() *cobra.Command {
	var cluster string
	cmd := &cobra.Command{
		Use:   "clone <repo> [target-dir]",
		Short: "Clone a mirrored repository",
		Long: "Clone a GitHub mirror by its `/gh/<owner>/<repo>` ref. Looks up where " +
			"the repo is mirrored: if it's on a single cluster, clones it directly; " +
			"if it's mirrored on more than one, prompts you to pick which to clone " +
			"from (or pass --cluster to choose non-interactively). The optional " +
			"[target-dir] is passed straight through to `git clone`.",
		Example: "  entire repo clone /gh/entirehq/entire-api\n" +
			"  entire repo clone /gh/entirehq/entire-api ./entire-api\n" +
			"  entire repo clone /gh/entirehq/entire-api --cluster aws-us-east-2.entire.io",
		Args: cobra.RangeArgs(1, 2),
		RunE: func(cmd *cobra.Command, args []string) error {
			cmd.SilenceUsage = true
			provider, owner, repo, err := parseMirrorCloneRef(args[0])
			if err != nil {
				return fmt.Errorf("invalid <repo>: %w", err)
			}
			var targetDir string
			if len(args) > 1 {
				targetDir = args[1]
			}

var mirrors []coreapi.Mirror
			if err := runCore(cmd, func(ctx context.Context, c *coreapi.Client) error {
				ms, err := listMirrorsForRepo(ctx, c, provider, owner, repo)
				if err != nil {
					return err
				}
				mirrors = ms
				return nil
			}); err != nil {
				return err
			}

if len(mirrors) == 0 {
				return fmt.Errorf("no mirror found for gh/%s/%s; run 'entire repo mirror create github.com/%s/%s' to onboard it", owner, repo, owner, repo)
			}

chosen, err := selectCloneTarget(cmd, mirrors, cluster)
			if err != nil {
				return err
			}

cloneURL := fmt.Sprintf("entire://%s/gh/%s/%s", chosen.ClusterHost, owner, repo)
			return runGitClone(cmd.Context(), cmd, cloneURL, targetDir)
			},
	}
	cmd.Flags().StringVar(&cluster, "cluster", "", "cluster host to clone from when the repo is mirrored on more than one")
	return cmd
}

// Further code content removed for brevity.
