oojs-update.sh: Replace target dir instead of adding file copies
authorTimo Tijhof <krinklemail@gmail.com>
Sat, 19 Apr 2014 00:48:59 +0000 (02:48 +0200)
committerKrinkle <krinklemail@gmail.com>
Sat, 19 Apr 2014 01:12:59 +0000 (01:12 +0000)
* A command like "cp dist/* $TARGET_DIR" does not remove files
  that were deleted in the release dist. Using rsync instead.

  Though addition can happen, deletion should never happen as we
  refer to these files in Resources.php. But we also shouldn't
  leave behind old files mixed with new files, so delete them.
  Our structure unit tests for resources take care of catching
  references to files that don't (or no longer) exist.

* Use bash -n instead of != "".

* Refactor slightly to make it re-usable in other projects without
  too much modification (only REPO_DIR and TARGET_DIR need updating,
  the rest is generic now).

* The "git commit <dir>" doesn't properly take care of staging
  deletion of files removed by the update script, nor does it
  reliably (?) stage new files.
  Instead using a combination of "git add -u <dir>" (The -u
  stages deletion and modifcation of tracked files), and
  "git add <dir>" (stages modification and creation of files in
  that directory).

Change-Id: Ieb20978c887c5a141f31cab704b8d239f1572af0

maintenance/resources/update-oojs.sh

index f26350f..2c50002 100755 (executable)
@@ -1,23 +1,24 @@
 #!/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
@@ -32,14 +33,14 @@ then
 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
 
@@ -48,4 +49,5 @@ Release notes:
 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