#!/bin/bash -e

# Copyright 2016 The Rook 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.

# ignore the some commits that were lost in the past when the project was first getting started.
blacklist=(
  0000000000000000000000000000000000000000
  0cbdba99b4ed85cfdf6d4eb8d6bd65013ed26c23
  12209224c24f589fe0e4918052dbface874faff1
  137d2718d89128f23387ca15aefdb22bdc175a64
  239cbebf96dba6c1532702b7a460937ed4ae6785
  47b199109bbff1db37ddff9461652e30d79df330
  56131a5545544369f2cfe5d3c43333082859d69b
  763750b0f06f6e27b976853bfe314aa555757e61
  8953972151cae60be4999ba4896f78fc358b1150
  949e02e7af4ab140e207a25baabeb90e6c06553c
  ad1f5e0c3d9679d1f46250497927112ccb72c9be
  b7ce5af680249349a76d79ccf54ff568a736757c
  c4da56d0cbfcd75e37be29dd4853b23487a3526f
  ccf17262cbaf4cb2795d4b6e72df6c175b504687
  d2088b133b2a29a6c1efb22072016ee3268ca3df
  d43b87b16610f5278da38953f4bffa03fb788b17
  d536828eaafa3866e0aaaee703ae0f54dc66ea91
  dc578817ca6d169e2225f1ec9428942668f46a8c
  f1f1910c488036500abc19a36d22cfd7937741aa
)

blacklisted() {
  for b in ${blacklist[@]}; do
    if [[ $b == $1 ]]; then
      return 0
    fi
  done
  return 1
}

# find the remote for upstream rook
function get_remote() {
    gitdir=$1
    repo=$2
    local remote

    for r in $(cd ${gitdir} && git remote); do
        url=$(cd ${gitdir} && git config --get remote.${r}.url)
        if [[ "${url}" =~ .*github.com[/:]rook/${repo}.* ]]; then
            remote=${r}
            break
        fi
    done

    echo $remote
}

rook_remote=$(get_remote . rook)
if [[ ${rook_remote} == "" ]]; then
    echo ERROR: could not find a git remote pointing to https://github.com/rook/rook
    exit 2
fi
echo INFO: using remote ${rook_remote} branch master

# find an active remote pointing to the ceph repo
ceph_path=$(git config --file .gitmodules --get-regexp path | awk '{ print $2 }' | head -n1)
ceph_remote=$(get_remote ${ceph_path} ceph)
if [[ ${ceph_remote} == "" ]]; then
    echo ERROR: could not find a git remote pointing to https://github.com/rook/ceph
    exit 2
fi
echo INFO: using ceph remote ${ceph_remote} at ${ceph_path}

# find all commits in rook master that have modified the .gitmodules file
gitmodule_commits=$(git log --pretty=format:%H --no-abbrev ${rook_remote}/master -- .gitmodules)
combined_gitmodules=$(mktemp "${TMPDIR:-/tmp/}$(basename 0).XXXXX")
for c in ${gitmodule_commits}; do
  # get the .gitmodules file contents from that commit and append it to a combined .gitmodules
  # file across all commits
  git show ${c}:.gitmodules >> ${combined_gitmodules}
done

# parse the combined .gitmodules file looking for unique submodule paths
submodule_paths=$(git config --file ${combined_gitmodules} --get-regexp path | awk '{print $2}' | sort | uniq)
rm ${combined_gitmodules}

echo INFO: checking all submodules commits for paths ${submodule_paths}

errors=0

# for each submodule path across the revision history
for p in ${submodule_paths}; do

  # find all commits that had modified the submodule at that path
  commits=$(git log --pretty=format:%H --no-abbrev ${rook_remote}/master --submodule=log -- ${p})
  for c in ${commits}; do

    # now find the commit hash pointed to by the submodule (in the ceph repo)
    ceph_commit=$(git show --no-abbrev --oneline --raw ${c} --submodule -- ${p} | grep '^:' | cut -d' ' -f4)

    # check if its blacklisted
    if blacklisted ${ceph_commit}; then
      continue
    fi

    # check if the commit hash is valid
    if $(cd ${ceph_path}; git show -s "$ceph_commit" > /dev/null 2>&1); then
        ceph_branch=$(cd ${ceph_path}; git branch -r --no-color --contains "$ceph_commit" "${ceph_remote}/rook*" | tr -d "* ")
        if [[ "$ceph_branch" == "" ]]; then
            echo ERROR: rook commit ${c} has a submodule reference at \"${p}\" pointing to ${ceph_commit} but is not part of a branch
            ((++errors))
        fi
        echo INFO: rook commit ${c} has a submodule reference at \"${p}\" that is intact and is in branch ${ceph_branch#${ceph_remote}/}
    else
        echo ERROR: rook commit ${c} had a submodule reference at \"${p}\" that is missing from rook/ceph
        ((++errors))
    fi
  done
done

if [[ ${errors} != 0 ]]; then
    exit 1
fi

echo INFO: SUCCESS
exit 0
