-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from stuartleeks/sl/workspace-folder-default
Add git repo handling to default workspace path Fixes incorrect path error when devcontainer folder isn't in the repo root
- Loading branch information
Showing
2 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package git | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
"syscall" | ||
) | ||
|
||
// GetTopLevelPath returns the top-level folder for the git-repo that contains path, or empty string if not a repo | ||
func GetTopLevelPath(path string) (string, error) { | ||
cmd := exec.Command("git", "rev-parse", "--show-toplevel") | ||
|
||
buf, err := cmd.Output() | ||
if err != nil { | ||
if exiterr, ok := err.(*exec.ExitError); ok { | ||
// The program has exited with an exit code != 0 | ||
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok { | ||
if status.ExitStatus() == 128 { | ||
// exit code 128 indictates not a git repo | ||
return "", nil | ||
} | ||
} | ||
return "", fmt.Errorf("Error git rev-parse --show-toplevel: %s", err) | ||
} | ||
return "", fmt.Errorf("Error git rev-parse --show-toplevel: %s", err) | ||
} | ||
return strings.TrimSpace(string(buf)), nil | ||
} |