MCPcopy Create free account
hub / github.com/cli/cli / cmdRepo

Function cmdRepo

script/ghmock/gitcmd.go:43–98  ·  view source on GitHub ↗
(args []string, stdout io.Writer)

Source from the content-addressed store, hash-verified

41}
42
43func cmdRepo(args []string, stdout io.Writer) error {
44 opts, err := parseRepoArgs(args)
45 if err != nil {
46 return err
47 }
48 st, err := openState()
49 if err != nil {
50 return err
51 }
52 world, err := st.ensureGitFixture()
53 if err != nil {
54 return err
55 }
56 host, repo, err := parseGitRepo(opts.name)
57 if err != nil {
58 return err
59 }
60 dir := gitRepoPath(world, host, repo)
61 if _, err := os.Stat(dir); err == nil {
62 return fmt.Errorf("repository %s already exists", opts.name)
63 } else if !errors.Is(err, os.ErrNotExist) {
64 return err
65 }
66 scratch, err := newScratch(nil)
67 if err != nil {
68 return err
69 }
70 defer os.RemoveAll(scratch)
71 env, err := gitEnvironment(ghEnv(scratch, "", runOptions{localhost: true}), scratch, world)
72 if err != nil {
73 return err
74 }
75 if _, err := localGit(world, env, "check-ref-format", "refs/heads/"+opts.branch); err != nil {
76 return fmt.Errorf("invalid branch %q: %w", opts.branch, err)
77 }
78 if err := os.MkdirAll(filepath.Dir(dir), 0o755); err != nil {
79 return err
80 }
81 tmp, err := os.MkdirTemp(filepath.Dir(dir), "repo-")
82 if err != nil {
83 return err
84 }
85 defer os.RemoveAll(tmp)
86 if _, err := localGit(world, env, "init", "--bare", "--quiet", "--initial-branch="+opts.branch, tmp); err != nil {
87 return err
88 }
89 if _, err := localGit(tmp, env, "config", "http.receivepack", "true"); err != nil {
90 return err
91 }
92 if err := os.Rename(tmp, dir); err != nil {
93 return err
94 }
95 fmt.Fprintf(stdout, "Created %s/%s (branch %s)\nHTTPS: https://%s/%s.git\nSSH: git@%s:%s.git\n",
96 host, repo, opts.branch, host, repo, host, repo)
97 return nil
98}
99
100type writeOptions struct {

Callers 4

TestCmdRepoFunction · 0.85
TestCmdGitFunction · 0.85
prepareGitRepoFunction · 0.85
runMainFunction · 0.85

Calls 11

parseRepoArgsFunction · 0.85
openStateFunction · 0.85
parseGitRepoFunction · 0.85
gitRepoPathFunction · 0.85
newScratchFunction · 0.85
gitEnvironmentFunction · 0.85
ghEnvFunction · 0.85
localGitFunction · 0.85
ensureGitFixtureMethod · 0.80
MkdirAllMethod · 0.80
ErrorfMethod · 0.65

Tested by 3

TestCmdRepoFunction · 0.68
TestCmdGitFunction · 0.68
prepareGitRepoFunction · 0.68