MCPcopy Create free account
hub / github.com/angular/dev-infra / GitClient

Class GitClient

ng-dev/utils/git/git-client.ts:34–249  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected