use source mode for deleted files

This commit is contained in:
Peter Evans 2024-08-08 15:15:58 +00:00
parent b2a409b0a3
commit 6b1053d0d8
6 changed files with 132 additions and 13 deletions

View file

@ -177,6 +177,7 @@ interface CreateOrUpdateBranchResult {
hasDiffWithBase: boolean
headSha: string
branchFileChanges?: BranchFileChanges
branchCommits: Commit[]
}
export async function createOrUpdateBranch(
@ -206,7 +207,8 @@ export async function createOrUpdateBranch(
action: 'none',
base: base,
hasDiffWithBase: false,
headSha: ''
headSha: '',
branchCommits: []
}
// Save the working base changes to a temporary branch
@ -351,6 +353,9 @@ export async function createOrUpdateBranch(
result.hasDiffWithBase = await isAhead(git, base, branch)
}
// Build the branch commits
result.branchCommits = await buildBranchCommits(git, base, branch)
// Build the branch file changes
result.branchFileChanges = await buildBranchFileChanges(git, base, branch)

View file

@ -196,6 +196,16 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
`Pushing pull request branch to '${branchRemoteName}/${inputs.branch}'`
)
if (inputs.signCommit) {
// Stash any uncommitted tracked and untracked changes
const stashed = await git.stashPush(['--include-untracked'])
await git.checkout(inputs.branch)
// await githubHelper.pushSignedCommits(
// branchRepository,
// inputs.branch,
// inputs.base,
// inputs.commitMessage,
// result.branchCommits
// )
await githubHelper.pushSignedCommit(
branchRepository,
inputs.branch,
@ -203,6 +213,10 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
inputs.commitMessage,
result.branchFileChanges
)
await git.checkout('-')
if (stashed) {
await git.stashPop()
}
} else {
await git.push([
'--force-with-lease',

View file

@ -172,12 +172,14 @@ export class GitCommandManager {
subject: detailLines[3],
body: detailLines.slice(4, endOfBodyIndex).join('\n'),
changes: lines.slice(endOfBodyIndex + 2, -1).map(line => {
const change = line.match(/^:\d{6} (\d{6}) \w{7} \w{7} ([AMD])\s+(.*)$/)
const change = line.match(
/^:(\d{6}) (\d{6}) \w{7} \w{7} ([AMD])\s+(.*)$/
)
if (change) {
return {
mode: change[1],
status: change[2],
path: change[3]
mode: change[3] === 'D' ? change[1] : change[2],
status: change[3],
path: change[4]
}
} else {
throw new Error(`Unexpected line format: ${line}`)

View file

@ -1,10 +1,11 @@
import * as core from '@actions/core'
import {Inputs} from './create-pull-request'
// import {Commit} from './git-command-manager'
import {Octokit, OctokitOptions} from './octokit-client'
import type {
Repository as TempRepository,
Ref,
Commit,
Commit as CommitTemp,
FileChanges
} from '@octokit/graphql-schema'
import {BranchFileChanges} from './create-or-update-branch'
@ -192,6 +193,47 @@ export class GitHubHelper {
return pull
}
// async pushSignedCommits(
// branchCommits: Commit[],
// repoPath: string,
// branchRepository: string,
// branch: string,
// base: string,
// commitMessage: string
// ): Promise<void> {
// for (const commit of branchCommits) {
// await this.createCommit(commit, repoPath, branchRepository)
// }
// // update branch ref
// }
// private async createCommit(
// commit: Commit,
// repoPath: string,
// branchRepository: string
// ): Promise<void> {
// const tree = await Promise.all(
// commit.changes.map(async ({path, mode, status}) => {
// let sha: string | null = null
// if (status === 'A' || status === 'M') {
// const {data: blob} = await this.octokit.rest.git.createBlob({
// ...this.parseRepository(branchRepository),
// content: utils.readFileBase64([repoPath, path]),
// encoding: 'base64'
// })
// sha = blob.sha
// }
// return {
// path,
// mode,
// sha,
// type: 'blob'
// }
// })
// )
// }
async pushSignedCommit(
branchRepository: string,
branch: string,
@ -350,7 +392,7 @@ export class GitHubHelper {
)
const commit = await this.octokit.graphql<{
createCommitOnBranch: {ref: Ref; commit: Commit}
createCommitOnBranch: {ref: Ref; commit: CommitTemp}
}>(pushCommitMutation, pushCommitVars)
core.debug(`Pushed commit - '${JSON.stringify(commit)}'`)