@@ -49,6 +49,38 @@ export function getTemporaryDirectory(): string {
4949 : getRequiredEnvParam ( "RUNNER_TEMP" ) ;
5050}
5151
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+
5284/**
5385 * Gets the SHA of the commit that is currently checked out.
5486 */
@@ -63,38 +95,14 @@ export const getCommitOid = async function (
6395 // the merge commit, which must mean that git is available.
6496 // Even if this does go wrong, it's not a huge problem for the alerts to
6597 // reported on the merge commit.
66- let stderr = "" ;
6798 try {
68- let commitOid = "" ;
69- await new toolrunner . ToolRunner (
70- await safeWhich . safeWhich ( "git" ) ,
99+ const stdout = await runGitCommand (
100+ checkoutPath ,
71101 [ "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 ( ) ;
86105 } 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-
98106 return getOptionalInput ( "sha" ) || getRequiredEnvParam ( "GITHUB_SHA" ) ;
99107 }
100108} ;
@@ -113,37 +121,29 @@ export const determineMergeBaseCommitOid = async function (
113121 const mergeSha = getRequiredEnvParam ( "GITHUB_SHA" ) ;
114122 const checkoutPath =
115123 checkoutPathOverride ?? getOptionalInput ( "checkout_path" ) ;
116- let stderr = "" ;
117124
118125 try {
119126 let commitOid = "" ;
120127 let baseOid = "" ;
121128 let headOid = "" ;
122129
123- await new toolrunner . ToolRunner (
124- await safeWhich . safeWhich ( "git" ) ,
130+ const stdout = await runGitCommand (
131+ checkoutPath ,
125132 [ "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+ }
147147
148148 // Let's confirm our assumptions: We had a merge commit and the parsed parent data looks correct
149149 if (
@@ -155,17 +155,6 @@ export const determineMergeBaseCommitOid = async function (
155155 }
156156 return undefined ;
157157 } 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- }
169158 return undefined ;
170159 }
171160} ;
0 commit comments