Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9153d834b6 | ||
|
|
c55203cfde | ||
|
|
6ce4eca6b6 | ||
|
|
36ef0ed92f | ||
|
|
8500972a13 | ||
|
|
bda5ade93c |
@@ -210,8 +210,8 @@ describe('create-or-update-branch tests', () => {
|
||||
}
|
||||
|
||||
it('tests if a branch exists and can be fetched', async () => {
|
||||
expect(await tryFetch(git, REMOTE_NAME, NOT_BASE_BRANCH)).toBeTruthy()
|
||||
expect(await tryFetch(git, REMOTE_NAME, NOT_EXIST_BRANCH)).toBeFalsy()
|
||||
expect(await tryFetch(git, REMOTE_NAME, NOT_BASE_BRANCH, 1)).toBeTruthy()
|
||||
expect(await tryFetch(git, REMOTE_NAME, NOT_EXIST_BRANCH, 1)).toBeFalsy()
|
||||
})
|
||||
|
||||
it('tests getWorkingBaseAndType on a checked out ref', async () => {
|
||||
|
||||
28
dist/index.js
vendored
28
dist/index.js
vendored
@@ -44,6 +44,7 @@ const core = __importStar(__nccwpck_require__(2186));
|
||||
const uuid_1 = __nccwpck_require__(5840);
|
||||
const CHERRYPICK_EMPTY = 'The previous cherry-pick is now empty, possibly due to conflict resolution.';
|
||||
const NOTHING_TO_COMMIT = 'nothing to commit, working tree clean';
|
||||
const FETCH_DEPTH_MARGIN = 10;
|
||||
var WorkingBaseType;
|
||||
(function (WorkingBaseType) {
|
||||
WorkingBaseType["Branch"] = "branch";
|
||||
@@ -64,11 +65,12 @@ function getWorkingBaseAndType(git) {
|
||||
});
|
||||
}
|
||||
exports.getWorkingBaseAndType = getWorkingBaseAndType;
|
||||
function tryFetch(git, remote, branch) {
|
||||
function tryFetch(git, remote, branch, depth) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
yield git.fetch([`${branch}:refs/remotes/${remote}/${branch}`], remote, [
|
||||
'--force'
|
||||
'--force',
|
||||
`--depth=${depth}`
|
||||
]);
|
||||
return true;
|
||||
}
|
||||
@@ -164,20 +166,12 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName
|
||||
}
|
||||
// Stash any uncommitted tracked and untracked changes
|
||||
const stashed = yield git.stashPush(['--include-untracked']);
|
||||
// Perform fetch and reset the working base
|
||||
// Reset the working base
|
||||
// Commits made during the workflow will be removed
|
||||
if (workingBaseType == WorkingBaseType.Branch) {
|
||||
core.info(`Resetting working base branch '${workingBase}'`);
|
||||
if (branchRemoteName == 'fork') {
|
||||
// If pushing to a fork we must fetch with 'unshallow' to avoid the following error on git push
|
||||
// ! [remote rejected] HEAD -> tests/push-branch-to-fork (shallow update not allowed)
|
||||
yield git.fetch([`${workingBase}:${workingBase}`], baseRemote, ['--force'], true);
|
||||
}
|
||||
else {
|
||||
// If the remote is 'origin' we can git reset
|
||||
yield git.checkout(workingBase);
|
||||
yield git.exec(['reset', '--hard', `${baseRemote}/${workingBase}`]);
|
||||
}
|
||||
yield git.checkout(workingBase);
|
||||
yield git.exec(['reset', '--hard', `${baseRemote}/${workingBase}`]);
|
||||
}
|
||||
// If the working base is not the base, rebase the temp branch commits
|
||||
// This will also be true if the working base type is a commit
|
||||
@@ -204,8 +198,13 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName
|
||||
// Reset the base
|
||||
yield git.fetch([`${base}:${base}`], baseRemote, fetchArgs);
|
||||
}
|
||||
// Determine the fetch depth for the pull request branch (best effort)
|
||||
const tempBranchCommitsAhead = yield commitsAhead(git, base, tempBranch);
|
||||
const fetchDepth = tempBranchCommitsAhead > 0
|
||||
? tempBranchCommitsAhead + FETCH_DEPTH_MARGIN
|
||||
: FETCH_DEPTH_MARGIN;
|
||||
// Try to fetch the pull request branch
|
||||
if (!(yield tryFetch(git, branchRemoteName, branch))) {
|
||||
if (!(yield tryFetch(git, branchRemoteName, branch, fetchDepth))) {
|
||||
// The pull request branch does not exist
|
||||
core.info(`Pull request branch '${branch}' does not exist yet.`);
|
||||
// Create the pull request branch
|
||||
@@ -236,7 +235,6 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName
|
||||
// temp branch. This catches a case where the base branch has been force pushed to
|
||||
// a new commit.
|
||||
// For changes on base this reset is equivalent to a rebase of the pull request branch.
|
||||
const tempBranchCommitsAhead = yield commitsAhead(git, base, tempBranch);
|
||||
const branchCommitsAhead = yield commitsAhead(git, base, branch);
|
||||
if ((yield git.hasDiff([`${branch}..${tempBranch}`])) ||
|
||||
branchCommitsAhead != tempBranchCommitsAhead ||
|
||||
|
||||
8
package-lock.json
generated
8
package-lock.json
generated
@@ -20,7 +20,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^29.5.12",
|
||||
"@types/node": "^18.19.23",
|
||||
"@types/node": "^18.19.31",
|
||||
"@typescript-eslint/parser": "^5.62.0",
|
||||
"@vercel/ncc": "^0.38.1",
|
||||
"eslint": "^8.57.0",
|
||||
@@ -1656,9 +1656,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@types/node": {
|
||||
"version": "18.19.23",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.19.23.tgz",
|
||||
"integrity": "sha512-wtE3d0OUfNKtZYAqZb8HAWGxxXsImJcPUAgZNw+dWFxO6s5tIwIjyKnY76tsTatsNCLJPkVYwUpq15D38ng9Aw==",
|
||||
"version": "18.19.31",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.19.31.tgz",
|
||||
"integrity": "sha512-ArgCD39YpyyrtFKIqMDvjz79jto5fcI/SVUs2HwB+f0dAzq68yqOdyaSivLiLugSziTpNXLQrVb7RZFmdZzbhA==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"undici-types": "~5.26.4"
|
||||
|
||||
@@ -40,7 +40,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^29.5.12",
|
||||
"@types/node": "^18.19.23",
|
||||
"@types/node": "^18.19.31",
|
||||
"@typescript-eslint/parser": "^5.62.0",
|
||||
"@vercel/ncc": "^0.38.1",
|
||||
"eslint": "^8.57.0",
|
||||
|
||||
@@ -6,6 +6,8 @@ const CHERRYPICK_EMPTY =
|
||||
'The previous cherry-pick is now empty, possibly due to conflict resolution.'
|
||||
const NOTHING_TO_COMMIT = 'nothing to commit, working tree clean'
|
||||
|
||||
const FETCH_DEPTH_MARGIN = 10
|
||||
|
||||
export enum WorkingBaseType {
|
||||
Branch = 'branch',
|
||||
Commit = 'commit'
|
||||
@@ -31,11 +33,13 @@ export async function getWorkingBaseAndType(
|
||||
export async function tryFetch(
|
||||
git: GitCommandManager,
|
||||
remote: string,
|
||||
branch: string
|
||||
branch: string,
|
||||
depth: number
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
await git.fetch([`${branch}:refs/remotes/${remote}/${branch}`], remote, [
|
||||
'--force'
|
||||
'--force',
|
||||
`--depth=${depth}`
|
||||
])
|
||||
return true
|
||||
} catch {
|
||||
@@ -173,24 +177,12 @@ export async function createOrUpdateBranch(
|
||||
// Stash any uncommitted tracked and untracked changes
|
||||
const stashed = await git.stashPush(['--include-untracked'])
|
||||
|
||||
// Perform fetch and reset the working base
|
||||
// Reset the working base
|
||||
// Commits made during the workflow will be removed
|
||||
if (workingBaseType == WorkingBaseType.Branch) {
|
||||
core.info(`Resetting working base branch '${workingBase}'`)
|
||||
if (branchRemoteName == 'fork') {
|
||||
// If pushing to a fork we must fetch with 'unshallow' to avoid the following error on git push
|
||||
// ! [remote rejected] HEAD -> tests/push-branch-to-fork (shallow update not allowed)
|
||||
await git.fetch(
|
||||
[`${workingBase}:${workingBase}`],
|
||||
baseRemote,
|
||||
['--force'],
|
||||
true
|
||||
)
|
||||
} else {
|
||||
// If the remote is 'origin' we can git reset
|
||||
await git.checkout(workingBase)
|
||||
await git.exec(['reset', '--hard', `${baseRemote}/${workingBase}`])
|
||||
}
|
||||
await git.checkout(workingBase)
|
||||
await git.exec(['reset', '--hard', `${baseRemote}/${workingBase}`])
|
||||
}
|
||||
|
||||
// If the working base is not the base, rebase the temp branch commits
|
||||
@@ -227,8 +219,15 @@ export async function createOrUpdateBranch(
|
||||
await git.fetch([`${base}:${base}`], baseRemote, fetchArgs)
|
||||
}
|
||||
|
||||
// Determine the fetch depth for the pull request branch (best effort)
|
||||
const tempBranchCommitsAhead = await commitsAhead(git, base, tempBranch)
|
||||
const fetchDepth =
|
||||
tempBranchCommitsAhead > 0
|
||||
? tempBranchCommitsAhead + FETCH_DEPTH_MARGIN
|
||||
: FETCH_DEPTH_MARGIN
|
||||
|
||||
// Try to fetch the pull request branch
|
||||
if (!(await tryFetch(git, branchRemoteName, branch))) {
|
||||
if (!(await tryFetch(git, branchRemoteName, branch, fetchDepth))) {
|
||||
// The pull request branch does not exist
|
||||
core.info(`Pull request branch '${branch}' does not exist yet.`)
|
||||
// Create the pull request branch
|
||||
@@ -262,7 +261,6 @@ export async function createOrUpdateBranch(
|
||||
// temp branch. This catches a case where the base branch has been force pushed to
|
||||
// a new commit.
|
||||
// For changes on base this reset is equivalent to a rebase of the pull request branch.
|
||||
const tempBranchCommitsAhead = await commitsAhead(git, base, tempBranch)
|
||||
const branchCommitsAhead = await commitsAhead(git, base, branch)
|
||||
if (
|
||||
(await git.hasDiff([`${branch}..${tempBranch}`])) ||
|
||||
|
||||
Reference in New Issue
Block a user