Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 71c4745

Browse files
authored
Fixes #628: Allowing custom git hooks to be used. (#692)
* Fixes #628: Allowing custom git hooks to be used. * Adding documentation.
1 parent 222de74 commit 71c4745

File tree

6 files changed

+66
-13
lines changed

6 files changed

+66
-13
lines changed

phing/build.yml

+8
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ drush:
6969
logoutput: yes
7070
verbose: ${blt.verbose}
7171

72+
git:
73+
# The value of a hook should be the file path to a directory containing an
74+
# executable file named after the hook.
75+
# Changing a hook value to 'false' will disable it.
76+
hooks:
77+
pre-commit: ${blt.root}/scripts/git-hooks
78+
commit-msg: ${blt.root}/scripts/git-hooks
79+
7280
multisite:
7381
# The docroot/sites/default directory is used by default.
7482
name: default

phing/tasks/setup.xml

+20-8
Original file line numberDiff line numberDiff line change
@@ -228,16 +228,28 @@
228228
</target>
229229

230230
<target name="setup:git-hooks" description="Installs git hooks to local .git/hooks directory from version controlled scripts/git-hooks directory.">
231-
<echo>Removing ${repo.root}/.git/hooks, if it exists.</echo>
232-
<!-- Symlink into .git directory. -->
233-
<delete dir="${repo.root}/.git/hooks" quiet="true" failonerror="false"/>
234-
<delete file="${repo.root}/.git/hooks" quiet="true" failonerror="false"/>
235-
<echo>Symlinking ${repo.root}/scripts/git-hooks to ${repo.root}/.git/hooks.</echo>
236-
<exec dir="${repo.root}/.git" command="ln -snv ../vendor/acquia/blt/scripts/git-hooks hooks" logoutput="true" checkreturn="true" level="${blt.exec_level}" passthru="true"/>
231+
<exec dir="${repo.root}/.git" command="mkdir -p hooks" logoutput="true" checkreturn="true" level="${blt.exec_level}" passthru="true"/>
232+
<delete file="${repo.root}/.git/hooks/pre-commit" failonerror="false" quiet="true" verbose="${blt.verbose}"/>
233+
<delete file="${repo.root}/.git/hooks/commit-msg" failonerror="false" quiet="true" verbose="${blt.verbose}"/>
234+
235+
<if>
236+
<not><equals arg1="${git.hooks.pre-commit}" arg2="false"/></not>
237+
<then>
238+
<echo level="info">Symlinking ${repo.root}/scripts/git-hooks/pre-commit to ${git.hooks.pre-commit}/pre-commit</echo>
239+
<exec dir="${repo.root}/.git/hooks" command="ln -snv ${git.hooks.pre-commit}/pre-commit pre-commit" logoutput="true" checkreturn="true" level="${blt.exec_level}" passthru="true"/>
240+
</then>
241+
</if>
242+
<if>
243+
<not><equals arg1="${git.hooks.commit-msg}" arg2="false"/></not>
244+
<then>
245+
<echo level="verbose">Symlinking ${repo.root}/scripts/git-hooks/commit-msg to ${git.hooks.pre-commit}/commit-msg</echo>
246+
<exec dir="${repo.root}/.git/hooks" command="ln -snv ${git.hooks.commit-msg}/commit-msg commit-msg" logoutput="true" checkreturn="true" level="${blt.exec_level}" passthru="true"/>
247+
</then>
248+
</if>
237249

238250
<!-- Grant execution permissions. -->
239-
<echo>Making git hooks executable.</echo>
240-
<chmod mode="0755">
251+
<echo level="verbose">Making git hooks executable.</echo>
252+
<chmod mode="0755" verbose="false">
241253
<fileset dir="${blt.root}/scripts/git-hooks">
242254
<exclude name="**/*.sample" />
243255
<exclude name="**/README.md" />

readme/extending-blt.md

+18
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,24 @@ More specifically, you can modify the build artifact in the following key ways:
8686
dir: ${deploy.dir}/docroot/profiles/contrib/lightning
8787
command: npm run install-libraries
8888

89+
### setup:*
90+
91+
#### setup:git-hooks
92+
93+
You may disable a git hook by setting its value under `git.hooks` to false:
94+
95+
git:
96+
hooks:
97+
pre-commit: false
98+
99+
You may use a custom git hook in place of BLT's default git hooks by setting its value under `git.hooks` to the directory path containing of the hook. The directory must contain an executable file named after the git hook:
100+
101+
git:
102+
hooks:
103+
pre-commit: ${repo.root}/my-custom-git-hooks
104+
105+
In this example, an executable file named `pre-commit` should exist in `${repo.root}/my-custom-git-hooks`.
106+
89107
### tests:*
90108

91109
#### tests:behat

src/Update/Updater.php

+15
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ public function setRepoRoot($repoRoot) {
5757
$this->repoRoot = $repoRoot;
5858
}
5959

60+
/**
61+
* @return ConsoleOutput
62+
*/
63+
public function getOutput() {
64+
return $this->output;
65+
}
66+
6067
/**
6168
* @return \Symfony\Component\Filesystem\Filesystem
6269
*/
@@ -268,4 +275,12 @@ public function moveFile($source, $target, $overwrite = FALSE) {
268275
return FALSE;
269276
}
270277

278+
/**
279+
* @param $filepath
280+
*/
281+
public function deleteFile($filepath) {
282+
$abs_path = $this->getRepoRoot() . '/' . $filepath;
283+
$this->getFileSystem()->remove($abs_path);
284+
}
285+
271286
}

src/Update/Updates.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(Updater $updater) {
2929
/**
3030
* @Update(
3131
* version = "8.5.1",
32-
* description = "Removes deprecated features patch. Moves configuration files to blt subdirectory."
32+
* description = "Removes deprecated features patch. Moves configuration files to blt subdirectory. Removes .git/hooks symlink."
3333
* )
3434
*/
3535
public function update_851() {
@@ -39,6 +39,10 @@ public function update_851() {
3939
$this->updater->moveFile('project.yml', 'blt/project.yml', TRUE);
4040
$this->updater->moveFile('project.local.yml', 'blt/project.local.yml', TRUE);
4141
$this->updater->moveFile('example.project.local.yml', 'blt/example.project.local.yml', TRUE);
42+
43+
// Delete symlink to hooks directory. Individual git hooks are now symlinked, not the entire directory.
44+
$this->updater->deleteFile('.git/hooks');
45+
$this->updater->getOutput()->writeln('.git/hooks was deleted. Please re-run setup:git-hooks to install git hooks locally.');
4246
}
4347

4448
}

template/blt/project.yml

-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ git:
1919
remotes:
2020
# Defining git remotes allows builds deployed via CI.
2121
- [email protected]:bolt8.git
22-
# - [email protected]:radass4.git
23-
hooks:
24-
pre-commit: true
25-
commit-msg: true
2622

2723
drush:
2824
# You can set custom project aliases in drush/site-aliases/aliases.drushrc.php.

0 commit comments

Comments
 (0)