Skip to content

Commit 8cf5d47

Browse files
authored
Prevent overwriting manual changes made to branch (#2)
1 parent 8b92be3 commit 8cf5d47

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/git.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,41 @@ export default class Git {
131131
const prefix = BRANCH_PREFIX.replace('SOURCE_REPO_NAME', GITHUB_REPOSITORY.split('/')[1])
132132

133133
let newBranch = path.join(prefix, this.repo.branch).replace(/\\/g, '/').replace(/\/\./g, '/')
134+
this.prBranch = newBranch
134135

135136
if (OVERWRITE_EXISTING_PR === false) {
136137
newBranch += `-${ Math.round((new Date()).getTime() / 1000) }`
138+
this.prBranch = newBranch
139+
140+
core.debug(`Creating PR Branch ${ newBranch }`)
141+
142+
await execCmd(
143+
`git switch -b "${ newBranch }"`,
144+
this.workingDir
145+
)
146+
147+
return
137148
}
138149

139-
core.debug(`Creating PR Branch ${ newBranch }`)
150+
core.debug(`Switch/Create PR Branch ${ newBranch }`)
140151

141152
await execCmd(
142-
`git checkout -b "${ newBranch }"`,
153+
`git remote set-branches origin '*'`,
143154
this.workingDir
144155
)
145156

146-
this.prBranch = newBranch
157+
await execCmd(
158+
`git fetch -v --depth=1`,
159+
this.workingDir
160+
)
161+
162+
await execCmd(
163+
`git switch "${ newBranch }" 2>/dev/null || git switch -c "${ newBranch }"`,
164+
this.workingDir
165+
)
166+
167+
await this.getLastCommitSha()
168+
147169
}
148170

149171
async add(file) {
@@ -304,7 +326,7 @@ export default class Git {
304326
// Gets the commit list in chronological order
305327
async getCommitsToPush() {
306328
const output = await execCmd(
307-
`git log --format=%H --reverse ${ SKIP_PR === false ? `` : `origin/` }${ this.baseBranch }..HEAD`,
329+
`git log --format=%H --reverse ${ this.lastCommitSha }..HEAD`,
308330
this.workingDir
309331
)
310332

@@ -321,7 +343,7 @@ export default class Git {
321343

322344
// A wrapper for running all the flow to generate all the pending commits using the GitHub API
323345
async createGithubVerifiedCommits() {
324-
core.debug(`Creating Commits using GitHub API`)
346+
core.debug(`Creating commits using GitHub API`)
325347
const commits = await this.getCommitsToPush()
326348

327349
if (SKIP_PR === false) {
@@ -338,6 +360,8 @@ export default class Git {
338360
} catch (error) {
339361
// If the branch exists ignores the error
340362
if (error.message !== 'Reference already exists') throw error
363+
364+
core.debug(`Branch ${ this.prBranch } already exists`)
341365
}
342366
}
343367

@@ -366,15 +390,15 @@ export default class Git {
366390
async push() {
367391
if (FORK) {
368392
return execCmd(
369-
`git push -u fork ${ this.prBranch } --force`,
393+
`git push -u fork ${ this.prBranch } --force-with-lease`,
370394
this.workingDir
371395
)
372396
}
373397
if (IS_INSTALLATION_TOKEN) {
374398
return await this.createGithubVerifiedCommits()
375399
}
376400
return execCmd(
377-
`git push ${ this.gitUrl } --force`,
401+
`git push ${ this.gitUrl } --force-with-lease`,
378402
this.workingDir
379403
)
380404
}
@@ -428,7 +452,7 @@ export default class Git {
428452
`)
429453

430454
if (this.existingPr) {
431-
core.info(`Overwriting existing PR`)
455+
core.info(`Updating existing PR`)
432456

433457
const { data } = await this.github.pulls.update({
434458
owner: this.repo.user,

0 commit comments

Comments
 (0)