Skip to content

Commit

Permalink
Prevent endless loop by checking result of filepath.Dir
Browse files Browse the repository at this point in the history
filepath.Dir will return the same result when you're calling it on the
root directory of your filesystem. Checking when this happens is
required so that you break out of your loop.

This code used to loop endlessly when called outside of a git repo.
This commit fixes that.
  • Loading branch information
JeffreyVdb committed Sep 13, 2023
1 parent 73a2642 commit cff5ec0
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/cmd/stack/open_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ func getGitRepositorySubdir() (string, error) {
} else if !os.IsNotExist(err) {
return "", fmt.Errorf("couldn't stat .git directory: %w", err)
}

if newRoot := filepath.Dir(root); newRoot != root {
root = newRoot
} else {
return "", fmt.Errorf("couldn't find .git directory in %s or any of its parents", current)
}

root = filepath.Dir(root)
}

Expand Down

0 comments on commit cff5ec0

Please sign in to comment.