diff --git a/dist/cli/index.js b/dist/cli/index.js index d2255fb..aeba715 100755 --- a/dist/cli/index.js +++ b/dist/cli/index.js @@ -541,6 +541,13 @@ class GitCLIService { return; } this.logger.info(`Folder ${to} already exist. Won't clone`); + // ensure the working tree is properly reset - no stale changes + // from previous (failed) backport + const ongoingCherryPick = await this.anyConflict(to); + if (ongoingCherryPick) { + this.logger.warn("Found previously failed cherry-pick, aborting it"); + await this.git(to).raw(["cherry-pick", "--abort"]); + } // checkout to the proper branch this.logger.info(`Checking out branch ${branch}`); await this.git(to).checkout(branch); @@ -598,6 +605,20 @@ class GitCLIService { throw error; } } + /** + * Check whether there are some conflicts in the current working directory + * which means there is an ongoing cherry-pick that did not complete successfully + * @param cwd repository in which the check should be performed + * @return true if there is some conflict, false otherwise + */ + async anyConflict(cwd) { + const status = await this.git(cwd).status(); + if (status.conflicted.length > 0) { + this.logger.debug(`Found conflicts in branch ${status.current}`); + return true; + } + return false; + } /** * Push a branch to a remote * @param cwd repository in which the push should be performed diff --git a/dist/gha/index.js b/dist/gha/index.js index 5798d22..80b6947 100755 --- a/dist/gha/index.js +++ b/dist/gha/index.js @@ -506,6 +506,13 @@ class GitCLIService { return; } this.logger.info(`Folder ${to} already exist. Won't clone`); + // ensure the working tree is properly reset - no stale changes + // from previous (failed) backport + const ongoingCherryPick = await this.anyConflict(to); + if (ongoingCherryPick) { + this.logger.warn("Found previously failed cherry-pick, aborting it"); + await this.git(to).raw(["cherry-pick", "--abort"]); + } // checkout to the proper branch this.logger.info(`Checking out branch ${branch}`); await this.git(to).checkout(branch); @@ -563,6 +570,20 @@ class GitCLIService { throw error; } } + /** + * Check whether there are some conflicts in the current working directory + * which means there is an ongoing cherry-pick that did not complete successfully + * @param cwd repository in which the check should be performed + * @return true if there is some conflict, false otherwise + */ + async anyConflict(cwd) { + const status = await this.git(cwd).status(); + if (status.conflicted.length > 0) { + this.logger.debug(`Found conflicts in branch ${status.current}`); + return true; + } + return false; + } /** * Push a branch to a remote * @param cwd repository in which the push should be performed diff --git a/src/service/git/git-cli.ts b/src/service/git/git-cli.ts index fc22f44..d7fc7e6 100644 --- a/src/service/git/git-cli.ts +++ b/src/service/git/git-cli.ts @@ -68,6 +68,15 @@ export default class GitCLIService { } this.logger.info(`Folder ${to} already exist. Won't clone`); + + // ensure the working tree is properly reset - no stale changes + // from previous (failed) backport + const ongoingCherryPick = await this.anyConflict(to); + if (ongoingCherryPick) { + this.logger.warn("Found previously failed cherry-pick, aborting it"); + await this.git(to).raw(["cherry-pick", "--abort"]); + } + // checkout to the proper branch this.logger.info(`Checking out branch ${branch}`); await this.git(to).checkout(branch); @@ -131,6 +140,21 @@ export default class GitCLIService { } } + /** + * Check whether there are some conflicts in the current working directory + * which means there is an ongoing cherry-pick that did not complete successfully + * @param cwd repository in which the check should be performed + * @return true if there is some conflict, false otherwise + */ + async anyConflict(cwd: string): Promise { + const status = await this.git(cwd).status(); + if (status.conflicted.length > 0) { + this.logger.debug(`Found conflicts in branch ${status.current}`); + return true; + } + return false; + } + /** * Push a branch to a remote * @param cwd repository in which the push should be performed