feat: command to simulate Merge/Pull Request

- Adds `git merge(M|P)R <source_branch> <target_branch> [--delete-after-merge]` command
to simulate a Merge/Pull Request being merged into the target branch
- The optional flag `--delete-after-merge` will remove the merged branch on the origin tree
  - The local remote branch remains as it is Git default behavior, even after `git fetch`
(it would require `git fetch --prune` support to automatically prune remote tracking branches)
- You can push a branch with a deleted remote tracking branch on origin, the ref will just update.

Resolves #1057
This commit is contained in:
Lucie Lenglet 2023-12-15 19:32:47 +01:00
parent 99bc0ca8c5
commit acffcc1616
3 changed files with 128 additions and 4 deletions

View file

@ -589,6 +589,47 @@ var commandConfig = {
}
},
mergeMR: {
regex: /^git +merge[MP]R($|\s)/,
options: ['--delete-after-merge'],
execute: function(engine, command) {
var generalArgs = command.getGeneralArgs();
var commandOptions = command.getOptionsMap();
if (!engine.hasOrigin()) {
throw new GitError({
msg: intl.str('git-error-origin-required'),
});
}
command.validateArgBounds(generalArgs, 2, 2);
var fromBranch = validateOriginBranchName(engine, generalArgs[0]);
var intoBranch = validateOriginBranchName(engine, generalArgs[1]);
var origin = engine.origin;
origin.checkout(intoBranch);
var mergeCommit = origin.merge(fromBranch, { noFF: true });
origin.animationFactory.genCommitBirthAnimation(
origin.animationQueue,
mergeCommit,
origin.gitVisuals
);
if (!!commandOptions['--delete-after-merge']) {
origin.validateAndDeleteBranch(fromBranch);
}
origin.checkout('main');
origin.animationFactory.playRefreshAnimationAndFinish(
origin.gitVisuals,
origin.animationQueue
);
}
},
revlist: {
dontCountForGolf: true,
displayName: 'rev-list',

View file

@ -453,10 +453,17 @@ GitEngine.prototype.findCommonAncestorWithRemote = function(originTarget) {
};
GitEngine.prototype.makeBranchOnOriginAndTrack = function(branchName, target) {
var remoteBranch = this.makeBranch(
ORIGIN_PREFIX + branchName,
this.getCommitFromRef(target)
);
var remoteBranch = this.refs[ORIGIN_PREFIX + branchName];
// If the remote branch exists but the branch on origin was deleted, updates its target location
if (remoteBranch) {
this.setTargetLocation(remoteBranch, target);
} else {
remoteBranch = this.makeBranch(
ORIGIN_PREFIX + branchName,
this.getCommitFromRef(target)
);
}
if (this.refs[branchName]) { // not all remote branches have tracking ones
this.setLocalToTrackRemote(this.refs[branchName], remoteBranch);