@@ -97,13 +97,21 @@ export type GitCreateAndPushBranchOptions = {
9797 user ?: string | undefined
9898}
9999
100- export async function gitCleanFdx ( cwd = process . cwd ( ) ) : Promise < void > {
100+ export async function gitCleanFdx ( cwd = process . cwd ( ) ) : Promise < boolean > {
101101 const stdioIgnoreOptions : SpawnOptions = {
102102 cwd,
103103 stdio : isDebug ( 'stdio' ) ? 'inherit' : 'ignore' ,
104104 }
105- // TODO: propagate CResult?
106- await spawn ( 'git' , [ 'clean' , '-fdx' ] , stdioIgnoreOptions )
105+ const quotedCmd = '`git clean -fdx`'
106+ debugFn ( 'stdio' , `spawn: ${ quotedCmd } ` )
107+ try {
108+ await spawn ( 'git' , [ 'clean' , '-fdx' ] , stdioIgnoreOptions )
109+ return true
110+ } catch ( e ) {
111+ debugFn ( 'error' , `caught: ${ quotedCmd } failed` )
112+ debugDir ( 'inspect' , { error : e } )
113+ }
114+ return false
107115}
108116
109117export async function gitCheckoutBranch (
@@ -114,10 +122,15 @@ export async function gitCheckoutBranch(
114122 cwd,
115123 stdio : isDebug ( 'stdio' ) ? 'inherit' : 'ignore' ,
116124 }
125+ const quotedCmd = `\`git checkout ${ branch } \``
126+ debugFn ( 'stdio' , `spawn: ${ quotedCmd } ` )
117127 try {
118128 await spawn ( 'git' , [ 'checkout' , branch ] , stdioIgnoreOptions )
119129 return true
120- } catch { }
130+ } catch ( e ) {
131+ debugFn ( 'error' , `caught: ${ quotedCmd } failed` )
132+ debugDir ( 'inspect' , { error : e } )
133+ }
121134 return false
122135}
123136
@@ -132,10 +145,15 @@ export async function gitCreateBranch(
132145 cwd,
133146 stdio : isDebug ( 'stdio' ) ? 'inherit' : 'ignore' ,
134147 }
148+ const quotedCmd = `\`git branch ${ branch } \``
149+ debugFn ( 'stdio' , `spawn: ${ quotedCmd } ` )
135150 try {
136151 await spawn ( 'git' , [ 'branch' , branch ] , stdioIgnoreOptions )
137152 return true
138- } catch { }
153+ } catch ( e ) {
154+ debugFn ( 'error' , `caught: ${ quotedCmd } failed` )
155+ debugDir ( 'inspect' , { error : e } )
156+ }
139157 return false
140158}
141159
@@ -147,6 +165,8 @@ export async function gitPushBranch(
147165 cwd,
148166 stdio : isDebug ( 'stdio' ) ? 'inherit' : 'ignore' ,
149167 }
168+ const quotedCmd = `\`git push --force --set-upstream origin ${ branch } \``
169+ debugFn ( 'stdio' , `spawn: ${ quotedCmd } ` )
150170 try {
151171 await spawn (
152172 'git' ,
@@ -155,14 +175,11 @@ export async function gitPushBranch(
155175 )
156176 return true
157177 } catch ( e ) {
158- debugFn (
159- 'error' ,
160- `caught: git push --force --set-upstream origin ${ branch } failed` ,
161- )
178+ debugFn ( 'error' , `caught: ${ quotedCmd } failed` )
162179 if ( isSpawnError ( e ) && e . code === 128 ) {
163180 debugFn (
164181 'error' ,
165- ' denied: token requires contents: write; pull-requests: write permissions' ,
182+ " denied: token requires write permissions for ' contents' and ' pull-requests'" ,
166183 )
167184 }
168185 debugDir ( 'inspect' , { error : e } )
@@ -186,16 +203,31 @@ export async function gitCommit(
186203 // Lazily access constants.ENV.SOCKET_CLI_GIT_USER_NAME.
187204 user = constants . ENV . SOCKET_CLI_GIT_USER_NAME ,
188205 } = { __proto__ : null , ...options } as GitCreateAndPushBranchOptions
206+
207+ await gitEnsureIdentity ( user , email , cwd )
208+
189209 const stdioIgnoreOptions : SpawnOptions = {
190210 cwd,
191211 stdio : isDebug ( 'stdio' ) ? 'inherit' : 'ignore' ,
192212 }
213+ const quotedAddCmd = `\`git add ${ filepaths . join ( ' ' ) } \``
214+ debugFn ( 'stdio' , `spawn: ${ quotedAddCmd } ` )
193215 try {
194- await gitEnsureIdentity ( user , email , cwd )
195216 await spawn ( 'git' , [ 'add' , ...filepaths ] , stdioIgnoreOptions )
217+ } catch ( e ) {
218+ debugFn ( 'error' , `caught: ${ quotedAddCmd } failed` )
219+ debugDir ( 'inspect' , { error : e } )
220+ }
221+
222+ const quotedCommitCmd = `\`git commit -m ${ commitMsg } \``
223+ debugFn ( 'stdio' , `spawn: ${ quotedCommitCmd } ` )
224+ try {
196225 await spawn ( 'git' , [ 'commit' , '-m' , commitMsg ] , stdioIgnoreOptions )
197226 return true
198- } catch { }
227+ } catch ( e ) {
228+ debugFn ( 'error' , `caught: ${ quotedCommitCmd } failed` )
229+ debugDir ( 'inspect' , { error : e } )
230+ }
199231 return false
200232}
201233
@@ -207,11 +239,16 @@ export async function gitDeleteBranch(
207239 cwd,
208240 stdio : isDebug ( 'stdio' ) ? 'inherit' : 'ignore' ,
209241 }
242+ const quotedCmd = `\`git branch -D ${ branch } \``
243+ debugFn ( 'stdio' , `spawn: ${ quotedCmd } ` )
210244 try {
211245 // Will throw with exit code 1 if branch does not exist.
212246 await spawn ( 'git' , [ 'branch' , '-D' , branch ] , stdioIgnoreOptions )
213247 return true
214- } catch { }
248+ } catch ( e ) {
249+ debugFn ( 'error' , `caught: ${ quotedCmd } failed` )
250+ debugDir ( 'inspect' , { error : e } )
251+ }
215252 return false
216253}
217254
@@ -220,10 +257,6 @@ export async function gitEnsureIdentity(
220257 email : string ,
221258 cwd = process . cwd ( ) ,
222259) : Promise < void > {
223- const stdioIgnoreOptions : SpawnOptions = {
224- cwd,
225- stdio : isDebug ( 'stdio' ) ? 'inherit' : 'ignore' ,
226- }
227260 const stdioPipeOptions : SpawnOptions = { cwd }
228261 const identEntries : Array < [ string , string ] > = [
229262 [ 'user.email' , name ] ,
@@ -239,10 +272,16 @@ export async function gitEnsureIdentity(
239272 ) . stdout
240273 } catch { }
241274 if ( configValue !== value ) {
275+ const stdioIgnoreOptions : SpawnOptions = {
276+ cwd,
277+ stdio : isDebug ( 'stdio' ) ? 'inherit' : 'ignore' ,
278+ }
279+ const quotedCmd = `\`git config ${ prop } ${ value } \``
280+ debugFn ( 'stdio' , `spawn: ${ quotedCmd } ` )
242281 try {
243282 await spawn ( 'git' , [ 'config' , prop , value ] , stdioIgnoreOptions )
244283 } catch ( e ) {
245- debugFn ( 'error' , `caught: git config ${ prop } ${ value } failed` )
284+ debugFn ( 'error' , `caught: ${ quotedCmd } failed` )
246285 debugDir ( 'inspect' , { error : e } )
247286 }
248287 }
@@ -258,6 +297,8 @@ export async function gitLocalBranchExists(
258297 cwd,
259298 stdio : isDebug ( 'stdio' ) ? 'inherit' : 'ignore' ,
260299 }
300+ const quotedCmd = `\`git show-ref --quiet refs/heads/${ branch } \``
301+ debugFn ( 'stdio' , `spawn: ${ quotedCmd } ` )
261302 try {
262303 // Will throw with exit code 1 if the branch does not exist.
263304 await spawn (
@@ -266,7 +307,10 @@ export async function gitLocalBranchExists(
266307 stdioIgnoreOptions ,
267308 )
268309 return true
269- } catch { }
310+ } catch ( e ) {
311+ debugFn ( 'error' , `caught: ${ quotedCmd } failed` )
312+ debugDir ( 'inspect' , { error : e } )
313+ }
270314 return false
271315}
272316
@@ -302,29 +346,39 @@ export async function gitResetAndClean(
302346export async function gitResetHard (
303347 branch = 'HEAD' ,
304348 cwd = process . cwd ( ) ,
305- ) : Promise < void > {
349+ ) : Promise < boolean > {
306350 const stdioIgnoreOptions : SpawnOptions = {
307351 cwd,
308352 stdio : isDebug ( 'stdio' ) ? 'inherit' : 'ignore' ,
309353 }
310- await spawn ( 'git' , [ 'reset' , '--hard' , branch ] , stdioIgnoreOptions )
354+ const quotedCmd = `\`git reset --hard ${ branch } \``
355+ debugFn ( 'stdio' , `spawn: ${ quotedCmd } ` )
356+ try {
357+ await spawn ( 'git' , [ 'reset' , '--hard' , branch ] , stdioIgnoreOptions )
358+ return true
359+ } catch ( e ) {
360+ debugFn ( 'error' , `caught: ${ quotedCmd } failed` )
361+ debugDir ( 'inspect' , { error : e } )
362+ }
363+ return false
311364}
312365
313366export async function gitUnstagedModifiedFiles (
314367 cwd = process . cwd ( ) ,
315368) : Promise < CResult < string [ ] > > {
369+ const stdioPipeOptions : SpawnOptions = { cwd }
370+ const quotedCmd = `\`git diff --name-only\``
316371 try {
317- const stdioPipeOptions : SpawnOptions = { cwd }
318372 const changedFilesDetails = (
319373 await spawn ( 'git' , [ 'diff' , '--name-only' ] , stdioPipeOptions )
320374 ) . stdout
321- const relPaths = changedFilesDetails . split ( '\n' ) ?? [ ]
375+ const relPaths = changedFilesDetails . split ( '\n' )
322376 return {
323377 ok : true ,
324378 data : relPaths . map ( p => normalizePath ( p ) ) ,
325379 }
326380 } catch ( e ) {
327- debugFn ( 'error' , ' caught: git diff --name-only failed' )
381+ debugFn ( 'error' , ` caught: ${ quotedCmd } failed` )
328382 debugDir ( 'inspect' , { error : e } )
329383 return {
330384 ok : false ,
0 commit comments