Skip to content

Commit 271a8d0

Browse files
authored
fix: suppress output for some git operations (#3776)
* fix: suppress output for some git operations * update dist
1 parent 6f7efd1 commit 271a8d0

File tree

2 files changed

+89
-18
lines changed

2 files changed

+89
-18
lines changed

dist/index.js

+35-4
Original file line numberDiff line numberDiff line change
@@ -660,12 +660,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
660660
step((generator = generator.apply(thisArg, _arguments || [])).next());
661661
});
662662
};
663+
var __importDefault = (this && this.__importDefault) || function (mod) {
664+
return (mod && mod.__esModule) ? mod : { "default": mod };
665+
};
663666
Object.defineProperty(exports, "__esModule", ({ value: true }));
664667
exports.GitCommandManager = void 0;
665668
const exec = __importStar(__nccwpck_require__(5236));
666669
const io = __importStar(__nccwpck_require__(4994));
667670
const utils = __importStar(__nccwpck_require__(9277));
668671
const path = __importStar(__nccwpck_require__(6928));
672+
const stream_1 = __importDefault(__nccwpck_require__(2203));
669673
const tagsRefSpec = '+refs/tags/*:refs/tags/*';
670674
class GitCommandManager {
671675
constructor(workingDirectory, gitPath) {
@@ -781,7 +785,7 @@ class GitCommandManager {
781785
'--no-abbrev',
782786
`--format=%H%n%T%n%P%n%G?%n%s%n%b%n${endOfBody}`,
783787
ref
784-
]);
788+
], { suppressGitCmdOutput: true });
785789
const lines = output.stdout.split('\n');
786790
const endOfBodyIndex = lines.lastIndexOf(endOfBody);
787791
const detailLines = lines.slice(0, endOfBodyIndex);
@@ -895,7 +899,10 @@ class GitCommandManager {
895899
showFileAtRefBase64(ref, path) {
896900
return __awaiter(this, void 0, void 0, function* () {
897901
const args = ['show', `${ref}:${path}`];
898-
const output = yield this.exec(args, { encoding: 'base64' });
902+
const output = yield this.exec(args, {
903+
encoding: 'base64',
904+
suppressGitCmdOutput: true
905+
});
899906
return output.stdout.trim();
900907
});
901908
}
@@ -964,8 +971,12 @@ class GitCommandManager {
964971
});
965972
}
966973
exec(args_1) {
967-
return __awaiter(this, arguments, void 0, function* (args, { encoding = 'utf8', allowAllExitCodes = false } = {}) {
974+
return __awaiter(this, arguments, void 0, function* (args, { encoding = 'utf8', allowAllExitCodes = false, suppressGitCmdOutput = false } = {}) {
968975
const result = new GitOutput();
976+
if (process.env['CPR_SHOW_GIT_CMD_OUTPUT']) {
977+
// debug mode overrides the suppressGitCmdOutput option
978+
suppressGitCmdOutput = false;
979+
}
969980
const env = {};
970981
for (const key of Object.keys(process.env)) {
971982
env[key] = process.env[key];
@@ -987,7 +998,9 @@ class GitCommandManager {
987998
stderr.push(data);
988999
stderrLength += data.length;
9891000
}
990-
}
1001+
},
1002+
outStream: outStreamHandler(process.stdout, suppressGitCmdOutput),
1003+
errStream: outStreamHandler(process.stderr, suppressGitCmdOutput)
9911004
};
9921005
result.exitCode = yield exec.exec(`"${this.gitPath}"`, args, options);
9931006
result.stdout = Buffer.concat(stdout, stdoutLength).toString(encoding);
@@ -1004,6 +1017,24 @@ class GitOutput {
10041017
this.exitCode = 0;
10051018
}
10061019
}
1020+
const outStreamHandler = (outStream, suppressGitCmdOutput) => {
1021+
return new stream_1.default.Writable({
1022+
write(chunk, _, next) {
1023+
if (suppressGitCmdOutput) {
1024+
const lines = chunk.toString().trimEnd().split('\n');
1025+
for (const line of lines) {
1026+
if (line.startsWith('[command]')) {
1027+
outStream.write(`${line}\n`);
1028+
}
1029+
}
1030+
}
1031+
else {
1032+
outStream.write(chunk);
1033+
}
1034+
next();
1035+
}
1036+
});
1037+
};
10071038

10081039

10091040
/***/ }),

