6
6
"gopkg.in/src-d/go-billy.v4/osfs"
7
7
"os"
8
8
"path/filepath"
9
- "regexp"
10
9
"strconv"
10
+ "strings"
11
11
)
12
12
13
13
type GitCliManager interface {
@@ -152,27 +152,27 @@ func (impl *GitCliManagerImpl) GitShow(gitCtx GitContext, rootDir string, hash s
152
152
return commits [0 ], nil
153
153
}
154
154
155
+ func (impl * GitCliManagerImpl ) GetCommitStats (gitCtx GitContext , commit GitCommit ) (FileStats , error ) {
156
+ gitCommit := commit .GetCommit ()
157
+ fileStat , errorMsg , err := impl .FetchDiffStatBetweenCommits (gitCtx , gitCommit .Commit , "" , gitCommit .CheckoutPath )
158
+ if err != nil {
159
+ impl .logger .Errorw ("error in fetching fileStat of commit: " , gitCommit .Commit , "checkoutPath" , gitCommit .CheckoutPath , "errorMsg" , errorMsg , "err" , err )
160
+ return nil , err
161
+ }
162
+ return getFileStat (fileStat )
163
+ }
164
+
155
165
func (impl * GitCliManagerImpl ) processGitLogOutput (out string , rootDir string ) ([]GitCommit , error ) {
156
166
167
+ gitCommits := make ([]GitCommit , 0 )
157
168
if len (out ) == 0 {
158
- return make ([] GitCommit , 0 ) , nil
169
+ return gitCommits , nil
159
170
}
160
-
161
- logOut := strconv .Quote (out )
162
-
163
- m1 := regexp .MustCompile ("devtron_delimiter" )
164
- logOut = m1 .ReplaceAllString (logOut , `"` )
165
-
166
- logOut = logOut [:len (logOut )- 1 ] // Remove the last ","
167
- logOut = fmt .Sprintf ("[%s]" , logOut ) // Add []
168
-
169
- var gitCommitFormattedList []GitCommitFormat
170
- err := json .Unmarshal ([]byte (logOut ), & gitCommitFormattedList )
171
+ gitCommitFormattedList , err := parseFormattedLogOutput (out )
171
172
if err != nil {
172
- return nil , err
173
+ return gitCommits , err
173
174
}
174
175
175
- gitCommits := make ([]GitCommit , 0 )
176
176
for _ , formattedCommit := range gitCommitFormattedList {
177
177
178
178
cm := GitCommitBase {
@@ -189,12 +189,23 @@ func (impl *GitCliManagerImpl) processGitLogOutput(out string, rootDir string) (
189
189
return gitCommits , nil
190
190
}
191
191
192
- func (impl * GitCliManagerImpl ) GetCommitStats (gitCtx GitContext , commit GitCommit ) (FileStats , error ) {
193
- gitCommit := commit .GetCommit ()
194
- fileStat , errorMsg , err := impl .FetchDiffStatBetweenCommits (gitCtx , gitCommit .Commit , "" , gitCommit .CheckoutPath )
192
+ func parseFormattedLogOutput (out string ) ([]GitCommitFormat , error ) {
193
+ //remove the new line character which is after each terminal comma
194
+ out = strings .ReplaceAll (out , "},\n " , "}," )
195
+
196
+ // to escape the special characters like quotes and newline characters in the commit data
197
+ logOut := strconv .Quote (out )
198
+
199
+ //replace the delimiter with quotes to make it parsable json
200
+ logOut = strings .ReplaceAll (logOut , "devtron_delimiter" , `"` )
201
+
202
+ logOut = logOut [1 : len (logOut )- 2 ] // trim surround characters (surrounding quotes and trailing com,a)
203
+ logOut = fmt .Sprintf ("[%s]" , logOut ) // Add []
204
+
205
+ var gitCommitFormattedList []GitCommitFormat
206
+ err := json .Unmarshal ([]byte (logOut ), & gitCommitFormattedList )
195
207
if err != nil {
196
- impl .logger .Errorw ("error in fetching fileStat of commit: " , gitCommit .Commit , "checkoutPath" , gitCommit .CheckoutPath , "errorMsg" , errorMsg , "err" , err )
197
208
return nil , err
198
209
}
199
- return getFileStat ( fileStat )
200
- }
210
+ return gitCommitFormattedList , nil
211
+ }
0 commit comments