Skip to content

Commit

Permalink
fix when project isn't at git repo root
Browse files Browse the repository at this point in the history
Signed-off-by: House <[email protected]>
  • Loading branch information
dr-housemd committed Jun 9, 2024
1 parent 0f91752 commit e849fb6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
25 changes: 23 additions & 2 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down

0 comments on commit e849fb6

Please sign in to comment.