You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.0 KiB

  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package vcs
  5. import (
  6. "os"
  7. "strings"
  8. )
  9. // envForDir returns a copy of the environment
  10. // suitable for running in the given directory.
  11. // The environment is the current process's environment
  12. // but with an updated $PWD, so that an os.Getwd in the
  13. // child will be faster.
  14. func envForDir(dir string) []string {
  15. env := os.Environ()
  16. // Internally we only use rooted paths, so dir is rooted.
  17. // Even if dir is not rooted, no harm done.
  18. return mergeEnvLists([]string{"PWD=" + dir}, env)
  19. }
  20. // mergeEnvLists merges the two environment lists such that
  21. // variables with the same name in "in" replace those in "out".
  22. func mergeEnvLists(in, out []string) []string {
  23. NextVar:
  24. for _, inkv := range in {
  25. k := strings.SplitAfterN(inkv, "=", 2)[0]
  26. for i, outkv := range out {
  27. if strings.HasPrefix(outkv, k) {
  28. out[i] = inkv
  29. continue NextVar
  30. }
  31. }
  32. out = append(out, inkv)
  33. }
  34. return out
  35. }