Tip the 21st

Use the Power of Command Shells

Harness the power of the command shell

— “The Programmer’s Way: From Little Employee to Expert”

Writing in the front

A Git project has only one hooks directory and only one hook file per stage, which can cause configuration conflicts between developers and corporate global hooks. This simple project is featured for implementing global Git Hooks and developer custom Hooks.

Global and custom Hooks

The project structure

─ Bass Exercises ── Bass Exercises ── recommit-Msg.Sample │ ├─ Bass Exercises ── recommit-Msg Fsmonitor -watchman.sample │ ├─ Post-Update. Sample │ ├─ Pre-ApplyPatch. Sample │ ├─ Pre-Commission. sample │ ├─ Pre-Commission. sample │ ├─ Pre-Commission. sample Pre-merge-commit. Sample │ ├─ Pre-push. Sample │ ├─ Pre-rebase. Sample │ ├─ Pre-receive. Sample │ ├─ Pre-merge Prepare -commit-msg. Sample │ ├── update. Sample ├── key # Swagger - Swagger - Swagger - Swagger - Swagger - Swagger - Swagger - Swagger - Swagger - Swagger FSMonitor - Watchman - Hook - Exec # Hook - Post - Update Bass Exercises - Pre-Applypatch Bass Exercises - Pre-Merge - Commit Bass Exercises - FSMonitor - Watchman - Hook-Exec # Hook - Post-update Bass Exercises - Pre-Applypatch Bass Exercises - Pre-Merge - Commit Bass Exercises Pre-Push ─ Pre-Rebase Heavy Exercises ─ Pre-Receive Heavy Exercises ─ Prepare-Commit - MSG ├─ Update

The core file hook-exec

This file is responsible for binding global/

and customized/

to a

. This file can be modified if you want to do more directory extensions, more extensions to the same stage hooks.

hook-exec

#! /bin/bash HOOK_DIR=$(dirname "$0") GLOBAL_HOOK="$HOOK_DIR/global/$HOOK_NAME" CUSTOM_HOOK="$HOOK_DIR/customized/$HOOK_NAME" if [ -f "$GLOBAL_HOOK" ]; then source $GLOBAL_HOOK fi if [ -f "$CUSTOM_HOOK" ]; then source $CUSTOM_HOOK fi

Git hook execution files

Hook files in the root directory are all the same. The purpose is to pass the filename of hook HOOK_NAME and all parameters $@ into hook-exec, so as to realize the call of global and custom hooks and ensure that the original parameters can be used normally.

hook name

#! /bin/bash HOOK_NAME=$(echo $0 | sed 's/.*\///') source $(dirname "$0")/hook-exec $@

The Makefile implements the instruction installation

Fork the project for this article into its repository, and save the global githooks to the project’s global/ directory, where the key/can hold the developer’s own githooks.

Copy the following Makefile to the root of your Git project, and you can use the make directive to initialize and update the multi hooks. (replace [email protected]: DoneSpeak/gromithooks. Git for fork after project address)

Makefile

REPO := $(PWD)
REPO_GIT := $(REPO)/.git

install-git-hooks: .git/hooks/hook-exec

.git/hooks/hook-exec:
ifeq ($(wildcard $@),)
    cd $(REPO_GIT) && git clone [email protected]:DoneSpeak/gromithooks.git
    mv $(REPO_GIT)/hooks/* $(REPO_GIT)/gromithooks/git-hooks/multi-hooks/customized/
    rm -rf $(REPO_GIT)/hooks
    ln -s $(REPO_GIT)/gromithooks/git-hooks/multi-hooks $(REPO_GIT)/hooks
endif

update-git-hooks: install-git-hooks
    cd $(REPO_GIT)/gromithooks && git pull

Execute the following instructions to complete the configuration of the project.

# make update-git-hooks # make update-git-hooks # make update-git-hooks # make update-git-hooks

reference

  • This article source Donespeak/Gromithooks
  • 8.3 Custom git-git hook @git-scm