@@ -4,8 +4,47 @@ const execPromisified = util.promisify(exec);
4
4
5
5
const fetchRepopath = require ( "../global/fetchGitRepoPath" ) ;
6
6
7
- const gitFetchApi = async ( repoId ) => {
8
- return await execPromisified ( `git fetch` , {
7
+ const getRemoteName = async ( repoId , remoteUrl ) => {
8
+ return await execPromisified ( `git remote -v` , {
9
+ cwd : fetchRepopath . getRepoPath ( repoId ) ,
10
+ windowsHide : true ,
11
+ } )
12
+ . then ( ( { stdout, stderr } ) => {
13
+ if ( stdout && ! stderr ) {
14
+ console . log ( stdout ) ;
15
+ const localName = stdout . trim ( ) . split ( "\n" ) ;
16
+ return localName
17
+ . filter ( ( item ) => {
18
+ if ( item . includes ( remoteUrl ) && item . includes ( "fetch" ) ) {
19
+ return true ;
20
+ }
21
+ } )
22
+ . join ( )
23
+ . split ( / \s / gi) [ 0 ] ;
24
+ } else {
25
+ console . log ( stderr ) ;
26
+ return "" ;
27
+ }
28
+ } )
29
+ . catch ( ( err ) => {
30
+ console . log ( err ) ;
31
+ return "" ;
32
+ } ) ;
33
+ } ;
34
+
35
+ const gitFetchApi = async ( repoId , remoteUrl , remoteBranch ) => {
36
+ const remoteName = await getRemoteName ( repoId , remoteUrl ) ;
37
+ console . log ( "Selected remote name : " , remoteName ) ;
38
+
39
+ if ( ! remoteName ) {
40
+ console . log ( "NO REMOTE MATCHING THE URL" ) ;
41
+
42
+ return {
43
+ status : "FETCH_ERROR" ,
44
+ } ;
45
+ }
46
+
47
+ return await execPromisified ( `git fetch ${ remoteName } ${ remoteBranch } ` , {
9
48
cwd : fetchRepopath . getRepoPath ( repoId ) ,
10
49
windowsHide : true ,
11
50
} )
@@ -41,14 +80,25 @@ const gitFetchApi = async (repoId) => {
41
80
} ) ;
42
81
} ;
43
82
44
- const gitPullApi = async ( repoId ) => {
45
- return await execPromisified ( `git pull` , {
83
+ const gitPullApi = async ( repoId , remoteUrl , remoteBranch ) => {
84
+ const remoteName = await getRemoteName ( repoId , remoteUrl ) ;
85
+ console . log ( "Selected remote name : " , remoteName ) ;
86
+
87
+ if ( ! remoteName ) {
88
+ console . log ( "NO REMOTE MATCHING THE URL" ) ;
89
+
90
+ return {
91
+ status : "PULL_ERROR" ,
92
+ } ;
93
+ }
94
+
95
+ return await execPromisified ( `git pull ${ remoteName } ${ remoteBranch } ` , {
46
96
cwd : fetchRepopath . getRepoPath ( repoId ) ,
47
97
windowsHide : true ,
48
98
} )
49
99
. then ( async ( { stdout, stderr } ) => {
50
- if ( stdout && ! stderr ) {
51
- const pullResponse = stdout . trim ( ) . split ( "\n" ) ;
100
+ if ( stdout || stderr ) {
101
+ const pullResponse = stderr . trim ( ) . split ( "\n" ) ;
52
102
53
103
if ( pullResponse && pullResponse . length > 0 ) {
54
104
return {
@@ -57,19 +107,19 @@ const gitPullApi = async (repoId) => {
57
107
} ;
58
108
} else {
59
109
return {
60
- status : "PULL_EMPTY " ,
110
+ status : "PULL_ABSENT " ,
61
111
} ;
62
112
}
63
113
} else {
64
114
return {
65
- status : "PULL_FAILED " ,
115
+ status : "PULL_ERROR " ,
66
116
} ;
67
117
}
68
118
} )
69
119
. catch ( ( err ) => {
70
120
console . log ( err ) ;
71
121
return {
72
- status : "PULL_FAILED " ,
122
+ status : "PULL_ERROR " ,
73
123
} ;
74
124
} ) ;
75
125
} ;
0 commit comments