-
-
Notifications
You must be signed in to change notification settings - Fork 84
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
Prevent overwriting manual changes made to branch #341
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,19 +131,41 @@ export default class Git { | |
const prefix = BRANCH_PREFIX.replace('SOURCE_REPO_NAME', GITHUB_REPOSITORY.split('/')[1]) | ||
|
||
let newBranch = path.join(prefix, this.repo.branch).replace(/\\/g, '/').replace(/\/\./g, '/') | ||
this.prBranch = newBranch | ||
|
||
if (OVERWRITE_EXISTING_PR === false) { | ||
newBranch += `-${ Math.round((new Date()).getTime() / 1000) }` | ||
this.prBranch = newBranch | ||
|
||
core.debug(`Creating PR Branch ${ newBranch }`) | ||
|
||
await execCmd( | ||
`git switch -b "${ newBranch }"`, | ||
this.workingDir | ||
) | ||
|
||
return | ||
} | ||
|
||
core.debug(`Creating PR Branch ${ newBranch }`) | ||
core.debug(`Switch/Create PR Branch ${ newBranch }`) | ||
|
||
await execCmd( | ||
`git checkout -b "${ newBranch }"`, | ||
`git remote set-branches origin '*'`, | ||
this.workingDir | ||
) | ||
|
||
this.prBranch = newBranch | ||
await execCmd( | ||
`git fetch -v --depth=1`, | ||
this.workingDir | ||
) | ||
|
||
await execCmd( | ||
`git switch "${ newBranch }" 2>/dev/null || git switch -c "${ newBranch }"`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Attempt to switch to the existing branch, if this doesn't exists (i.e. no PR already exists) then create a new branch. |
||
this.workingDir | ||
) | ||
|
||
await this.getLastCommitSha() | ||
|
||
} | ||
|
||
async add(file) { | ||
|
@@ -304,7 +326,7 @@ export default class Git { | |
// Gets the commit list in chronological order | ||
async getCommitsToPush() { | ||
const output = await execCmd( | ||
`git log --format=%H --reverse ${ SKIP_PR === false ? `` : `origin/` }${ this.baseBranch }..HEAD`, | ||
`git log --format=%H --reverse ${ this.lastCommitSha }..HEAD`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. base the commits on the difference from the last pushed commit to now. This is safe as this reference is not updated until the
that follows. |
||
this.workingDir | ||
) | ||
|
||
|
@@ -321,7 +343,7 @@ export default class Git { | |
|
||
// A wrapper for running all the flow to generate all the pending commits using the GitHub API | ||
async createGithubVerifiedCommits() { | ||
core.debug(`Creating Commits using GitHub API`) | ||
core.debug(`Creating commits using GitHub API`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const commits = await this.getCommitsToPush() | ||
|
||
if (SKIP_PR === false) { | ||
|
@@ -338,6 +360,8 @@ export default class Git { | |
} catch (error) { | ||
// If the branch exists ignores the error | ||
if (error.message !== 'Reference already exists') throw error | ||
|
||
core.debug(`Branch ${ this.prBranch } already exists`) | ||
} | ||
} | ||
|
||
|
@@ -366,15 +390,15 @@ export default class Git { | |
async push() { | ||
if (FORK) { | ||
return execCmd( | ||
`git push -u fork ${ this.prBranch } --force`, | ||
`git push -u fork ${ this.prBranch } --force-with-lease`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
this.workingDir | ||
) | ||
} | ||
if (IS_INSTALLATION_TOKEN) { | ||
return await this.createGithubVerifiedCommits() | ||
} | ||
return execCmd( | ||
`git push ${ this.gitUrl } --force`, | ||
`git push ${ this.gitUrl } --force-with-lease`, | ||
this.workingDir | ||
) | ||
} | ||
|
@@ -428,7 +452,7 @@ export default class Git { | |
`) | ||
|
||
if (this.existingPr) { | ||
core.info(`Overwriting existing PR`) | ||
core.info(`Updating existing PR`) | ||
|
||
const { data } = await this.github.pulls.update({ | ||
owner: this.repo.user, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code currently only checks out the
default
branch. We need to fetch the other branches so we can switch to as required.