Skip to content

Commit 71a023d

Browse files
committed
Modified installation behaviour for git hooks
Hooks are now installed when the /common/git-hooks folder is non-empty. Any hooks already installed will be deleted before new hooks are added.
1 parent a97dcee commit 71a023d

6 files changed

Lines changed: 24 additions & 38 deletions

File tree

‎apps/rush-lib/src/cli/actions/BaseInstallAction.ts‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export abstract class BaseInstallAction extends BaseRushAction {
3030
protected _noLinkParameter: CommandLineFlagParameter;
3131
protected _networkConcurrencyParameter: CommandLineIntegerParameter;
3232
protected _debugPackageManagerParameter: CommandLineFlagParameter;
33-
protected _withHooksParameter: CommandLineFlagParameter;
3433

3534
protected onDefineParameters(): void {
3635
this._purgeParameter = this.defineFlagParameter({
@@ -61,11 +60,6 @@ export abstract class BaseInstallAction extends BaseRushAction {
6160
+ ' the output of Rush to a file when using this command.'
6261
});
6362
this._variant = this.defineStringParameter(Variants.VARIANT_PARAMETER);
64-
this._withHooksParameter = this.defineFlagParameter({
65-
parameterLongName: '--with-hooks',
66-
description: 'If specified, will copy over git hooks from "common/git-hooks" folder into the'
67-
+ ' git config directory. Each file should be an applicably named, such as "pre-commit.sh".'
68-
});
6963
}
7064

7165
protected abstract buildInstallOptions(): IInstallManagerOptions;

‎apps/rush-lib/src/cli/actions/InstallAction.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ export class InstallAction extends BaseInstallAction {
3434
recheckShrinkwrap: false,
3535
networkConcurrency: this._networkConcurrencyParameter.value,
3636
collectLogFile: this._debugPackageManagerParameter.value!,
37-
variant: this._variant.value,
38-
withHooks: this._withHooksParameter.value
37+
variant: this._variant.value
3938
};
4039
}
4140
}

‎apps/rush-lib/src/cli/actions/UpdateAction.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ export class UpdateAction extends BaseInstallAction {
6262
recheckShrinkwrap: this._recheckParameter.value!,
6363
networkConcurrency: this._networkConcurrencyParameter.value,
6464
collectLogFile: this._debugPackageManagerParameter.value!,
65-
variant: this._variant.value,
66-
withHooks: this._withHooksParameter.value
65+
variant: this._variant.value
6766
};
6867
}
6968
}

