diff --git a/cmd/commit/main.go b/cmd/commit/main.go index 854a9ce..411054c 100644 --- a/cmd/commit/main.go +++ b/cmd/commit/main.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "time" "github.com/artem-y/commit/internal/config" @@ -13,7 +14,6 @@ import ( "github.com/artem-y/commit/internal/user" "github.com/go-git/go-git/v5" - "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" ) @@ -47,12 +47,25 @@ func main() { ) } - headRef := getCurrentHead(repo) + headFilePath := filepath.Join( + worktree.Filesystem.Root(), + ".git", + "HEAD", + ) + + fileReader := config.FileReader{} - // Read branch name or HEAD - if headRef.Name().IsBranch() { + // Read current HEAD from file + headFile, _ := fileReader.ReadFile(headFilePath) + headFileText := string(headFile) + + // If there is a branch name in the HEAD file, modify the commit message + if strings.HasPrefix(headFileText, helpers.HEAD_REF_PREFIX) { + branchName := strings.TrimPrefix( + headFileText, + helpers.HEAD_REF_PREFIX, + ) - fileReader := config.FileReader{} cfg, err := config.ReadCommitConfig(fileReader, configFilePath) if err != nil { fmt.Fprintf(os.Stderr, helpers.Red("Failed to read config: %v\n"), err) @@ -60,23 +73,18 @@ func main() { } messageGenerator := message_generator.MessageGenerator{ - BranchName: headRef.Name().Short(), + BranchName: branchName, UserMessage: commitMessage, Config: cfg, } commitMessage = messageGenerator.GenerateMessage() + } - if !dryRun { - commitChanges(repo, worktree, commitMessage) - } - - fmt.Println(commitMessage) - - } else if headRef.Name().IsTag() { - fmt.Printf("HEAD is a tag: %v\n", headRef.Name().Short()) - } else { - fmt.Printf("Detached HEAD at %v\n", headRef.Hash()) + if !dryRun { + commitChanges(repo, worktree, commitMessage) } + + fmt.Println(commitMessage) } // Reads commit message from command line arguments @@ -104,16 +112,6 @@ func openRepo() *git.Repository { return repo } -// Reads the current HEAD reference -func getCurrentHead(repo *git.Repository) *plumbing.Reference { - headRef, err := repo.Head() - if err != nil { - fmt.Fprintf(os.Stderr, helpers.Red("Failed to read current HEAD: %v\n"), err) - os.Exit(1) - } - return headRef -} - // Opens worktree func openWorktree(repo *git.Repository) *git.Worktree { worktree, err := repo.Worktree() diff --git a/e2e.sh b/e2e.sh index d26a531..f169387 100755 --- a/e2e.sh +++ b/e2e.sh @@ -32,8 +32,7 @@ setup_test_repository() { git init && \ # Set up local git config inside the new directory git config --local user.name "GitHub Actions CI Runner" && \ - git config --local user.email "--" && \ - touch file && git add file && git commit -m "Initial commit" + git config --local user.email "--" } start_test() { @@ -251,6 +250,56 @@ test_use_config_with_empty_regex() { } +test_commit_with_detached_head() { + TESTNAME="test_commit_with_detached_head" + start_test $TESTNAME + + setup_test_repository &&\ + git config --local advice.detachedHead false && \ + git checkout -b feature/DEV-21-validation && \ + + # Write a config file + echo ' + { + "issueRegex": "DEV-[0-9]+", + "outputIssuePrefix": "", + "outputIssueSuffix": "", + "outputStringPrefix": "[", + "outputStringSuffix": "] " + } + ' > .commit.json && \ + + # Create the initial commit + echo "Hello, World!" > hello1 && \ + git add hello1 && \ + ../bin/commit "Initial commit" && \ + + # Create the second commit + touch hello2 && \ + git add hello2 && \ + ../bin/commit "Second commit" && \ + + # Checkout the previous commit + git checkout 'HEAD^' && \ + + # Create commit with alternative changes + touch hello3 && \ + git add hello3 && \ + ../bin/commit "Alternative commit" + + # Check if the commit was successful + if [ $? -ne 0 ]; then + fail_test $TESTNAME + fi + + # Check if the commit message is correct + if [ "$(git log -1 --pretty=%B)" != 'Alternative commit' ]; then + fail_test $TESTNAME + fi + + pass_test $TESTNAME +} + # MARK: - Run Tests build_if_needed @@ -260,3 +309,4 @@ test_use_config_from_current_directory test_commit_from_subdirectory test_set_correct_author test_use_config_with_empty_regex +test_commit_with_detached_head diff --git a/internal/helpers/constants.go b/internal/helpers/constants.go index c21588f..e86e4bf 100644 --- a/internal/helpers/constants.go +++ b/internal/helpers/constants.go @@ -7,4 +7,5 @@ const ( DEFAULT_OUTPUT_ISSUE_SUFFIX = "" DEFAULT_OUTPUT_STRING_PREFIX = "" DEFAULT_OUTPUT_STRING_SUFFIX = ": " + HEAD_REF_PREFIX = "ref: refs/heads/" )