Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (c) 2019, Arm Limited. All rights reserved. |
| 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | # |
| 7 | |
| 8 | # Make a backup of the following repositories on Github: |
| 9 | # - arm-trusted-firmware-private.git |
| 10 | # - arm-trusted-firmware-private.wiki.git |
| 11 | # - tf-issues.git |
| 12 | # |
| 13 | # Also backup the following repositories from review.trustedfirmware.org: |
| 14 | # - trusted-firmware-a.git |
| 15 | # - tf-a-tests.git |
| 16 | |
| 17 | set -e |
| 18 | |
| 19 | ci_root="$(readlink -f "$(dirname "$0")/..")" |
| 20 | source "$ci_root/utils.sh" |
| 21 | |
| 22 | backup_dir="${BACKUP_DIR:-/arm/ref/pd/pdsw/external-repo-backup}" |
| 23 | |
| 24 | |
| 25 | initial_clone() { |
| 26 | local repo_url="${1:?}" |
| 27 | local repo_dir="${2:?}" |
| 28 | local repo_name="$(basename $repo_dir)" |
| 29 | local s_before s_after s_diff |
| 30 | |
| 31 | s_before="$(date +%s)" |
| 32 | |
| 33 | echo |
| 34 | echo "Cloning repository $repo_name..." |
| 35 | |
| 36 | git clone --quiet --mirror "$repo_url" "$repo_dir" |
| 37 | |
| 38 | pushd "$repo_dir" |
| 39 | git show --quiet | sed 's/^/ > /g' |
| 40 | popd |
| 41 | |
| 42 | s_after="$(date +%s)" |
| 43 | let "s_diff = $s_after - $s_before" || true |
| 44 | echo "Cloned in $s_diff seconds." |
| 45 | echo |
| 46 | } |
| 47 | |
| 48 | update_repo() { |
| 49 | local repo_dir="${1:?}" |
| 50 | local repo_name="$(basename $repo_dir)" |
| 51 | local s_before s_after s_diff |
| 52 | |
| 53 | pushd "$repo_dir" |
| 54 | |
| 55 | s_before="$(date +%s)" |
| 56 | |
| 57 | echo |
| 58 | echo "Updating repo $repo_name..." |
| 59 | |
| 60 | git gc --quiet |
| 61 | git remote update --prune |
| 62 | git show --quiet | sed 's/^/ > /g' |
| 63 | |
| 64 | s_after="$(date +%s)" |
| 65 | let "s_diff = $s_after - $s_before" || true |
| 66 | echo "Updated in $s_diff seconds." |
| 67 | echo |
| 68 | |
| 69 | popd |
| 70 | } |
| 71 | |
| 72 | get_repo_url() { |
| 73 | local url_var="${1:?}" |
| 74 | local repo_location="${2:?}" |
| 75 | local repo_name="${3:?}" |
| 76 | |
| 77 | case "$repo_location" in |
| 78 | "github") |
| 79 | if upon "$anonymous"; then |
| 80 | eval $url_var="https://github.com/ARM-software/$repo_name" |
| 81 | else |
| 82 | GITHUB_USER="${GITHUB_USER:-arm-tf-bot}" |
| 83 | GITHUB_PASSWORD="${GITHUB_PASSWORD:?}" |
| 84 | eval $url_var="https://$GITHUB_USER:$GITHUB_PASSWORD@github.com/ARM-software/$repo_name" |
| 85 | fi |
| 86 | ;; |
| 87 | |
| 88 | "tf.org") |
| 89 | if not_upon "$anonymous"; then |
| 90 | echo "Authenticated access to repo $repo_name not supported." |
| 91 | exit 1 |
| 92 | fi |
| 93 | eval $url_var="https://review.trustedfirmware.org/TF-A/$repo_name" |
| 94 | ;; |
| 95 | |
| 96 | *) |
| 97 | echo "Unsupported repository location: $repo_location." |
| 98 | exit 1 |
| 99 | ;; |
| 100 | esac |
| 101 | } |
| 102 | |
| 103 | backup_repo() { |
| 104 | local repo_location="${1:?}" |
| 105 | local repo_name="${2:?}" |
| 106 | local repo_dir="${3:-$repo_location/$repo_name}" |
| 107 | |
| 108 | if [ ! -d "$repo_dir" ]; then |
| 109 | local repo_url |
| 110 | get_repo_url "repo_url" "$repo_location" "$repo_name" |
| 111 | initial_clone "$repo_url" "$repo_dir" |
| 112 | else |
| 113 | update_repo "${repo_dir:?}" |
| 114 | fi |
| 115 | } |
| 116 | |
| 117 | |
| 118 | cd "$backup_dir" |
| 119 | |
| 120 | # Private repositories. Need arm-tf-bot credentials for authentication. |
| 121 | anonymous=0 backup_repo "github" "arm-trusted-firmware-private.git" |
| 122 | anonymous=0 backup_repo "github" "arm-trusted-firmware-private.wiki.git" |
| 123 | |
| 124 | # Public repositories. Anonymous access is allowed. |
| 125 | anonymous=1 backup_repo "github" "tf-issues.git" |
| 126 | |
| 127 | anonymous=1 backup_repo "tf.org" "trusted-firmware-a.git" |
| 128 | anonymous=1 backup_repo "tf.org" "tf-a-tests.git" |