Skip to content

Commit 6ded7cd

Browse files
authored
Merge pull request #238 from edgardmessias/ignore_extension
Added support to ignoring files by ext Close #234)
2 parents 4cd4f7b + 60f8fe6 commit 6ded7cd

File tree

5 files changed

+192
-33
lines changed

5 files changed

+192
-33
lines changed

package.json

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,13 @@
212212
"category": "SVN"
213213
},
214214
{
215-
"command": "svn.addFileToIgnore",
216-
"title": "Ignore",
215+
"command": "svn.addToIgnoreSCM",
216+
"title": "Ignore file/ext from SVN (svn:ignore)",
217+
"category": "SVN"
218+
},
219+
{
220+
"command": "svn.addToIgnoreExplorer",
221+
"title": "Ignore file/ext from SVN (svn:ignore)",
217222
"category": "SVN"
218223
}
219224
],
@@ -311,7 +316,7 @@
311316
"when": "config.svn.enabled && svnOpenRepositoryCount != 0"
312317
},
313318
{
314-
"command": "svn.addFileToIgnore",
319+
"command": "svn.addToIgnoreExplorer",
315320
"when": "false"
316321
}
317322
],
@@ -449,7 +454,7 @@
449454
"group": "2_modification"
450455
},
451456
{
452-
"command": "svn.addFileToIgnore",
457+
"command": "svn.addToIgnoreSCM",
453458
"when":
454459
"config.svn.enabled && scmProvider == svn && scmResourceGroup == unversioned",
455460
"group": "1_modification"
@@ -486,6 +491,13 @@
486491
"when":
487492
"config.svn.enabled && svnOpenRepositoryCount != 0 && isInDiffEditor && resourceScheme != merge-conflicts.conflicts-diff && && svnHasSupportToRegisterDiffCommand == 1"
488493
}
494+
],
495+
"explorer/context": [
496+
{
497+
"command": "svn.addToIgnoreExplorer",
498+
"group": "9_svn",
499+
"when": "config.svn.enabled && svnOpenRepositoryCount != 0"
500+
}
489501
]
490502
},
491503
"configuration": {

src/commands.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
} from "./changelistItems";
3737
import { configuration } from "./helpers/configuration";
3838
import { selectBranch } from "./branches";
39+
import { inputIgnoreList } from "./ignoreitems";
3940

