9
9
"github.com/neel1996/gitconvex-server/global"
10
10
"github.com/neel1996/gitconvex-server/utils"
11
11
"runtime"
12
+ "strings"
12
13
"time"
13
14
)
14
15
@@ -33,40 +34,55 @@ func windowsCommit(repoPath string, msg string) string {
33
34
// The function falls back to the native git client for Windows platform due to an existing bug in the go-git library which
34
35
// blocks commits in windows platform
35
36
func CommitChanges (repo * git.Repository , commitMessage string ) string {
37
+ var formattedMessage = commitMessage
36
38
logger := global.Logger {}
37
39
w , wErr := repo .Worktree ()
38
40
39
- // Checking OS platform for switching to git client for Windows systems
40
- platform := runtime .GOOS
41
- if platform == "windows" && w != nil {
42
- logger .Log (fmt .Sprintf ("OS is %s -- Switching to native git client" , platform ), global .StatusWarning )
43
- return windowsCommit (w .Filesystem .Root (), commitMessage )
44
- }
41
+ if wErr != nil {
42
+ logger .Log (fmt .Sprintf ("Error occurred while fetching repo worktree -> %s" , wErr .Error ()), global .StatusError )
43
+ return "COMMIT_FAILED"
44
+ } else {
45
+ //Checking and splitting multi-line commit messages
46
+ if strings .Contains (commitMessage , "||" ) {
47
+ splitMessage := strings .Split (commitMessage , "||" )
48
+ formattedMessage = strings .Join (splitMessage , "\n " )
49
+ }
45
50
46
- // Logic to check if the repo / global config has proper user information setup
47
- // Commit will be signed by default user if no user config is present
48
- globalConfig , gCfgErr := repo .ConfigScoped (config .GlobalScope )
49
- localConfig , lCfgErr := repo .ConfigScoped (config .LocalScope )
50
- var author string
51
+ // Checking OS platform for switching to git client for Windows systems
52
+ platform := runtime .GOOS
53
+ if platform == "windows" && w != nil {
54
+ logger .Log (fmt .Sprintf ("OS is %s -- Switching to native git client" , platform ), global .StatusWarning )
55
+ return windowsCommit (w .Filesystem .Root (), formattedMessage )
56
+ }
57
+
58
+ // Checking if repo is a fresh repo with no branches
59
+ // fallback function will be used to commit with git if no branches are present
60
+ head , _ := repo .Head ()
61
+ if head == nil {
62
+ logger .Log ("Repo with no HEAD" , global .StatusWarning )
63
+ return windowsCommit (w .Filesystem .Root (), formattedMessage )
64
+ }
51
65
52
- if gCfgErr == nil && lCfgErr == nil {
53
- fmt .Println (localConfig .User )
54
- fmt .Println (globalConfig .User )
66
+ // Logic to check if the repo / global config has proper user information setup
67
+ // Commit will be signed by default user if no user config is present
68
+ globalConfig , gCfgErr := repo .ConfigScoped (config .GlobalScope )
69
+ localConfig , lCfgErr := repo .ConfigScoped (config .LocalScope )
70
+ var author string
55
71
56
- if globalConfig .User .Name != "" {
57
- author = globalConfig .User .Name
58
- } else if localConfig .User .Name != "" {
59
- author = localConfig .User .Name
72
+ if gCfgErr == nil && lCfgErr == nil {
73
+ fmt .Println (localConfig .User )
74
+ fmt .Println (globalConfig .User )
75
+
76
+ if globalConfig .User .Name != "" {
77
+ author = globalConfig .User .Name
78
+ } else if localConfig .User .Name != "" {
79
+ author = localConfig .User .Name
80
+ }
81
+ } else {
82
+ logger .Log (fmt .Sprintf ("Unable to fetch repo config -> %v || %v" , gCfgErr , lCfgErr ), global .StatusError )
83
+ return "COMMIT_FAILED"
60
84
}
61
- } else {
62
- logger .Log (fmt .Sprintf ("Unable to fetch repo config -> %v || %v" , gCfgErr , lCfgErr ), global .StatusError )
63
- return "COMMIT_FAILED"
64
- }
65
85
66
- if wErr != nil {
67
- logger .Log (fmt .Sprintf ("Error occurred while fetching repo worktree -> %s" , wErr .Error ()), global .StatusError )
68
- return "COMMIT_FAILED"
69
- } else {
70
86
var commitOptions * git.CommitOptions
71
87
var parentHash plumbing.Hash
72
88
head , headErr := repo .Head ()
@@ -89,13 +105,18 @@ func CommitChanges(repo *git.Repository, commitMessage string) string {
89
105
Parents : []plumbing.Hash {parentHash },
90
106
}
91
107
} else {
92
- logger .Log (fmt .Sprintf ("Commiting changes with author -> %s" , author ), global .StatusInfo )
108
+ logger .Log (fmt .Sprintf ("Commiting changes with author -> %s, message -> %s " , author , formattedMessage ), global .StatusInfo )
93
109
commitOptions = & git.CommitOptions {
94
110
All : false ,
95
111
Parents : []plumbing.Hash {parentHash },
96
112
}
97
113
}
98
- hash , err := w .Commit (commitMessage , commitOptions )
114
+
115
+ if formattedMessage == "" {
116
+ return "COMMIT_FAILED"
117
+ }
118
+
119
+ hash , err := w .Commit (formattedMessage , commitOptions )
99
120
if err != nil {
100
121
logger .Log (fmt .Sprintf ("Error occurred while committing changes -> %s\n %v" , err .Error (), err ), global .StatusError )
101
122
return "COMMIT_FAILED"
0 commit comments