|
1 |
| -import { SourceControlResourceState, window } from "vscode"; |
| 1 | +import { commands, SourceControlResourceState, Uri, window } from "vscode"; |
2 | 2 | import { inputSwitchChangelist } from "../changelistItems";
|
| 3 | +import { Model } from "../model"; |
| 4 | +import { Resource } from "../resource"; |
| 5 | +import { normalizePath } from "../util"; |
3 | 6 | import { Command } from "./command";
|
4 | 7 |
|
5 | 8 | export class ChangeList extends Command {
|
6 | 9 | constructor() {
|
7 | 10 | super("svn.changelist");
|
8 | 11 | }
|
9 | 12 |
|
10 |
| - public async execute(...resourceStates: SourceControlResourceState[]) { |
11 |
| - const selection = await this.getResourceStates(resourceStates); |
| 13 | + public async execute(...args: any[]) { |
| 14 | + let uris: Uri[]; |
12 | 15 |
|
13 |
| - if (selection.length === 0) { |
| 16 | + if (args[0] instanceof Resource) { |
| 17 | + uris = (args as Resource[]).map(resource => resource.resourceUri); |
| 18 | + } else if (args[0] instanceof Uri) { |
| 19 | + uris = args[1] as Uri[]; |
| 20 | + } else { |
| 21 | + console.error("Unhandled type for changelist command"); |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + const model = (await commands.executeCommand("svn.getModel", "")) as Model; |
| 26 | + |
| 27 | + const promiseArray = uris.map( |
| 28 | + async uri => await model.getRepositoryFromUri(uri) |
| 29 | + ); |
| 30 | + let repositories = await Promise.all(promiseArray); |
| 31 | + repositories = repositories.filter(repository => repository); |
| 32 | + |
| 33 | + if (repositories.length === 0) { |
14 | 34 | window.showErrorMessage(
|
15 |
| - `Unable to add file to changelist. File is not under version control` |
| 35 | + "Files are not under version control and cannot be added to a change list" |
16 | 36 | );
|
17 | 37 | return;
|
18 | 38 | }
|
19 | 39 |
|
20 |
| - const uris = selection.map(resource => resource.resourceUri); |
| 40 | + const uniqueRepositories = Array.from(new Set(repositories)); |
21 | 41 |
|
22 |
| - await this.runByRepository(uris, async (repository, resources) => { |
23 |
| - if (!repository) { |
24 |
| - return; |
25 |
| - } |
| 42 | + if (uniqueRepositories.length !== 1) { |
| 43 | + window.showErrorMessage( |
| 44 | + "Unable to add files from different repositories to change list" |
| 45 | + ); |
| 46 | + return; |
| 47 | + } |
26 | 48 |
|
27 |
| - let canRemove = false; |
28 |
| - |
29 |
| - repository.changelists.forEach((group, changelist) => { |
30 |
| - if ( |
31 |
| - group.resourceStates.some(state => { |
32 |
| - return resources.some(resource => { |
33 |
| - return resource.path === state.resourceUri.path; |
34 |
| - }); |
35 |
| - }) |
36 |
| - ) { |
37 |
| - canRemove = true; |
38 |
| - return false; |
39 |
| - } |
40 |
| - }); |
41 |
| - |
42 |
| - const changelistName = await inputSwitchChangelist(repository, canRemove); |
43 |
| - |
44 |
| - if (!changelistName && changelistName !== false) { |
45 |
| - return; |
46 |
| - } |
| 49 | + if (repositories.length !== uris.length) { |
| 50 | + window.showErrorMessage( |
| 51 | + "Some Files are not under version control and cannot be added to a change list" |
| 52 | + ); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + const repository = repositories[0]; |
47 | 57 |
|
48 |
| - const paths = resources.map(resource => resource.fsPath); |
49 |
| - |
50 |
| - if (changelistName === false) { |
51 |
| - try { |
52 |
| - await repository.removeChangelist(paths); |
53 |
| - } catch (error) { |
54 |
| - console.log(error); |
55 |
| - window.showErrorMessage( |
56 |
| - `Unable to remove file "${paths.join(",")}" from changelist` |
57 |
| - ); |
58 |
| - } |
59 |
| - } else { |
60 |
| - try { |
61 |
| - await repository.addChangelist(paths, changelistName); |
62 |
| - } catch (error) { |
63 |
| - console.log(error); |
64 |
| - window.showErrorMessage( |
65 |
| - `Unable to add file "${paths.join( |
66 |
| - "," |
67 |
| - )}" to changelist "${changelistName}"` |
68 |
| - ); |
69 |
| - } |
| 58 | + if (!repository) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + const paths = uris.map(uri => uri.fsPath); |
| 63 | + let canRemove = false; |
| 64 | + |
| 65 | + repository.changelists.forEach((group, changelist) => { |
| 66 | + if ( |
| 67 | + group.resourceStates.some(state => { |
| 68 | + return paths.some(path => { |
| 69 | + return ( |
| 70 | + normalizePath(path) === normalizePath(state.resourceUri.path) |
| 71 | + ); |
| 72 | + }); |
| 73 | + }) |
| 74 | + ) { |
| 75 | + canRemove = true; |
| 76 | + return false; |
70 | 77 | }
|
71 | 78 | });
|
| 79 | + |
| 80 | + const changelistName = await inputSwitchChangelist(repository, canRemove); |
| 81 | + |
| 82 | + if (!changelistName && changelistName !== false) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + if (changelistName === false) { |
| 87 | + try { |
| 88 | + await repository.removeChangelist(paths); |
| 89 | + } catch (error) { |
| 90 | + console.log(error); |
| 91 | + window.showErrorMessage( |
| 92 | + `Unable to remove file "${paths.join(",")}" from changelist` |
| 93 | + ); |
| 94 | + } |
| 95 | + } else { |
| 96 | + try { |
| 97 | + await repository.addChangelist(paths, changelistName); |
| 98 | + window.showInformationMessage( |
| 99 | + `Added files "${paths.join(",")}" to changelist "${changelistName}"` |
| 100 | + ); |
| 101 | + } catch (error) { |
| 102 | + console.log(error); |
| 103 | + window.showErrorMessage( |
| 104 | + `Unable to add file "${paths.join( |
| 105 | + "," |
| 106 | + )}" to changelist "${changelistName}"` |
| 107 | + ); |
| 108 | + } |
| 109 | + } |
72 | 110 | }
|
73 | 111 | }
|
0 commit comments