‎apps/rush-lib/src/cli/test/__snapshots__/CommandLineHelp.test.ts.snap‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ Optional arguments:
259259
exports[`CommandLineHelp prints the help for each action: install 1`] = `
260260
"usage: rush install [-h] [-p] [--bypass-policy] [--no-link]
261261
[--network-concurrency COUNT] [--debug-package-manager]
262-
[--variant VARIANT] [--with-hooks]
262+
[--variant VARIANT]
263263
264264
265265
The \\"rush install\\" command installs package dependencies for all your
@@ -296,10 +296,6 @@ Optional arguments:
296296
--variant VARIANT Run command using a variant installation
297297
configuration. This parameter may alternatively
298298
specified via the RUSH_VARIANT environment variable.
299-
--with-hooks If specified, will copy over git hooks from
300-
\\"common/git-hooks\\" folder into the git config
301-
directory. Each file should be an applicably named,
302-
such as \\"pre-commit.sh\\".
303299
"
304300
`;
305301
@@ -481,7 +477,7 @@ Optional arguments:
481477
exports[`CommandLineHelp prints the help for each action: update 1`] = `
482478
"usage: rush update [-h] [-p] [--bypass-policy] [--no-link]
483479
[--network-concurrency COUNT] [--debug-package-manager]
484-
[--variant VARIANT] [--with-hooks] [--full] [--recheck]
480+
[--variant VARIANT] [--full] [--recheck]
485481
486482
487483
The \\"rush update\\" command installs the dependencies described in your package.
@@ -517,10 +513,6 @@ Optional arguments:
517513
--variant VARIANT Run command using a variant installation
518514
configuration. This parameter may alternatively
519515
specified via the RUSH_VARIANT environment variable.
520-
--with-hooks If specified, will copy over git hooks from
521-
\\"common/git-hooks\\" folder into the git config
522-
directory. Each file should be an applicably named,
523-
such as \\"pre-commit.sh\\".
524516
--full Normally \\"rush update\\" tries to preserve your
525517
existing installed versions and only makes the
526518
minimum updates needed to satisfy the package.json

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,6 @@ export interface IInstallManagerOptions {
109109
* The variant to consider when performing installations and validating shrinkwrap updates.
110110
*/
111111
variant?: string | undefined;
112-
113-
/**
114-
* Whether to copy over git hooks into the git config when installing.
115-
*/
116-
withHooks: boolean;
117112
}
118113

119114
/**
@@ -264,22 +259,30 @@ export class InstallManager {
264259

265260
ApprovedPackagesChecker.rewriteConfigFiles(this._rushConfiguration);
266261

267-
if (options.withHooks) {
268-
const source: string = path.join(this._rushConfiguration.commonFolder, 'git-hooks');
269-
const destination: string = path.join(this._rushConfiguration.rushJsonFolder, FolderConstants.Git, 'hooks');
262+
// Git hooks are only installed if the repo opts in by including files in /common/git-hooks
263+
const hookSource: string = path.join(this._rushConfiguration.commonFolder, 'git-hooks');
264+
const hookDestination: string = path.join(this._rushConfiguration.rushJsonFolder, FolderConstants.Git, 'hooks');
265+
if (FileSystem.exists(hookSource)) {
266+
console.log(os.EOL + colors.bold('Detected /common/git-hooks, clearing and reinstalling Git hooks'));
267+
const hookFilenames: Array<string> = FileSystem.readFolder(hookSource);
270268

271-
if (FileSystem.exists(source) && FileSystem.exists(destination)) {
272-
FileSystem.readFolder(source).forEach(filename => {
269+
if (hookFilenames.length === 0) {
270+
console.log(colors.yellow('Warning - You have not added any git hooks yet, skipping for now'));
273271

274-
const match: RegExpMatchArray | null = filename.match(/[a-z\-]+/);
272+
} else {
273+
// Clear the currently installed git hooks and install fresh copies
274+
FileSystem.ensureEmptyFolder(hookDestination);
275+
hookFilenames.forEach(filename => {
276+
277+
const match: RegExpMatchArray | null = filename.match(/^[a-z\-]+/);
275278
if (match !== null) {
279+
console.log(`Installing hook ${match[0]}`);
276280
FileSystem.copyFile({
277-
sourcePath: path.join(source, filename),
278-
destinationPath: path.join(destination, match[0])
281+
sourcePath: path.join(hookSource, filename),
282+
destinationPath: path.join(hookDestination, match[0])
279283
});
280-
FileSystem.changePosixModeBits(path.join(destination, match[0]), PosixModeBits.UserExecute);
284+
FileSystem.changePosixModeBits(path.join(hookDestination, match[0]), PosixModeBits.UserExecute);
281285
}
282-
283286
});
284287
}
285288
}
@@ -1001,7 +1004,7 @@ export class InstallManager {
10011004

10021005
private _queryIfReleaseIsPublished(registryUrl: string): Promise<boolean> {
10031006
let queryUrl: string = registryUrl;
1004-
if (queryUrl[-1] !== '/') {
1007+
if (queryUrl[- 1] !== '/') {
10051008
queryUrl += '/';
10061009
}
10071010
// Note that the "@" symbol does not normally get URL-encoded

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,7 @@ export class PackageJsonUpdater {
192192
recheckShrinkwrap: false,
193193
networkConcurrency: undefined,
194194
collectLogFile: false,
195-
variant: variant,
196-
withHooks: false
195+
variant: variant
197196
};
198197
const installManager: InstallManager = new InstallManager(
199198
this._rushConfiguration,

0 commit comments

Comments
 (0)