Skip to content

Commit 2b076b0

Browse files
committed
getting commands to work with new multi-root
1 parent 70bc12d commit 2b076b0

File tree

5 files changed

+94
-24
lines changed

5 files changed

+94
-24
lines changed

src/commands.js

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,77 @@ const { commands, scm, window } = require("vscode");
22
const { inputCommitMessage, changesCommitted } = require("./messages");
33
const svn = require("./svn");
44

5-
function SvnCommands() {
6-
commands.registerCommand("svn.fileOpen", this.fileOpen);
7-
commands.registerCommand("svn.commitWithMessage", this.commitWithMessage);
8-
commands.registerCommand("svn.add", this.addFile);
5+
function SvnCommands(model) {
6+
this.model = model;
7+
this.commands = [
8+
{
9+
commandId: "svn.commitWithMessage",
10+
method: this.commitWithMessage,
11+
options: { repository: true }
12+
}
13+
];
14+
15+
this.commands.map(({ commandId, method, options }) => {
16+
const command = this.createCommand(method, options);
17+
commands.registerCommand(commandId, command);
18+
});
19+
// commands.registerCommand("svn.fileOpen", this.fileOpen);
20+
// commands.registerCommand("svn.commitWithMessage", this.commitWithMessage);
21+
// commands.registerCommand("svn.add", this.addFile);
922
}
1023

24+
SvnCommands.prototype.createCommand = function(method, options) {
25+
const result = (...args) => {
26+
let result;
27+
28+
if (!options.repository) {
29+
result = Promise.resolve(method.apply(this, args));
30+
} else {
31+
const repository = this.model.getRepository(args[0]);
32+
let repositoryPromise;
33+
34+
if (repository) {
35+
repositoryPromise = Promise.resolve(repository);
36+
} else if (this.model.openRepositories.length === 1) {
37+
repositoryPromise = Promise.resolve(this.model.openRepositories[0]);
38+
} else {
39+
repositoryPromise = this.model.pickRepository();
40+
}
41+
42+
result = repositoryPromise.then(repository => {
43+
if (!repository) {
44+
return Promise.resolve();
45+
}
46+
47+
return Promise.resolve(method.apply(this, [repository, ...args]));
48+
});
49+
}
50+
51+
return result.catch(async err => {
52+
console.error(err);
53+
});
54+
};
55+
56+
return result;
57+
};
58+
1159
SvnCommands.prototype.fileOpen = resourceUri => {
1260
commands.executeCommand("vscode.open", resourceUri);
1361
};
1462

15-
SvnCommands.prototype.commitWithMessage = async function() {
16-
this.svn = new svn();
17-
let message = await inputCommitMessage(scm.inputBox.value);
63+
SvnCommands.prototype.commitWithMessage = async function(repository) {
64+
// console.log("fsdsfsf");
65+
// this.svn = new svn();
66+
// let message = await inputCommitMessage(scm.inputBox.value);
1867

19-
try {
20-
await this.svn.commit(message);
21-
scm.inputBox.value = "";
22-
changesCommitted();
23-
} catch (error) {
24-
console.log(error);
25-
}
68+
console.log(repository.inputBox.value);
69+
// try {
70+
// await this.svn.commit(message);
71+
// scm.inputBox.value = "";
72+
// changesCommitted();
73+
// } catch (error) {
74+
// console.log(error);
75+
// }
2676
};
2777

2878
SvnCommands.prototype.addFile = async uri => {

src/extension.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ function activate(context) {
99
console.log("svn-scm is now active!");
1010

1111
const disposable = [];
12-
const commands = new SvnCommands();
1312
const svn = new Svn();
1413
const model = new Model(svn);
1514
const contentProvider = new svnContentProvider();
15+
const commands = new SvnCommands(model);
1616

1717
context.subscriptions.push(disposable);
1818
}

src/model.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { workspace, Uri } = require("vscode");
1+
const { workspace, Uri, window } = require("vscode");
22
const fs = require("fs");
33
const path = require("path");
44
const Repository = require("./repository");
@@ -25,7 +25,7 @@ Model.prototype.tryOpenRepository = async function(path) {
2525
const repositoryRoot = await this.svn.getRepositoryRoot(path);
2626
const repository = new Repository(this.svn.open(repositoryRoot));
2727

28-
// this.open(repository);
28+
this.open(repository);
2929
} catch (err) {
3030
return;
3131
}
@@ -61,4 +61,21 @@ Model.prototype.open = function(repository) {
6161
this.openRepositories.push(repository);
6262
};
6363

64+
Model.prototype.pickRepository = async function() {
65+
if (this.openRepositories.length === 0) {
66+
throw new Error("There are no available repositories");
67+
}
68+
69+
const picks = this.openRepositories.map(repo => {
70+
return {
71+
label: path.basename(repo.repository.root),
72+
repository: repo
73+
};
74+
});
75+
const placeholder = "Choose a repository";
76+
const pick = await window.showQuickPick(picks, { placeholder });
77+
78+
return pick && pick.repository;
79+
};
80+
6481
module.exports = Model;

src/repository.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ function Repository(repository) {
1111
);
1212
this.sourceControl.acceptInputCommand = {
1313
command: "svn.commitWithMessage",
14-
title: "commit"
14+
title: "commit",
15+
arguments: this.sourceControl
1516
};
1617
this.sourceControl.quickDiffProvider = this;
18+
this.inputBox = this.sourceControl.inputBox;
1719

1820
this.changes = this.sourceControl.createResourceGroup("changes", "Changes");
1921
this.notTracked = this.sourceControl.createResourceGroup(

src/svn.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,16 @@ svn.prototype.add = function(filePath) {
5454
module.exports = svn;
5555

5656
function Repository(svn, repositoryRoot) {
57-
this.svn = new SvnSpawn({
58-
cwd: repositoryRoot,
57+
this.svn = svn;
58+
this.root = repositoryRoot;
59+
60+
this.svn.client.option({
61+
cwd: this.root,
5962
noAuthCache: true
6063
});
61-
this.root = repositoryRoot;
64+
6265
}
6366

6467
Repository.prototype.getStatus = function() {
65-
return new Promise((resolve, reject) => {
66-
this.svn.getStatus((err, data) => (err ? reject(err) : resolve(data)));
67-
});
68+
return this.svn.getStatus();
6869
}

0 commit comments

Comments
 (0)