#!/usr/bin/env bash
set -euo pipefail

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
control_root="$(cd "$script_dir/../../.." && pwd -P)"
zhct_root="$(dirname "$control_root")"
manifest="$(mktemp)"

trap 'rm -f "$manifest"' EXIT

cat >"$manifest" <<EOF
name,local_path,remote_url,default_action,notes
kxxl,zhctproject/kxxl,git@codeup.aliyun.com:60069db88deaa14d9e02b875/kx/kxxl.git,manual_branch,customer branch
EOF

sync_output="$(ZHCTPROJECT_REPO_MANIFEST="$manifest" "$control_root/control/scripts/sync-zhctproject-repos.sh")"
grep -F "SKIP kxxl: manual_branch preserves the current customer branch" <<<"$sync_output" >/dev/null

check_output="$(ZHCTPROJECT_REPO_MANIFEST="$manifest" "$control_root/control/scripts/check-zhctproject-canonical-repos.sh")"
grep -F "OK   kxxl manual_branch" <<<"$check_output" >/dev/null

[[ "$(git -C "$zhct_root/zhctproject/kxxl" branch --show-current)" == "master_jiangxitks" ]]

echo "manual_branch repository handling passed"
