From e849fb6f6a071ae715d42e937406bb4d3ac824bd Mon Sep 17 00:00:00 2001 From: House Date: Sun, 9 Jun 2024 16:48:01 +0530 Subject: [PATCH] fix when project isn't at git repo root Signed-off-by: House --- .vscode/launch.json | 5 +++++ pkg/git/git.go | 25 +++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 9d41b1f9..50a59f31 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -7,6 +7,11 @@ "mode": "auto", "program": "${workspaceFolder}/main.go", "args": ["build"], + "env": { + "BSF_DEBUG": "true", + "BSF_DEBUG_DIR": "/Users/house/Desktop/buildsafe/examples/go-server-example", + }, + "console": "integratedTerminal" } ] } \ No newline at end of file diff --git a/pkg/git/git.go b/pkg/git/git.go index 777dcd00..1ca70beb 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -12,7 +12,13 @@ import ( func Add(path string) error { var r *git.Repository var err error - r, err = git.PlainOpen(".") + + cwd, err := os.Getwd() + if err != nil { + return err + } + + r, err = git.PlainOpenWithOptions(".", &git.PlainOpenOptions{DetectDotGit: true}) if err == git.ErrRepositoryNotExists { // If it's not a Git repository, initialize it r, err = git.PlainInit(".", false) @@ -29,14 +35,29 @@ func Add(path string) error { return err } + root := w.Filesystem.Root() + leafDir := getLeafDir(root+"/", cwd) + if leafDir != "" { + path = leafDir + "/" + path + } + // Add all changes to the working directory - _, err = w.Add(path) + err = w.AddWithOptions(&git.AddOptions{ + Path: path, + }) if err != nil { return err } return nil } +func getLeafDir(root string, path string) string { + if strings.Compare(root, path+"/") == 0 { + return "" + } + return strings.TrimPrefix(path, root) +} + // Ignore adds the path to the .gitignore file func Ignore(path string) error { gitignorePath := filepath.Join(".", ".gitignore")