Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] when project isn't at git repo root #18

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading