Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions .github/workflows/patch-provenance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,25 @@ jobs:
return false
}
}
// The fetch below brings every commit and tree, so whether a commit has a path reads no file.
const hasFile = (commit, path) => gitSucceeds('rev-parse', '--verify', '--quiet', `${commit}:${path}`)
// A git command that talks to GitHub. Its failure fails the check with git's message, as the
// other refusals do, instead of ending the job with a stack trace and no summary.
const network = (what, ...args) => {
try {
return git(...args)
} catch (error) {
fail(`${what} failed: ${error.stderr?.toString().trim() || error.message}`)
}
}
// Whether a commit has a path. The fetch below brings every commit and tree, so this reads no
// file. `git ls-tree` prints nothing for a missing path and fails on a failed read, which fails
// the check: like a file's text, a failed read never reads as a missing file.
const hasFile = (commit, path) => {
try {
return run('git', ['--literal-pathspecs', 'ls-tree', '--full-tree', commit, '--', path]) !== ''
} catch (error) {
fail(`Reading ${commit}'s tree for ${path} failed: ${error.stderr?.toString().trim() || error.message}`)
}
}
// A file's text at a commit, or undefined when the commit doesn't have it. Git fetches the
// text from GitHub on first read, and a failed fetch fails the check: it never reads as a
// missing file.
Expand Down Expand Up @@ -105,7 +122,7 @@ jobs:
return changes
}
// "<mode> <type> <object>" for a path at a commit, or '' when it isn't there.
const entryAt = (commit, path) => git('ls-tree', '--full-tree', commit, '--', path).split('\t')[0]
const entryAt = (commit, path) => git('--literal-pathspecs', 'ls-tree', '--full-tree', commit, '--', path).split('\t')[0]
// {name: {directory, version, manifest}} for the workspace packages at a commit.
function packagesAt(commit) {
const packages = {}
Expand Down Expand Up @@ -141,14 +158,14 @@ jobs:
git('remote', 'add', 'origin', `https://github.com/${repository}`)
// ls-remote matches the end of ref names, so a branch such as a/refs/heads/<line> matches too:
// only the exact ref is the line's head.
const lineHead = git('ls-remote', 'origin', `refs/heads/${BASE_REF}`)
const lineHead = network(`Reading the head of ${BASE_REF}`, 'ls-remote', 'origin', `refs/heads/${BASE_REF}`)
.split('\n')
.map((line) => line.split('\t'))
.find(([, ref]) => ref === `refs/heads/${BASE_REF}`)?.[0]
if (!/^[0-9a-f]{40}$/.test(lineHead ?? '')) fail(`Couldn't read the head of ${BASE_REF}.`)
// Every commit and tree of the line, the pull request and main, without file contents, so
// merge bases and histories are exact.
git('fetch', '--quiet', '--no-tags', '--filter=blob:none', 'origin', lineHead, HEAD_SHA, '+refs/heads/main:refs/remotes/origin/main')
network(`Fetching ${BASE_REF}, the pull request and main`, 'fetch', '--quiet', '--no-tags', '--filter=blob:none', 'origin', lineHead, HEAD_SHA, '+refs/heads/main:refs/remotes/origin/main')
if (!gitSucceeds('merge-base', lineHead, HEAD_SHA)) fail(`${HEAD_SHA} doesn't share history with ${BASE_REF}.`)

if (HEAD_REF === `changeset-release/${BASE_REF}`) {
Expand Down
Loading