MCPcopy Index your code
hub / github.com/coder/coder / ensureCorrectGitBranch

Function ensureCorrectGitBranch

cli/dotfiles.go:308–341  ·  view source on GitHub ↗
(baseInv *serpent.Invocation, params ensureCorrectGitBranchParams)

Source from the content-addressed store, hash-verified

306}
307
308func ensureCorrectGitBranch(baseInv *serpent.Invocation, params ensureCorrectGitBranchParams) error {
309 dotfileCmd := func(cmd string, args ...string) *exec.Cmd {
310 c := exec.CommandContext(baseInv.Context(), cmd, args...)
311 c.Dir = params.repoDir
312 c.Env = append(baseInv.Environ.ToOS(), fmt.Sprintf(`GIT_SSH_COMMAND=%s -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no`, params.gitSSHCommand))
313 c.Stdout = baseInv.Stdout
314 c.Stderr = baseInv.Stderr
315 return c
316 }
317 c := dotfileCmd("git", "branch", "--show-current")
318 // Save the output
319 var out bytes.Buffer
320 c.Stdout = &out
321 err := c.Run()
322 if err != nil {
323 return xerrors.Errorf("getting current git branch: %w", err)
324 }
325
326 if strings.TrimSpace(out.String()) != params.gitBranch {
327 // Checkout and pull the branch
328 c := dotfileCmd("git", "checkout", params.gitBranch)
329 err := c.Run()
330 if err != nil {
331 return xerrors.Errorf("checkout git branch %q: %w", params.gitBranch, err)
332 }
333
334 c = dotfileCmd("git", "pull", "--ff-only")
335 err = c.Run()
336 if err != nil {
337 return xerrors.Errorf("pull git branch %q: %w", params.gitBranch, err)
338 }
339 }
340 return nil
341}
342
343// dirExists checks if the path exists and is a directory.
344func dirExists(name string) (bool, error) {

Callers 1

dotfilesMethod · 0.85

Calls 5

CommandContextMethod · 0.65
ContextMethod · 0.65
RunMethod · 0.65
ErrorfMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected