Skip to content

Commit

Permalink
fix: Fixed set changelist context menu (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnstonCode committed Nov 17, 2018
1 parent d5423e9 commit 7c0886c
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 64 deletions.
142 changes: 90 additions & 52 deletions src/commands/changeList.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,111 @@
import { SourceControlResourceState, window } from "vscode";
import { commands, SourceControlResourceState, Uri, window } from "vscode";
import { inputSwitchChangelist } from "../changelistItems";
import { Model } from "../model";
import { Resource } from "../resource";
import { normalizePath } from "../util";
import { Command } from "./command";

export class ChangeList extends Command {
constructor() {
super("svn.changelist");
}

public async execute(...resourceStates: SourceControlResourceState[]) {
const selection = await this.getResourceStates(resourceStates);
public async execute(...args: any[]) {
let uris: Uri[];

if (selection.length === 0) {
if (args[0] instanceof Resource) {
uris = (args as Resource[]).map(resource => resource.resourceUri);
} else if (args[0] instanceof Uri) {
uris = args[1] as Uri[];
} else {
console.error("Unhandled type for changelist command");
return;
}

const model = (await commands.executeCommand("svn.getModel", "")) as Model;

const promiseArray = uris.map(
async uri => await model.getRepositoryFromUri(uri)
);
let repositories = await Promise.all(promiseArray);
repositories = repositories.filter(repository => repository);

if (repositories.length === 0) {
window.showErrorMessage(
`Unable to add file to changelist. File is not under version control`
"Files are not under version control and cannot be added to a change list"
);
return;
}

const uris = selection.map(resource => resource.resourceUri);
const uniqueRepositories = Array.from(new Set(repositories));

await this.runByRepository(uris, async (repository, resources) => {
if (!repository) {
return;
}
if (uniqueRepositories.length !== 1) {
window.showErrorMessage(
"Unable to add files from different repositories to change list"
);
return;
}

let canRemove = false;

repository.changelists.forEach((group, changelist) => {
if (
group.resourceStates.some(state => {
return resources.some(resource => {
return resource.path === state.resourceUri.path;
});
})
) {
canRemove = true;
return false;
}
});

const changelistName = await inputSwitchChangelist(repository, canRemove);

if (!changelistName && changelistName !== false) {
return;
}
if (repositories.length !== uris.length) {
window.showErrorMessage(
"Some Files are not under version control and cannot be added to a change list"
);
return;
}

const repository = repositories[0];

const paths = resources.map(resource => resource.fsPath);

if (changelistName === false) {
try {
await repository.removeChangelist(paths);
} catch (error) {
console.log(error);
window.showErrorMessage(
`Unable to remove file "${paths.join(",")}" from changelist`
);
}
} else {
try {
await repository.addChangelist(paths, changelistName);
} catch (error) {
console.log(error);
window.showErrorMessage(
`Unable to add file "${paths.join(
","
)}" to changelist "${changelistName}"`
);
}
if (!repository) {
return;
}

const paths = uris.map(uri => uri.fsPath);
let canRemove = false;

repository.changelists.forEach((group, changelist) => {
if (
group.resourceStates.some(state => {
return paths.some(path => {
return (
normalizePath(path) === normalizePath(state.resourceUri.path)
);
});
})
) {
canRemove = true;
return false;
}
});

const changelistName = await inputSwitchChangelist(repository, canRemove);

if (!changelistName && changelistName !== false) {
return;
}

if (changelistName === false) {
try {
await repository.removeChangelist(paths);
} catch (error) {
console.log(error);
window.showErrorMessage(
`Unable to remove file "${paths.join(",")}" from changelist`
);
}
} else {
try {
await repository.addChangelist(paths, changelistName);
window.showInformationMessage(
`Added files "${paths.join(",")}" to changelist "${changelistName}"`
);
} catch (error) {
console.log(error);
window.showErrorMessage(
`Unable to add file "${paths.join(
","
)}" to changelist "${changelistName}"`
);
}
}
}
}
3 changes: 2 additions & 1 deletion src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ export enum Operation {
Status = "Status",
StatusRemote = "StatusRemote",
SwitchBranch = "SwitchBranch",
Update = "Update"
Update = "Update",
Info = "Info"
}

export interface ISvnResourceGroup extends SourceControlResourceGroup {
Expand Down
15 changes: 15 additions & 0 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,21 @@ export class Model implements IDisposable {
return undefined;
}

public async getRepositoryFromUri(uri: Uri) {
for (const liveRepository of this.openRepositories) {
const repository = liveRepository.repository;

try {
const path = normalizePath(uri.fsPath);
const info = await repository.info(path);

return repository;
} catch (error) {
console.error();
}
}
}

private open(repository: Repository): void {
const onDidDisappearRepository = filterEvent(
repository.onDidChangeState,
Expand Down
25 changes: 14 additions & 11 deletions src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,8 @@ export class Repository {

if (
(status.status === Status.NORMAL || status.status === Status.NONE) &&
(status.props === Status.NORMAL || status.props === Status.NONE)
(status.props === Status.NORMAL || status.props === Status.NONE) &&
!status.changelist
) {
// Ignore non changed itens
continue;
Expand All @@ -572,17 +573,15 @@ export class Repository {
} else {
unversioned.push(resource);
}
} else {
if (!status.changelist) {
changes.push(resource);
} else {
let changelist = changelists.get(status.changelist);
if (!changelist) {
changelist = [];
}
changelist.push(resource);
changelists.set(status.changelist, changelist);
} else if (status.changelist) {
let changelist = changelists.get(status.changelist);
if (!changelist) {
changelist = [];
}
changelist.push(resource);
changelists.set(status.changelist, changelist);
} else {
changes.push(resource);
}
}

Expand Down Expand Up @@ -820,6 +819,10 @@ export class Repository {
return this.run(Operation.Revert, () => this.repository.revert(files));
}

public async info(path: string) {
return this.run(Operation.Info, () => this.repository.getInfo(path));
}

public async patch(files: string[]) {
return this.run(Operation.Patch, () => this.repository.patch(files));
}
Expand Down

0 comments on commit 7c0886c

Please sign in to comment.