diff --git a/.gitignore b/.gitignore index 6e77a31..a6f002c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,3 @@ -# If you prefer the allow list template instead of the deny list, see community template: -# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore -# # Binaries for programs and plugins *.exe *.exe~ @@ -22,3 +19,6 @@ bin # Go workspace file go.work + +# Developer tools configs +.vscode diff --git a/cmd/commit/main.go b/cmd/commit/main.go index b28256a..f32af1d 100644 --- a/cmd/commit/main.go +++ b/cmd/commit/main.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "os" + "path/filepath" "regexp" "strings" "time" @@ -22,7 +23,7 @@ func main() { flag.StringVar( &configFilePath, "config-path", - helpers.DEFAULT_CONFIG_FILE_PATH, + "", "Path to the config json file", ) @@ -38,6 +39,15 @@ func main() { commitMessage := getCommitMessage() repo := openRepo() + worktree := openWorktree(repo) + + if configFilePath == "" { + configFilePath = filepath.Join( + worktree.Filesystem.Root(), + helpers.DEFAULT_CONFIG_FILE_PATH, + ) + } + headRef := getCurrentHead(repo) // Read branch name or HEAD @@ -52,7 +62,7 @@ func main() { } if !dryRun { - commitChanges(repo, commitMessage) + commitChanges(repo, worktree, commitMessage) } fmt.Println(commitMessage) @@ -76,9 +86,12 @@ func getCommitMessage() string { return args[0] } -// Opens the repository in current directory +// Opens the current repository func openRepo() *git.Repository { - repo, err := git.PlainOpen(".") + + options := git.PlainOpenOptions{DetectDotGit: true} + + repo, err := git.PlainOpenWithOptions(".", &options) if err != nil { fmt.Fprintf(os.Stderr, helpers.Red("Failed to open repository: %v\n"), err) os.Exit(1) @@ -158,8 +171,7 @@ func makeCommitOptions(usr user.User) git.CommitOptions { } // Commits changes with provided message -func commitChanges(repo *git.Repository, commitMessage string) { - worktree := openWorktree(repo) +func commitChanges(repo *git.Repository, worktree *git.Worktree, commitMessage string) { checkStagedChanges(worktree) diff --git a/docs/README.md b/docs/README.md index 1e13f59..da73ea6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -87,5 +87,5 @@ Originally I wrote this tool for myself in shell and Swift and used a lot on Mac If you find it useful and see that something's wrong or missing, feel free to raise issues and contribute to the project. When doing so, please follow the ususal code of conduct and contribution guidelines from GitHub, and just common sense. ## License -This repository is distributed under the MIT license (see [LICENSE.md](/docs/LICENSE.md). +This repository is distributed under the MIT license (see [LICENSE.md](/docs/LICENSE)). My tool uses [go-git](https://github.com/go-git/go-git) as a 3rd party dependency to work with git directly, you might want to check it out too.