src/git-command-manager.ts

+54-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as exec from '@actions/exec'
22
import * as io from '@actions/io'
33
import * as utils from './utils'
44
import * as path from 'path'
5+
import stream, {Writable} from 'stream'
56

67
const tagsRefSpec = '+refs/tags/*:refs/tags/*'
78

@@ -24,6 +25,7 @@ export type Commit = {
2425
export type ExecOpts = {
2526
allowAllExitCodes?: boolean
2627
encoding?: 'utf8' | 'base64'
28+
suppressGitCmdOutput?: boolean
2729
}
2830

2931
export class GitCommandManager {
@@ -161,17 +163,20 @@ export class GitCommandManager {
161163

162164
async getCommit(ref: string): Promise<Commit> {
163165
const endOfBody = '###EOB###'
164-
const output = await this.exec([
165-
'-c',
166-
'core.quotePath=false',
167-
'show',
168-
'--raw',
169-
'--cc',
170-
'--no-renames',
171-
'--no-abbrev',
172-
`--format=%H%n%T%n%P%n%G?%n%s%n%b%n${endOfBody}`,
173-
ref
174-
])
166+
const output = await this.exec(
167+
[
168+
'-c',
169+
'core.quotePath=false',
170+
'show',
171+
'--raw',
172+
'--cc',
173+
'--no-renames',
174+
'--no-abbrev',
175+
`--format=%H%n%T%n%P%n%G?%n%s%n%b%n${endOfBody}`,
176+
ref
177+
],
178+
{suppressGitCmdOutput: true}
179+
)
175180
const lines = output.stdout.split('\n')
176181
const endOfBodyIndex = lines.lastIndexOf(endOfBody)
177182
const detailLines = lines.slice(0, endOfBodyIndex)
@@ -285,7 +290,10 @@ export class GitCommandManager {
285290

286291
async showFileAtRefBase64(ref: string, path: string): Promise<string> {
287292
const args = ['show', `${ref}:${path}`]
288-
const output = await this.exec(args, {encoding: 'base64'})
293+
const output = await this.exec(args, {
294+
encoding: 'base64',
295+
suppressGitCmdOutput: true
296+
})
289297
return output.stdout.trim()
290298
}
291299

@@ -362,10 +370,19 @@ export class GitCommandManager {
362370

363371
async exec(
364372
args: string[],
365-
{encoding = 'utf8', allowAllExitCodes = false}: ExecOpts = {}
373+
{
374+
encoding = 'utf8',
375+
allowAllExitCodes = false,
376+
suppressGitCmdOutput = false
377+
}: ExecOpts = {}
366378
): Promise<GitOutput> {
367379
const result = new GitOutput()
368380

381+
if (process.env['CPR_SHOW_GIT_CMD_OUTPUT']) {
382+
// debug mode overrides the suppressGitCmdOutput option
383+
suppressGitCmdOutput = false
384+
}
385+
369386
const env = {}
370387
for (const key of Object.keys(process.env)) {
371388
env[key] = process.env[key]
@@ -389,7 +406,9 @@ export class GitCommandManager {
389406
stderr.push(data)
390407
stderrLength += data.length
391408
}
392-
}
409+
},
410+
outStream: outStreamHandler(process.stdout, suppressGitCmdOutput),
411+
errStream: outStreamHandler(process.stderr, suppressGitCmdOutput)
393412
}
394413

395414
result.exitCode = await exec.exec(`"${this.gitPath}"`, args, options)
@@ -404,3 +423,24 @@ class GitOutput {
404423
stderr = ''
405424
exitCode = 0
406425
}
426+
427+
const outStreamHandler = (
428+
outStream: Writable,
429+
suppressGitCmdOutput: boolean
430+
): Writable => {
431+
return new stream.Writable({
432+
write(chunk, _, next) {
433+
if (suppressGitCmdOutput) {
434+
const lines = chunk.toString().trimEnd().split('\n')
435+
for (const line of lines) {
436+
if (line.startsWith('[command]')) {
437+
outStream.write(`${line}\n`)
438+
}
439+
}
440+
} else {
441+
outStream.write(chunk)
442+
}
443+
next()
444+
}
445+
})
446+
}

0 commit comments

Comments
 (0)