<commit_before>#!/usr/bin/env bash
# Update local git repositories
# Command run in VM by `vagrant git-update`

. /etc/profile.d/set_MW_INSTALL_PATH.sh

set -u
declare -a UPDATE_ERRORS

UPDATE_ERRORS=( )

function pull {
  git_cmd=git
  # /srv dirs are all root, so use sudo in that case
  [[ -O . ]] || git_cmd="sudo git"
  echo -e "\e[36m==> Updating $(pwd) ...\e[0m"
  branch=$(expr $($git_cmd symbolic-ref HEAD) : 'refs/heads/\(.*\)')
  if [[ -z "$branch" ]]; then
    $git_cmd checkout master
    branch=$(expr $($git_cmd symbolic-ref HEAD) : 'refs/heads/\(.*\)')
  fi
  err=''
  if [[ -n "$branch" ]]; then
    remote=$($git_cmd config branch.${branch}.remote)
    if [[ -n "$remote" ]]; then
        url=$($git_cmd config --get remote.${remote}.url)
        if [[ $url == ssh://* ]]; then
            # Convert remote git url from ssh:// to anonymous https://
            tempurl=$(echo $url | sed -e 's!ssh://[^@]\+@!https://!g' -e 's!:29418!/r!g')
            $git_cmd pull --rebase $tempurl
        else
            $git_cmd pull --rebase
        fi
        if [[ $? -ne 0 ]]; then
          # If we didn't successfully update (possibly because we're on
          # a local branch), leave the submodules alone.
          err="GIT PULL failed in $(pwd) for branch '$branch'"
        else
          $git_cmd submodule update --init --recursive
          if [[ $? -ne 0 ]]; then
            err="GIT SUBMODULE UPDATE failed in $(pwd) for branch '$branch'"
          fi
        fi

    else
        err="Remote not found in $(pwd) for branch '$branch'"
    fi
  else
    err="Unable to get current branch in $(pwd)"
  fi

  if [[ -n "$err" ]]; then
    echo -e "\e[1;31m ***** $err\e[0m"
    UPDATE_ERRORS+=("$err")
  fi
  echo
}

for srvdir in $(find <%= scope['::mwv::services_dir'] %> -maxdepth 2 -type d -name .git -printf "%h\n"); do
  cd "${srvdir}"
  pull
  if ! git ls-files node_modules --error-unmatch > /dev/null 2>&1 && [[ -f package.json ]]; then
    npm update
  fi
done

cd "$MW_INSTALL_PATH"
pull

for f in extensions/*; do
  if [[ -d $f ]] && [[ -d $f/.git ]]; then
    pushd $f > /dev/null
    pull
    popd > /dev/null
  fi
done

for f in skins/*; do
  if [[ -d $f ]] && [[ -d $f/.git ]]; then
    pushd $f > /dev/null
    pull
    popd > /dev/null
  fi
done

for f in . extensions/*; do
  if [[ -d $f ]] && [[ -f $f/composer.json ]]; then
    pushd $f > /dev/null
    echo -e "\e[36m==> Updating composer dependencies in $(pwd) ...\e[0m"
    composer update --no-interaction --optimize-autoloader
    popd > /dev/null
    echo
  fi
done

echo -e "\e[36m==> Updating database ...\e[0m"
foreachwiki update.php --quick --doshared

if [[ ${#UPDATE_ERRORS[@]} -ne 0 ]]; then
  echo
  echo -e "\e[1;31m ************* Errors ***********\e[0m"
  printf -- '* %s\n' "${UPDATE_ERRORS[@]}"<commit_msg>Use composer install if composer.lock is versioned<commit_after>#!/usr/bin/env bash
# Update local git repositories
# Command run in VM by `vagrant git-update`

. /etc/profile.d/set_MW_INSTALL_PATH.sh

set -u
declare -a UPDATE_ERRORS

UPDATE_ERRORS=( )

function pull {
  git_cmd=git
  # /srv dirs are all root, so use sudo in that case
  [[ -O . ]] || git_cmd="sudo git"
  echo -e "\e[36m==> Updating $(pwd) ...\e[0m"
  branch=$(expr $($git_cmd symbolic-ref HEAD) : 'refs/heads/\(.*\)')
  if [[ -z "$branch" ]]; then
    $git_cmd checkout master
    branch=$(expr $($git_cmd symbolic-ref HEAD) : 'refs/heads/\(.*\)')
  fi
  err=''
  if [[ -n "$branch" ]]; then
    remote=$($git_cmd config branch.${branch}.remote)
    if [[ -n "$remote" ]]; then
        url=$($git_cmd config --get remote.${remote}.url)
        if [[ $url == ssh://* ]]; then
            # Convert remote git url from ssh:// to anonymous https://
            tempurl=$(echo $url | sed -e 's!ssh://[^@]\+@!https://!g' -e 's!:29418!/r!g')
            $git_cmd pull --rebase $tempurl
        else
            $git_cmd pull --rebase
        fi
        if [[ $? -ne 0 ]]; then
          # If we didn't successfully update (possibly because we're on
          # a local branch), leave the submodules alone.
          err="GIT PULL failed in $(pwd) for branch '$branch'"
        else
          $git_cmd submodule update --init --recursive
          if [[ $? -ne 0 ]]; then
            err="GIT SUBMODULE UPDATE failed in $(pwd) for branch '$branch'"
          fi
        fi

    else
        err="Remote not found in $(pwd) for branch '$branch'"
    fi
  else
    err="Unable to get current branch in $(pwd)"
  fi

  if [[ -n "$err" ]]; then
    echo -e "\e[1;31m ***** $err\e[0m"
    UPDATE_ERRORS+=("$err")
  fi
  echo
}

for srvdir in $(find <%= scope['::mwv::services_dir'] %> -maxdepth 2 -type d -name .git -printf "%h\n"); do
  cd "${srvdir}"
  pull
  if ! git ls-files node_modules --error-unmatch > /dev/null 2>&1 && [[ -f package.json ]]; then
    npm update
  fi
done

cd "$MW_INSTALL_PATH"
pull

for f in extensions/*; do
  if [[ -d $f ]] && [[ -d $f/.git ]]; then
    pushd $f > /dev/null
    pull
    popd > /dev/null
  fi
done

for f in skins/*; do
  if [[ -d $f ]] && [[ -d $f/.git ]]; then
    pushd $f > /dev/null
    pull
    popd > /dev/null
  fi
done

for f in . extensions/*; do
  if [[ -d $f ]] && [[ -f $f/composer.json ]]; then
    pushd $f > /dev/null
    if git ls-files --error-unmatch composer.lock >/dev/null 2>&1; then
      # composer.lock is git-versioned, so rely on it
      composer_cmd=install
      composer_action=Installing
    else
      composer_cmd=update
      composer_action=Updating
    fi

    echo -e "\e[36m==> ${composer_action} composer dependencies in $(pwd) ...\e[0m"
    composer "$composer_cmd" --no-interaction --optimize-autoloader
    popd > /dev/null
    echo
  fi
done

echo -e "\e[36m==> Updating database ...\e[0m"
foreachwiki update.php --quick --doshared

if [[ ${#UPDATE_ERRORS[@]} -ne 0 ]]; then
  echo
  echo -e "\e[1;31m ************* Errors ***********\e[0m"
  printf -- '* %s\n' "${UPDATE_ERRORS[@]}"
{'fraction_non_alphanumeric': 0.18459986550100874, 'fraction_numerical': 0.009751176866173503, 'mean_word_length': 2.305, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1212400', 'n_tokens_mistral': 2255, 'n_tokens_neox': 2141, 'n_words': 652}