Skip to content

Commit d17c31e

Browse files
authored
fix(scripts-update-release-notes): properly handle git for-each-ref cmd call to not fail release notes update (#27757)
1 parent f0b0574 commit d17c31e

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

scripts/update-release-notes/changelogsAndTags.ts

+38-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import * as path from 'path';
2-
import * as fs from 'fs-extra';
31
import { execSync } from 'child_process';
2+
import * as path from 'path';
3+
44
import { ChangelogJson } from 'beachball';
5+
import * as fs from 'fs-extra';
56
import { rollup as lernaAliases } from 'lerna-alias';
7+
68
import { IChangelogEntry } from './types';
79

810
const MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
@@ -49,28 +51,48 @@ export function getTagToChangelogMap(maxAgeDays?: number): Map<string, IChangelo
4951
* @returns List of tags
5052
*/
5153
export function getTags(maxAgeDays?: number): string[] {
54+
const ONE_MEGABYTE = 1024 * 1000;
55+
const TEN_MEGABYTES = ONE_MEGABYTE * 10;
5256
console.log(`Getting tags${maxAgeDays ? ` up to ${maxAgeDays} days old` : ''}...`);
5357

54-
const cmd = 'git for-each-ref --sort=-creatordate --format="%(refname:short) -- %(creatordate)" refs/tags';
58+
try {
59+
const cmd = 'git for-each-ref --sort=-creatordate --format="%(refname:short) -- %(creatordate)" refs/tags';
60+
const gitForEachRefBuffer = execSync(cmd, {
61+
// execSync buffer is by default 1MB (node 16). Our git refs tog is much bigger than that, thus setting for 10MB
62+
maxBuffer: TEN_MEGABYTES,
63+
});
64+
const currentBufferSize = (gitForEachRefBuffer.byteLength / ONE_MEGABYTE).toFixed(2);
5565

56-
let tagsAndDates = execSync(cmd, { cwd: process.cwd() })
57-
.toString()
58-
.split(/\r?\n/g)
59-
.map(tag => tag.split(' -- '))
60-
.filter(arr => arr.length === 2);
66+
console.warn(`
67+
📣 NOTE: "git for-each-ref" current buffer size is ${currentBufferSize}MB.
68+
If this will be more than maxBuffer ${TEN_MEGABYTES}MB this command will fail and you'll have to increase maxBuffer!
69+
`);
6170

62-
if (maxAgeDays) {
63-
const endIndex = tagsAndDates.findIndex(([, date]) => !_isNewEnough(date, maxAgeDays));
64-
if (endIndex !== -1) {
65-
tagsAndDates = tagsAndDates.slice(0, endIndex);
71+
let tagsAndDates = gitForEachRefBuffer
72+
.toString()
73+
.split(/\r?\n/g)
74+
.map(tag => tag.split(' -- '))
75+
.filter(arr => arr.length === 2);
76+
77+
if (maxAgeDays) {
78+
const endIndex = tagsAndDates.findIndex(([, date]) => !_isNewEnough(date, maxAgeDays));
79+
if (endIndex !== -1) {
80+
tagsAndDates = tagsAndDates.slice(0, endIndex);
81+
}
6682
}
67-
}
6883

69-
const tags = tagsAndDates.map(([tag]) => tag);
84+
const tags = tagsAndDates.map(([tag]) => tag);
7085

71-
console.log(`Found ${tags.length} tag(s).\n`);
86+
console.log(`Found ${tags.length} tag(s).\n`);
7287

73-
return tags;
88+
return tags;
89+
} catch (err) {
90+
if (err.code === 'ENOBUFS') {
91+
throw new Error(`maxBuffer ${TEN_MEGABYTES}MB was reached. Increase its size in the codebase`);
92+
}
93+
94+
throw err;
95+
}
7496
}
7597

7698
/**

0 commit comments

Comments
 (0)