|
| 1 | +#!/bin/sh |
| 2 | +# This `post-merge` hook watches the changed files after merge |
| 3 | +# and can execute external command if files matched pattern were changed. |
| 4 | +# Example of config: |
| 5 | +# ~~~ |
| 6 | +# [files-watcher] |
| 7 | +# watch-branches = master |
| 8 | +# mode = "auto" |
| 9 | +# |
| 10 | +# [files-watcher "composer"] |
| 11 | +# command = "composer install" |
| 12 | +# |
| 13 | +# [files-watcher ".php$"] |
| 14 | +# command = "php-cs-fixer --dry-run" |
| 15 | +# mode = "prompt" |
| 16 | +# |
| 17 | +# [files-watcher ".php$"] |
| 18 | +# command = "phpunit" |
| 19 | +# mode = "confirm" |
| 20 | +# ~~~ |
| 21 | + |
| 22 | +pwd |
| 23 | + |
| 24 | +watch_branches=$(git config --default master --get files-watcher.watch-branches) |
| 25 | +default_mode=$(git config --default prompt --get files-watcher.mode) |
| 26 | + |
| 27 | +branch_name=$(git rev-parse --abbrev-ref HEAD) |
| 28 | + |
| 29 | +if [[ ! "${branch_name}" =~ "${watch_branches}" ]]; then |
| 30 | + exit |
| 31 | +fi |
| 32 | + |
| 33 | +watch_tasks_config_pattern=files-watcher\..\*\.command |
| 34 | +watch_tasks=$(git config --name-only --get-regexp ${watch_tasks_config_pattern}) |
| 35 | + |
| 36 | +if [[ -z ${watch_tasks} ]]; then |
| 37 | + exit |
| 38 | +fi |
| 39 | + |
| 40 | +# Read user input, assign stdin to keyboard |
| 41 | +exec < /dev/tty |
| 42 | + |
| 43 | +FCR='\033[1;31m' # Red |
| 44 | +FCG='\033[1;32m' # Green |
| 45 | +FCY='\033[0;33m' # Yellow |
| 46 | +FCS='\033[0;37m' # Light gray (silver) |
| 47 | +NC='\033[0m' |
| 48 | + |
| 49 | +function prompt() { |
| 50 | + echo "$@ [y/N] \c"; read answer |
| 51 | + case "${answer}" in |
| 52 | + y*|Y*) return 0 ;; |
| 53 | + *) return 1 ;; |
| 54 | + esac |
| 55 | +} |
| 56 | + |
| 57 | +function confirm() { |
| 58 | + echo "$@ [Y/n] \c"; read answer |
| 59 | + case "${answer}" in |
| 60 | + n*|N*) return 1 ;; |
| 61 | + *) return 0 ;; |
| 62 | + esac |
| 63 | +} |
| 64 | + |
| 65 | +report_start() { |
| 66 | + echo "${FCG}$@${NC}" |
| 67 | +} |
| 68 | + |
| 69 | +report_done() { |
| 70 | + [[ $# > 0 ]] && echo "${FCG}✓ $@${NC}" || echo "${FCG}✓ Done${NC}" |
| 71 | +} |
| 72 | + |
| 73 | +report_start "Looking for triggers..." |
| 74 | + |
| 75 | +for task in ${watch_tasks}; do |
| 76 | + if [[ ! "$task" =~ ${watch_tasks_config_pattern} ]]; then |
| 77 | + continue |
| 78 | + fi |
| 79 | + |
| 80 | + pattern=${task#*.} # remove prefix ending in "." |
| 81 | + pattern=${pattern%.*} # remove suffix starting with "." |
| 82 | + echo "Checking '${pattern}' files..." |
| 83 | + |
| 84 | + changed_files=$(git diff HEAD@\{1\} --name-only --diff-filter=ACMRTUXB | grep -iE ${pattern}) |
| 85 | + |
| 86 | + command=$(git config --type path --get ${task}) |
| 87 | + mode=$(git config --default ${default_mode} --get ${task%.*}.mode) |
| 88 | + |
| 89 | + if [[ -n ${changed_files} ]]; then |
| 90 | + echo "Found changed files:" |
| 91 | + |
| 92 | + for file in ${changed_files} ; do |
| 93 | + echo "- ${FCS}${file}${NC}" |
| 94 | + done |
| 95 | + |
| 96 | + case "${mode}" in |
| 97 | + "auto" ) |
| 98 | + command ${command} |
| 99 | + ;; |
| 100 | + "prompt" ) |
| 101 | + if prompt "Do you like to run '${command}'?"; then |
| 102 | + command ${command} |
| 103 | + fi |
| 104 | + ;; |
| 105 | + "confirm" ) |
| 106 | + if confirm "Do you like to run '${command}'?"; then |
| 107 | + command ${command} |
| 108 | + fi |
| 109 | + ;; |
| 110 | + * ) |
| 111 | + echo "${FCR}Invalid mode option${NC}" |
| 112 | + esac |
| 113 | + fi |
| 114 | +done |
| 115 | + |
| 116 | +report_done |
| 117 | + |
| 118 | +exec <&- |
0 commit comments