From 7ca5766bfacc6c307d7e69645aa94ac44318af46 Mon Sep 17 00:00:00 2001 From: eric sciple Date: Thu, 5 Mar 2020 14:21:59 -0500 Subject: add support for submodules (#173) --- src/git-command-manager.ts | 95 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 83 insertions(+), 12 deletions(-) (limited to 'src/git-command-manager.ts') diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts index 2b0d054..4cbfe4a 100644 --- a/src/git-command-manager.ts +++ b/src/git-command-manager.ts @@ -3,6 +3,7 @@ import * as exec from '@actions/exec' import * as fshelper from './fs-helper' import * as io from '@actions/io' import * as path from 'path' +import * as regexpHelper from './regexp-helper' import * as retryHelper from './retry-helper' import {GitVersion} from './git-version' @@ -16,8 +17,12 @@ export interface IGitCommandManager { branchList(remote: boolean): Promise checkout(ref: string, startPoint: string): Promise checkoutDetach(): Promise - config(configKey: string, configValue: string): Promise - configExists(configKey: string): Promise + config( + configKey: string, + configValue: string, + globalConfig?: boolean + ): Promise + configExists(configKey: string, globalConfig?: boolean): Promise fetch(fetchDepth: number, refSpec: string[]): Promise getWorkingDirectory(): string init(): Promise @@ -26,10 +31,14 @@ export interface IGitCommandManager { lfsInstall(): Promise log1(): Promise remoteAdd(remoteName: string, remoteUrl: string): Promise + removeEnvironmentVariable(name: string): void setEnvironmentVariable(name: string, value: string): void + submoduleForeach(command: string, recursive: boolean): Promise + submoduleSync(recursive: boolean): Promise + submoduleUpdate(fetchDepth: number, recursive: boolean): Promise tagExists(pattern: string): Promise tryClean(): Promise - tryConfigUnset(configKey: string): Promise + tryConfigUnset(configKey: string, globalConfig?: boolean): Promise tryDisableAutomaticGarbageCollection(): Promise tryGetFetchUrl(): Promise tryReset(): Promise @@ -124,16 +133,32 @@ class GitCommandManager { await this.execGit(args) } - async config(configKey: string, configValue: string): Promise { - await this.execGit(['config', '--local', configKey, configValue]) + async config( + configKey: string, + configValue: string, + globalConfig?: boolean + ): Promise { + await this.execGit([ + 'config', + globalConfig ? '--global' : '--local', + configKey, + configValue + ]) } - async configExists(configKey: string): Promise { - const pattern = configKey.replace(/[^a-zA-Z0-9_]/g, x => { - return `\\${x}` - }) + async configExists( + configKey: string, + globalConfig?: boolean + ): Promise { + const pattern = regexpHelper.escape(configKey) const output = await this.execGit( - ['config', '--local', '--name-only', '--get-regexp', pattern], + [ + 'config', + globalConfig ? '--global' : '--local', + '--name-only', + '--get-regexp', + pattern + ], true ) return output.exitCode === 0 @@ -208,10 +233,48 @@ class GitCommandManager { await this.execGit(['remote', 'add', remoteName, remoteUrl]) } + removeEnvironmentVariable(name: string): void { + delete this.gitEnv[name] + } + setEnvironmentVariable(name: string, value: string): void { this.gitEnv[name] = value } + async submoduleForeach(command: string, recursive: boolean): Promise { + const args = ['submodule', 'foreach'] + if (recursive) { + args.push('--recursive') + } + args.push(command) + + const output = await this.execGit(args) + return output.stdout + } + + async submoduleSync(recursive: boolean): Promise { + const args = ['submodule', 'sync'] + if (recursive) { + args.push('--recursive') + } + + await this.execGit(args) + } + + async submoduleUpdate(fetchDepth: number, recursive: boolean): Promise { + const args = ['-c', 'protocol.version=2'] + args.push('submodule', 'update', '--init', '--force') + if (fetchDepth > 0) { + args.push(`--depth=${fetchDepth}`) + } + + if (recursive) { + args.push('--recursive') + } + + await this.execGit(args) + } + async tagExists(pattern: string): Promise { const output = await this.execGit(['tag', '--list', pattern]) return !!output.stdout.trim() @@ -222,9 +285,17 @@ class GitCommandManager { return output.exitCode === 0 } - async tryConfigUnset(configKey: string): Promise { + async tryConfigUnset( + configKey: string, + globalConfig?: boolean + ): Promise { const output = await this.execGit( - ['config', '--local', '--unset-all', configKey], + [ + 'config', + globalConfig ? '--global' : '--local', + '--unset-all', + configKey + ], true ) return output.exitCode === 0 -- cgit v1.2.3