Skip to content

Commit

Permalink
Fixes #628: Allowing custom git hooks to be used. (#692)
Browse files Browse the repository at this point in the history
* Fixes #628: Allowing custom git hooks to be used.

* Adding documentation.
  • Loading branch information
grasmash authored Nov 18, 2016
1 parent 222de74 commit 71c4745
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 13 deletions.
8 changes: 8 additions & 0 deletions phing/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ drush:
logoutput: yes
verbose: ${blt.verbose}

git:
# The value of a hook should be the file path to a directory containing an
# executable file named after the hook.
# Changing a hook value to 'false' will disable it.
hooks:
pre-commit: ${blt.root}/scripts/git-hooks
commit-msg: ${blt.root}/scripts/git-hooks

multisite:
# The docroot/sites/default directory is used by default.
name: default
Expand Down
28 changes: 20 additions & 8 deletions phing/tasks/setup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,28 @@
</target>

<target name="setup:git-hooks" description="Installs git hooks to local .git/hooks directory from version controlled scripts/git-hooks directory.">
<echo>Removing ${repo.root}/.git/hooks, if it exists.</echo>
<!-- Symlink into .git directory. -->
<delete dir="${repo.root}/.git/hooks" quiet="true" failonerror="false"/>
<delete file="${repo.root}/.git/hooks" quiet="true" failonerror="false"/>
<echo>Symlinking ${repo.root}/scripts/git-hooks to ${repo.root}/.git/hooks.</echo>
<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"/>
<exec dir="${repo.root}/.git" command="mkdir -p hooks" logoutput="true" checkreturn="true" level="${blt.exec_level}" passthru="true"/>
<delete file="${repo.root}/.git/hooks/pre-commit" failonerror="false" quiet="true" verbose="${blt.verbose}"/>
<delete file="${repo.root}/.git/hooks/commit-msg" failonerror="false" quiet="true" verbose="${blt.verbose}"/>

<if>
<not><equals arg1="${git.hooks.pre-commit}" arg2="false"/></not>
<then>
<echo level="info">Symlinking ${repo.root}/scripts/git-hooks/pre-commit to ${git.hooks.pre-commit}/pre-commit</echo>
<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"/>
</then>
</if>
<if>
<not><equals arg1="${git.hooks.commit-msg}" arg2="false"/></not>
<then>
<echo level="verbose">Symlinking ${repo.root}/scripts/git-hooks/commit-msg to ${git.hooks.pre-commit}/commit-msg</echo>
<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"/>
</then>
</if>

<!-- Grant execution permissions. -->
<echo>Making git hooks executable.</echo>
<chmod mode="0755">
<echo level="verbose">Making git hooks executable.</echo>
<chmod mode="0755" verbose="false">
<fileset dir="${blt.root}/scripts/git-hooks">
<exclude name="**/*.sample" />
<exclude name="**/README.md" />
Expand Down
18 changes: 18 additions & 0 deletions readme/extending-blt.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ More specifically, you can modify the build artifact in the following key ways:
dir: ${deploy.dir}/docroot/profiles/contrib/lightning
command: npm run install-libraries

### setup:*

#### setup:git-hooks

You may disable a git hook by setting its value under `git.hooks` to false:

git:
hooks:
pre-commit: false

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:

git:
hooks:
pre-commit: ${repo.root}/my-custom-git-hooks

In this example, an executable file named `pre-commit` should exist in `${repo.root}/my-custom-git-hooks`.

### tests:*

#### tests:behat
Expand Down
15 changes: 15 additions & 0 deletions src/Update/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public function setRepoRoot($repoRoot) {
$this->repoRoot = $repoRoot;
}

/**
* @return ConsoleOutput
*/
public function getOutput() {
return $this->output;
}

/**
* @return \Symfony\Component\Filesystem\Filesystem
*/
Expand Down Expand Up @@ -268,4 +275,12 @@ public function moveFile($source, $target, $overwrite = FALSE) {
return FALSE;
}

/**
* @param $filepath
*/
public function deleteFile($filepath) {
$abs_path = $this->getRepoRoot() . '/' . $filepath;
$this->getFileSystem()->remove($abs_path);
}

}
6 changes: 5 additions & 1 deletion src/Update/Updates.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(Updater $updater) {
/**
* @Update(
* version = "8.5.1",
* description = "Removes deprecated features patch. Moves configuration files to blt subdirectory."
* description = "Removes deprecated features patch. Moves configuration files to blt subdirectory. Removes .git/hooks symlink."
* )
*/
public function update_851() {
Expand All @@ -39,6 +39,10 @@ public function update_851() {
$this->updater->moveFile('project.yml', 'blt/project.yml', TRUE);
$this->updater->moveFile('project.local.yml', 'blt/project.local.yml', TRUE);
$this->updater->moveFile('example.project.local.yml', 'blt/example.project.local.yml', TRUE);

// Delete symlink to hooks directory. Individual git hooks are now symlinked, not the entire directory.
$this->updater->deleteFile('.git/hooks');
$this->updater->getOutput()->writeln('.git/hooks was deleted. Please re-run setup:git-hooks to install git hooks locally.');
}

}
4 changes: 0 additions & 4 deletions template/blt/project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ git:
remotes:
# Defining git remotes allows builds deployed via CI.
- [email protected]:bolt8.git
# - [email protected]:radass4.git
hooks:
pre-commit: true
commit-msg: true

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

0 comments on commit 71c4745

Please sign in to comment.