dir-analysis-tool 3.0.1 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -41
- package/dist/cli.js +688 -14
- package/dist/index.d.ts +0 -1
- package/dist/index.js +1 -1
- package/dist/{shared-BhASrUSI.js → shared-htkguqJG.js} +460 -154
- package/package.json +6 -11
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
|
-
import { createHash } from "node:crypto";
|
|
3
|
-
import {
|
|
2
|
+
import { createHash, hash } from "node:crypto";
|
|
3
|
+
import { existsSync, readFile } from "node:fs";
|
|
4
|
+
import { lstat, open, readFile as readFile$1, readdir, stat } from "node:fs/promises";
|
|
4
5
|
import { dirname, join, relative, resolve, sep } from "node:path";
|
|
5
|
-
import { existsSync } from "node:fs";
|
|
6
|
-
import ignore from "ignore";
|
|
7
6
|
//#region src/errors.ts
|
|
8
7
|
/**
|
|
9
8
|
* An expected, user-facing failure (bad input, missing directory, invalid
|
|
@@ -723,79 +722,13 @@ function emptyTypeStats() {
|
|
|
723
722
|
};
|
|
724
723
|
}
|
|
725
724
|
//#endregion
|
|
726
|
-
//#region src/core/limiter.ts
|
|
727
|
-
/**
|
|
728
|
-
* A tiny concurrency limiter for filesystem work.
|
|
729
|
-
*
|
|
730
|
-
* Tasks run at most `concurrency` at a time. Pending tasks are taken
|
|
731
|
-
* last-in-first-out, which makes a directory walk proceed depth-first and
|
|
732
|
-
* keeps the pending queue small even on very wide trees.
|
|
733
|
-
*/
|
|
734
|
-
var Limiter = class {
|
|
735
|
-
#concurrency;
|
|
736
|
-
#queue = [];
|
|
737
|
-
#active = 0;
|
|
738
|
-
#failure = void 0;
|
|
739
|
-
#failed = false;
|
|
740
|
-
#idle = null;
|
|
741
|
-
constructor(concurrency) {
|
|
742
|
-
if (!Number.isInteger(concurrency) || concurrency < 1) throw new RangeError(`concurrency must be a positive integer (got ${concurrency})`);
|
|
743
|
-
this.#concurrency = concurrency;
|
|
744
|
-
}
|
|
745
|
-
/** Schedules a task. Tasks are expected to handle their own errors; a thrown error aborts the run. */
|
|
746
|
-
run(task) {
|
|
747
|
-
if (this.#failed) return;
|
|
748
|
-
this.#queue.push(task);
|
|
749
|
-
this.#pump();
|
|
750
|
-
}
|
|
751
|
-
/** Resolves once every scheduled task, including tasks scheduled by tasks, has finished. */
|
|
752
|
-
idle() {
|
|
753
|
-
if (this.#failed) return Promise.reject(this.#failure);
|
|
754
|
-
if (this.#active === 0 && this.#queue.length === 0) return Promise.resolve();
|
|
755
|
-
return new Promise((resolve, reject) => {
|
|
756
|
-
this.#idle = {
|
|
757
|
-
resolve,
|
|
758
|
-
reject
|
|
759
|
-
};
|
|
760
|
-
});
|
|
761
|
-
}
|
|
762
|
-
/** Stops scheduling new work and rejects `idle()` with `reason`. */
|
|
763
|
-
abort(reason) {
|
|
764
|
-
if (this.#failed) return;
|
|
765
|
-
this.#failed = true;
|
|
766
|
-
this.#failure = reason;
|
|
767
|
-
this.#queue.length = 0;
|
|
768
|
-
this.#idle?.reject(reason);
|
|
769
|
-
this.#idle = null;
|
|
770
|
-
}
|
|
771
|
-
#pump() {
|
|
772
|
-
while (this.#active < this.#concurrency && this.#queue.length > 0) {
|
|
773
|
-
const task = this.#queue.pop();
|
|
774
|
-
this.#active++;
|
|
775
|
-
task().then(this.#onDone, this.#onError);
|
|
776
|
-
}
|
|
777
|
-
if (this.#active === 0 && this.#queue.length === 0 && this.#idle) {
|
|
778
|
-
const idle = this.#idle;
|
|
779
|
-
this.#idle = null;
|
|
780
|
-
idle.resolve();
|
|
781
|
-
}
|
|
782
|
-
}
|
|
783
|
-
#onDone = () => {
|
|
784
|
-
this.#active--;
|
|
785
|
-
this.#pump();
|
|
786
|
-
};
|
|
787
|
-
#onError = (error) => {
|
|
788
|
-
this.#active--;
|
|
789
|
-
this.abort(error);
|
|
790
|
-
};
|
|
791
|
-
};
|
|
792
|
-
//#endregion
|
|
793
725
|
//#region src/core/duplicates.ts
|
|
794
|
-
/** Files up to this size are hashed in full straight away. */
|
|
726
|
+
/** Files up to this size are read in one call and hashed in full straight away. */
|
|
795
727
|
const SAMPLE_THRESHOLD = 262144;
|
|
796
728
|
/** Bytes read from each end of a larger file for its sample hash. */
|
|
797
729
|
const SAMPLE_BYTES = 65536;
|
|
798
|
-
const READ_BUFFER_BYTES =
|
|
730
|
+
const READ_BUFFER_BYTES = 524288;
|
|
731
|
+
const ALGORITHM = "sha256";
|
|
799
732
|
/**
|
|
800
733
|
* Finds files with identical contents among groups of same-sized files.
|
|
801
734
|
*
|
|
@@ -803,7 +736,10 @@ const READ_BUFFER_BYTES = 1048576;
|
|
|
803
736
|
* 1. Only sizes shared by at least two distinct inodes are considered.
|
|
804
737
|
* 2. Large files are first compared by a sample hash of their first and
|
|
805
738
|
* last 64 KiB, which separates almost all non-duplicates cheaply.
|
|
806
|
-
* 3. Survivors (and small files) get a full
|
|
739
|
+
* 3. Survivors (and small files) get a full SHA-256.
|
|
740
|
+
*
|
|
741
|
+
* Groups are hashed independently and finished as soon as their last member
|
|
742
|
+
* is done, so memory holds only the groups in flight plus the results.
|
|
807
743
|
*/
|
|
808
744
|
async function findDuplicates(sizeGroups, options) {
|
|
809
745
|
const small = [];
|
|
@@ -813,104 +749,150 @@ async function findDuplicates(sizeGroups, options) {
|
|
|
813
749
|
if (distinct.length < 2) continue;
|
|
814
750
|
(distinct[0].bytes <= SAMPLE_THRESHOLD ? small : large).push(distinct);
|
|
815
751
|
}
|
|
816
|
-
const buffers = new BufferPool(
|
|
752
|
+
const buffers = new BufferPool();
|
|
817
753
|
const sampled = await hashStage(large, "sample", options, buffers);
|
|
818
|
-
|
|
754
|
+
for (const group of sampled) small.push(group.members);
|
|
755
|
+
return hashStage(small, "full", options, buffers);
|
|
819
756
|
}
|
|
820
757
|
async function hashStage(groups, stage, options, buffers) {
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
const
|
|
824
|
-
|
|
825
|
-
const { signal } = options;
|
|
826
|
-
const onAbort = () => limiter.abort(new AbortError());
|
|
827
|
-
signal?.addEventListener("abort", onAbort, { once: true });
|
|
758
|
+
let total = 0;
|
|
759
|
+
for (const group of groups) total += group.length;
|
|
760
|
+
const results = [];
|
|
761
|
+
if (total === 0) return results;
|
|
762
|
+
const { signal, onProgress } = options;
|
|
828
763
|
let done = 0;
|
|
829
|
-
|
|
764
|
+
onProgress?.({
|
|
830
765
|
stage,
|
|
831
766
|
done,
|
|
832
767
|
total
|
|
833
768
|
});
|
|
834
|
-
|
|
835
|
-
const
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
769
|
+
const finish = (state) => {
|
|
770
|
+
for (const [digest, members] of state.byHash) if (members.length > 1) results.push({
|
|
771
|
+
hash: digest,
|
|
772
|
+
bytes: state.bytes,
|
|
773
|
+
members
|
|
774
|
+
});
|
|
775
|
+
};
|
|
776
|
+
function* tasks() {
|
|
777
|
+
for (const group of groups) {
|
|
778
|
+
const state = {
|
|
779
|
+
bytes: group[0].bytes,
|
|
780
|
+
pending: group.length,
|
|
781
|
+
byHash: /* @__PURE__ */ new Map()
|
|
782
|
+
};
|
|
783
|
+
for (const candidate of group) yield async () => {
|
|
784
|
+
try {
|
|
785
|
+
const path = options.pathOf(candidate);
|
|
786
|
+
const digest = stage === "sample" ? await sampleHash(path, candidate.bytes, buffers) : await hashFileOfSize(path, candidate.bytes, buffers);
|
|
787
|
+
const members = state.byHash.get(digest);
|
|
788
|
+
if (members) members.push(candidate);
|
|
789
|
+
else state.byHash.set(digest, [candidate]);
|
|
790
|
+
} catch (error) {
|
|
791
|
+
options.onError?.(candidate, error);
|
|
792
|
+
} finally {
|
|
793
|
+
done++;
|
|
794
|
+
if (--state.pending === 0) finish(state);
|
|
795
|
+
onProgress?.({
|
|
796
|
+
stage,
|
|
797
|
+
done,
|
|
798
|
+
total
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
};
|
|
860
802
|
}
|
|
861
|
-
}
|
|
803
|
+
}
|
|
804
|
+
await runLimited(tasks(), options.concurrency, signal);
|
|
805
|
+
return results;
|
|
806
|
+
}
|
|
807
|
+
/**
|
|
808
|
+
* Runs tasks from a lazy iterator with at most `concurrency` in flight, so a
|
|
809
|
+
* million-file stage never materializes a million closures up front.
|
|
810
|
+
*/
|
|
811
|
+
async function runLimited(tasks, concurrency, signal) {
|
|
812
|
+
if (signal?.aborted) throw new AbortError();
|
|
813
|
+
let aborted = false;
|
|
814
|
+
const onAbort = () => {
|
|
815
|
+
aborted = true;
|
|
816
|
+
};
|
|
817
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
818
|
+
const worker = async () => {
|
|
819
|
+
for (;;) {
|
|
820
|
+
if (aborted) return;
|
|
821
|
+
const next = tasks.next();
|
|
822
|
+
if (next.done) return;
|
|
823
|
+
await next.value();
|
|
824
|
+
}
|
|
825
|
+
};
|
|
862
826
|
try {
|
|
863
|
-
|
|
827
|
+
const workers = [];
|
|
828
|
+
for (let i = 0; i < concurrency; i++) workers.push(worker());
|
|
829
|
+
await Promise.all(workers);
|
|
864
830
|
} finally {
|
|
865
831
|
signal?.removeEventListener("abort", onAbort);
|
|
866
832
|
}
|
|
867
|
-
|
|
833
|
+
if (aborted) throw new AbortError();
|
|
868
834
|
}
|
|
869
835
|
/** Streams a file through SHA-256 and returns the hex digest. */
|
|
870
|
-
async function hashFile(path, buffer
|
|
871
|
-
const
|
|
836
|
+
async function hashFile(path, buffer) {
|
|
837
|
+
const hasher = createHash(ALGORITHM);
|
|
872
838
|
const handle = await open(path, "r");
|
|
873
839
|
try {
|
|
840
|
+
const chunk = buffer ?? Buffer.allocUnsafe(READ_BUFFER_BYTES);
|
|
874
841
|
let position = 0;
|
|
875
842
|
for (;;) {
|
|
876
|
-
const { bytesRead } = await handle.read(
|
|
843
|
+
const { bytesRead } = await handle.read(chunk, 0, chunk.length, position);
|
|
877
844
|
if (bytesRead === 0) break;
|
|
878
|
-
|
|
845
|
+
hasher.update(bytesRead === chunk.length ? chunk : chunk.subarray(0, bytesRead));
|
|
879
846
|
position += bytesRead;
|
|
880
847
|
}
|
|
881
848
|
} finally {
|
|
882
849
|
await handle.close();
|
|
883
850
|
}
|
|
884
|
-
return
|
|
851
|
+
return hasher.digest("hex");
|
|
852
|
+
}
|
|
853
|
+
/** Hashes a file whose size is known: one read for small files, streaming otherwise. */
|
|
854
|
+
async function hashFileOfSize(path, size, buffers) {
|
|
855
|
+
if (size <= SAMPLE_THRESHOLD) return hash(ALGORITHM, await readWhole(path), "hex");
|
|
856
|
+
const buffer = buffers?.acquire();
|
|
857
|
+
try {
|
|
858
|
+
return await hashFile(path, buffer);
|
|
859
|
+
} finally {
|
|
860
|
+
if (buffer) buffers.release(buffer);
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
function readWhole(path) {
|
|
864
|
+
return new Promise((resolve, reject) => {
|
|
865
|
+
readFile(path, (error, data) => error ? reject(error) : resolve(data));
|
|
866
|
+
});
|
|
885
867
|
}
|
|
886
|
-
async function sampleHash(path, size,
|
|
887
|
-
const
|
|
868
|
+
async function sampleHash(path, size, buffers) {
|
|
869
|
+
const hasher = createHash(ALGORITHM);
|
|
870
|
+
const buffer = buffers.acquire();
|
|
888
871
|
const handle = await open(path, "r");
|
|
889
872
|
try {
|
|
890
873
|
for (const position of [0, Math.max(0, size - SAMPLE_BYTES)]) {
|
|
891
874
|
const { bytesRead } = await handle.read(buffer, 0, SAMPLE_BYTES, position);
|
|
892
|
-
|
|
875
|
+
hasher.update(buffer.subarray(0, bytesRead));
|
|
893
876
|
}
|
|
894
877
|
} finally {
|
|
878
|
+
buffers.release(buffer);
|
|
895
879
|
await handle.close();
|
|
896
880
|
}
|
|
897
|
-
return
|
|
881
|
+
return hasher.digest("hex");
|
|
898
882
|
}
|
|
899
883
|
function distinctInodes(group) {
|
|
900
884
|
if (group.length < 2) return group;
|
|
901
885
|
const seen = /* @__PURE__ */ new Set();
|
|
902
886
|
return group.filter((candidate) => {
|
|
903
|
-
|
|
904
|
-
seen.
|
|
887
|
+
const key = `${candidate.dev}:${candidate.ino}`;
|
|
888
|
+
if (seen.has(key)) return false;
|
|
889
|
+
seen.add(key);
|
|
905
890
|
return true;
|
|
906
891
|
});
|
|
907
892
|
}
|
|
908
|
-
/** Reuses read buffers so
|
|
893
|
+
/** Reuses read buffers so streaming large files allocates a fixed amount of memory. */
|
|
909
894
|
var BufferPool = class {
|
|
910
895
|
#free = [];
|
|
911
|
-
constructor(size) {
|
|
912
|
-
for (let i = 0; i < size; i++) this.#free.push(Buffer.allocUnsafe(READ_BUFFER_BYTES));
|
|
913
|
-
}
|
|
914
896
|
acquire() {
|
|
915
897
|
return this.#free.pop() ?? Buffer.allocUnsafe(READ_BUFFER_BYTES);
|
|
916
898
|
}
|
|
@@ -919,6 +901,261 @@ var BufferPool = class {
|
|
|
919
901
|
}
|
|
920
902
|
};
|
|
921
903
|
//#endregion
|
|
904
|
+
//#region src/core/gitignore.ts
|
|
905
|
+
/**
|
|
906
|
+
* The compiled rules of one ignore file.
|
|
907
|
+
*
|
|
908
|
+
* `test()` returns `true` when the last matching rule ignores the path,
|
|
909
|
+
* `false` when it un-ignores it, and `undefined` when no rule matches, so
|
|
910
|
+
* several files can be chained with Git's precedence.
|
|
911
|
+
*/
|
|
912
|
+
var GitignoreMatcher = class GitignoreMatcher {
|
|
913
|
+
#runs;
|
|
914
|
+
#ignoreCase;
|
|
915
|
+
constructor(runs, ignoreCase) {
|
|
916
|
+
this.#runs = runs;
|
|
917
|
+
this.#ignoreCase = ignoreCase;
|
|
918
|
+
}
|
|
919
|
+
/** True when the file contains at least one rule. */
|
|
920
|
+
get hasRules() {
|
|
921
|
+
return this.#runs.length > 0;
|
|
922
|
+
}
|
|
923
|
+
/**
|
|
924
|
+
* @param path Path relative to the ignore file's directory, `/`-separated,
|
|
925
|
+
* without leading or trailing slashes.
|
|
926
|
+
*/
|
|
927
|
+
test(path, isDirectory) {
|
|
928
|
+
const runs = this.#runs;
|
|
929
|
+
let name;
|
|
930
|
+
let folded;
|
|
931
|
+
for (let i = runs.length - 1; i >= 0; i--) {
|
|
932
|
+
const run = runs[i];
|
|
933
|
+
const fast = isDirectory && run.dirFast !== null ? true : run.anyFast !== null;
|
|
934
|
+
const byName = run.anyName !== null || isDirectory && run.dirName !== null;
|
|
935
|
+
if (fast || byName) {
|
|
936
|
+
name ??= path.slice(path.lastIndexOf("/") + 1);
|
|
937
|
+
if (fast) {
|
|
938
|
+
folded ??= this.#ignoreCase ? name.toLowerCase() : name;
|
|
939
|
+
if (run.anyFast?.has(folded) || isDirectory && run.dirFast?.has(folded)) return !run.negated;
|
|
940
|
+
}
|
|
941
|
+
if (run.anyName?.test(name) || isDirectory && run.dirName?.test(name)) return !run.negated;
|
|
942
|
+
}
|
|
943
|
+
if (run.anyPath?.test(path) || isDirectory && run.dirPath?.test(path)) return !run.negated;
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
/** Compiles the contents of an ignore file (or a list of patterns). */
|
|
947
|
+
static compile(source, options = {}) {
|
|
948
|
+
const lines = typeof source === "string" ? source.split(/\r?\n/) : source;
|
|
949
|
+
const ignoreCase = options.ignoreCase === true;
|
|
950
|
+
const flags = ignoreCase ? "i" : "";
|
|
951
|
+
const runs = [];
|
|
952
|
+
let negated = false;
|
|
953
|
+
let parts = new RunParts();
|
|
954
|
+
const regex = (sources) => sources.length > 0 ? new RegExp(`^(?:${sources.join("|")})$`, flags) : null;
|
|
955
|
+
const flush = () => {
|
|
956
|
+
const run = {
|
|
957
|
+
negated,
|
|
958
|
+
anyFast: parts.anyFast.isEmpty ? null : parts.anyFast,
|
|
959
|
+
dirFast: parts.dirFast.isEmpty ? null : parts.dirFast,
|
|
960
|
+
anyName: regex(parts.anyName),
|
|
961
|
+
dirName: regex(parts.dirName),
|
|
962
|
+
anyPath: regex(parts.anyPath),
|
|
963
|
+
dirPath: regex(parts.dirPath)
|
|
964
|
+
};
|
|
965
|
+
if (run.anyFast || run.dirFast || run.anyName || run.dirName || run.anyPath || run.dirPath) runs.push(run);
|
|
966
|
+
parts = new RunParts();
|
|
967
|
+
};
|
|
968
|
+
for (const line of lines) {
|
|
969
|
+
const rule = parseRule(line);
|
|
970
|
+
if (rule === null) continue;
|
|
971
|
+
if (rule.negated !== negated) {
|
|
972
|
+
flush();
|
|
973
|
+
negated = rule.negated;
|
|
974
|
+
}
|
|
975
|
+
if (rule.fast) {
|
|
976
|
+
const target = rule.dirOnly ? parts.dirFast : parts.anyFast;
|
|
977
|
+
const value = ignoreCase ? rule.fast.value.toLowerCase() : rule.fast.value;
|
|
978
|
+
if (rule.fast.kind === "literal") target.literals.add(value);
|
|
979
|
+
else target.addSuffix(value);
|
|
980
|
+
} else if (rule.nameOnly) (rule.dirOnly ? parts.dirName : parts.anyName).push(rule.source);
|
|
981
|
+
else (rule.dirOnly ? parts.dirPath : parts.anyPath).push(rule.source);
|
|
982
|
+
}
|
|
983
|
+
flush();
|
|
984
|
+
return new GitignoreMatcher(runs, ignoreCase);
|
|
985
|
+
}
|
|
986
|
+
};
|
|
987
|
+
/** Exact names and `*suffix` rules of one run, matched with hash lookups. */
|
|
988
|
+
var FastNames = class {
|
|
989
|
+
literals = /* @__PURE__ */ new Set();
|
|
990
|
+
/** Suffixes keyed by their last character, so a name checks only a few. */
|
|
991
|
+
#suffixes = /* @__PURE__ */ new Map();
|
|
992
|
+
get isEmpty() {
|
|
993
|
+
return this.literals.size === 0 && this.#suffixes.size === 0;
|
|
994
|
+
}
|
|
995
|
+
addSuffix(suffix) {
|
|
996
|
+
const key = suffix[suffix.length - 1];
|
|
997
|
+
const list = this.#suffixes.get(key);
|
|
998
|
+
if (list) list.push(suffix);
|
|
999
|
+
else this.#suffixes.set(key, [suffix]);
|
|
1000
|
+
}
|
|
1001
|
+
has(name) {
|
|
1002
|
+
if (this.literals.has(name)) return true;
|
|
1003
|
+
if (name === "") return false;
|
|
1004
|
+
const list = this.#suffixes.get(name[name.length - 1]);
|
|
1005
|
+
if (list === void 0) return false;
|
|
1006
|
+
for (const suffix of list) if (name.endsWith(suffix)) return true;
|
|
1007
|
+
return false;
|
|
1008
|
+
}
|
|
1009
|
+
};
|
|
1010
|
+
var RunParts = class {
|
|
1011
|
+
anyFast = new FastNames();
|
|
1012
|
+
dirFast = new FastNames();
|
|
1013
|
+
anyName = [];
|
|
1014
|
+
dirName = [];
|
|
1015
|
+
anyPath = [];
|
|
1016
|
+
dirPath = [];
|
|
1017
|
+
};
|
|
1018
|
+
/** No wildcards, classes, or escapes: the pattern is a literal name (or suffix). */
|
|
1019
|
+
const LITERAL = /^[^*?[\\]+$/;
|
|
1020
|
+
/** Turns one gitignore line into a regex fragment; `null` for blanks and comments. */
|
|
1021
|
+
function parseRule(line) {
|
|
1022
|
+
let pattern = line.replace(/(?<!\\)\s+$/, "");
|
|
1023
|
+
if (pattern === "" || pattern.startsWith("#")) return null;
|
|
1024
|
+
let negated = false;
|
|
1025
|
+
if (pattern.startsWith("!")) {
|
|
1026
|
+
negated = true;
|
|
1027
|
+
pattern = pattern.slice(1);
|
|
1028
|
+
} else if (pattern.startsWith("\\!") || pattern.startsWith("\\#")) pattern = pattern.slice(1);
|
|
1029
|
+
if (pattern === "") return null;
|
|
1030
|
+
let dirOnly = false;
|
|
1031
|
+
if (pattern.endsWith("/") && !pattern.endsWith("\\/")) {
|
|
1032
|
+
dirOnly = true;
|
|
1033
|
+
pattern = pattern.replace(/\/+$/, "");
|
|
1034
|
+
if (pattern === "") return null;
|
|
1035
|
+
}
|
|
1036
|
+
let anchored = false;
|
|
1037
|
+
if (pattern.startsWith("/")) {
|
|
1038
|
+
anchored = true;
|
|
1039
|
+
pattern = pattern.replace(/^\/+/, "");
|
|
1040
|
+
} else if (pattern.includes("/")) anchored = true;
|
|
1041
|
+
if (pattern.startsWith("**/")) {
|
|
1042
|
+
anchored = false;
|
|
1043
|
+
pattern = pattern.replace(/^(\*\*\/)+/, "");
|
|
1044
|
+
}
|
|
1045
|
+
if (pattern === "") return null;
|
|
1046
|
+
const body = globToRegex(pattern);
|
|
1047
|
+
if (body === null) return null;
|
|
1048
|
+
const nameOnly = !anchored && !pattern.includes("/") && !pattern.includes("**");
|
|
1049
|
+
const source = anchored || nameOnly ? body : `(?:.*/)?${body}`;
|
|
1050
|
+
let fast = null;
|
|
1051
|
+
if (nameOnly) {
|
|
1052
|
+
if (LITERAL.test(pattern)) fast = {
|
|
1053
|
+
kind: "literal",
|
|
1054
|
+
value: pattern
|
|
1055
|
+
};
|
|
1056
|
+
else if (pattern.startsWith("*") && LITERAL.test(pattern.slice(1))) fast = {
|
|
1057
|
+
kind: "suffix",
|
|
1058
|
+
value: pattern.slice(1)
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1061
|
+
return {
|
|
1062
|
+
negated,
|
|
1063
|
+
dirOnly,
|
|
1064
|
+
nameOnly,
|
|
1065
|
+
fast,
|
|
1066
|
+
source
|
|
1067
|
+
};
|
|
1068
|
+
}
|
|
1069
|
+
/**
|
|
1070
|
+
* Translates a gitignore glob (already stripped of anchors and trailing
|
|
1071
|
+
* slashes) into regex source. Returns `null` for an unusable pattern.
|
|
1072
|
+
*/
|
|
1073
|
+
function globToRegex(glob) {
|
|
1074
|
+
let out = "";
|
|
1075
|
+
const n = glob.length;
|
|
1076
|
+
for (let i = 0; i < n; i++) {
|
|
1077
|
+
const c = glob[i];
|
|
1078
|
+
if (c === "\\") {
|
|
1079
|
+
if (i + 1 < n) out += escapeRegex(glob[++i]);
|
|
1080
|
+
else out += "\\\\";
|
|
1081
|
+
} else if (c === "*") {
|
|
1082
|
+
if (glob[i + 1] === "*") {
|
|
1083
|
+
let j = i + 1;
|
|
1084
|
+
while (glob[j] === "*") j++;
|
|
1085
|
+
const atStart = i === 0 || glob[i - 1] === "/";
|
|
1086
|
+
if (atStart && j === n) out += ".*";
|
|
1087
|
+
else if (atStart && glob[j] === "/") {
|
|
1088
|
+
out += "(?:.*/)?";
|
|
1089
|
+
j++;
|
|
1090
|
+
} else out += "[^/]*";
|
|
1091
|
+
i = j - 1;
|
|
1092
|
+
} else out += "[^/]*";
|
|
1093
|
+
} else if (c === "?") out += "[^/]";
|
|
1094
|
+
else if (c === "[") {
|
|
1095
|
+
const cls = parseClass(glob, i);
|
|
1096
|
+
if (cls === null) out += "\\[";
|
|
1097
|
+
else {
|
|
1098
|
+
out += cls.source;
|
|
1099
|
+
i = cls.end;
|
|
1100
|
+
}
|
|
1101
|
+
} else if (c === "/") out += "/";
|
|
1102
|
+
else out += escapeRegex(c);
|
|
1103
|
+
}
|
|
1104
|
+
return out;
|
|
1105
|
+
}
|
|
1106
|
+
/** Parses a bracket expression starting at `start`; `null` when it is not closed. */
|
|
1107
|
+
function parseClass(glob, start) {
|
|
1108
|
+
let i = start + 1;
|
|
1109
|
+
let negate = false;
|
|
1110
|
+
if (glob[i] === "!" || glob[i] === "^") {
|
|
1111
|
+
negate = true;
|
|
1112
|
+
i++;
|
|
1113
|
+
}
|
|
1114
|
+
let body = "";
|
|
1115
|
+
let first = true;
|
|
1116
|
+
for (; i < glob.length; i++) {
|
|
1117
|
+
const c = glob[i];
|
|
1118
|
+
if (c === "]" && !first) return {
|
|
1119
|
+
source: negate ? `(?!/)[^${body}]` : `[${body}]`,
|
|
1120
|
+
end: i
|
|
1121
|
+
};
|
|
1122
|
+
first = false;
|
|
1123
|
+
if (c === "\\" && i + 1 < glob.length) body += `\\${glob[++i]}`;
|
|
1124
|
+
else if (c === "[" && glob[i + 1] === ":") {
|
|
1125
|
+
const close = glob.indexOf(":]", i + 2);
|
|
1126
|
+
if (close === -1) return null;
|
|
1127
|
+
const name = glob.slice(i + 2, close);
|
|
1128
|
+
const posix = POSIX_CLASSES[name];
|
|
1129
|
+
if (posix === void 0) return null;
|
|
1130
|
+
body += posix;
|
|
1131
|
+
i = close + 1;
|
|
1132
|
+
} else if (c === "/") return null;
|
|
1133
|
+
else if (c === "-" || c === "]" || c === "^") body += c === "]" ? "\\]" : c === "^" ? "\\^" : "-";
|
|
1134
|
+
else body += escapeInClass(c);
|
|
1135
|
+
}
|
|
1136
|
+
return null;
|
|
1137
|
+
}
|
|
1138
|
+
const POSIX_CLASSES = {
|
|
1139
|
+
alnum: "a-zA-Z0-9",
|
|
1140
|
+
alpha: "a-zA-Z",
|
|
1141
|
+
blank: " \\t",
|
|
1142
|
+
cntrl: "\\x00-\\x1f\\x7f",
|
|
1143
|
+
digit: "0-9",
|
|
1144
|
+
graph: "\\x21-\\x7e",
|
|
1145
|
+
lower: "a-z",
|
|
1146
|
+
print: "\\x20-\\x7e",
|
|
1147
|
+
punct: "!-\\/:-@\\[-`{-~",
|
|
1148
|
+
space: " \\t\\r\\n\\v\\f",
|
|
1149
|
+
upper: "A-Z",
|
|
1150
|
+
xdigit: "0-9A-Fa-f"
|
|
1151
|
+
};
|
|
1152
|
+
function escapeRegex(c) {
|
|
1153
|
+
return /[\\^$.*+?()[\]{}|/-]/.test(c) ? `\\${c}` : c;
|
|
1154
|
+
}
|
|
1155
|
+
function escapeInClass(c) {
|
|
1156
|
+
return c === "\\" ? "\\\\" : c;
|
|
1157
|
+
}
|
|
1158
|
+
//#endregion
|
|
922
1159
|
//#region src/core/exclude.ts
|
|
923
1160
|
/** Directories skipped unless default excludes are turned off. */
|
|
924
1161
|
const DEFAULT_EXCLUDES = [
|
|
@@ -944,7 +1181,8 @@ var Excluder = class {
|
|
|
944
1181
|
this.#ignoreCase = options.ignoreCase ?? process.platform !== "linux";
|
|
945
1182
|
this.#defaults = options.defaultExcludes ?? true ? new Set(DEFAULT_EXCLUDES) : null;
|
|
946
1183
|
const patterns = (options.patterns ?? []).map((p) => p.trim()).filter(Boolean);
|
|
947
|
-
|
|
1184
|
+
const user = patterns.length > 0 ? this.#compile(patterns) : null;
|
|
1185
|
+
this.#user = user?.hasRules ? user : null;
|
|
948
1186
|
}
|
|
949
1187
|
/** True when at least one rule could exclude something. */
|
|
950
1188
|
get active() {
|
|
@@ -960,7 +1198,7 @@ var Excluder = class {
|
|
|
960
1198
|
*/
|
|
961
1199
|
isExcluded(relPath, name, isDirectory, chain) {
|
|
962
1200
|
if (isDirectory && this.#defaults?.has(name)) return true;
|
|
963
|
-
if (this.#user !== null && this.#user.
|
|
1201
|
+
if (this.#user !== null && this.#user.test(relPath, isDirectory) === true) return true;
|
|
964
1202
|
return chain.length > 0 && isGitIgnored(chain, relPath, isDirectory);
|
|
965
1203
|
}
|
|
966
1204
|
/**
|
|
@@ -981,10 +1219,12 @@ var Excluder = class {
|
|
|
981
1219
|
}
|
|
982
1220
|
/** Builds the scope for a `.gitignore` found at `rootRelativeDir` (`''` for the root). */
|
|
983
1221
|
async loadScope(filePath, rootRelativeDir) {
|
|
984
|
-
const content = await readFile(filePath, "utf8").catch(() => null);
|
|
985
|
-
if (content === null
|
|
1222
|
+
const content = await readFile$1(filePath, "utf8").catch(() => null);
|
|
1223
|
+
if (content === null) return null;
|
|
1224
|
+
const matcher = this.#compile(content);
|
|
1225
|
+
if (!matcher.hasRules) return null;
|
|
986
1226
|
return {
|
|
987
|
-
matcher
|
|
1227
|
+
matcher,
|
|
988
1228
|
prefix: "",
|
|
989
1229
|
strip: rootRelativeDir ? rootRelativeDir.length + 1 : 0
|
|
990
1230
|
};
|
|
@@ -1013,11 +1253,13 @@ var Excluder = class {
|
|
|
1013
1253
|
const scopes = [];
|
|
1014
1254
|
const outermostFirst = repoRoot === root ? [] : ancestors.reverse();
|
|
1015
1255
|
const add = async (file, base) => {
|
|
1016
|
-
const content = await readFile(file, "utf8").catch(() => null);
|
|
1017
|
-
if (content === null
|
|
1256
|
+
const content = await readFile$1(file, "utf8").catch(() => null);
|
|
1257
|
+
if (content === null) return;
|
|
1258
|
+
const matcher = this.#compile(content);
|
|
1259
|
+
if (!matcher.hasRules) return;
|
|
1018
1260
|
const prefix = relative(base, root).split(sep).join("/");
|
|
1019
1261
|
scopes.push({
|
|
1020
|
-
matcher
|
|
1262
|
+
matcher,
|
|
1021
1263
|
prefix: prefix ? `${prefix}/` : "",
|
|
1022
1264
|
strip: 0
|
|
1023
1265
|
});
|
|
@@ -1026,11 +1268,8 @@ var Excluder = class {
|
|
|
1026
1268
|
for (const dir of outermostFirst) await add(join(dir, ".gitignore"), dir);
|
|
1027
1269
|
return scopes;
|
|
1028
1270
|
}
|
|
1029
|
-
#
|
|
1030
|
-
return
|
|
1031
|
-
ignorecase: this.#ignoreCase,
|
|
1032
|
-
allowRelativePaths: true
|
|
1033
|
-
});
|
|
1271
|
+
#compile(source) {
|
|
1272
|
+
return GitignoreMatcher.compile(source, { ignoreCase: this.#ignoreCase });
|
|
1034
1273
|
}
|
|
1035
1274
|
};
|
|
1036
1275
|
function isGitIgnored(chain, relPath, isDirectory) {
|
|
@@ -1038,13 +1277,79 @@ function isGitIgnored(chain, relPath, isDirectory) {
|
|
|
1038
1277
|
for (const scope of chain) {
|
|
1039
1278
|
const local = scope.prefix + relPath.slice(scope.strip);
|
|
1040
1279
|
if (local === "") continue;
|
|
1041
|
-
const result = scope.matcher.test(
|
|
1042
|
-
if (result
|
|
1043
|
-
else if (result.unignored) ignored = false;
|
|
1280
|
+
const result = scope.matcher.test(local, isDirectory);
|
|
1281
|
+
if (result !== void 0) ignored = result;
|
|
1044
1282
|
}
|
|
1045
1283
|
return ignored;
|
|
1046
1284
|
}
|
|
1047
1285
|
//#endregion
|
|
1286
|
+
//#region src/core/limiter.ts
|
|
1287
|
+
/**
|
|
1288
|
+
* A tiny concurrency limiter for filesystem work.
|
|
1289
|
+
*
|
|
1290
|
+
* Tasks run at most `concurrency` at a time. Pending tasks are taken
|
|
1291
|
+
* last-in-first-out, which makes a directory walk proceed depth-first and
|
|
1292
|
+
* keeps the pending queue small even on very wide trees.
|
|
1293
|
+
*/
|
|
1294
|
+
var Limiter = class {
|
|
1295
|
+
#concurrency;
|
|
1296
|
+
#queue = [];
|
|
1297
|
+
#active = 0;
|
|
1298
|
+
#failure = void 0;
|
|
1299
|
+
#failed = false;
|
|
1300
|
+
#idle = null;
|
|
1301
|
+
constructor(concurrency) {
|
|
1302
|
+
if (!Number.isInteger(concurrency) || concurrency < 1) throw new RangeError(`concurrency must be a positive integer (got ${concurrency})`);
|
|
1303
|
+
this.#concurrency = concurrency;
|
|
1304
|
+
}
|
|
1305
|
+
/** Schedules a task. Tasks are expected to handle their own errors; a thrown error aborts the run. */
|
|
1306
|
+
run(task) {
|
|
1307
|
+
if (this.#failed) return;
|
|
1308
|
+
this.#queue.push(task);
|
|
1309
|
+
this.#pump();
|
|
1310
|
+
}
|
|
1311
|
+
/** Resolves once every scheduled task, including tasks scheduled by tasks, has finished. */
|
|
1312
|
+
idle() {
|
|
1313
|
+
if (this.#failed) return Promise.reject(this.#failure);
|
|
1314
|
+
if (this.#active === 0 && this.#queue.length === 0) return Promise.resolve();
|
|
1315
|
+
return new Promise((resolve, reject) => {
|
|
1316
|
+
this.#idle = {
|
|
1317
|
+
resolve,
|
|
1318
|
+
reject
|
|
1319
|
+
};
|
|
1320
|
+
});
|
|
1321
|
+
}
|
|
1322
|
+
/** Stops scheduling new work and rejects `idle()` with `reason`. */
|
|
1323
|
+
abort(reason) {
|
|
1324
|
+
if (this.#failed) return;
|
|
1325
|
+
this.#failed = true;
|
|
1326
|
+
this.#failure = reason;
|
|
1327
|
+
this.#queue.length = 0;
|
|
1328
|
+
this.#idle?.reject(reason);
|
|
1329
|
+
this.#idle = null;
|
|
1330
|
+
}
|
|
1331
|
+
#pump() {
|
|
1332
|
+
while (this.#active < this.#concurrency && this.#queue.length > 0) {
|
|
1333
|
+
const task = this.#queue.pop();
|
|
1334
|
+
this.#active++;
|
|
1335
|
+
task().then(this.#onDone, this.#onError);
|
|
1336
|
+
}
|
|
1337
|
+
if (this.#active === 0 && this.#queue.length === 0 && this.#idle) {
|
|
1338
|
+
const idle = this.#idle;
|
|
1339
|
+
this.#idle = null;
|
|
1340
|
+
idle.resolve();
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
#onDone = () => {
|
|
1344
|
+
this.#active--;
|
|
1345
|
+
this.#pump();
|
|
1346
|
+
};
|
|
1347
|
+
#onError = (error) => {
|
|
1348
|
+
this.#active--;
|
|
1349
|
+
this.abort(error);
|
|
1350
|
+
};
|
|
1351
|
+
};
|
|
1352
|
+
//#endregion
|
|
1048
1353
|
//#region src/core/paths.ts
|
|
1049
1354
|
/** Ordinal comparison, so output order never depends on the locale. */
|
|
1050
1355
|
function comparePaths(a, b) {
|
|
@@ -1318,14 +1623,14 @@ const ERROR_DESCRIPTIONS = {
|
|
|
1318
1623
|
//#endregion
|
|
1319
1624
|
//#region src/core/options.ts
|
|
1320
1625
|
/** Default number of concurrent filesystem operations. */
|
|
1321
|
-
const DEFAULT_CONCURRENCY =
|
|
1626
|
+
const DEFAULT_CONCURRENCY = 32;
|
|
1322
1627
|
/** Validates the root and turns user-facing settings into scanner options. */
|
|
1323
1628
|
async function resolveScan(path, input, now) {
|
|
1324
1629
|
const root = resolve(path);
|
|
1325
1630
|
await assertDirectory(root);
|
|
1326
1631
|
const maxDepth = input.maxDepth ?? null;
|
|
1327
1632
|
if (maxDepth !== null && (!Number.isInteger(maxDepth) || maxDepth < 0)) throw new DatError(`maxDepth must be a non-negative integer (got ${maxDepth}).`);
|
|
1328
|
-
const concurrency = input.concurrency ??
|
|
1633
|
+
const concurrency = input.concurrency ?? 32;
|
|
1329
1634
|
if (!Number.isInteger(concurrency) || concurrency < 1) throw new DatError(`concurrency must be a positive integer (got ${concurrency}).`);
|
|
1330
1635
|
const minSize = toBytes(input.minSize, "minSize");
|
|
1331
1636
|
const maxSize = toBytes(input.maxSize, "maxSize");
|
|
@@ -1696,8 +2001,8 @@ var Collector = class {
|
|
|
1696
2001
|
dir,
|
|
1697
2002
|
name,
|
|
1698
2003
|
bytes,
|
|
1699
|
-
|
|
1700
|
-
|
|
2004
|
+
dev: stats.dev,
|
|
2005
|
+
ino: stats.ino
|
|
1701
2006
|
};
|
|
1702
2007
|
const existing = this.#bySize.get(bytes);
|
|
1703
2008
|
if (existing === void 0) this.#bySize.set(bytes, candidate);
|
|
@@ -1717,7 +2022,7 @@ var Collector = class {
|
|
|
1717
2022
|
if (dir === void 0) dirPaths.set(c.dir, dir = scanner.absolutePath(c.dir));
|
|
1718
2023
|
return joinRelative(dir, c.name);
|
|
1719
2024
|
},
|
|
1720
|
-
concurrency
|
|
2025
|
+
concurrency,
|
|
1721
2026
|
signal,
|
|
1722
2027
|
onProgress,
|
|
1723
2028
|
onError: (c, error) => scanner.recordError(scanner.relativePath(c.dir, c.name), error)
|
|
@@ -1973,7 +2278,7 @@ async function compare(left, right, options = {}) {
|
|
|
1973
2278
|
}
|
|
1974
2279
|
for (const [path, r] of rightIndex.files) if (!leftIndex.files.has(path)) added.push(toEntry(path, r));
|
|
1975
2280
|
if (sameSize.length > 0) {
|
|
1976
|
-
const differing = await findContentChanges(sameSize, a.options.root, b.options.root, options);
|
|
2281
|
+
const differing = await findContentChanges(sameSize, (path) => leftIndex.files.get(path).bytes, a.options.root, b.options.root, options);
|
|
1977
2282
|
for (const path of sameSize) if (differing.has(path)) {
|
|
1978
2283
|
const bytes = leftIndex.files.get(path).bytes;
|
|
1979
2284
|
changed.push({
|
|
@@ -2050,15 +2355,16 @@ function side(root, scanner, index) {
|
|
|
2050
2355
|
types: index.types
|
|
2051
2356
|
};
|
|
2052
2357
|
}
|
|
2053
|
-
async function findContentChanges(paths, leftRoot, rightRoot, options) {
|
|
2358
|
+
async function findContentChanges(paths, sizeOf, leftRoot, rightRoot, options) {
|
|
2054
2359
|
const differing = /* @__PURE__ */ new Set();
|
|
2055
|
-
const limiter = new Limiter(
|
|
2360
|
+
const limiter = new Limiter(options.concurrency ?? 32);
|
|
2056
2361
|
const onAbort = () => limiter.abort(new AbortError());
|
|
2057
2362
|
options.signal?.addEventListener("abort", onAbort, { once: true });
|
|
2058
2363
|
let done = 0;
|
|
2059
2364
|
for (const path of paths) limiter.run(async () => {
|
|
2060
2365
|
try {
|
|
2061
|
-
const
|
|
2366
|
+
const bytes = sizeOf(path);
|
|
2367
|
+
const [l, r] = await Promise.all([hashFileOfSize(joinRelative(leftRoot, path), bytes), hashFileOfSize(joinRelative(rightRoot, path), bytes)]);
|
|
2062
2368
|
if (l !== r) differing.add(path);
|
|
2063
2369
|
} catch {
|
|
2064
2370
|
differing.add(path);
|
|
@@ -2430,7 +2736,7 @@ async function loadConfig(options) {
|
|
|
2430
2736
|
};
|
|
2431
2737
|
let text;
|
|
2432
2738
|
try {
|
|
2433
|
-
text = await readFile(path, "utf8");
|
|
2739
|
+
text = await readFile$1(path, "utf8");
|
|
2434
2740
|
} catch (error) {
|
|
2435
2741
|
throw new DatError(errorCode(error) === "ENOENT" ? `Config file not found: ${path}` : `Cannot read config ${path}: ${errorMessage(error)}`);
|
|
2436
2742
|
}
|