github.com/TBD54566975/ftl@v0.219.0/internal/source_root.go (about)

     1  package internal
     2  
     3  import (
     4  	"os"
     5  	"os/exec" //nolint:depguard
     6  	"strings"
     7  )
     8  
     9  // GitRoot returns the root of the git repository containing dir, or empty string if dir is not in a git repository.
    10  //
    11  // If dir is empty, the current working directory is used.
    12  func GitRoot(dir string) string {
    13  	if dir == "" {
    14  		var err error
    15  		dir, err = os.Getwd()
    16  		if err != nil {
    17  			return ""
    18  		}
    19  	}
    20  	cmd := exec.Command("git", "rev-parse", "--show-toplevel")
    21  	cmd.Dir = dir
    22  	output, err := cmd.CombinedOutput()
    23  	if err != nil {
    24  		return ""
    25  	}
    26  	return strings.TrimSpace(string(output))
    27  }