@@ -3,10 +3,49 @@ const { exec } = require("child_process");
3
3
const util = require ( "util" ) ;
4
4
const execPromisified = util . promisify ( exec ) ;
5
5
6
- async function gitCommitLogHandler ( repoId ) {
6
+ async function gitCommitLogHandler ( repoId , skipLimit = 0 ) {
7
7
const repoPath = fetchRepopath . getRepoPath ( repoId ) ;
8
+ let commitLogLimit = 0 ;
9
+
10
+ const totalCommits = await execPromisified ( `git log --oneline` , {
11
+ cwd : repoPath ,
12
+ windowsHide : true ,
13
+ } )
14
+ . then ( ( res ) => {
15
+ const { stdout, stderr } = res ;
16
+ if ( stderr ) {
17
+ console . log ( stderr ) ;
18
+ }
19
+ if ( res && ! res . stderr ) {
20
+ const gitLocalTotal = stdout . trim ( ) . split ( "\n" ) . length ;
21
+ return gitLocalTotal ;
22
+ } else {
23
+ console . log ( stderr ) ;
24
+ return 0 ;
25
+ }
26
+ } )
27
+ . catch ( ( err ) => {
28
+ console . log ( err ) ;
29
+ return 0 ;
30
+ } ) ;
31
+
32
+ console . log ( "Total commits in the repo : " , totalCommits ) ;
33
+
34
+ commitLogLimit = totalCommits < 10 ? totalCommits : 10 ;
35
+
36
+ if ( ! totalCommits ) {
37
+ return {
38
+ hash : "" ,
39
+ author : "" ,
40
+ commitTime : "" ,
41
+ commitMessage : "" ,
42
+ commitRelativeTime : "" ,
43
+ commitFilesCount : 0 ,
44
+ } ;
45
+ }
46
+
8
47
return await execPromisified (
9
- `git log --pretty=format:"%h||%an||%ad||%s" --date=short` ,
48
+ `git log -n ${ commitLogLimit } --skip ${ skipLimit } - -pretty=format:"%h||%an||%ad||%s" --date=short` ,
10
49
{
11
50
cwd : repoPath ,
12
51
windowsHide : true ,
@@ -19,7 +58,7 @@ async function gitCommitLogHandler(repoId) {
19
58
let commits = stdout . trim ( ) ;
20
59
21
60
let commitRelativeTime = await execPromisified (
22
- `git log --pretty=format:"%ad" --date=relative` ,
61
+ `git log -n ${ commitLogLimit } --skip ${ skipLimit } - -pretty=format:"%ad" --date=relative` ,
23
62
{ cwd : repoPath , windowsHide : true }
24
63
)
25
64
. then ( ( { stdout, stderr } ) => {
@@ -47,28 +86,30 @@ async function gitCommitLogHandler(repoId) {
47
86
if ( stdout ) {
48
87
return stdout . trim ( ) . split ( "\n" ) . length ;
49
88
} else {
50
- console . log ( "Error occurred!" ) ;
89
+ console . log ( stderr ) ;
51
90
return 0 ;
52
91
}
53
92
} )
54
93
. catch ( ( err ) => {
55
- console . log ( "Error occurred!" ) ;
94
+ console . log ( err ) ;
56
95
return 0 ;
57
96
} ) ;
58
97
commit += "||" + commitFilesCount ;
59
98
return commitModel ( commit ) ;
60
99
} ) ;
61
100
return {
101
+ totalCommits : totalCommits ,
62
102
commits : commitArray ,
63
103
} ;
64
104
} else {
65
105
return {
106
+ totalCommits : 0 ,
66
107
commits : [ ] ,
67
108
} ;
68
109
}
69
110
} )
70
111
. catch ( ( err ) => {
71
- console . log ( "ERROR : Commit log collection Error!" ) ;
112
+ console . log ( "ERROR : Commit log collection Error!" , err ) ;
72
113
return {
73
114
hash : "" ,
74
115
author : "" ,
@@ -92,12 +133,11 @@ function commitModel(commit) {
92
133
93
134
let commitSplit = commit . split ( "||" ) ;
94
135
95
- commitObject . hash = commitSplit [ 0 ] ;
96
- commitObject . author = commitSplit [ 1 ] ;
97
- commitObject . commitTime = commitSplit [ 2 ] ;
98
- commitObject . commitMessage = commitSplit [ 3 ] ;
99
- commitObject . commitRelativeTime = commitSplit [ 4 ] ;
100
- commitObject . commitFilesCount = commitSplit [ 5 ] ;
136
+ const objKeys = Object . keys ( commitObject ) ;
137
+
138
+ for ( let i = 0 ; i < objKeys . length ; i ++ ) {
139
+ commitObject [ objKeys [ i ] ] = commitSplit [ i ] ;
140
+ }
101
141
102
142
return commitObject ;
103
143
}
0 commit comments