-
Notifications
You must be signed in to change notification settings - Fork 2
Run workspace-side mirror git commands with the checkout's git environment; adopt mirrors owned by another user #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f8c8cc2
Run workspace-side mirror git commands with the checkout's git enviro…
piob-io ff41753
ci: cover container dissociate and cross-user mirror takeover on Blac…
piob-io d8e5c41
ci: run the container verification step with bash
piob-io 9248df6
ci: container test checks out into a runner-owned directory instead o…
piob-io 7ea9023
ci: container job asserts the mirror takeover; VM job after it replac…
piob-io File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| /** | ||
| * Real-git coverage for the workspace-side mirror operations (ref copy, | ||
| * dissociate) on a workspace git considers owned by another user, as in a | ||
| * job container where the runner creates the workspace and git runs as | ||
| * root. Git's own GIT_TEST_ASSUME_DIFFERENT_OWNER knob stands in for the | ||
| * uid mismatch; the checkout's safe.directory entry lives in a temporary | ||
| * HOME, so the operations only succeed when run with that environment. | ||
| */ | ||
| jest.mock('@connectrpc/connect', () => ({ | ||
| createClient: jest.fn(), | ||
| ConnectError: class ConnectError extends Error {}, | ||
| Code: {Aborted: 'ABORTED'} | ||
| })) | ||
|
|
||
| jest.mock('@connectrpc/connect-node', () => ({ | ||
| createGrpcTransport: jest.fn() | ||
| })) | ||
|
|
||
| jest.mock( | ||
| '@buf/blacksmith_vm-agent.connectrpc_es/stickydisk/v1/stickydisk_connect', | ||
| () => ({ | ||
| StickyDiskService: {} | ||
| }) | ||
| ) | ||
|
|
||
| jest.mock('../src/container-detector', () => ({ | ||
| isRunningInContainer: jest.fn(() => false) | ||
| })) | ||
|
|
||
| import * as fs from 'fs' | ||
| import * as os from 'os' | ||
| import * as path from 'path' | ||
| import {execFileSync} from 'child_process' | ||
| import * as blacksmithCache from '../src/blacksmith-cache' | ||
|
|
||
| function git(cwd: string, ...args: string[]): string { | ||
| return execFileSync('git', ['-C', cwd, ...args], {encoding: 'utf8'}).trim() | ||
| } | ||
|
|
||
| function commit(repo: string, msg: string): void { | ||
| fs.writeFileSync(path.join(repo, 'file.txt'), msg) | ||
| git(repo, 'add', 'file.txt') | ||
| git( | ||
| repo, | ||
| '-c', | ||
| 'user.email=test@example.com', | ||
| '-c', | ||
| 'user.name=Test', | ||
| 'commit', | ||
| '-m', | ||
| msg | ||
| ) | ||
| } | ||
|
|
||
| function baseEnv(): {[key: string]: string} { | ||
| const env: {[key: string]: string} = {} | ||
| for (const [key, value] of Object.entries(process.env)) { | ||
| if (value !== undefined) { | ||
| env[key] = value | ||
| } | ||
| } | ||
| env['GIT_TEST_ASSUME_DIFFERENT_OWNER'] = '1' | ||
| return env | ||
| } | ||
|
|
||
| function gitRefusesForeignOwner(): boolean { | ||
| const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'owner-probe-')) | ||
| try { | ||
| execFileSync('git', ['init', '-q', tmp]) | ||
| execFileSync('git', ['-C', tmp, 'rev-parse', '--git-dir'], { | ||
| env: baseEnv(), | ||
| stdio: 'ignore' | ||
| }) | ||
| return false | ||
| } catch { | ||
| return true | ||
| } finally { | ||
| fs.rmSync(tmp, {recursive: true, force: true}) | ||
| } | ||
| } | ||
|
|
||
| const describeIfSupported = gitRefusesForeignOwner() ? describe : describe.skip | ||
|
|
||
| describeIfSupported( | ||
| 'workspace mirror operations on a foreign-owned workspace', | ||
| () => { | ||
| let tmpDir: string | ||
| let mirrorPath: string | ||
| let workspace: string | ||
| let checkoutEnv: {[key: string]: string} | ||
|
|
||
| beforeEach(() => { | ||
| tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mirror-env-test-')) | ||
| const sourceRepo = path.join(tmpDir, 'source') | ||
| mirrorPath = path.join(tmpDir, 'mirror') | ||
| workspace = path.join(tmpDir, 'workspace') | ||
|
|
||
| fs.mkdirSync(sourceRepo) | ||
| git(sourceRepo, 'init', '-q', '-b', 'main', '.') | ||
| commit(sourceRepo, 'one') | ||
| commit(sourceRepo, 'two') | ||
| git(sourceRepo, 'tag', 'v1') | ||
| execFileSync('git', ['clone', '-q', '--mirror', sourceRepo, mirrorPath]) | ||
|
|
||
| fs.mkdirSync(workspace) | ||
| git(workspace, 'init', '-q', '.') | ||
| git(workspace, 'remote', 'add', 'origin', sourceRepo) | ||
| const infoDir = path.join(workspace, '.git', 'objects', 'info') | ||
| fs.mkdirSync(infoDir, {recursive: true}) | ||
| fs.writeFileSync( | ||
| path.join(infoDir, 'alternates'), | ||
| `${mirrorPath}/objects\n` | ||
| ) | ||
|
|
||
| // What the action's temporary global config holds after | ||
| // `git config --global --add safe.directory <workspace>` | ||
| const tempHome = path.join(tmpDir, 'home') | ||
| fs.mkdirSync(tempHome) | ||
| fs.writeFileSync( | ||
| path.join(tempHome, '.gitconfig'), | ||
| `[safe]\n\tdirectory = ${workspace}\n` | ||
| ) | ||
| checkoutEnv = {...baseEnv(), HOME: tempHome} | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| fs.rmSync(tmpDir, {recursive: true, force: true}) | ||
| }) | ||
|
|
||
| it('dissociate fails without the checkout environment', async () => { | ||
| await expect( | ||
| blacksmithCache.dissociate(workspace, baseEnv()) | ||
| ).rejects.toThrow(/exit code 128/) | ||
| expect( | ||
| fs.existsSync( | ||
| path.join(workspace, '.git', 'objects', 'info', 'alternates') | ||
| ) | ||
| ).toBe(true) | ||
| }) | ||
|
|
||
| it('ref copy and dissociate succeed with the checkout environment and leave a self-contained workspace', async () => { | ||
| expect( | ||
| await blacksmithCache.fetchRefsFromMirror( | ||
| workspace, | ||
| mirrorPath, | ||
| checkoutEnv | ||
| ) | ||
| ).toBe(true) | ||
| // The fast path wrote packed-refs; the ref listing is what verifies it | ||
| expect(git(workspace, 'rev-parse', 'refs/remotes/origin/main')).toBe( | ||
| git(mirrorPath, 'rev-parse', 'refs/heads/main') | ||
| ) | ||
|
|
||
| await blacksmithCache.dissociate(workspace, checkoutEnv) | ||
|
|
||
| expect( | ||
| fs.existsSync( | ||
| path.join(workspace, '.git', 'objects', 'info', 'alternates') | ||
| ) | ||
| ).toBe(false) | ||
| fs.rmSync(mirrorPath, {recursive: true, force: true}) | ||
| git(workspace, 'fsck', '--no-dangling') | ||
| git(workspace, 'rev-list', '--objects', '--all', '--quiet') | ||
| expect(git(workspace, 'rev-parse', 'refs/tags/v1^{commit}')).toBe( | ||
| git(workspace, 'rev-parse', 'refs/remotes/origin/main') | ||
| ) | ||
| }) | ||
| } | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.