Skip to content

Commit

Permalink
added ability to revert
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnstonCode committed Nov 27, 2017
1 parent 210e295 commit 5656ea4
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 22 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# **v1.1.0**

## What's New

* Added file revert command

# **v1.0.3**

## Changes
Expand Down
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "svn-scm",
"displayName": "SVN",
"description": "Integrated Subversion source control",
"version": "1.0.3",
"version": "1.1.0",
"publisher": "johnstoncode",
"engines": {
"vscode": "^1.17.0"
Expand Down Expand Up @@ -71,6 +71,11 @@
"command": "svn.switchBranch",
"title": "Switch Branch",
"category": "SVN"
},
{
"command": "svn.revert",
"title": "Revert Selected",
"category": "SVN"
}
],
"menus": {
Expand Down Expand Up @@ -102,6 +107,11 @@
"command": "svn.openDiffHead",
"when": "scmProvider == svn && scmResourceGroup == changes",
"group": "1_modification"
},
{
"command": "svn.revert",
"when": "scmProvider == svn && scmResourceGroup == changes",
"group": "1_modification"
}
],
"editor/title": []
Expand Down
21 changes: 20 additions & 1 deletion src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CreateBranchItem implements QuickPickItem {
class SwitchBranchItem implements QuickPickItem {
protected tree: string = "";
protected name: string = "";

constructor(protected ref: string) {
let parts = ref.split("/");
if (parts[1]) {
Expand Down Expand Up @@ -106,6 +106,11 @@ export class SvnCommands {
commandId: "svn.branch",
method: this.branch,
options: { repository: true }
},
{
commandId: "svn.revert",
method: this.revert,
options: { repository: true }
}
];

Expand Down Expand Up @@ -313,4 +318,18 @@ export class SvnCommands {
);
await repository.branch(name);
}

async revert(repository: Repository, ...args: any[][]) {
try {
const paths = args[0].map(state => {
return state.resourceUri.fsPath;
});

await repository.repository.revert(paths);
repository.update();
} catch (error) {
console.error(error);
window.showErrorMessage("Unable to revert");
}
}
}
65 changes: 45 additions & 20 deletions src/svn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ export class Svn {
switchBranch(root: string, path: string) {
return this.exec(root, ["switch", path]);
}

revert(files: any[]) {
let args = ["revert"];

for (let file of files) {
args.push(file);
}

return this.exec("", args);
}
}

export class Repository {
Expand Down Expand Up @@ -188,13 +198,15 @@ export class Repository {

async getRepoUrl() {
const info = await this.svn.info(this.root);

if (info.exitCode !== 0) {
throw new Error(info.stderr);
}

let repoUrl = info.stdout.match(/<root>(.*?)<\/root>/)[1];
const match = info.stdout.match(/<url>(.*?)\/(trunk|branches|tags).*?<\/url>/);
const match = info.stdout.match(
/<url>(.*?)\/(trunk|branches|tags).*?<\/url>/
);

if (match[1]) {
repoUrl = match[1];
Expand All @@ -208,7 +220,12 @@ export class Repository {

const branches = [];

let trunkExists = await this.svn.exec("", ["ls", repoUrl + "/trunk", "--depth", "empty"]);
let trunkExists = await this.svn.exec("", [
"ls",
repoUrl + "/trunk",
"--depth",
"empty"
]);

if (trunkExists.exitCode === 0) {
branches.push("trunk");
Expand All @@ -219,23 +236,21 @@ export class Repository {
for (let index in trees) {
const tree = trees[index];
const branchUrl = repoUrl + "/" + tree;

const result = await this.svn.list(branchUrl);

if (result.exitCode !== 0) {
continue;
}

const list = result.stdout
.trim()
.replace(/\/|\\/g, "")
.split(/[\r\n]+/)
.map((i: string) => tree + "/" + i);

branches.push(...list);

}

const result = await this.svn.list(branchUrl);

if (result.exitCode !== 0) {
continue;
}

const list = result.stdout
.trim()
.replace(/\/|\\/g, "")
.split(/[\r\n]+/)
.map((i: string) => tree + "/" + i);

branches.push(...list);
}

return branches;
}
Expand All @@ -262,7 +277,7 @@ export class Repository {

async switchBranch(ref: string) {
const repoUrl = await this.getRepoUrl();

var branchUrl = repoUrl + "/" + ref;

const switchBranch = await this.svn.switchBranch(this.root, branchUrl);
Expand All @@ -273,4 +288,14 @@ export class Repository {

return true;
}

async revert(files: any[]) {
const result = await this.svn.revert(files);

if (result.exitCode !== 0) {
throw new Error(result.stderr);
}

return result.stdout;
}
}

0 comments on commit 5656ea4

Please sign in to comment.