diff options
| author | Cory Miller <13227161+cory-miller@users.noreply.github.com> | 2022-12-14 16:08:53 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-14 16:08:53 -0500 |
| commit | 79ad7893d504c7b4ea55250d8352751d91435e79 (patch) | |
| tree | 4d1aea1effa1525e39b74deeb896956784723248 /src/git-command-manager.ts | |
| parent | d197bc795ff008ec1d6796efa56dcc6f8c37843b (diff) | |
Implement branch list using callbacks from exec function (#1045)
When trying to list local branches to figure out what needs cleaned up during runs on non-ephemeral Actions Runners, we use git rev-parse --symbolic-full-name to get a list of branches. This can lead to ambiguous ref name errors when there are branches and tags with similar names.
Part of the reason we use rev-parse --symbolic-full-name vs git branch --list or git rev-parse --symbolic seems to related to a bug in Git 2.18. Until we can deprecate our usage of Git 2.18, I think we need to keep --symbolic-full-name. Since part of the problem is that these ambiguous ref name errors clog the Actions annotation limits, this is a mitigation to suppress those messages until we can get rid of the workaround.
Diffstat (limited to 'src/git-command-manager.ts')
| -rw-r--r-- | src/git-command-manager.ts | 75 |
1 files changed, 57 insertions, 18 deletions
diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts index 699a963..01aedfe 100644 --- a/src/git-command-manager.ts +++ b/src/git-command-manager.ts @@ -94,8 +94,11 @@ class GitCommandManager { // Note, this implementation uses "rev-parse --symbolic-full-name" because the output from // "branch --list" is more difficult when in a detached HEAD state. - // Note, this implementation uses "rev-parse --symbolic-full-name" because there is a bug - // in Git 2.18 that causes "rev-parse --symbolic" to output symbolic full names. + + // TODO(https://github.com/actions/checkout/issues/786): this implementation uses + // "rev-parse --symbolic-full-name" because there is a bug + // in Git 2.18 that causes "rev-parse --symbolic" to output symbolic full names. When + // 2.18 is no longer supported, we can switch back to --symbolic. const args = ['rev-parse', '--symbolic-full-name'] if (remote) { @@ -104,19 +107,47 @@ class GitCommandManager { args.push('--branches') } - const output = await this.execGit(args) + const stderr: string[] = [] + const errline: string[] = [] + const stdout: string[] = [] + const stdline: string[] = [] + + const listeners = { + stderr: (data: Buffer) => { + stderr.push(data.toString()) + }, + errline: (data: Buffer) => { + errline.push(data.toString()) + }, + stdout: (data: Buffer) => { + stdout.push(data.toString()) + }, + stdline: (data: Buffer) => { + stdline.push(data.toString()) + } + } - for (let branch of output.stdout.trim().split('\n')) { + // Suppress the output in order to avoid flooding annotations with innocuous errors. + await this.execGit(args, false, true, listeners) + + core.debug(`stderr callback is: ${stderr}`) + core.debug(`errline callback is: ${errline}`) + core.debug(`stdout callback is: ${stdout}`) + core.debug(`stdline callback is: ${stdline}`) + + for (let branch of stdline) { branch = branch.trim() - if (branch) { - if (branch.startsWith('refs/heads/')) { - branch = branch.substr('refs/heads/'.length) - } else if (branch.startsWith('refs/remotes/')) { - branch = branch.substr('refs/remotes/'.length) - } + if (!branch) { + continue + } - result.push(branch) + if (branch.startsWith('refs/heads/')) { + branch = branch.substring('refs/heads/'.length) + } else if (branch.startsWith('refs/remotes/')) { + branch = branch.substring('refs/remotes/'.length) } + + result.push(branch) } return result @@ -395,7 +426,8 @@ class GitCommandManager { private async execGit( args: string[], allowAllExitCodes = false, - silent = false + silent = false, + customListeners = {} ): Promise<GitOutput> { fshelper.directoryExistsSync(this.workingDirectory, true) @@ -409,22 +441,29 @@ class GitCommandManager { env[key] = this.gitEnv[key] } - const stdout: string[] = [] + const defaultListener = { + stdout: (data: Buffer) => { + stdout.push(data.toString()) + } + } + const mergedListeners = {...defaultListener, ...customListeners} + + const stdout: string[] = [] const options = { cwd: this.workingDirectory, env, silent, ignoreReturnCode: allowAllExitCodes, - listeners: { - stdout: (data: Buffer) => { - stdout.push(data.toString()) - } - } + listeners: mergedListeners } result.exitCode = await exec.exec(`"${this.gitPath}"`, args, options) result.stdout = stdout.join('') + + core.debug(result.exitCode.toString()) + core.debug(result.stdout) + return result } |
