Skip to content

Commit faff4dc

Browse files
author
Nicholas Pape
authored
Package-Deps-Hash: add support for submodules (microsoft#233)
* Add incremental build support for git submodules * Changefile * Update getPackageDeps.ts
1 parent 0d4add2 commit faff4dc

3 files changed

Lines changed: 36 additions & 7 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/package-deps-hash",
5+
"comment": "Fix an issue where Git submodules were unable to be hashed",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@microsoft/package-deps-hash",
10+
"email": "nickpape@users.noreply.github.com"
11+
}

‎libraries/package-deps-hash/src/getPackageDeps.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,26 @@ export function parseGitLsTree(output: string): Map<string, string> {
1010
const changes: Map<string, string> = new Map<string, string>();
1111

1212
if (output) {
13+
// A line is expected to look like:
14+
// 100644 blob 3451bccdc831cb43d7a70ed8e628dcf9c7f888c8 src/typings/tsd.d.ts
15+
// 160000 commit c5880bf5b0c6c1f2e2c43c95beeb8f0a808e8bac web-build-tools
16+
const gitRegex: RegExp = /([0-9]{6})\s(blob|commit)\s([a-f0-9]{40})\s*(.*)/;
17+
1318
// Note: The output of git ls-tree uses \n newlines regardless of OS.
1419
output.split('\n').forEach(line => {
1520

16-
// A line is expected to look like:
17-
// 100644 blob 3451bccdc831cb43d7a70ed8e628dcf9c7f888c8 src/typings/tsd.d.ts
18-
1921
if (line) {
2022
// Take everything after the "100644 blob", which is just the hash and filename
21-
const [hash, filename]: string[] = line.substr(line.indexOf('blob ') + 5).split('\t');
22-
if (!filename || !hash) {
23+
const matches: RegExpMatchArray = line.match(gitRegex);
24+
if (matches && matches[3] && matches[4]) {
25+
const hash: string = matches[3];
26+
const filename: string = matches[4];
27+
28+
changes.set(filename, hash);
29+
30+
} else {
2331
throw new Error(`Cannot parse git ls-tree input: "${line}"`);
2432
}
25-
26-
changes.set(filename, hash);
2733
}
2834
});
2935
}

‎libraries/package-deps-hash/src/tests/getPackageDeps.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ describe('parseGitLsTree', () => {
2525
done();
2626
});
2727

28+
it('can handle a submodule', (done) => {
29+
const filename: string = 'web-build-tools';
30+
const hash: string = 'c5880bf5b0c6c1f2e2c43c95beeb8f0a808e8bac';
31+
32+
const output: string = `160000 commit ${hash}\t${filename}`;
33+
const changes: Map<string, string> = parseGitLsTree(output);
34+
35+
assert.equal(changes.size, 1, 'Expect there to be exactly 1 change');
36+
assert.equal(changes.get(filename), hash, `Expect the hash to be ${hash}`);
37+
done();
38+
});
39+
2840
it('can handle multiple lines', (done) => {
2941
const filename1: string = 'src/typings/tsd.d.ts';
3042
const hash1: string = '3451bccdc831cb43d7a70ed8e628dcf9c7f888c8';

0 commit comments

Comments
 (0)