@@ -49,6 +49,38 @@ export function getTemporaryDirectory(): string {
49
49
: getRequiredEnvParam ( "RUNNER_TEMP" ) ;
50
50
}
51
51
52
+ async function runGitCommand (
53
+ checkoutPath : string | undefined ,
54
+ args : string [ ] ,
55
+ customErrorMessage : string ,
56
+ ) : Promise < string > {
57
+ let stdout = "" ;
58
+ let stderr = "" ;
59
+ try {
60
+ await new toolrunner . ToolRunner ( await safeWhich . safeWhich ( "git" ) , args , {
61
+ silent : true ,
62
+ listeners : {
63
+ stdout : ( data ) => {
64
+ stdout += data . toString ( ) ;
65
+ } ,
66
+ stderr : ( data ) => {
67
+ stderr += data . toString ( ) ;
68
+ } ,
69
+ } ,
70
+ cwd : checkoutPath ,
71
+ } ) . exec ( ) ;
72
+ return stdout ;
73
+ } catch ( error ) {
74
+ let reason = stderr ;
75
+ if ( stderr . includes ( "not a git repository" ) ) {
76
+ reason =
77
+ "The checkout path provided to the action does not appear to be a git repository." ;
78
+ }
79
+ core . info ( `git call failed. ${ customErrorMessage } Error: ${ reason } ` ) ;
80
+ throw error ;
81
+ }
82
+ }
83
+
52
84
/**
53
85
* Gets the SHA of the commit that is currently checked out.
54
86
*/
@@ -63,38 +95,14 @@ export const getCommitOid = async function (
63
95
// the merge commit, which must mean that git is available.
64
96
// Even if this does go wrong, it's not a huge problem for the alerts to
65
97
// reported on the merge commit.
66
- let stderr = "" ;
67
98
try {
68
- let commitOid = "" ;
69
- await new toolrunner . ToolRunner (
70
- await safeWhich . safeWhich ( "git" ) ,
99
+ const stdout = await runGitCommand (
100
+ checkoutPath ,
71
101
[ "rev-parse" , ref ] ,
72
- {
73
- silent : true ,
74
- listeners : {
75
- stdout : ( data ) => {
76
- commitOid += data . toString ( ) ;
77
- } ,
78
- stderr : ( data ) => {
79
- stderr += data . toString ( ) ;
80
- } ,
81
- } ,
82
- cwd : checkoutPath ,
83
- } ,
84
- ) . exec ( ) ;
85
- return commitOid . trim ( ) ;
102
+ "Continuing with commit SHA from user input or environment." ,
103
+ ) ;
104
+ return stdout . trim ( ) ;
86
105
} catch {
87
- if ( stderr . includes ( "not a git repository" ) ) {
88
- core . info (
89
- "Could not determine current commit SHA using git. Continuing with data from user input or environment. " +
90
- "The checkout path provided to the action does not appear to be a git repository." ,
91
- ) ;
92
- } else {
93
- core . info (
94
- `Could not determine current commit SHA using git. Continuing with data from user input or environment. ${ stderr } ` ,
95
- ) ;
96
- }
97
-
98
106
return getOptionalInput ( "sha" ) || getRequiredEnvParam ( "GITHUB_SHA" ) ;
99
107
}
100
108
} ;
@@ -113,37 +121,29 @@ export const determineMergeBaseCommitOid = async function (
113
121
const mergeSha = getRequiredEnvParam ( "GITHUB_SHA" ) ;
114
122
const checkoutPath =
115
123
checkoutPathOverride ?? getOptionalInput ( "checkout_path" ) ;
116
- let stderr = "" ;
117
124
118
125
try {
119
126
let commitOid = "" ;
120
127
let baseOid = "" ;
121
128
let headOid = "" ;
122
129
123
- await new toolrunner . ToolRunner (
124
- await safeWhich . safeWhich ( "git" ) ,
130
+ const stdout = await runGitCommand (
131
+ checkoutPath ,
125
132
[ "show" , "-s" , "--format=raw" , mergeSha ] ,
126
- {
127
- silent : true ,
128
- listeners : {
129
- stdline : ( data ) => {
130
- if ( data . startsWith ( "commit " ) && commitOid === "" ) {
131
- commitOid = data . substring ( 7 ) ;
132
- } else if ( data . startsWith ( "parent " ) ) {
133
- if ( baseOid === "" ) {
134
- baseOid = data . substring ( 7 ) ;
135
- } else if ( headOid === "" ) {
136
- headOid = data . substring ( 7 ) ;
137
- }
138
- }
139
- } ,
140
- stderr : ( data ) => {
141
- stderr += data . toString ( ) ;
142
- } ,
143
- } ,
144
- cwd : checkoutPath ,
145
- } ,
146
- ) . exec ( ) ;
133
+ "Will calculate the base branch SHA on the server." ,
134
+ ) ;
135
+
136
+ for ( const data of stdout . split ( "\n" ) ) {
137
+ if ( data . startsWith ( "commit " ) && commitOid === "" ) {
138
+ commitOid = data . substring ( 7 ) ;
139
+ } else if ( data . startsWith ( "parent " ) ) {
140
+ if ( baseOid === "" ) {
141
+ baseOid = data . substring ( 7 ) ;
142
+ } else if ( headOid === "" ) {
143
+ headOid = data . substring ( 7 ) ;
144
+ }
145
+ }
146
+ }
147
147
148
148
// Let's confirm our assumptions: We had a merge commit and the parsed parent data looks correct
149
149
if (
@@ -155,17 +155,6 @@ export const determineMergeBaseCommitOid = async function (
155
155
}
156
156
return undefined ;
157
157
} catch {
158
- if ( stderr . includes ( "not a git repository" ) ) {
159
- core . info (
160
- "The checkout path provided to the action does not appear to be a git repository. " +
161
- "Will calculate the merge base on the server." ,
162
- ) ;
163
- } else {
164
- core . info (
165
- `Failed to call git to determine merge base. Will calculate the merge base on ` +
166
- `the server. Reason: ${ stderr } ` ,
167
- ) ;
168
- }
169
158
return undefined ;
170
159
}
171
160
} ;
0 commit comments