| 21 | |
| 22 | /** Helper class that can be used to initialize and control the sandbox test repo. */ |
| 23 | export class SandboxGitRepo { |
| 24 | private _nextBranchName = this._github.mainBranchName; |
| 25 | private _commitShaById = new Map<number, string>(); |
| 26 | |
| 27 | static withInitialCommit(github: GithubConfig) { |
| 28 | return new SandboxGitRepo(github).commit('feat(pkg1): initial commit'); |
| 29 | } |
| 30 | |
| 31 | protected constructor(private _github: GithubConfig) { |
| 32 | runGitInTmpDir(['init']); |
| 33 | runGitInTmpDir(['config', 'user.email', 'some-angular-caretaker@google.com']); |
| 34 | runGitInTmpDir(['config', 'user.name', 'Google Angular Caretaker']); |
| 35 | |
| 36 | // Note: We cannot use `--initial-branch=` as this Git option is rather |
| 37 | // new and we do not have a strict requirement on a specific Git version. |
| 38 | this.branchOff(this._nextBranchName); |
| 39 | this.commit('feat(pkg1): initial commit'); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Creates a commit with the given message. Optionally, an id can be specified to |
| 44 | * associate the created commit with a shortcut in order to reference it conveniently |
| 45 | * when writing tests (e.g. when cherry-picking later). |
| 46 | */ |
| 47 | commit(message: string, id?: number): this { |
| 48 | // Capture existing files in the temporary directory. e.g. if a changelog |
| 49 | // file has been written before we want to preserve that in the fake repo. |
| 50 | runGitInTmpDir(['add', '-A']); |
| 51 | runGitInTmpDir(['commit', '--allow-empty', '-m', message]); |
| 52 | |
| 53 | if (id !== undefined) { |
| 54 | const commitSha = runGitInTmpDir(['rev-parse', 'HEAD']); |
| 55 | this._commitShaById.set(id, commitSha); |
| 56 | } |
| 57 | |
| 58 | return this; |
| 59 | } |
| 60 | |
| 61 | /** Branches off the current repository `HEAD`. */ |
| 62 | branchOff(newBranchName: string): this { |
| 63 | runGitInTmpDir(['checkout', '-B', newBranchName]); |
| 64 | return this; |
| 65 | } |
| 66 | |
| 67 | /** Switches to an existing branch. */ |
| 68 | switchToBranch(branchName: string): this { |
| 69 | runGitInTmpDir(['checkout', branchName]); |
| 70 | return this; |
| 71 | } |
| 72 | |
| 73 | /** Creates a new tag for the current repo `HEAD`. */ |
| 74 | createTagForHead(tagName: string): this { |
| 75 | runGitInTmpDir(['tag', tagName, 'HEAD']); |
| 76 | return this; |
| 77 | } |
| 78 | |
| 79 | /** Cherry-picks a commit into the current branch. */ |
| 80 | cherryPick(commitId: number): this { |
nothing calls this directly
no outgoing calls
no test coverage detected