55 "fmt"
66 "os"
77 "path/filepath"
8+ "strings"
89 "time"
910
1011 "github.com/artem-y/commit/internal/config"
@@ -13,7 +14,6 @@ import (
1314 "github.com/artem-y/commit/internal/user"
1415
1516 "github.com/go-git/go-git/v5"
16- "github.com/go-git/go-git/v5/plumbing"
1717 "github.com/go-git/go-git/v5/plumbing/object"
1818)
1919
@@ -47,36 +47,44 @@ func main() {
4747 )
4848 }
4949
50- headRef := getCurrentHead (repo )
50+ headFilePath := filepath .Join (
51+ worktree .Filesystem .Root (),
52+ ".git" ,
53+ "HEAD" ,
54+ )
55+
56+ fileReader := config.FileReader {}
5157
52- // Read branch name or HEAD
53- if headRef .Name ().IsBranch () {
58+ // Read current HEAD from file
59+ headFile , _ := fileReader .ReadFile (headFilePath )
60+ headFileText := string (headFile )
61+
62+ // If there is a branch name in the HEAD file, modify the commit message
63+ if strings .HasPrefix (headFileText , helpers .HEAD_REF_PREFIX ) {
64+ branchName := strings .TrimPrefix (
65+ headFileText ,
66+ helpers .HEAD_REF_PREFIX ,
67+ )
5468
55- fileReader := config.FileReader {}
5669 cfg , err := config .ReadCommitConfig (fileReader , configFilePath )
5770 if err != nil {
5871 fmt .Fprintf (os .Stderr , helpers .Red ("Failed to read config: %v\n " ), err )
5972 os .Exit (1 )
6073 }
6174
6275 messageGenerator := message_generator.MessageGenerator {
63- BranchName : headRef . Name (). Short () ,
76+ BranchName : branchName ,
6477 UserMessage : commitMessage ,
6578 Config : cfg ,
6679 }
6780 commitMessage = messageGenerator .GenerateMessage ()
81+ }
6882
69- if ! dryRun {
70- commitChanges (repo , worktree , commitMessage )
71- }
72-
73- fmt .Println (commitMessage )
74-
75- } else if headRef .Name ().IsTag () {
76- fmt .Printf ("HEAD is a tag: %v\n " , headRef .Name ().Short ())
77- } else {
78- fmt .Printf ("Detached HEAD at %v\n " , headRef .Hash ())
83+ if ! dryRun {
84+ commitChanges (repo , worktree , commitMessage )
7985 }
86+
87+ fmt .Println (commitMessage )
8088}
8189
8290// Reads commit message from command line arguments
@@ -104,16 +112,6 @@ func openRepo() *git.Repository {
104112 return repo
105113}
106114
107- // Reads the current HEAD reference
108- func getCurrentHead (repo * git.Repository ) * plumbing.Reference {
109- headRef , err := repo .Head ()
110- if err != nil {
111- fmt .Fprintf (os .Stderr , helpers .Red ("Failed to read current HEAD: %v\n " ), err )
112- os .Exit (1 )
113- }
114- return headRef
115- }
116-
117115// Opens worktree
118116func openWorktree (repo * git.Repository ) * git.Worktree {
119117 worktree , err := repo .Worktree ()
0 commit comments