Skip to content
This repository was archived by the owner on Aug 21, 2022. It is now read-only.

Commit fd9bb54

Browse files
committed
Simplify findGitRoot logic, and improve error message when git is not installed
1 parent e9e636b commit fd9bb54

File tree

5 files changed

+20
-29
lines changed

5 files changed

+20
-29
lines changed

cmd/make.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ import (
88
)
99

1010
func doMake(cmd *cobra.Command, args []string) error {
11-
root := findGitRoot()
12-
if root == "" {
13-
root = "."
14-
}
11+
root := findGitRootOrCwd()
1512
container := searchContainer(root, true)
1613

1714
// Reconstruct the relative path within the git root, so that it can be

cmd/start.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,7 @@ func searchContainer(path string, autostart bool) string {
7777
}
7878

7979
func doStart(cmd *cobra.Command, args []string) error {
80-
path := findGitRoot()
81-
if path == "" {
82-
// If we're not in a git repository, create a container that
83-
// mounts the current directory.
84-
path = "."
85-
}
80+
path := findGitRootOrCwd()
8681
searchContainer(path, true)
8782
return nil
8883
}

cmd/stop.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ import (
88
)
99

1010
func doStop(cmd *cobra.Command, args []string) error {
11-
path := findGitRoot()
12-
if path == "" {
13-
// Same logic of doStart
14-
path = "."
15-
}
16-
11+
path := findGitRootOrCwd()
1712
out := searchContainer(path, false)
1813
if out != "" {
1914
mustRun("docker", "container", "rm", "--force", out)

cmd/update.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ func updateToolchain() {
2626
// Persist the image name by creating a local file in the repo root.
2727
// Users might want to commit this file to persist their custom
2828
// toolchain selection
29-
repoRoot := findGitRoot()
30-
if repoRoot != "" {
31-
repoRoot = "."
32-
}
33-
29+
repoRoot := findGitRootOrCwd()
3430
if err := os.WriteFile(filepath.Join(repoRoot, CACHED_IMAGE_FILE), []byte(image+"\n"), 0666); err != nil {
3531
fatal("error persisting toolchain change: %v\n", err)
3632
}

cmd/utils.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,13 @@ var (
141141
)
142142

143143
// findGitRoot checks whether the current directory is part of a git repo; if so,
144-
// returns its root, otherwise returns empty string.
145-
func findGitRoot() string {
144+
// returns its root, otherwise returns "." which is a good default for all
145+
// situations in which we want to optionally find the root of a repo but we are
146+
// unable to.
147+
func findGitRootOrCwd() string {
146148
if !cachedGitRootOnce {
147149
cachedGitRootOnce = true
150+
cachedGitRoot = "."
148151

149152
rootdir1, err := getOutput("git", "rev-parse", "--show-toplevel")
150153
if err == nil {
@@ -161,8 +164,16 @@ func findGitRoot() string {
161164
// mustFindGitRoot is like findGitRoot, but aborts with fatal if no git repository
162165
// is found.
163166
func mustFindGitRoot() string {
164-
path := findGitRoot()
165-
if path == "" {
167+
path := findGitRootOrCwd()
168+
if path == "." {
169+
if _, err := exec.LookPath("git"); err != nil {
170+
critical("error: this command requires Git\n")
171+
if runtime.GOOS == "windows" || true {
172+
fatal("Please download it from: https://git-scm.com/downloads\n")
173+
} else {
174+
fatal("Please install it with your favorite package manager")
175+
}
176+
}
166177
fatal("error: this command must be run from a git repository\n")
167178
}
168179
return path
@@ -207,10 +218,7 @@ func findLibdragon() (string, bool) {
207218
func findDockerImage() string {
208219
// Check if there's a cached image file in the repository root. This is
209220
// a local override requested by the user, so it wins over anything.
210-
repoRoot := findGitRoot()
211-
if repoRoot == "" {
212-
repoRoot = "."
213-
}
221+
repoRoot := findGitRootOrCwd()
214222
if imagebytes, err := os.ReadFile(filepath.Join(repoRoot, CACHED_IMAGE_FILE)); err == nil {
215223
return strings.TrimSpace(string(imagebytes))
216224
}

0 commit comments

Comments
 (0)