4041
interface CommandOptions {
4142
repository?: boolean;
@@ -1055,7 +1056,7 @@ export class SvnCommands implements IDisposable {
10551056
await repository.finishCheckout();
10561057
}
10571058

1058-
@command("svn.addFileToIgnore")
1059+
@command("svn.addToIgnoreSCM")
10591060
async addFileToIgnore(
10601061
...resourceStates: SourceControlResourceState[]
10611062
): Promise<void> {
@@ -1067,22 +1068,31 @@ export class SvnCommands implements IDisposable {
10671068

10681069
const uris = selection.map(resource => resource.resourceUri);
10691070

1071+
return await this.addToIgnore(uris);
1072+
}
1073+
1074+
@command("svn.addToIgnoreExplorer")
1075+
async addToIgnoreExplorer(mainUri?: Uri, allUris?: Uri[]): Promise<void> {
1076+
if (!allUris || allUris.length === 0) {
1077+
return;
1078+
}
1079+
1080+
return await this.addToIgnore(allUris);
1081+
}
1082+
1083+
async addToIgnore(uris: Uri[]): Promise<void> {
10701084
await this.runByRepository(uris, async (repository, resources) => {
10711085
if (!repository) {
10721086
return;
10731087
}
10741088

1075-
for (const resource of resources) {
1076-
try {
1077-
await repository.addFileToIgnore(resource.fsPath);
1078-
1079-
const assetName = path.basename(resource.fsPath);
1089+
try {
1090+
await inputIgnoreList(repository, resources);
10801091

1081-
window.showInformationMessage(`${assetName} is now being ignored`);
1082-
} catch (error) {
1083-
console.log(error);
1084-
window.showErrorMessage("Unable to set property ignore");
1085-
}
1092+
window.showInformationMessage(`File(s) is now being ignored`);
1093+
} catch (error) {
1094+
console.log(error);
1095+
window.showErrorMessage("Unable to set property ignore");
10861096
}
10871097
});
10881098
}

src/ignoreitems.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import {
2+
QuickPickItem,
3+
SourceControlResourceGroup,
4+
window,
5+
workspace,
6+
Uri
7+
} from "vscode";
8+
import { Repository } from "./repository";
9+
import { configuration } from "./helpers/configuration";
10+
import * as path from "path";
11+
import { Status } from "./svn";
12+
13+
export class IgnoreSingleItem implements QuickPickItem {
14+
constructor(public expression: string, public recursive: boolean = false) {}
15+
16+
get label(): string {
17+
const r_text = this.recursive ? " (Recursive)" : "";
18+
return `${this.expression}${r_text}`;
19+
}
20+
21+
get description(): string {
22+
const r_text = this.recursive ? " (Recursive)" : "";
23+
return `Add '${this.expression}' to 'svn:ignore'${r_text}`;
24+
}
25+
}
26+
27+
export async function inputIgnoreList(repository: Repository, uris: Uri[]) {
28+
if (uris.length === 0) {
29+
return;
30+
}
31+
32+
const regexExtension = new RegExp("\\.[^\\.]+(\\.map)?$", "i");
33+
34+
if (uris.length === 1) {
35+
const uri = uris[0];
36+
const matchExt = uri.fsPath.match(regexExtension);
37+
const ext = matchExt && matchExt[0] ? matchExt[0] : "";
38+
const fileName = path.basename(uri.fsPath);
39+
const dirName = path.dirname(uri.fsPath);
40+
41+
const picks: IgnoreSingleItem[] = [];
42+
picks.push(new IgnoreSingleItem(fileName));
43+
if (ext) {
44+
picks.push(new IgnoreSingleItem("*" + ext));
45+
}
46+
picks.push(new IgnoreSingleItem(fileName, true));
47+
if (ext) {
48+
picks.push(new IgnoreSingleItem("*" + ext, true));
49+
}
50+
51+
const pick = await window.showQuickPick(picks);
52+
53+
if (!pick) {
54+
return;
55+
}
56+
57+
return await repository.addToIgnore(
58+
[pick.expression],
59+
dirName,
60+
pick.recursive
61+
);
62+
}
63+
64+
const count = uris.length;
65+
const recursive = "(Recursive)";
66+
67+
const ignoreByFileName = `Ignore ${count} by filename`;
68+
const ignoreByExtension = `Ignore ${count} by extension`;
69+
const ignoreByFileNameRecursive = `Ignore ${count} by filename ${recursive}`;
70+
const ignoreByExtensionRecursive = `Ignore ${count} by extension ${recursive}`;
71+
72+
const picks: string[] = [
73+
ignoreByFileName,
74+
ignoreByExtension,
75+
ignoreByFileNameRecursive,
76+
ignoreByExtensionRecursive
77+
];
78+
79+
const pick = await window.showQuickPick(picks);
80+
81+
if (!pick) {
82+
return;
83+
}
84+
85+
const isByFile = pick.startsWith(ignoreByFileName);
86+
const isRecursive = pick.endsWith(recursive);
87+
88+
const byDir: { [key: string]: string[] } = {};
89+
90+
for (const uri of uris) {
91+
const dirname = path.dirname(uri.fsPath);
92+
const filename = path.basename(uri.fsPath);
93+
const matchExt = uri.fsPath.match(regexExtension);
94+
const ext = matchExt && matchExt[0] ? matchExt[0] : "";
95+
96+
if (typeof byDir[dirname] === "undefined") {
97+
byDir[dirname] = [];
98+
}
99+
100+
if (isByFile) {
101+
byDir[dirname].push(filename);
102+
} else if (ext) {
103+
byDir[dirname].push("*" + ext);
104+
}
105+
}
106+
107+
for (const dir in byDir) {
108+
const files = [...new Set(byDir[dir])]; // Unique list
109+
await repository.addToIgnore(files, dir, isRecursive);
110+
}
111+
}

src/repository.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,9 +696,13 @@ export class Repository {
696696
);
697697
}
698698

699-
async addFileToIgnore(filePath: string) {
699+
async addToIgnore(
700+
expressions: string[],
701+
directory: string,
702+
recursive: boolean = false
703+
) {
700704
return await this.run(Operation.Ignore, () =>
701-
this.repository.addFileToIgnore(filePath)
705+
this.repository.addToIgnore(expressions, directory, recursive)
702706
);
703707
}
704708

src/svnRepository.ts

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -405,33 +405,55 @@ export class Repository {
405405
return parseSvnList(result.stdout);
406406
}
407407

408-
async addFileToIgnore(filePath: string) {
409-
const fileName = path.basename(filePath);
410-
const parentDir = path.dirname(filePath);
408+
async getCurrentIgnore(directory: string) {
409+
directory = this.removeAbsolutePath(directory);
410+
411411
let currentIgnore = "";
412412

413413
try {
414-
const currentIgnoreResult = await this.exec([
415-
"propget",
416-
"svn:ignore",
417-
parentDir
418-
]);
414+
const args = ["propget", "svn:ignore"];
415+
416+
if (directory) {
417+
args.push(directory);
418+
}
419+
420+
const currentIgnoreResult = await this.exec(args);
419421

420422
currentIgnore = currentIgnoreResult.stdout.trim();
421423
} catch (error) {}
422424

423425
const ignores = currentIgnore.split(/[\r\n]+/);
424426

425-
ignores.push(fileName);
427+
return ignores;
428+
}
426429

427-
const newIgnore = ignores.sort().join("\n");
430+
async addToIgnore(
431+
expressions: string[],
432+
directory: string,
433+
recursive: boolean = false
434+
) {
435+
const ignores = await this.getCurrentIgnore(directory);
428436

429-
const result = await this.exec([
430-
"propset",
431-
"svn:ignore",
432-
newIgnore,
433-
parentDir
434-
]);
437+
directory = this.removeAbsolutePath(directory);
438+
439+
ignores.push(...expressions);
440+
const newIgnore = [...new Set(ignores)]
441+
.filter(v => !!v)
442+
.sort()
443+
.join("\n");
444+
445+
const args = ["propset", "svn:ignore", newIgnore];
446+
447+
if (directory) {
448+
args.push(directory);
449+
} else {
450+
args.push(".");
451+
}
452+
if (recursive) {
453+
args.push("--recursive");
454+
}
455+
456+
const result = await this.exec(args);
435457

436458
return result.stdout;
437459
}

0 commit comments

Comments
 (0)