post-checkout-merge.sh

Script, pour GIT, d'interception d'événements "post-merge" et "post-checkout" qui devrait exécuter :

"composer.phar install"
si composer.lock a été modifié ;
"npm install"
si package-lock.json a été modifié,

ou, s'il ne s'agit pas d'un "rebase" :

"composer.phar update"
si composer.json a été modifié ;
"npm update"
si package.json a été modifié.
  1. #!/bin/sh
  2.  
  3. REBASE_FLAG=.git/post-checkout-rebase-flag
  4.  
  5. case $(basename $0) in
  6. "post-checkout")
  7. GIT_COMMAND="diff"
  8. GIT_PARAMETERS="--name-only $1..$2"
  9. ;;
  10.  
  11. "post-merge")
  12. GIT_COMMAND="diff-tree"
  13. GIT_PARAMETERS="--name-only --no-commit-id ORIG_HEAD HEAD"
  14. ;;
  15.  
  16. "pre-rebase")
  17. [ -f $REBASE_FLAG ] || touch $REBASE_FLAG
  18. exit;
  19. ;;
  20.  
  21. "post-rewrite")
  22. [ -f $REBASE_FLAG ] && rm $REBASE_FLAG
  23.  
  24. FIRST_NEW_COMMIT=
  25. LAST_NEW_COMMIT=
  26.  
  27. while read line
  28. do
  29. LAST_NEW_COMMIT=$(echo $line | awk '{ print $2 }')
  30. if [ "x$FIRST_NEW_COMMIT" = "x" ];
  31. then
  32. FIRST_NEW_COMMIT=$LAST_NEW_COMMIT
  33. fi
  34. done
  35.  
  36. GIT_COMMAND="diff"
  37. GIT_PARAMETERS="--name-only $FIRST_NEW_COMMIT..$LAST_NEW_COMMIT"
  38. ;;
  39.  
  40. *)
  41. echo Run the following commands in order to install $(readlink $0) :
  42. echo cp $0 .git/hooks
  43. echo cd .git/hooks
  44. echo chmod +x $(basename $(readlink $0))
  45. echo ln -s $(basename $(readlink $0)) post-checkout
  46. echo ln -s $(basename $(readlink $0)) post-merge
  47. echo ln -s $(basename $(readlink $0)) post-rewrite
  48. echo ln -s $(basename $(readlink $0)) pre-rebase
  49. exit;
  50. ;;
  51. esac
  52.  
  53. if [ -f $REBASE_FLAG ];
  54. then
  55. rm $REBASE_FLAG;
  56. exit;
  57. fi
  58.  
  59. CHANGED_COMPOSER_FILES=$(git $GIT_COMMAND $GIT_PARAMETERS | \
  60. grep '^composer\.\(json\|lock\)$')
  61.  
  62. if [ -n "$CHANGED_COMPOSER_FILES" ];
  63. then
  64. if [ "x$COMPOSER_PHAR" = "x" ];
  65. then
  66. for P in $HOME/bin $HOME/Library $(echo $PATH | sed 's/:/ /g')
  67. do
  68. if [ -d $P -a -f $P/composer.phar ];
  69. then
  70. COMPOSER_PHAR=$P/composer.phar
  71. fi
  72. done
  73. fi
  74.  
  75. if [ "x$COMPOSER_PHAR" != "x" ];
  76. then
  77. if [ -x $COMPOSER_PHAR ];
  78. then
  79. COMPOSER=$COMPOSER_PHAR
  80. else
  81. COMPOSER="php $COMPOSER_PHAR"
  82. fi
  83. fi
  84.  
  85. echo $CHANGED_COMPOSER_FILES | grep lock > /dev/null \
  86. && $COMPOSER install \
  87. || $COMPOSER update
  88. fi
  89.  
  90. CHANGED_NODEJS_FILES=$(git $GIT_COMMAND $GIT_PARAMETERS | \
  91. grep '^package\(-lock\)\?\.json$')
  92.  
  93. if [ -n "$CHANGED_NODEJS_FILES" ];
  94. then
  95. echo $CHANGED_NODEJS_FILES | grep lock > /dev/null \
  96. && npm install \
  97. || npm update
  98. fi