@@ -41,28 +41,43 @@ const getGitStatus = async (repoPath) => {
41
41
} ) ;
42
42
43
43
// Module to get git remote repo URL
44
- await execPromised (
45
- `${ currentDir } if [ ! -z "\`git remote\`" ]; then git remote | xargs -L 1 git remote get-url; else echo "NO_REMOTE"; fi`
46
- )
47
- . then ( ( res ) => {
48
- const { stdout, stderr } = res ;
49
- if ( stderr !== "" ) {
50
- console . log ( stderr ) ;
51
- gitRemoteData = "NO_REMOTE" ;
52
- } else {
53
- gitRemoteData = stdout . trim ( ) ;
54
- console . log ( "REMOTE : " + gitRemoteData ) ;
55
- if ( gitRemoteData . split ( "\n" ) . length > 0 ) {
56
- const splitRemote = gitRemoteData . split ( "\n" ) ;
57
- gitRemoteData = splitRemote . join ( "||" ) ;
58
- console . log ( gitRemoteData ) ;
44
+
45
+ let gitRemotePromise =
46
+ isGitLogAvailable &&
47
+ ( await execPromised ( `${ currentDir } git remote` ) . then (
48
+ ( { stdout, stderr } ) => {
49
+ if ( stdout && ! stderr ) {
50
+ const localRemote = stdout . trim ( ) . split ( "\n" ) ;
51
+
52
+ const multiPromise = Promise . all (
53
+ localRemote &&
54
+ localRemote . map ( async ( remote ) => {
55
+ console . log ( "LOOP ::" , remote ) ;
56
+ return await execPromised (
57
+ `${ currentDir } git remote get-url ${ remote } `
58
+ ) . then ( ( { stdout, stderr } ) => {
59
+ if ( stdout && ! stderr ) {
60
+ console . log ( "REMOTE :: " , stdout ) ;
61
+ return stdout . trim ( ) ;
62
+ } else {
63
+ console . log ( stderr ) ;
64
+ }
65
+ } ) ;
66
+ } )
67
+ ) ;
68
+ return multiPromise ;
69
+ } else {
70
+ console . log ( stderr ) ;
71
+ return null ;
59
72
}
60
73
}
61
- } )
62
- . catch ( ( err ) => {
63
- console . log ( "Error GIT : " + err ) ;
64
- gitRemoteData = "NO_REMOTE" ;
65
- } ) ;
74
+ ) ) ;
75
+
76
+ if ( gitRemotePromise ) {
77
+ gitRemoteData = gitRemotePromise . join ( "||" ) ;
78
+ } else {
79
+ gitRemoteData = "NO_REMOTE" ;
80
+ }
66
81
67
82
// Module to get Git actual repo name
68
83
if ( gitRemoteData && gitRemoteData !== "NO_REMOTE" ) {
@@ -117,15 +132,24 @@ const getGitStatus = async (repoPath) => {
117
132
118
133
// Module to get total number of commits to current branch
119
134
isGitLogAvailable &&
120
- ( await execPromised ( `${ currentDir } git log --oneline | wc -l ` )
135
+ ( await execPromised ( `${ currentDir } git log --oneline` )
121
136
. then ( ( res ) => {
122
137
const { stdout, stderr } = res ;
123
138
if ( stderr ) {
124
139
console . log ( stderr ) ;
125
140
}
126
141
if ( res && ! res . stderr ) {
127
- gitTotalCommits = res . stdout . trim ( ) ;
142
+ const gitLocalTotal = res . stdout . trim ( ) . split ( "\n" ) ;
143
+ if ( gitLocalTotal && gitLocalTotal . length > 0 ) {
144
+ gitTotalCommits = gitLocalTotal . length ;
145
+ } else if ( gitLocalTotal . length === 1 ) {
146
+ gitTotalCommits = 1 ;
147
+ }
148
+ } else {
149
+ gitTotalCommits = 0 ;
150
+ console . log ( stderr ) ;
128
151
}
152
+ return gitTotalCommits ;
129
153
} )
130
154
. catch ( ( err ) => {
131
155
gitTotalCommits = 0 ;
@@ -144,44 +168,74 @@ const getGitStatus = async (repoPath) => {
144
168
//Module to get all git tracked files
145
169
var gitTrackedFileDetails = [ ] ;
146
170
147
- isGitLogAvailable &&
148
- ( await execPromised (
149
- `${ currentDir } for i in \`git ls-tree --name-status HEAD\`; do if [ -f $i ] || [ -d $i ] ; then file $i; fi; done`
150
- ) . then ( ( res ) => {
151
- const { stdout, stderr } = res ;
152
- if ( res && ! stderr ) {
153
- gitTrackedFiles = stdout . trim ( ) . split ( "\n" ) ;
154
- } else {
155
- console . log ( stderr ) ;
171
+ gitTrackedFiles =
172
+ isGitLogAvailable &&
173
+ ( await execPromised ( `${ currentDir } git ls-tree --name-status HEAD` ) . then (
174
+ ( { stdout, stderr } ) => {
175
+ if ( stdout && ! stderr ) {
176
+ const fileList = stdout . trim ( ) . split ( "\n" ) ;
177
+
178
+ const localFiles = Promise . all (
179
+ fileList . map ( async ( item ) => {
180
+ gitTrackedFileDetails . push ( item ) ;
181
+
182
+ return await fs . promises
183
+ . stat ( `${ item } ` )
184
+ . then ( ( fileType ) => {
185
+ if ( fileType . isFile ( ) ) {
186
+ return `${ item } : File` ;
187
+ } else if ( fileType . isDirectory ( ) ) {
188
+ return `${ item } : directory` ;
189
+ } else {
190
+ return `${ item } : File` ;
191
+ }
192
+ } )
193
+ . catch ( ( err ) => {
194
+ console . log ( err ) ;
195
+ return `${ item } : File` ;
196
+ } ) ;
197
+ } )
198
+ ) ;
199
+ return localFiles ;
200
+ } else {
201
+ console . log ( stderr ) ;
202
+ return [ ] ;
203
+ }
156
204
}
157
- } ) ) ;
205
+ ) ) ;
158
206
159
207
//Module to fetch commit for each file and folder
160
208
161
209
var gitFileBasedCommit = [ ] ;
162
210
163
- isGitLogAvailable &&
164
- ( await execPromised (
165
- `${ currentDir } for i in \`git ls-tree --name-status HEAD\`; do git log -1 --oneline $i; done 2> /dev/null`
166
- ) . then ( ( res ) => {
167
- const { stdout, stderr } = res ;
168
-
169
- if ( res && ! stderr ) {
170
- gitFileBasedCommit = stdout
171
- . split ( "\n" )
172
- . filter ( ( elm ) => ( elm ? elm : null ) ) ;
173
- } else {
174
- console . log ( stderr ) ;
175
- }
176
- } ) ) ;
211
+ gitFileBasedCommit =
212
+ isGitLogAvailable &&
213
+ ( await Promise . all (
214
+ gitTrackedFileDetails . map ( async ( gitFile ) => {
215
+ return await execPromised (
216
+ `${ currentDir } git log -1 --oneline ${ gitFile } `
217
+ ) . then ( ( { stdout, stderr } ) => {
218
+ if ( stdout && ! stderr ) {
219
+ return stdout . trim ( ) ;
220
+ } else {
221
+ console . log ( stderr ) ;
222
+ return "" ;
223
+ }
224
+ } ) ;
225
+ } )
226
+ ) ) ;
177
227
178
228
//Module to get totally tracked git artifacts
179
229
180
230
isGitLogAvailable &&
181
- ( await execPromised ( `${ currentDir } git ls-files | wc -l ` ) . then ( ( res ) => {
231
+ ( await execPromised ( `${ currentDir } git ls-files` ) . then ( ( res ) => {
182
232
const { stdout, stderr } = res ;
183
- if ( res && ! stderr ) {
184
- gitTotalTrackedFiles = Number ( stdout . trim ( ) ) ;
233
+ if ( stdout && ! stderr ) {
234
+ if ( stdout . split ( "\n" ) ) {
235
+ gitTotalTrackedFiles = Number ( stdout . trim ( ) . split ( "\n" ) . length ) ;
236
+ } else {
237
+ return 0 ;
238
+ }
185
239
} else {
186
240
console . log ( stderr ) ;
187
241
}
0 commit comments