#!/usr/bin/env bash
-if [ "$2" != "" ]
+if [ -n "$2" ]
then
+ # Too many parameters
echo >&2 "Usage: $0 [<version>]"
exit 1
fi
-MW_DIR=$(cd $(dirname $0)/../..; pwd) # e.g. mediawiki-core/
-NPM_DIR=`mktemp -d 2>/dev/null || mktemp -d -t 'mw-update-oojs'` # e.g. /tmp/mw-update-oojs.rI0I5Vir
-
-# Prepare MediaWiki working copy
-cd $MW_DIR
-git reset resources/lib/oojs/ && git checkout resources/lib/oojs/ && git fetch origin || exit 1
+REPO_DIR=$(cd $(dirname $0)/../..; pwd) # Root dir of the git repo working tree
+TARGET_DIR=resources/lib/oojs # Destination relative to the root of the repo
+NPM_DIR=`mktemp -d 2>/dev/null || mktemp -d -t 'update-oojs'` # e.g. /tmp/update-oojs.rI0I5Vir
+# Prepare working tree
+cd $REPO_DIR &&
+git reset $TARGET_DIR && git checkout $TARGET_DIR && git fetch origin &&
git checkout -B upstream-oojs origin/master || exit 1
# Fetch upstream version
cd $NPM_DIR
-if [ "$1" != "" ]
+if [ -n "$1" ]
then
npm install oojs@$1 || exit 1
else
fi
# Copy file(s)
-mv ./node_modules/oojs/dist/* $MW_DIR/resources/lib/oojs/ || exit 1
-
-# Generate commit
-cd $MW_DIR || exit 1
+rsync --recursive --delete --force ./node_modules/oojs/dist $REPO_DIR/$TARGET_DIR || exit 1
# Clean up temporary area
rm -rf $NPM_DIR
+# Generate commit
+cd $REPO_DIR || exit 1
+
COMMITMSG=$(cat <<END
Update OOjs to v$OOJS_VERSION
END
)
-git commit resources/lib/oojs/ -m "$COMMITMSG" || exit 1
+# Stage deletion, modification and creation of files. Then commit.
+git add --update $TARGET_DIR && git add $TARGET_DIR && git commit -m "$COMMITMSG" || exit 1