forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconflictGuide.ts
More file actions
168 lines (153 loc) · 6.38 KB
/
conflictGuide.ts
File metadata and controls
168 lines (153 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { Change, Repository } from '../api/api';
import { commands } from '../common/executeCommands';
import { Disposable } from '../common/lifecycle';
import { asPromise } from '../common/utils';
export class ConflictModel extends Disposable {
public readonly startingConflictsCount: number;
private _lastReportedRemainingCount: number;
private _onConflictCountChanged: vscode.EventEmitter<number> = new vscode.EventEmitter();
public readonly onConflictCountChanged: vscode.Event<number> = this._onConflictCountChanged.event; // reports difference in number of conflicts
private _finishedCommit: vscode.EventEmitter<boolean> = new vscode.EventEmitter();
public readonly message: string;
constructor(private readonly _repository: Repository, private readonly _upstream: string, private readonly _into: string, public readonly push: boolean) {
super();
this.startingConflictsCount = this.remainingConflicts.length;
this._lastReportedRemainingCount = this.startingConflictsCount;
this._repository.inputBox.value = this.message = `Merge branch '${this._upstream}' into ${this._into}`;
this._watchForRemainingConflictsChange();
}
private _watchForRemainingConflictsChange() {
this._register(vscode.window.tabGroups.onDidChangeTabs(async (e) => {
if (e.closed.length > 0) {
await this._repository.status();
this._reportProgress();
}
}));
this._register(this._repository.state.onDidChange(async () => {
this._reportProgress();
}));
}
private _reportProgress() {
if (this._lastReportedRemainingCount === 0) {
// Already done.
return;
}
const remainingCount = this.remainingConflicts.length;
if (this._lastReportedRemainingCount !== remainingCount) {
this._onConflictCountChanged.fire(this._lastReportedRemainingCount - remainingCount);
this._lastReportedRemainingCount = remainingCount;
}
if (this._lastReportedRemainingCount === 0) {
this.listenForCommit();
}
}
private async listenForCommit() {
let localDisposable: vscode.Disposable | undefined;
const result = await new Promise<boolean>(resolve => {
const startingCommit = this._repository.state.HEAD?.commit;
localDisposable = this._register(this._repository.state.onDidChange(() => {
if (this._repository.state.HEAD?.commit !== startingCommit && this._repository.state.indexChanges.length === 0 && this._repository.state.mergeChanges.length === 0) {
resolve(true);
}
}));
});
localDisposable?.dispose();
if (result && this.push) {
this._repository.push();
}
this._finishedCommit.fire(result);
}
get remainingConflicts(): Change[] {
return this._repository.state.mergeChanges;
}
private async closeMergeEditors(): Promise<void> {
for (const group of vscode.window.tabGroups.all) {
for (const tab of group.tabs) {
if (tab.input instanceof vscode.TabInputTextMerge) {
vscode.window.tabGroups.close(tab);
}
}
}
}
public async abort(): Promise<void> {
this._repository.inputBox.value = '';
// set up an event to listen for when we are all out of merge changes before closing the merge editors.
// Just waiting for the merge doesn't cut it
// Even with this, we still need to wait 1 second, and then it still might say there are conflicts. Why is this?
const disposable = this._register(this._repository.state.onDidChange(async () => {
if (this._repository.state.mergeChanges.length === 0) {
await new Promise<void>(resolve => setTimeout(resolve, 1000));
this.closeMergeEditors();
disposable.dispose();
}
}));
await this._repository.mergeAbort();
this._finishedCommit.fire(false);
}
private async first(): Promise<void> {
if (this.remainingConflicts.length === 0) {
return;
}
await commands.focusView('workbench.scm');
this._reportProgress();
await Promise.all(this.remainingConflicts.map(conflict => commands.executeCommand('git.openMergeEditor', conflict.uri)));
}
public static async begin(repository: Repository, upstream: string, into: string, push: boolean): Promise<ConflictModel | undefined> {
const model = new ConflictModel(repository, upstream, into, push);
if (model.remainingConflicts.length === 0) {
return undefined;
}
model._register(new ConflictNotification(model, repository));
model.first();
return model;
}
public finished(): Promise<boolean> {
return asPromise(this._finishedCommit.event);
}
}
class ConflictNotification extends Disposable {
constructor(private readonly _conflictModel: ConflictModel, private readonly _repository: Repository) {
super();
vscode.window.withProgress({ location: vscode.ProgressLocation.Notification, cancellable: true }, async (progress, token) => {
const report = (increment: number) => {
progress.report({ message: vscode.l10n.t('Use the Source Control view to resolve conflicts, {0} of {0} remaining', this._conflictModel.remainingConflicts.length, this._conflictModel.startingConflictsCount), increment });
};
report(0);
return new Promise<boolean>((resolve) => {
this._register(this._conflictModel.onConflictCountChanged((conflictsChangedBy) => {
const increment = conflictsChangedBy * (100 / this._conflictModel.startingConflictsCount);
report(increment);
if (this._conflictModel.remainingConflicts.length === 0) {
resolve(true);
}
}));
this._register(token.onCancellationRequested(() => {
this._conflictModel.abort();
resolve(false);
}));
});
}).then(async (result) => {
if (result) {
const commit = vscode.l10n.t('Commit');
const cancel = vscode.l10n.t('Abort Merge');
let message: string;
if (this._conflictModel.push) {
message = vscode.l10n.t('All conflicts resolved. Commit and push the resolution to continue.');
} else {
message = vscode.l10n.t('All conflicts resolved. Commit the resolution to continue.');
}
const result = await vscode.window.showInformationMessage(message, commit, cancel);
if (result === commit) {
await this._repository.commit(this._conflictModel.message);
} else if (result === cancel) {
await this._conflictModel.abort();
}
}
});
}
}