fix: abort conflicting cherry-pick before starting new one (#146)

Signed-off-by: Andrea Lamparelli <a.lamparelli95@gmail.com>
This commit is contained in:
Andrea Lamparelli 2024-10-28 11:51:36 +01:00 committed by GitHub
parent 2b4b429356
commit 3deee59d4c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 66 additions and 0 deletions

21
dist/cli/index.js vendored
View file

@ -541,6 +541,13 @@ class GitCLIService {
return; return;
} }
this.logger.info(`Folder ${to} already exist. Won't clone`); 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 // checkout to the proper branch
this.logger.info(`Checking out branch ${branch}`); this.logger.info(`Checking out branch ${branch}`);
await this.git(to).checkout(branch); await this.git(to).checkout(branch);
@ -598,6 +605,20 @@ class GitCLIService {
throw error; 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 * Push a branch to a remote
* @param cwd repository in which the push should be performed * @param cwd repository in which the push should be performed

21
dist/gha/index.js vendored
View file

@ -506,6 +506,13 @@ class GitCLIService {
return; return;
} }
this.logger.info(`Folder ${to} already exist. Won't clone`); 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 // checkout to the proper branch
this.logger.info(`Checking out branch ${branch}`); this.logger.info(`Checking out branch ${branch}`);
await this.git(to).checkout(branch); await this.git(to).checkout(branch);
@ -563,6 +570,20 @@ class GitCLIService {
throw error; 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 * Push a branch to a remote
* @param cwd repository in which the push should be performed * @param cwd repository in which the push should be performed

View file

@ -68,6 +68,15 @@ export default class GitCLIService {
} }
this.logger.info(`Folder ${to} already exist. Won't clone`); 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 // checkout to the proper branch
this.logger.info(`Checking out branch ${branch}`); this.logger.info(`Checking out branch ${branch}`);
await this.git(to).checkout(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<boolean> {
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 * Push a branch to a remote
* @param cwd repository in which the push should be performed * @param cwd repository in which the push should be performed