...

Source file src/golang.org/x/tools/go/vcs/env.go

Documentation: golang.org/x/tools/go/vcs

     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  
     5  package vcs
     6  
     7  import (
     8  	"os"
     9  	"strings"
    10  )
    11  
    12  // envForDir returns a copy of the environment
    13  // suitable for running in the given directory.
    14  // The environment is the current process's environment
    15  // but with an updated $PWD, so that an os.Getwd in the
    16  // child will be faster.
    17  func envForDir(dir string) []string {
    18  	env := os.Environ()
    19  	// Internally we only use rooted paths, so dir is rooted.
    20  	// Even if dir is not rooted, no harm done.
    21  	return mergeEnvLists([]string{"PWD=" + dir}, env)
    22  }
    23  
    24  // mergeEnvLists merges the two environment lists such that
    25  // variables with the same name in "in" replace those in "out".
    26  func mergeEnvLists(in, out []string) []string {
    27  NextVar:
    28  	for _, inkv := range in {
    29  		k := strings.SplitAfterN(inkv, "=", 2)[0]
    30  		for i, outkv := range out {
    31  			if strings.HasPrefix(outkv, k) {
    32  				out[i] = inkv
    33  				continue NextVar
    34  			}
    35  		}
    36  		out = append(out, inkv)
    37  	}
    38  	return out
    39  }
    40  

View as plain text