-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit.go
More file actions
195 lines (185 loc) · 6.23 KB
/
Copy pathgit.go
File metadata and controls
195 lines (185 loc) · 6.23 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package cli
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"github.com/spf13/cobra"
"codebox/internal/app"
"codebox/internal/settings"
)
func newGitCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "git",
Short: "Move git refs between the operator's repo and a sandbox instance",
Long: "Move git refs between the operator's repo and a sandbox instance.\n\n" +
"Use `push` to send a local ref into the instance and check it out\n" +
"there; use `pull` to fetch a branch back from the instance into\n" +
"a remote-tracking ref on the operator's machine.",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
return cmd.Help()
},
}
cmd.AddCommand(newGitPushCmd(), newGitPullCmd())
return cmd
}
func newGitPushCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "push INSTANCE [REFSPEC]",
Short: "Push a ref from the operator's repo into a sandbox instance",
Long: "Push a ref from the operator's repo into a sandbox instance.\n\n" +
"REFSPEC takes one of two shapes:\n\n" +
" source_remote/source_branch:target_branch\n" +
" e.g. `origin/main:issue-1234`. codebox runs\n" +
" `git fetch source_remote` locally, then pushes the resulting\n" +
" remote-tracking ref to refs/heads/target_branch on the\n" +
" instance.\n\n" +
" local_branch:target_branch\n" +
" e.g. `main:issue-1234`. No source remote and no local fetch;\n" +
" the named local branch is pushed straight to\n" +
" refs/heads/target_branch on the instance.\n\n" +
"The source side may be omitted when `git.push-from` is set in a\n" +
".codebox.conf (the project file, else the global one): write\n" +
"`:target_branch` (or, with REFSPEC left off entirely, just\n" +
"`codebox git push INSTANCE`, which targets a branch named after\n" +
"the instance) and the configured source is filled in.\n\n" +
"In every form target_branch is checked out at ~/source inside\n" +
"the sandbox after the push.",
Args: cobra.RangeArgs(1, 2),
ValidArgsFunction: completeInstances,
RunE: func(cmd *cobra.Command, args []string) error {
refspec := ""
if len(args) > 1 {
refspec = args[1]
}
return runGitPush(cmd.Context(),
cmd.OutOrStdout(), cmd.ErrOrStderr(), args[0], refspec, readCommonOpts(cmd))
},
}
cmd.Flags().SortFlags = false
return cmd
}
func newGitPullCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "pull INSTANCE [BRANCH]",
Short: "Fetch a branch from a sandbox instance into a remote-tracking ref",
Long: "Fetch a branch from a sandbox instance into a remote-tracking ref.\n\n" +
"BRANCH is optional; when omitted it defaults to INSTANCE, matching\n" +
"the branch `codebox workflow`/`codebox git push` check out at\n" +
"~/source inside a sandbox of the same name.\n\n" +
"The instance's published port is re-resolved each run, so this still\n" +
"works after the container has been restarted.",
Args: cobra.RangeArgs(1, 2),
ValidArgsFunction: completeInstances,
RunE: func(cmd *cobra.Command, args []string) error {
branch := ""
if len(args) > 1 {
branch = args[1]
}
return runGitPull(cmd.Context(),
cmd.OutOrStdout(), cmd.ErrOrStderr(), args[0], branch, readCommonOpts(cmd))
},
}
cmd.Flags().SortFlags = false
return cmd
}
func runGitPush(
ctx context.Context,
stdout, stderr io.Writer,
instance, refspec string,
opts commonOpts,
) error {
home, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("locate home directory: %w", err)
}
if err := requireGitCwd(); err != nil {
return err
}
pushSource, err := configuredPushSource(home)
if err != nil {
return err
}
// A bare `git push INSTANCE` (no refspec) pushes to a branch named
// after the instance — the same convention as `git pull` and
// `workflow` — with the source taken from git.push-from.
if refspec == "" {
refspec = instance
}
return app.New(home).GitPush(ctx, stdout, stderr, app.GitPushRequest{
Instance: instance,
Orchestrator: opts.orchestrator,
Remote: opts.remote,
InstanceKeys: opts.instanceKeys,
Refspec: refspec,
PushSource: pushSource,
})
}
func runGitPull(
ctx context.Context,
stdout, stderr io.Writer,
instance, branch string,
opts commonOpts,
) error {
home, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("locate home directory: %w", err)
}
if err := requireGitCwd(); err != nil {
return err
}
return app.New(home).GitPull(ctx, stdout, stderr, app.GitPullRequest{
Instance: instance,
Orchestrator: opts.orchestrator,
Remote: opts.remote,
InstanceKeys: opts.instanceKeys,
Branch: branch,
})
}
// configuredPushSource returns the `git.push-from` default, taken from
// the project's .codebox.conf when set and otherwise from the global
// ~/.codebox.conf. It is the source side filled into a push refspec when
// the operator omits it. An empty string means no default is configured
// — the callers turn that into a clear error only if the operator
// actually omitted the source.
func configuredPushSource(home string) (string, error) {
workDir, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("locate current directory: %w", err)
}
global, project, err := settings.Load(home, workDir)
if err != nil {
return "", err
}
if project.Git.Push != "" {
return project.Git.Push, nil
}
return global.Git.Push, nil
}
// requireGitCwd confirms the operator's current working directory is
// the root of a git repository (i.e. has a `.git/` directory). Both
// git subcommands need to run a local `git remote`/`git fetch`/`git
// push`, so a non-repo cwd is rejected up front rather than letting
// the orchestrator-side work happen and then failing at the local
// git invocation.
func requireGitCwd() error {
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("locate current directory: %w", err)
}
gitPath := filepath.Join(cwd, ".git")
info, err := os.Stat(gitPath)
switch {
case errors.Is(err, fs.ErrNotExist):
return fmt.Errorf("not a git repository: no .git directory in %s", cwd)
case err != nil:
return fmt.Errorf("check for .git in %s: %w", cwd, err)
case !info.IsDir():
return fmt.Errorf("not a git repository: %s exists but is not a directory", gitPath)
}
return nil
}