| 306 | } |
| 307 | |
| 308 | func 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. |
| 344 | func dirExists(name string) (bool, error) { |