1
1
import { Action , Step } from '../../actions' ;
2
2
import { validateUser } from './checkUserPushPermission' ;
3
3
import simpleGit from 'simple-git' ;
4
+ import { EMPTY_COMMIT_HASH } from '../constants' ;
4
5
5
6
const isEmptyBranch = async ( action : Action ) => {
6
7
const git = simpleGit ( `${ action . proxyGitPath } /${ action . repoName } ` ) ;
7
8
8
- if ( action . commitFrom === '0' . repeat ( 40 ) ) {
9
+ if ( action . commitFrom === EMPTY_COMMIT_HASH ) {
9
10
try {
10
11
const type = await git . raw ( [ 'cat-file' , '-t' , action . commitTo || '' ] ) ;
11
12
const known = type . trim ( ) === 'commit' ;
@@ -23,46 +24,47 @@ const isEmptyBranch = async (action: Action) => {
23
24
const exec = async ( req : any , action : Action ) : Promise < Action > => {
24
25
const step = new Step ( 'getMissingData' ) ;
25
26
26
- try {
27
- if ( ! action . commitData ) {
28
- action . commitData = [ ] ;
29
- }
27
+ if ( action . commitData && action . commitData . length > 0 ) {
28
+ console . log ( 'getMissingData' , action ) ;
29
+ return action ;
30
+ }
30
31
31
- if ( action . commitData . length === 0 ) {
32
- if ( await isEmptyBranch ( action ) ) {
33
- step . setError ( 'Push blocked: Empty branch. Please make a commit before pushing a new branch.' ) ;
34
- action . addStep ( step ) ;
35
- step . error = true ;
36
- return action ;
37
- }
38
- console . log ( `commitData not found, fetching missing commits from git...` ) ;
39
- const path = `${ action . proxyGitPath } /${ action . repoName } ` ;
40
- const git = simpleGit ( path ) ;
41
- const log = await git . log ( { from : action . commitFrom , to : action . commitTo } ) ;
32
+ if ( await isEmptyBranch ( action ) ) {
33
+ step . setError ( 'Push blocked: Empty branch. Please make a commit before pushing a new branch.' ) ;
34
+ action . addStep ( step ) ;
35
+ step . error = true ;
36
+ return action ;
37
+ }
38
+ console . log ( `commitData not found, fetching missing commits from git...` ) ;
42
39
43
- action . commitData = log . all . toReversed ( ) . map ( ( entry , i , array ) => {
44
- const parent = i === 0 ? action . commitFrom : array [ i - 1 ] . hash ;
45
- const timestamp = Math . floor ( new Date ( entry . date ) . getTime ( ) / 1000 ) . toString ( ) ;
46
- return {
47
- message : entry . message || '' ,
48
- committer : entry . author_name || '' ,
49
- tree : entry . hash || '' ,
50
- parent : parent || '0' . repeat ( 40 ) ,
51
- author : entry . author_name || '' ,
52
- authorEmail : entry . author_email || '' ,
53
- commitTimestamp : timestamp ,
54
- }
55
- } ) ;
56
- console . log ( `Updated commitData:` , { commitData : action . commitData } ) ;
40
+ try {
41
+ const path = `${ action . proxyGitPath } /${ action . repoName } ` ;
42
+ const git = simpleGit ( path ) ;
43
+ const log = await git . log ( { from : action . commitFrom , to : action . commitTo } ) ;
57
44
58
- if ( action . commitFrom === '0000000000000000000000000000000000000000' ) {
59
- action . commitFrom = action . commitData [ action . commitData . length - 1 ] . parent ;
45
+ action . commitData = log . all . toReversed ( ) . map ( ( entry , i , array ) => {
46
+ const parent = i === 0 ? action . commitFrom : array [ i - 1 ] . hash ;
47
+ const timestamp = Math . floor ( new Date ( entry . date ) . getTime ( ) / 1000 ) . toString ( ) ;
48
+ return {
49
+ message : entry . message || '' ,
50
+ committer : entry . author_name || '' ,
51
+ tree : entry . hash || '' ,
52
+ parent : parent || EMPTY_COMMIT_HASH ,
53
+ author : entry . author_name || '' ,
54
+ authorEmail : entry . author_email || '' ,
55
+ commitTimestamp : timestamp ,
60
56
}
61
- const user = action . commitData [ action . commitData . length - 1 ] . committer ;
62
- action . user = user ;
57
+ } ) ;
58
+ console . log ( `Updated commitData:` , { commitData : action . commitData } ) ;
63
59
64
- return await validateUser ( user , action , step ) ;
60
+ if ( action . commitFrom === EMPTY_COMMIT_HASH ) {
61
+ action . commitFrom = action . commitData [ action . commitData . length - 1 ] . parent ;
65
62
}
63
+
64
+ const user = action . commitData [ action . commitData . length - 1 ] . committer ;
65
+ action . user = user ;
66
+
67
+ return await validateUser ( user , action , step ) ;
66
68
} catch ( e : any ) {
67
69
step . setError ( e . toString ( 'utf-8' ) ) ;
68
70
} finally {
0 commit comments