# Copyright 2020 The Nomulus Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This file declares shell functions used by other scripts in this folder.

# Fetch the tag of the currently deployed release of Nomulus server
# or SQL schema. The caller is responsible for fetching relevant git
# tags to the local repo.
function fetchVersion() {
  local deployed_system=${1}
  local env=${2}
  local dev_project=${3}
  echo $(gcloud storage cat \
      gs://${dev_project}-deployed-tags/${deployed_system}.${env}.tag)
}

function getChangeCountSinceVersion() {
  local deployed_system=${1}
  local version=${2}
  local changes

  if [[  ${deployed_system} == "sql " ]]; then
    changes=$(git diff --name-only ${version} \
        db/src/main/resources/sql/flyway | wc -l)
  else
    changes=$(git diff --name-only ${version} \
        core/src/main/resources/META-INF \
        core/src/main/java/google/registry/persistence \
        db/src/main/resources/sql/schema/db-schema.sql.generated | wc -l)
  fi
  echo ${changes}
}
