git_windows.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //go:build windows
  2. // +build windows
  3. package vcs
  4. import (
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. )
  9. func handleSubmodules(g *GitRepo, dir string) ([]byte, error) {
  10. // Get the submodule directories
  11. out, err := g.RunFromDir("git", "submodule", "foreach", "--quiet", "--recursive", "echo $sm_path")
  12. if err != nil {
  13. return out, err
  14. }
  15. cleanOut := strings.TrimSpace(string(out))
  16. pths := strings.Split(strings.ReplaceAll(cleanOut, "\r\n", "\n"), "\n")
  17. // Create the new directories. Directories are sometimes not created under
  18. // Windows
  19. for _, pth := range pths {
  20. fpth := filepath.Join(dir + pth)
  21. os.MkdirAll(fpth, 0755)
  22. }
  23. // checkout-index for each submodule. Using $path or $sm_path while iterating
  24. // over the submodules does not work in Windows when called from Go.
  25. var cOut []byte
  26. for _, pth := range pths {
  27. // Get the path to the submodule in the exported location
  28. fpth := EscapePathSeparator(filepath.Join(dir, pth) + string(os.PathSeparator))
  29. // Call checkout-index directly in the submodule rather than in the
  30. // parent project. This stils git submodule foreach that has trouble
  31. // on Windows within Go where $sm_path isn't being handled properly
  32. c := g.CmdFromDir("git", "checkout-index", "-f", "-a", "--prefix="+fpth)
  33. c.Dir = filepath.Join(c.Dir, pth)
  34. out, err := c.CombinedOutput()
  35. cOut = append(cOut, out...)
  36. if err != nil {
  37. return cOut, err
  38. }
  39. }
  40. return cOut, nil
  41. }