forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconflictResolutionModel.ts
More file actions
88 lines (73 loc) · 3.5 KB
/
conflictResolutionModel.ts
File metadata and controls
88 lines (73 loc) · 3.5 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
/*---------------------------------------------------------------------------------------------
* 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 { Schemes, toGitHubUri } from '../common/uri';
export interface Conflict {
prHeadFilePath: string;
contentsConflict: boolean;
filePathConflict: boolean;
modeConflict: boolean;
}
export interface ResolvedConflict {
prHeadFilePath: string;
resolvedContents?: string;
// The other two fields can be added later. To begin with, we only support resolving the contents.
// resolvedFilePath: string;
// resolvedMode: string;
}
export class ConflictResolutionModel {
private _startingConflicts: Map<string, Conflict> = new Map();
private readonly _resolvedConflicts: Map<string, ResolvedConflict> = new Map();
private readonly _onAddedResolution: vscode.EventEmitter<void> = new vscode.EventEmitter<void>();
public readonly onAddedResolution: vscode.Event<void> = this._onAddedResolution.event;
public readonly mergeScheme = `${Schemes.MergeOutput}-${new Date().getTime()}`;
constructor(public readonly startingConflicts: Conflict[], public readonly repositoryName: string, public readonly prBaseOwner: string,
public readonly latestPrBaseSha: string,
public readonly prHeadOwner: string, public readonly prHeadBranchName: string,
public readonly prBaseBranchName: string, public readonly prMergeBaseRef: string) {
for (const conflict of startingConflicts) {
this._startingConflicts.set(conflict.prHeadFilePath, conflict);
}
}
isResolvable(): boolean {
return Array.from(this._startingConflicts.values()).every(conflict => {
return !conflict.filePathConflict && !conflict.modeConflict;
});
}
addResolution(filePath: string, contents: string): void {
this._resolvedConflicts.set(filePath, { prHeadFilePath: filePath, resolvedContents: contents });
this._onAddedResolution.fire();
}
isResolved(filePath: string): boolean {
if (!this._startingConflicts.has(filePath)) {
throw new Error('Not a conflict file');
}
return this._resolvedConflicts.has(filePath);
}
get areAllConflictsResolved(): boolean {
return this._resolvedConflicts.size === this._startingConflicts.size;
}
get resolvedConflicts(): Map<string, ResolvedConflict> {
if (this._resolvedConflicts.size !== this._startingConflicts.size) {
throw new Error('Not all conflicts have been resolved');
}
return this._resolvedConflicts;
}
public mergeOutputUri(conflict: Conflict) {
return vscode.Uri.parse(`${this.mergeScheme}:/${conflict.prHeadFilePath}`);
}
public mergeBaseUri(conflict: { prHeadFilePath: string }): vscode.Uri {
const fileUri = vscode.Uri.file(conflict.prHeadFilePath);
return toGitHubUri(fileUri, Schemes.GithubPr, { fileName: conflict.prHeadFilePath, branch: this.prMergeBaseRef, owner: this.prBaseOwner });
}
public baseUri(conflict: Conflict): vscode.Uri {
const fileUri = vscode.Uri.file(conflict.prHeadFilePath);
return toGitHubUri(fileUri, Schemes.GithubPr, { fileName: conflict.prHeadFilePath, branch: this.latestPrBaseSha, owner: this.prBaseOwner });
}
public prHeadUri(conflict: Conflict): vscode.Uri {
const fileUri = vscode.Uri.file(conflict.prHeadFilePath);
return toGitHubUri(fileUri, Schemes.GithubPr, { fileName: conflict.prHeadFilePath, branch: this.prHeadBranchName, owner: this.prHeadOwner });
}
}