| 32 | |
| 33 | /** Class that can be used to perform Git interactions with a given remote. **/ |
| 34 | export class GitClient { |
| 35 | /** Short-hand for accessing the default remote configuration. */ |
| 36 | readonly remoteConfig: GithubConfig; |
| 37 | |
| 38 | /** Octokit request parameters object for targeting the configured remote. */ |
| 39 | readonly remoteParams: {owner: string; repo: string}; |
| 40 | |
| 41 | /** Name of the primary branch of the upstream remote. */ |
| 42 | readonly mainBranchName: string; |
| 43 | |
| 44 | /** Instance of the Github client. */ |
| 45 | readonly github = new GithubClient(); |
| 46 | |
| 47 | /** The configuration, containing the github specific configuration. */ |
| 48 | readonly config: {github: GithubConfig}; |
| 49 | |
| 50 | /** |
| 51 | * Path to the Git executable. By default, `git` is assumed to exist |
| 52 | * in the shell environment (using `$PATH`). |
| 53 | */ |
| 54 | readonly gitBinPath: string = 'git'; |
| 55 | |
| 56 | constructor( |
| 57 | /** The configuration, containing the github specific configuration. */ |
| 58 | config: {github: GithubConfig}, |
| 59 | /** The full path to the root of the repository base. */ |
| 60 | readonly baseDir = determineRepoBaseDirFromCwd(), |
| 61 | ) { |
| 62 | this.config = config; |
| 63 | this.remoteConfig = config.github; |
| 64 | this.remoteParams = {owner: config.github.owner, repo: config.github.name}; |
| 65 | this.mainBranchName = config.github.mainBranchName; |
| 66 | } |
| 67 | |
| 68 | /** Executes the given git command. Throws if the command fails. */ |
| 69 | run(args: string[], options?: GitCommandRunOptions): Omit<SpawnSyncReturns<string>, 'status'> { |
| 70 | const result = this.runGraceful(args, options); |
| 71 | if (result.status !== 0) { |
| 72 | throw new GitCommandError(this, args); |
| 73 | } |
| 74 | // Omit `status` from the type so that it's obvious that the status is never |
| 75 | // non-zero as explained in the method description. |
| 76 | return result as Omit<SpawnSyncReturns<string>, 'status'>; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Spawns a given Git command process. Does not throw if the command fails. Additionally, |
| 81 | * if there is any stderr output, the output will be printed. This makes it easier to |
| 82 | * info failed commands. |
| 83 | */ |
| 84 | runGraceful(args: string[], options: GitCommandRunOptions = {}): SpawnSyncReturns<string> { |
| 85 | /** The git command to be run. */ |
| 86 | const gitCommand = args[0]; |
| 87 | |
| 88 | if (isDryRun() && gitCommand === 'push') { |
| 89 | Log.debug(`"git push" is not able to be run in dryRun mode.`); |
| 90 | throw new DryRunError(); |
| 91 | } |
nothing calls this directly
no outgoing calls
no test coverage detected