-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
124 lines (110 loc) · 3.86 KB
/
extension.js
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
'use strict';
const vscode = require('vscode');
const git = require('./git');
const path = require('path');
const QuickPickDiffItem = function (diffItem) {
this.name = diffItem.name;
this.sha = diffItem.sha;
this.msg = diffItem.msg;
this.label = diffItem.name;
this.description = diffItem.sha + ' (' + diffItem.msg + ')';
};
const gitLookupForBranch = branch => {
// Assuming we will either get back an internally defined branch/diff obj, or a simple branch name/sha
if (branch.hasOwnProperty('name')) {
return branch.name;
} else if (branch.hasOwnProperty('sha')) {
return branch.sha;
} else {
return branch;
}
};
exports.gitLookupForBranch = gitLookupForBranch;
const chooseBranch = () => git.branchList()
.then(branchList => {
let defaultBranch = branchList.length ? branchList[0].name : 'master';
return vscode.window.showQuickPick(
branchList.map(item => new QuickPickDiffItem(item)), {
placeHolder: defaultBranch
}
);
},
() => null);
const reviewSeparateBranches = () => {
chooseBranch()
.then(
baseBranch => {
chooseBranch()
.then(
patchBranch => {
reviewBranches(baseBranch, patchBranch);
},
() => null
);
},
() => null
);
};
const reviewAgainstCurrentBranch = () => {
git.currentBranch()
.then(
currentBranch => {
chooseBranch()
.then(
selectedBranch => {
reviewBranches(currentBranch, selectedBranch);
},
() => null
);
},
error => vscode.window.showInformationMessage(error)
);
};
function handleDiffs(targetBranch, baseFileName, patchTempFileName, stateFlag, deletedFileName) {
//if (!baseFileName || !patchTempFileName) return;
console.log(targetBranch, baseFileName, patchTempFileName);
let baseFilePath = vscode.Uri.file(path.join(vscode.workspace.rootPath, baseFileName));
let patchTempFilePath = vscode.Uri.file(patchTempFileName);
if (stateFlag === git.CONFLICT) {
console.log(targetBranch, baseFileName, patchTempFileName, stateFlag, deletedFileName);
console.log('conflict set');
return Promise.resolve();
} else if (stateFlag === git.NEW) {
console.log("New?", stateFlag);
return vscode.commands.executeCommand('vscode.diff', patchTempFilePath, baseFilePath, `(${targetBranch})[NEW]⟷${baseFileName}`)
.then(() => {
console.log("Called at:", baseFileName);
return vscode.commands.executeCommand('workbench.action.keepEditor', vscode.window.activeTextEditor);
});
} else if (stateFlag === git.DELETED) {
console.log("Deleted?", stateFlag);
return Promise.resolve();
} else {
console.log("Modified?", stateFlag);
return vscode.commands.executeCommand('vscode.diff', patchTempFilePath, baseFilePath, `(${targetBranch})⟷${baseFileName}`)
.then(() => {
console.log("Called at:", baseFileName);
return vscode.commands.executeCommand('workbench.action.keepEditor', vscode.window.activeTextEditor);
});
}
}
const reviewBranches = (baseBranch, patchBranch) => {
let baseBranchLookup = gitLookupForBranch(baseBranch);
let patchBranchLookup = gitLookupForBranch(patchBranch);
let resultsPromise = git.diffState(baseBranchLookup, patchBranchLookup);
const handler = r => {
r.unshift(baseBranchLookup);
return () => handleDiffs.apply(0, r);
};
resultsPromise.then(results => results.reduce((p, r) => p.then(handler(r)), Promise.resolve()));
};
function activate(context) {
git.setGitRepoBase(vscode.workspace.rootPath);
let reviewDisposable = vscode.commands.registerCommand('ps-vscode.review', reviewSeparateBranches);
let currentReviewDisposable = vscode.commands.registerCommand('ps-vscode.reviewCurrent', reviewAgainstCurrentBranch);
context.subscriptions.push(reviewDisposable);
context.subscriptions.push(currentReviewDisposable);
}
exports.activate = activate;
function deactivate() { }
exports.deactivate = deactivate;