Skip to content

Commit

Permalink
fix: Created matchAll function wrapper for minimatch so dir globs are…
Browse files Browse the repository at this point in the history
… properly ignored (#432)
  • Loading branch information
JohnstonCode committed Dec 20, 2018
1 parent 964fa6e commit dda6f13
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 15 deletions.
8 changes: 3 additions & 5 deletions src/model.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import * as fs from "fs";
import { Minimatch } from "minimatch";
import * as path from "path";
import {
commands,
Disposable,
Event,
EventEmitter,
SourceControlResourceGroup,
Uri,
window,
workspace,
Expand All @@ -19,7 +17,7 @@ import {
RepositoryState,
Status
} from "./common/types";
import { debounce, sequentialize } from "./decorators";
import { debounce } from "./decorators";
import { configuration } from "./helpers/configuration";
import { RemoteRepository } from "./remoteRepository";
import { Repository } from "./repository";
Expand All @@ -33,6 +31,7 @@ import {
isDescendant,
normalizePath
} from "./util";
import { matchAll } from "./util/globMatch";

export class Model implements IDisposable {
private _onDidOpenRepository = new EventEmitter<Repository>();
Expand Down Expand Up @@ -319,15 +318,14 @@ export class Model implements IDisposable {
return;
}

const mm = new Minimatch("*");
const newLevel = level + 1;
if (newLevel <= this.maxDepth) {
for (const file of fs.readdirSync(path)) {
const dir = path + "/" + file;

if (
fs.statSync(dir).isDirectory() &&
!mm.matchOne([dir], this.ignoreList, false)
!matchAll(dir, this.ignoreList, { dot: true })
) {
await this.tryOpenRepository(dir, newLevel);
}
Expand Down
9 changes: 3 additions & 6 deletions src/repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Minimatch } from "minimatch";
import * as path from "path";
import { clearInterval, setInterval } from "timers";
import {
Expand Down Expand Up @@ -46,6 +45,7 @@ import {
timeout,
toDisposable
} from "./util";
import { match, matchAll } from "./util/globMatch";

function shouldShowProgress(operation: Operation): boolean {
switch (operation) {
Expand Down Expand Up @@ -351,9 +351,7 @@ export class Repository implements IRemoteRepository {
"delete.ignoredRulesForDeletedFiles",
[]
);
const rules = ignoredRulesForDeletedFiles.map(
ignored => new Minimatch(ignored)
);
const rules = ignoredRulesForDeletedFiles.map(ignored => match(ignored));

if (rules.length) {
uris = uris.filter(uri => {
Expand Down Expand Up @@ -529,8 +527,7 @@ export class Repository implements IRemoteRepository {
continue;
}

const mm = new Minimatch("*");
if (mm.matchOne([status.path], excludeList, false)) {
if (matchAll(status.path, excludeList, { dot: true })) {
continue;
}

Expand Down
9 changes: 5 additions & 4 deletions src/test/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ suite("Extension Tests", () => {
// tslint:disable-next-line: only-arrow-functions
test("should be able to activate the extension", function(done) {
this.timeout(60 * 1000);
const extension = vscode.extensions.getExtension("johnstoncode.svn-scm");
const extension = vscode.extensions.getExtension(
"johnstoncode.svn-scm"
) as vscode.Extension<any>;

if (!extension) {
done("Extension not found");
return;
assert.fail("Extension not found");
}

if (!extension.isActive) {
Expand All @@ -48,7 +49,7 @@ suite("Extension Tests", () => {
done();
},
() => {
done("Failed to activate extension");
assert.fail("Failed to activate extension");
}
);
} else {
Expand Down
23 changes: 23 additions & 0 deletions src/test/globMatch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as assert from "assert";
import { matchAll, match } from "../util/globMatch";

suite("Glob match testing", () => {
test("Test single match", () => {
const matcher = match("**/*.js");

assert.ok(matcher.match("test/tester.js"));
assert.ok(matcher.match("tester/testing/test/tester.js"));
});

test("Test Multiple Matches", () => {
const patterns = ["**/.git", "**/.hg", "**/vendor", "**/node_modules"];

assert.strictEqual(matchAll(".git/test", patterns), false);
assert.strictEqual(matchAll(".hg/test", patterns), false);
assert.strictEqual(matchAll("vendor/test", patterns), false);
assert.strictEqual(
matchAll("project/test/node_modules/test/index.js", patterns),
false
);
});
});
27 changes: 27 additions & 0 deletions src/util/globMatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as minimatch from "minimatch";

export function matchAll(
path: string,
patterns: string[],
opts: minimatch.IOptions = {}
): boolean {
let match = false;

patterns.forEach(pattern => {
const isExclusion = pattern[0] === "!";

// If we've got a match, only re-test for exclusions.
// if we don't have a match, only re-test for inclusions.
if (match !== isExclusion) {
return;
}

match = minimatch(path, pattern, opts);
});

return match;
}

export function match(pattern: string) {
return new minimatch.Minimatch(pattern);
}

0 comments on commit dda6f13

Please sign in to comment.