Skip to content
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
50 changes: 24 additions & 26 deletions cmd/commit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"time"

"github.com/artem-y/commit/internal/config"
Expand All @@ -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"
)

Expand Down Expand Up @@ -47,36 +47,44 @@ 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)
os.Exit(1)
}

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
Expand Down Expand Up @@ -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()
Expand Down
54 changes: 52 additions & 2 deletions e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions internal/helpers/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ const (
DEFAULT_OUTPUT_ISSUE_SUFFIX = ""
DEFAULT_OUTPUT_STRING_PREFIX = ""
DEFAULT_OUTPUT_STRING_SUFFIX = ": "
HEAD_REF_PREFIX = "ref: refs/heads/"
)
Loading