Skip to content

Commit

Permalink
fix: abort conflicting cherry-pick before starting new one (#146)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Lamparelli <[email protected]>
  • Loading branch information
lampajr authored Oct 28, 2024
1 parent 2b4b429 commit 3deee59
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
21 changes: 21 additions & 0 deletions dist/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions dist/gha/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions src/service/git/git-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
* @param cwd repository in which the push should be performed
Expand Down

0 comments on commit 3deee59

Please sign in to comment.