Skip to content

Commit 8911a72

Browse files
committed
Test whether common/git-hooks contains any files, since the existence of the folder is not determined by Git
1 parent fe7f6f6 commit 8911a72

1 file changed

Lines changed: 15 additions & 17 deletions

File tree

‎apps/rush-lib/src/logic/InstallManager.ts‎

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -270,28 +270,26 @@ export class InstallManager {
270270
// Git hooks are only installed if the repo opts in by including files in /common/git-hooks
271271
const hookSource: string = path.join(this._rushConfiguration.commonFolder, 'git-hooks');
272272
const hookDestination: string = path.join(this._rushConfiguration.rushJsonFolder, FolderConstants.Git, 'hooks');
273+
273274
if (FileSystem.exists(hookSource)) {
274-
console.log(os.EOL + colors.bold('Detected /common/git-hooks, clearing and reinstalling Git hooks'));
275275
const hookFilenames: Array<string> = FileSystem.readFolder(hookSource);
276+
if (hookFilenames.length > 0) {
277+
console.log(os.EOL + colors.bold('Found files in the "common/git-hooks" folder.'));
276278

277-
if (hookFilenames.length === 0) {
278-
console.log(colors.yellow('Warning - You have not added any git hooks yet, skipping for now'));
279-
280-
} else {
281279
// Clear the currently installed git hooks and install fresh copies
282280
FileSystem.ensureEmptyFolder(hookDestination);
283-
hookFilenames.forEach(filename => {
284-
285-
const match: RegExpMatchArray | null = filename.match(/^[a-z\-]+/);
286-
if (match !== null) {
287-
console.log(`Installing hook ${match[0]}`);
288-
FileSystem.copyFile({
289-
sourcePath: path.join(hookSource, filename),
290-
destinationPath: path.join(hookDestination, match[0])
291-
});
292-
FileSystem.changePosixModeBits(path.join(hookDestination, match[0]), PosixModeBits.UserExecute);
293-
}
294-
});
281+
282+
// Only copy files that look like Git hook names
283+
const filteredHookFilenames: string[] = hookFilenames.filter(x => /^[a-z\-]+/.test(x));
284+
for (const filename of filteredHookFilenames) {
285+
FileSystem.copyFile({
286+
sourcePath: path.join(hookSource, filename),
287+
destinationPath: path.join(hookDestination, filename)
288+
});
289+
FileSystem.changePosixModeBits(path.join(hookDestination, filename), PosixModeBits.UserExecute);
290+
}
291+
292+
console.log('Successfully installed these Git hook scripts: ' + filteredHookFilenames.join(', ') + os.EOL);
295293
}
296294
}
297295

0 commit comments

Comments
 (0)