Git - Add merge abort command (#159753)

* Add merge abort command

* Pull request feedback
This commit is contained in:
Ladislau Szomoru 2022-09-07 15:06:20 +02:00 committed by GitHub
parent c6fd3c9c17
commit 1e99736c27
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 2 deletions

View file

@ -369,6 +369,11 @@
"title": "%command.merge%",
"category": "Git"
},
{
"command": "git.mergeAbort",
"title": "%command.mergeAbort%",
"category": "Git"
},
{
"command": "git.rebase",
"title": "%command.rebase%",
@ -898,6 +903,10 @@
"command": "git.merge",
"when": "config.git.enabled && !git.missing && gitOpenRepositoryCount != 0"
},
{
"command": "git.mergeAbort",
"when": "config.git.enabled && !git.missing && gitOpenRepositoryCount != 0 && gitMergeInProgress"
},
{
"command": "git.rebase",
"when": "config.git.enabled && !git.missing && gitOpenRepositoryCount != 0"

View file

@ -58,6 +58,7 @@
"command.renameBranch": "Rename Branch...",
"command.cherryPick": "Cherry Pick...",
"command.merge": "Merge Branch...",
"command.mergeAbort": "Abort Merge",
"command.rebase": "Rebase Branch...",
"command.createTag": "Create Tag",
"command.deleteTag": "Delete Tag",

View file

@ -205,7 +205,7 @@ export class ActionButtonCommand {
this.state = {
...this.state,
HEAD: this.repository.HEAD,
isMergeInProgress: this.repository.mergeGroup.resourceStates.length !== 0,
isMergeInProgress: this.repository.mergeInProgress,
isRebaseInProgress: !!this.repository.rebaseCommit,
repositoryHasChangesToCommit: this.repositoryHasChangesToCommit()
};

View file

@ -2202,6 +2202,11 @@ export class CommandCenter {
await choice.run(repository);
}
@command('git.mergeAbort', { repository: true })
async abortMerge(repository: Repository): Promise<void> {
await repository.mergeAbort();
}
@command('git.rebase', { repository: true })
async rebase(repository: Repository): Promise<void> {
const config = workspace.getConfiguration('git');

View file

@ -1570,6 +1570,10 @@ export class Repository {
}
}
async mergeAbort(): Promise<void> {
await this.exec(['merge', '--abort']);
}
async tag(name: string, message?: string): Promise<void> {
let args = ['tag'];

View file

@ -336,6 +336,7 @@ export const enum Operation {
RenameBranch = 'RenameBranch',
DeleteRef = 'DeleteRef',
Merge = 'Merge',
MergeAbort = 'MergeAbort',
Rebase = 'Rebase',
Ignore = 'Ignore',
Tag = 'Tag',
@ -840,6 +841,17 @@ export class Repository implements Disposable {
return this._rebaseCommit;
}
private _mergeInProgress: boolean = false;
set mergeInProgress(value: boolean) {
this._mergeInProgress = value;
commands.executeCommand('setContext', 'gitMergeInProgress', value);
}
get mergeInProgress() {
return this._mergeInProgress;
}
private _operations = new OperationsImpl();
get operations(): Operations { return this._operations; }
@ -1365,6 +1377,10 @@ export class Repository implements Disposable {
await this.run(Operation.Merge, () => this.repository.merge(ref));
}
async mergeAbort(): Promise<void> {
await this.run(Operation.MergeAbort, async () => await this.repository.mergeAbort());
}
async rebase(branch: string): Promise<void> {
await this.run(Operation.Rebase, () => this.repository.rebase(branch));
}
@ -1990,13 +2006,14 @@ export class Repository implements Disposable {
if (sort !== 'alphabetically' && sort !== 'committerdate') {
sort = 'alphabetically';
}
const [refs, remotes, submodules, rebaseCommit] = await Promise.all([this.repository.getRefs({ sort }), this.repository.getRemotes(), this.repository.getSubmodules(), this.getRebaseCommit()]);
const [refs, remotes, submodules, rebaseCommit, mergeInProgress] = await Promise.all([this.repository.getRefs({ sort }), this.repository.getRemotes(), this.repository.getSubmodules(), this.getRebaseCommit(), this.isMergeInProgress()]);
this._HEAD = HEAD;
this._refs = refs!;
this._remotes = remotes!;
this._submodules = submodules!;
this.rebaseCommit = rebaseCommit;
this.mergeInProgress = mergeInProgress;
const index: Resource[] = [];
const workingTree: Resource[] = [];
@ -2110,6 +2127,11 @@ export class Repository implements Disposable {
}
}
private isMergeInProgress(): Promise<boolean> {
const mergeHeadPath = path.join(this.repository.root, '.git', 'MERGE_HEAD');
return new Promise<boolean>(resolve => fs.exists(mergeHeadPath, resolve));
}
private async maybeAutoStash<T>(runOperation: () => Promise<T>): Promise<T> {
const config = workspace.getConfiguration('git', Uri.file(this.root));
const shouldAutoStash = config.get<boolean>('autoStash')