MCPcopy
hub / github.com/peter-evans/create-pull-request / createOrUpdateBranch

Function createOrUpdateBranch

src/create-or-update-branch.ts:169–372  ·  view source on GitHub ↗
(
  git: GitCommandManager,
  commitMessage: string,
  base: string,
  branch: string,
  branchRemoteName: string,
  signoff: boolean,
  addPaths: string[]
)

Source from the content-addressed store, hash-verified

167}
168
169export async function createOrUpdateBranch(
170 git: GitCommandManager,
171 commitMessage: string,
172 base: string,
173 branch: string,
174 branchRemoteName: string,
175 signoff: boolean,
176 addPaths: string[]
177): Promise<CreateOrUpdateBranchResult> {
178 // Get the working base.
179 // When a ref, it may or may not be the actual base.
180 // When a commit, we must rebase onto the actual base.
181 const [workingBase, workingBaseType] = await getWorkingBaseAndType(git)
182 core.info(`Working base is ${workingBaseType} '${workingBase}'`)
183 if (workingBaseType == WorkingBaseType.Commit && !base) {
184 throw new Error(`When in 'detached HEAD' state, 'base' must be supplied.`)
185 }
186
187 // If the base is not specified it is assumed to be the working base.
188 base = base ? base : workingBase
189 const baseRemote = 'origin'
190
191 // Save the working base changes to a temporary branch
192 const tempBranch = uuidv4()
193 await git.checkout(tempBranch, 'HEAD')
194 // Commit any uncommitted changes
195 if (await git.isDirty(true, addPaths)) {
196 core.info('Uncommitted changes found. Adding a commit.')
197 const aopts = ['add']
198 if (addPaths.length > 0) {
199 aopts.push(...['--', ...addPaths])
200 } else {
201 aopts.push('-A')
202 }
203 await git.exec(aopts, {allowAllExitCodes: true})
204 const popts = ['-m', commitMessage]
205 if (signoff) {
206 popts.push('--signoff')
207 }
208 const commitResult = await git.commit(popts, true)
209 // 'nothing to commit' can occur when core.autocrlf is set to true
210 if (
211 commitResult.exitCode != 0 &&
212 !commitResult.stdout.includes(NOTHING_TO_COMMIT)
213 ) {
214 throw new Error(`Unexpected error: ${commitResult.stderr}`)
215 }
216 }
217
218 // Stash any uncommitted tracked and untracked changes
219 const stashed = await git.stashPush(['--include-untracked'])
220
221 // Reset the working base
222 // Commits made during the workflow will be removed
223 if (workingBaseType == WorkingBaseType.Branch) {
224 core.info(`Resetting working base branch '${workingBase}'`)
225 await git.checkout(workingBase)
226 await git.exec(['reset', '--hard', `${baseRemote}/${workingBase}`])

Callers 2

createPullRequestFunction · 0.90

Calls 15

getWorkingBaseAndTypeFunction · 0.85
splitLinesFunction · 0.85
commitsAheadFunction · 0.85
tryFetchFunction · 0.85
isAheadFunction · 0.85
commitsHaveDiffFunction · 0.85
isEvenFunction · 0.85
buildBranchCommitsFunction · 0.85
checkoutMethod · 0.80
isDirtyMethod · 0.80
pushMethod · 0.80
execMethod · 0.80

Tested by

no test coverage detected