EBernhardson has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/114764

Change subject: share code between pre-commit and pre-review hooks
......................................................................

share code between pre-commit and pre-review hooks

Change-Id: I682be3fc8856fe9e885d9a1f6eeac4ed9a5f93fd
---
A scripts/hooks-shared.sh
M scripts/pre-commit
M scripts/pre-review
3 files changed, 51 insertions(+), 49 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Flow 
refs/changes/64/114764/1

diff --git a/scripts/hooks-shared.sh b/scripts/hooks-shared.sh
new file mode 100644
index 0000000..80db1ca
--- /dev/null
+++ b/scripts/hooks-shared.sh
@@ -0,0 +1,31 @@
+#
+# Shared functionality of the Flow git hooks
+#
+
+is_vagrant() {
+    DEST='.'
+    while [ "$(realpath $DEST)" != "/" ]; do
+        if [ -f $DEST/Vagrantfile ]; then
+            return 0;
+        fi
+        DEST="$DEST/.."
+    done
+    return 1
+}
+
+make() {
+    if is_vagrant; then
+        vagrant ssh -- cd /vagrant/mediawiki/extensions/Flow '&&' make $* || 
exit 1
+    else
+        /usr/bin/env make $* || exit 1
+    fi
+}
+
+file_changed_in_commit() {
+       git diff --name-only --cached | grep -P "$1" 2>&1 >/dev/null
+}
+
+file_changed_in_head() {
+       git diff-tree --no-commit-id --name-only -r HEAD | grep -P "$1" 2>&1 
>/dev/null
+}
+
diff --git a/scripts/pre-commit b/scripts/pre-commit
index 1d53688..94302a7 100755
--- a/scripts/pre-commit
+++ b/scripts/pre-commit
@@ -5,28 +5,12 @@
 # Code from https://gist.github.com/holysugar/1318698 , simpler than
 # http://stackoverflow.com/a/6262715/451712
 
-function whitespace_error() {
-  echo "'$1' has trailing whitespace\n" >&2
-}
-
-function is_vagrant() {
-       DEST='.'
-       while [ "$(realpath $DEST)" != "/" ]; do
-               if [ -f $DEST/Vagrantfile ]; then
-                       return 0;
-               fi
-               DEST="$DEST/.."
-       done
-       return 1
-}
-
-function make() {
-       if is_vagrant; then
-               vagrant ssh -- cd /vagrant/mediawiki/extensions/Flow '&&' make 
$* || exit 1
-       else
-               /usr/bin/env make $* || exit 1
-       fi
-}
+# Work out location of Flow/scripts/ directory
+dir=$(dirname $(php -r "echo realpath('$0'), \"\\n\";"))
+# Move to the project root
+cd $(dirname $dir)
+# Source the shared shell functions
+. $dir/hooks-shared.sh
 
 if [ "$IGNORE_WHITESPACE" != "1" ]; then
        # FIXME this reads the version of the file on-disk, which may not be 
the version
@@ -34,7 +18,7 @@
        git diff --cached --name-only | (while read f; do
          ERROR=0
          if grep -n '[[:space:]]$' "$f" ; then
-               whitespace_error $f
+               echo "'$f' has trailing whitespace\n" >&2
                ERROR=1
          fi
        done; exit $ERROR)
@@ -47,15 +31,15 @@
 
 COMMANDS=""
 
-if git diff --name-only --cached | grep -P '\.less$'; then
+if file_changed_in_commit '\.less$'; then
        COMMANDS="checkless $COMMANDS"
 fi
 
-if git diff --name-only --cached | grep -P '\.js$'; then
+if file_changed_in_commit '\.js$'; then
        COMMANDS="jshint $COMMANDS"
 fi
 
-if git diff --name-only --cached | grep -P '\.php$'; then
+if file_changed_in_commit '\.php$'; then
        COMMANDS="phplint $COMMANDS"
 fi
 
diff --git a/scripts/pre-review b/scripts/pre-review
index beb8c59..bc37f9e 100755
--- a/scripts/pre-review
+++ b/scripts/pre-review
@@ -1,34 +1,21 @@
 #!/bin/sh
 
-# TODO: share is_vagrant and make functions between scripts
-is_vagrant() {
-        DEST='.'
-        while [ "$(realpath $DEST)" != "/" ]; do
-                if [ -f $DEST/Vagrantfile ]; then
-                        return 0;
-                fi
-                DEST="$DEST/.."
-        done
-        return 1
-}
+# Work out location of Flow/scripts/ directory
+dir=$(dirname $(php -r "echo realpath('$0'), \"\\n\";"))
+# Move to the project root
+cd $(dirname $dir)
+# Source the shared shell functions
+. $dir/hooks-shared.sh
 
-make() {
-        if is_vagrant; then
-                vagrant ssh -- cd /vagrant/mediawiki/extensions/Flow '&&' make 
$* || exit 1
-        else
-                /usr/bin/env make $* || exit 1
-        fi
-}
-
-# only checks top commit for changes
-
-if git diff-tree --no-commit-id --name-only -r HEAD | grep -P '\.php$'; then
+# only checks top commit for changes. havn't figured out how to get
+# git-review to tell us which commits are being submitted
+if file_changed_in_head '\.php$'; then
        # bit of a hack ... other things run inside vagrant but phpstorm is 
probably
        # installed external to vagrant
        /usr/bin/env make analyze-phpstorm
 fi
 
-if git diff-tree --no-commit-id --name-only -r HEAD | grep -P '\.i18n\.php$'; 
then
+if file_changed_in_head '\.i18n\.php$'; then
        COMMANDS="check-i18n $COMMANDS"
 fi
 

-- 
To view, visit https://gerrit.wikimedia.org/r/114764
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I682be3fc8856fe9e885d9a1f6eeac4ed9a5f93fd
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Flow
Gerrit-Branch: master
Gerrit-Owner: EBernhardson <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to