<commit_before>#!/bin/sh

#######################################
# Echo/log function
# Arguments:
#   String: value to log
#######################################
log() {
  if [[ "$@" ]]; then echo "[${T3APP_NAME^^}] $@";
  else echo; fi
}

#########################################################
# Check in the loop (every 2s) if the database backend
# service is already available.
# Globals:
#   T3APP_DB_HOST: db hostname
#   T3APP_DB_PORT: db port number
#   T3APP_DB_USER: db username
#   MYSQL_CMD_PARAMS
#########################################################
function wait_for_db() {
  set +e
  local res=1
  while [[ $res -ne 0 ]]; do
    mysql $MYSQL_CMD_PARAMS --execute "status" 1>/dev/null
    res=$?
    if [[ $res -ne 0 ]]; then log "Waiting for DB service ($T3APP_DB_HOST:$T3APP_DB_PORT username:$T3APP_DB_USER)..." && sleep 2; fi
  done
  set -e
  
  # Display DB status...
  log "Database status:"
  mysql $MYSQL_CMD_PARAMS --execute "status"
}

#########################################################
# Moves pre-installed in /tmp TYPO3 to its
# target location ($APP_ROOT), if it's not there yet
# Globals:
#   WEB_SERVER_ROOT
#   APP_ROOT
#   T3APP_NAME
#   T3APP_BUILD_BRANCH
#########################################################
function install_typo3_app() {
  # Check if app is already installed (when restaring stopped container)
  if [ ! -d $APP_ROOT ]; then
    if [ "${T3APP_PREINSTALL^^}" = TRUE ]; then
      log "Installing TYPO3 app (from pre-installed archive)..."
      cd $WEB_SERVER_ROOT && tar -zxf /tmp/$INSTALLED_PACKAGE_NAME.tgz
      mv $INSTALLED_PACKAGE_NAME $T3APP_NAME
    else
      log "Installing TYPO3 app..."
      cd $WEB_SERVER_ROOT
      # Clone TYPO3 app code from provided repository
      git clone $T3APP_BUILD_REPO_URL $T3APP_NAME
      cd $T3APP_NAME
      # Do composer install
      git checkout $T3APP_BUILD_BRANCH
      git log -10 --pretty=format:"%h %an %cr: %s" --graph
      COMPOSER_PROCESS_TIMEOUT=900 composer install $T3APP_BUILD_COMPOSER_PARAMS
      echo
      echo "TYPO3 app from $T3APP_BUILD_REPO_URL ($T3APP_BUILD_BRANCH) installed."
      echo $(ls -lh $CWD)
      echo
    fi
  fi

  cd $APP_ROOT
  
  # Make sure cache is cleared for all contexts. This is empty during the 1st container launch,
  # but, when container is re-run (with shared data volume), not clearing it can cause random issues
  # (e.g. due to changes in the newly pulled code).
  rm -rf rm -rf Data/Temporary/*
  
  # Allow switching between branches for running containers
  # E.g. user can provide different branch for `docker build` (in Dockerfile)
  # and different when launching the container.
  # Note: it's done with --force param, so local conflicting changes will be thrown away. But we assume sb who is doing this knows about it.
  git fetch && git checkout --force $T3APP_BUILD_BRANCH
  
  # Debug: show most recent git log messages
  log "TYPO3 app installed. Most recent commits:"
  git log -5 --pretty=format:"%h %an %cr: %s" --graph && echo # Show most recent changes
  
  # If app is/was already installed, pull the most recent code
  if [ "${T3APP_ALWAYS_DO_PULL^^}" = TRUE ]; then
    install_typo3_app_do_pull
  fi
  
  # If composer.lock has changed, this will re-install things...
  composer install $T3APP_BUILD_COMPOSER_PARAMS
}

#########################################################
# Pull the newest codebase from the remote repository.
# It tries to handle the situation even when they are
# conflicting changes.
#
# Called when T3APP_ALWAYS_DO_PULL is set to TRUE.
#
# Globals:
#   WEB_SERVER_ROOT
#   APP_ROOT
#   T3APP_NAME
#########################################################
function install_typo3_app_do_pull() {
  set +e # allow non-zero command results (git pull might fail due to code conflicts)
  log "Pulling the newest codebase (due to T3APP_ALWAYS_DO_PULL set to TRUE)..."

  if [[ ! $(git status | grep "working directory clean") ]]; then
    log "There are some changes in the current working directory. Stashing..."
    git status
    git stash --include-untracked
  fi<commit_msg>Improved switching between branches using T3APP_BUILD_BRANCH<commit_after>#!/bin/sh

#######################################
# Echo/log function
# Arguments:
#   String: value to log
#######################################
log() {
  if [[ "$@" ]]; then echo "[${T3APP_NAME^^}] $@";
  else echo; fi
}

#########################################################
# Check in the loop (every 2s) if the database backend
# service is already available.
# Globals:
#   T3APP_DB_HOST: db hostname
#   T3APP_DB_PORT: db port number
#   T3APP_DB_USER: db username
#   MYSQL_CMD_PARAMS
#########################################################
function wait_for_db() {
  set +e
  local res=1
  while [[ $res -ne 0 ]]; do
    mysql $MYSQL_CMD_PARAMS --execute "status" 1>/dev/null
    res=$?
    if [[ $res -ne 0 ]]; then log "Waiting for DB service ($T3APP_DB_HOST:$T3APP_DB_PORT username:$T3APP_DB_USER)..." && sleep 2; fi
  done
  set -e
  
  # Display DB status...
  log "Database status:"
  mysql $MYSQL_CMD_PARAMS --execute "status"
}

#########################################################
# Moves pre-installed in /tmp TYPO3 to its
# target location ($APP_ROOT), if it's not there yet
# Globals:
#   WEB_SERVER_ROOT
#   APP_ROOT
#   T3APP_NAME
#   T3APP_BUILD_BRANCH
#########################################################
function install_typo3_app() {
  # Check if app is already installed (when restaring stopped container)
  if [ ! -d $APP_ROOT ]; then
    if [ "${T3APP_PREINSTALL^^}" = TRUE ]; then
      log "Installing TYPO3 app (from pre-installed archive)..."
      cd $WEB_SERVER_ROOT && tar -zxf /tmp/$INSTALLED_PACKAGE_NAME.tgz
      mv $INSTALLED_PACKAGE_NAME $T3APP_NAME
    else
      log "Installing TYPO3 app..."
      cd $WEB_SERVER_ROOT
      # Clone TYPO3 app code from provided repository
      git clone $T3APP_BUILD_REPO_URL $T3APP_NAME
      cd $T3APP_NAME
      # Do composer install
      git checkout $T3APP_BUILD_BRANCH
      git log -10 --pretty=format:"%h %an %cr: %s" --graph
      COMPOSER_PROCESS_TIMEOUT=900 composer install $T3APP_BUILD_COMPOSER_PARAMS
      echo
      echo "TYPO3 app from $T3APP_BUILD_REPO_URL ($T3APP_BUILD_BRANCH) installed."
      echo $(ls -lh $CWD)
      echo
    fi
  fi

  cd $APP_ROOT
  
  # Make sure cache is cleared for all contexts. This is empty during the 1st container launch,
  # but, when container is re-run (with shared data volume), not clearing it can cause random issues
  # (e.g. due to changes in the newly pulled code).
  rm -rf rm -rf Data/Temporary/*
  
  # Debug: show most recent git log messages
  log "TYPO3 app installed. Most recent commits:"
  git log -5 --pretty=format:"%h %an %cr: %s" --graph && echo # Show most recent changes
  
  # If app is/was already installed, pull the most recent code
  if [ "${T3APP_ALWAYS_DO_PULL^^}" = TRUE ]; then
    install_typo3_app_do_pull
  fi
  
  # If composer.lock has changed, this will re-install things...
  composer install $T3APP_BUILD_COMPOSER_PARAMS
}

#########################################################
# Pull the newest codebase from the remote repository.
# It tries to handle the situation even when they are
# conflicting changes.
#
# Called when T3APP_ALWAYS_DO_PULL is set to TRUE.
#
# Globals:
#   WEB_SERVER_ROOT
#   APP_ROOT
#   T3APP_NAME
#########################################################
function install_typo3_app_do_pull() {
  set +e # allow non-zero command results (git pull might fail due to code conflicts)
  log "Pulling the newest codebase (due to T3APP_ALWAYS_DO_PULL set to TRUE)..."

  if [[ ! $(git status | grep "working directory clean") ]]; then
    log "There are some changes in the current working directory. Stashing..."
    git status
    git stash --include-untracked
  fi
  
  # Allow switching between branches for running containers
  # E.g. user can provide different branch for `docker build` (in Dockerfile)
  # and different when launching the container.
  git fetch && git checkout --force $T3APP_BUILD_BRANCH
{'fraction_non_alphanumeric': 0.188688565077797, 'fraction_numerical': 0.011484317115337121, 'mean_word_length': 3.8093824228028503, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '6810764', 'n_tokens_mistral': 2604, 'n_tokens_neox': 2508, 'n_words': 849}