From 500092ae11c78e7c2520d63a894224fd3f4554be Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Fri, 29 Jan 2021 20:06:39 +0100 Subject: Rework for better integration with Jenkins --- gitmirror.sh | 50 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/gitmirror.sh b/gitmirror.sh index 53433dc..0bb1df6 100755 --- a/gitmirror.sh +++ b/gitmirror.sh @@ -1,6 +1,11 @@ #!/usr/bin/env bash set -Eeuo pipefail +REPOSITORY_PATH_FORMAT="/var/lib/gitolite/repositories/%s.git" +MERGE_JOB_FORMAT="%s-merge-patch" +JENKINS_SSH_PORT=38844 +JENKINS_SSH_HOST="localhost" + msg() { echo "==> $1" } @@ -52,24 +57,35 @@ archive_if_needed() { if ! git merge-base --is-ancestor "${old_tip}" "${new_tip}"; then local archive_name="${archive_prefix}${branch_name}" msg "Refusing to clobber ${branch_name}, archiving as ${archive_name}" - git tag -a -m "Archive before synchronization" "${archive_name}" "${old_tip}" + git tag -a -m "Archive before synchronization with ${new_tip}" "${archive_name}" "${old_tip}" fi } sync_mirror() { - if (( $# != 2 )); then - echo "Usage: $0 REPOSITORY [REMOTE]" >&2 + if (( $# != 1 )); then + echo "Usage: $0 REPOSITORY" >&2 exit 1 fi - local repository="$1" - local remote="$2" + local project="$1" + local repository + # shellcheck disable=2059 + repository="$(printf "${REPOSITORY_PATH_FORMAT}" "${project}")" local archive_prefix - archive_prefix="$(TZ='UTC' printf 'archives/%(%Y%m%dT%H%M%S)TZ/')" - + archive_prefix="$(TZ='UTC' printf 'archive/%(%Y%m%dT%H%M%S)TZ/')" + + cd "${repository}" + local remote + remote="$(git config --get gitmirror.remoteName)" + local remote_url + remote_url="$(git config --get gitmirror.remoteUrl)" + if [[ -z "${remote}" || -z "${remote_url}" ]]; then + msg "FATAL: ${repository} not configured for mirroring" >&2 + exit 2 + fi msg "Synchronizing ${repository} with ${remote}" - pushd "${repository}" > /dev/null + git config "remote.${remote}.url" "${remote_url}" msg "Remote tracking branches before fetch" # shellcheck disable=SC2034 @@ -82,7 +98,8 @@ sync_mirror() { read_branches "heads" local_branches msg "Fetch from ${remote}" - git fetch --tags "${remote}" + git fetch --tags --force --progress -- "${remote_url}" \ + "+refs/heads/*:refs/remotes/${remote}/*" msg "Remote tracking branches after fetch" local -A new_tracking @@ -93,6 +110,11 @@ sync_mirror() { local branch_name for branch_name in "${!new_tracking[@]}"; do + if [[ "${branch_name}" =~ ^patch-for/.* ]]; then + msg "FATAL: Refusing to pull patch ${branch_name} from remote" >&2 + exit 2 + fi + local new_tip="${new_tracking["${branch_name}"]}" local old_tip get_or_empty old_tracking "${branch_name}" old_tip @@ -109,7 +131,6 @@ sync_mirror() { if [[ -z "${local_tip}" ]]; then msg "Create local branch ${branch_name} at ${new_tip}" git branch --no-track "${branch_name}" "${patch_tip:-"${new_tip}"}" - git branch --set-upstream-to="${remote}/${branch_name}" "${branch_name}" if [[ -n "${patch_tip}" ]]; then msg "Will patch newly created local branch ${branch_name}" to_patch+=("${branch_name}") @@ -131,14 +152,15 @@ sync_mirror() { fi done - local job_name="${repository}-apply-patch" + local job_name + # shellcheck disable=2059 + job_name="$(printf "${MERGE_JOB_FORMAT}" "${project}")" for branch_name in "${to_patch[@]}"; do msg "Triggering Jenkings job ${job_name} for branch ${branch_name}" - # TODO + ssh -p "${JENKINS_SSH_PORT}" "${JENKINS_SSH_HOST}" \ + build "${job_name}" -p "BRANCH=${branch_name}" done - - popd > /dev/null } if [[ "$0" == "${BASH_SOURCE[0]}" ]]; then -- cgit v1.2.3-54-g00ecf