| // Copyright 2013 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| // envForDir returns a copy of the environment |
| // suitable for running in the given directory. |
| // The environment is the current process's environment |
| // but with an updated $PWD, so that an os.Getwd in the |
| func envForDir(dir string) []string { |
| // Internally we only use rooted paths, so dir is rooted. |
| // Even if dir is not rooted, no harm done. |
| return mergeEnvLists([]string{"PWD=" + dir}, env) |
| // mergeEnvLists merges the two environment lists such that |
| // variables with the same name in "in" replace those in "out". |
| func mergeEnvLists(in, out []string) []string { |
| for _, inkv := range in { |
| k := strings.SplitAfterN(inkv, "=", 2)[0] |
| for i, outkv := range out { |
| if strings.HasPrefix(outkv, k) { |