<commit_before>#!/bin/bash -Ee
# [cyber-dojo] start-point build cyberdojo/languages-start-points --languages <tag-url>...

shift                                # start-point
shift                                # build
readonly IMAGE_NAME="${1}"           # cyberdojo/languages-start-points
readonly IMAGE_TYPE="${2}"           # --languages
declare -ar GIT_REPO_URLS="(${@:3})" # <tag-url>...

# In Docker Toolbox /tmp cannot be docker volume-mounted, so ~/tmp
readonly CONTEXT_DIR=$(mktemp -d ~/tmp.cyber-dojo.commander.start-point.build.context-dir.XXX)
remove_tmp_dir()
{
  rm -rf "${CONTEXT_DIR}" > /dev/null
}
trap remove_tmp_dir EXIT

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# cyber-dojo start-point build <name> --languages <tag-url>...
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
show_use()
{
  local -r MY_NAME=cyber-dojo
  cat <<- EOF

  Use:
  ${MY_NAME} start-point build <name> --languages <tag-url>...

  Builds a cyber-dojo start-point image named <name>
  containing git clones of the specified git-repo <url>s.
  Its base image will be CYBER_DOJO_START_POINTS_BASE_IMAGE:CYBER_DOJO_START_POINTS_BASE_TAG
  <tag-url> is a git-repo url with an optional 7-character tag prefix. Eg
    https://github.com/cyber-dojo-start-points/gcc-assert
    7686e9d@https://github.com/cyber-dojo-start-points/gcc-assert

  Example 1: non local <tag-url>

  ${MY_NAME} start-point build \\
        eg/first \\
          --languages \\
            800f67d@https://github.com/.../java-junit

  Example 2: read git-repo <tag-url>s from a local file

  ${MY_NAME} start-point build \\
        eg/second \\
          --languages \\
            \$(< my-language-selection.txt)

  cat my-language-selection.txt
  800f67d@https://github.com/.../java-junit
  055826a@https://github.com/.../javascript-jasmine
  fcacae3@https://github.com/.../python-pytest
  3d7b8ea@https://github.com/.../ruby-minitest

  Example 3: read git-repo <tag-url>s from a curl'd file

  ${MY_NAME} start-point build \\
        eg/third \\
          --languages \\
            \$(curl --silent https://raw.githubusercontent.com/.../start-points/git_repo_urls.all.tagged)

EOF
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
exit_zero_if_show_use()
{
  if [ -z "${1}" ] || [ "${1}" = '-h' ] || [ "${1}" = '--help' ]; then
    show_use
    exit 0
  fi
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
exit_non_zero_unless_docker_installed()
{
  if ! hash docker 2> /dev/null; then
    stderr 'ERROR: docker is not installed!'
    exit 42
  fi
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
exit_non_zero_unless_git_installed()
{
  if ! hash git 2> /dev/null; then
    stderr 'ERROR: git is not installed!'
    exit 42
  fi
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
exit_non_zero_if_bad_args()
{
  local -r args="${@:1}"
  set +e
  docker container run --rm $(base_image_name) \
    /app/src/from_script/bad_args.rb ${args}
  local -r status=$?
  set -e
  if [ "${status}" != '0' ]; then
    exit "${status}"
  fi
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
git_clone_tagged_urls_into_context_dir()
{
  # Two or more git-repo-urls could have the same repo name
  # but be from different repositories.
  # So git clone each repo into its own unique directory
  # based on a simple incrementing index.
  for i in "${!GIT_REPO_URLS[@]}"; do
    git_clone_one_tagged_url_into_context_dir "${GIT_REPO_URLS[$i]}" "${i}"
  done
  echo -e "$(image_type)" > "${CONTEXT_DIR}/image.type"
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
git_clone_one_tagged_url_into_context_dir()
{
  # git-clone directly, from this script, into the
  # context dir before running [docker image build].
  # Run [git clone] on the _host_ rather than wherever
  # the docker daemon is (via a command in the Dockerfile).
  local output
  local -r tagged_url="${1}"       # bbd75a1@https://github.com/cyber-dojo-languages/gcc-assert
  local -r url_index="${2}"        # 0
  local -r tag="${tagged_url:0:7}" # bbd75a1
  local -r url="${tagged_url:8}"   # https://github.com/cyber-dojo-languages/gcc-assert

  cd "${CONTEXT_DIR}"
  echo "Git cloning ${url}"
  if ! output="$(git clone --single-branch --branch master "${url}" "${url_index}" 2>&1)"
  then
    stderr "ERROR: git clone ... ${url}"
    stderr "${IMAGE_TYPE}"
    stderr "${output}"
    exit 3
  fi

  cd "${CONTEXT_DIR}/${url_index}"
  if ! output=$(git checkout "${tag}" 2>&1)
  then
    stderr "ERROR: git checkout ${tag}"
    stderr "${IMAGE_TYPE}"
    stderr "${output}"
    exit 3
  fi

  local -r sha="$(git rev-parse HEAD)"
  echo -e "${IMAGE_TYPE} \t ${url}"
  echo -e "${url_index} \t ${sha} \t ${url}" >> "${CONTEXT_DIR}/shas.txt"
  rm -rf "${CONTEXT_DIR}/${url_index}/.git"
  rm -rf "${CONTEXT_DIR}/${url_index}/docker"
}
<commit_msg>Add [git tag] echo for [cyber-dojo start-point build] command<commit_after>#!/bin/bash -Ee
# [cyber-dojo] start-point build cyberdojo/languages-start-points --languages <tag-url>...

shift                                # start-point
shift                                # build
readonly IMAGE_NAME="${1}"           # cyberdojo/languages-start-points
readonly IMAGE_TYPE="${2}"           # --languages
declare -ar GIT_REPO_URLS="(${@:3})" # <tag-url>...

# In Docker Toolbox /tmp cannot be docker volume-mounted, so ~/tmp
readonly CONTEXT_DIR=$(mktemp -d ~/tmp.cyber-dojo.commander.start-point.build.context-dir.XXX)
remove_tmp_dir()
{
  rm -rf "${CONTEXT_DIR}" > /dev/null
}
trap remove_tmp_dir EXIT

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# cyber-dojo start-point build <name> --languages <tag-url>...
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
show_use()
{
  local -r MY_NAME=cyber-dojo
  cat <<- EOF

  Use:
  ${MY_NAME} start-point build <name> --languages <tag-url>...

  Builds a cyber-dojo start-point image named <name>
  containing git clones of the specified git-repo <url>s.
  Its base image will be CYBER_DOJO_START_POINTS_BASE_IMAGE:CYBER_DOJO_START_POINTS_BASE_TAG
  <tag-url> is a git-repo url with an optional 7-character tag prefix. Eg
    https://github.com/cyber-dojo-start-points/gcc-assert
    7686e9d@https://github.com/cyber-dojo-start-points/gcc-assert

  Example 1: non local <tag-url>

  ${MY_NAME} start-point build \\
        eg/first \\
          --languages \\
            800f67d@https://github.com/.../java-junit

  Example 2: read git-repo <tag-url>s from a local file

  ${MY_NAME} start-point build \\
        eg/second \\
          --languages \\
            \$(< my-language-selection.txt)

  cat my-language-selection.txt
  800f67d@https://github.com/.../java-junit
  055826a@https://github.com/.../javascript-jasmine
  fcacae3@https://github.com/.../python-pytest
  3d7b8ea@https://github.com/.../ruby-minitest

  Example 3: read git-repo <tag-url>s from a curl'd file

  ${MY_NAME} start-point build \\
        eg/third \\
          --languages \\
            \$(curl --silent https://raw.githubusercontent.com/.../start-points/git_repo_urls.all.tagged)

EOF
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
exit_zero_if_show_use()
{
  if [ -z "${1}" ] || [ "${1}" = '-h' ] || [ "${1}" = '--help' ]; then
    show_use
    exit 0
  fi
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
exit_non_zero_unless_docker_installed()
{
  if ! hash docker 2> /dev/null; then
    stderr 'ERROR: docker is not installed!'
    exit 42
  fi
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
exit_non_zero_unless_git_installed()
{
  if ! hash git 2> /dev/null; then
    stderr 'ERROR: git is not installed!'
    exit 42
  fi
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
exit_non_zero_if_bad_args()
{
  local -r args="${@:1}"
  set +e
  docker container run --rm $(base_image_name) \
    /app/src/from_script/bad_args.rb ${args}
  local -r status=$?
  set -e
  if [ "${status}" != '0' ]; then
    exit "${status}"
  fi
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
git_clone_tagged_urls_into_context_dir()
{
  # Two or more git-repo-urls could have the same repo name
  # but be from different repositories.
  # So git clone each repo into its own unique directory
  # based on a simple incrementing index.
  for i in "${!GIT_REPO_URLS[@]}"; do
    git_clone_one_tagged_url_into_context_dir "${GIT_REPO_URLS[$i]}" "${i}"
  done
  echo -e "$(image_type)" > "${CONTEXT_DIR}/image.type"
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
git_clone_one_tagged_url_into_context_dir()
{
  # git-clone directly, from this script, into the
  # context dir before running [docker image build].
  # Run [git clone] on the _host_ rather than wherever
  # the docker daemon is (via a command in the Dockerfile).
  local output
  local -r tagged_url="${1}"       # bbd75a1@https://github.com/cyber-dojo-languages/gcc-assert
  local -r url_index="${2}"        # 0
  local -r tag="${tagged_url:0:7}" # bbd75a1
  local -r url="${tagged_url:8}"   # https://github.com/cyber-dojo-languages/gcc-assert

  cd "${CONTEXT_DIR}"
  echo "git clone ${url}"
  if ! output="$(git clone --single-branch --branch master "${url}" "${url_index}" 2>&1)"
  then
    stderr "ERROR: git clone ... ${url}"
    stderr "${IMAGE_TYPE}"
    stderr "${output}"
    exit 3
  fi

  cd "${CONTEXT_DIR}/${url_index}"
  echo "git checkout ${tag}"
  if ! output=$(git checkout "${tag}" 2>&1)
  then
    stderr "ERROR: git checkout ${tag}"
    stderr "${IMAGE_TYPE}"
    stderr "${output}"
    exit 3
  fi

  local -r sha="$(git rev-parse HEAD)"
  echo -e "${IMAGE_TYPE} \t ${url}"
  echo -e "${url_index} \t ${sha} \t ${url}" >> "${CONTEXT_DIR}/shas.txt"
  rm -rf "${CONTEXT_DIR}/${url_index}/.git"
  rm -rf "${CONTEXT_DIR}/${url_index}/docker"
}

{'fraction_non_alphanumeric': 0.19940089865202196, 'fraction_numerical': 0.01238142785821268, 'mean_word_length': 1.9346615880457076, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 43, 'https://': 20, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 92, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2615244', 'n_tokens_mistral': 3927, 'n_tokens_neox': 3749, 'n_words': 788}