diff options
author | Kristóf Marussy <kristof@marussy.com> | 2021-01-29 20:06:39 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2021-01-29 20:06:39 +0100 |
commit | 500092ae11c78e7c2520d63a894224fd3f4554be (patch) | |
tree | 2af9d23678f4240a96e40d66f4bad530718777dc | |
parent | Remove merge script (diff) | |
download | gitmirror-500092ae11c78e7c2520d63a894224fd3f4554be.tar.gz gitmirror-500092ae11c78e7c2520d63a894224fd3f4554be.tar.zst gitmirror-500092ae11c78e7c2520d63a894224fd3f4554be.zip |
Rework for better integration with Jenkins
-rwxr-xr-x | gitmirror.sh | 50 |
1 files 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 @@ | |||
1 | #!/usr/bin/env bash | 1 | #!/usr/bin/env bash |
2 | set -Eeuo pipefail | 2 | set -Eeuo pipefail |
3 | 3 | ||
4 | REPOSITORY_PATH_FORMAT="/var/lib/gitolite/repositories/%s.git" | ||
5 | MERGE_JOB_FORMAT="%s-merge-patch" | ||
6 | JENKINS_SSH_PORT=38844 | ||
7 | JENKINS_SSH_HOST="localhost" | ||
8 | |||
4 | msg() { | 9 | msg() { |
5 | echo "==> $1" | 10 | echo "==> $1" |
6 | } | 11 | } |
@@ -52,24 +57,35 @@ archive_if_needed() { | |||
52 | if ! git merge-base --is-ancestor "${old_tip}" "${new_tip}"; then | 57 | if ! git merge-base --is-ancestor "${old_tip}" "${new_tip}"; then |
53 | local archive_name="${archive_prefix}${branch_name}" | 58 | local archive_name="${archive_prefix}${branch_name}" |
54 | msg "Refusing to clobber ${branch_name}, archiving as ${archive_name}" | 59 | msg "Refusing to clobber ${branch_name}, archiving as ${archive_name}" |
55 | git tag -a -m "Archive before synchronization" "${archive_name}" "${old_tip}" | 60 | git tag -a -m "Archive before synchronization with ${new_tip}" "${archive_name}" "${old_tip}" |
56 | fi | 61 | fi |
57 | } | 62 | } |
58 | 63 | ||
59 | sync_mirror() { | 64 | sync_mirror() { |
60 | if (( $# != 2 )); then | 65 | if (( $# != 1 )); then |
61 | echo "Usage: $0 REPOSITORY [REMOTE]" >&2 | 66 | echo "Usage: $0 REPOSITORY" >&2 |
62 | exit 1 | 67 | exit 1 |
63 | fi | 68 | fi |
64 | 69 | ||
65 | local repository="$1" | 70 | local project="$1" |
66 | local remote="$2" | 71 | local repository |
72 | # shellcheck disable=2059 | ||
73 | repository="$(printf "${REPOSITORY_PATH_FORMAT}" "${project}")" | ||
67 | 74 | ||
68 | local archive_prefix | 75 | local archive_prefix |
69 | archive_prefix="$(TZ='UTC' printf 'archives/%(%Y%m%dT%H%M%S)TZ/')" | 76 | archive_prefix="$(TZ='UTC' printf 'archive/%(%Y%m%dT%H%M%S)TZ/')" |
70 | 77 | ||
78 | cd "${repository}" | ||
79 | local remote | ||
80 | remote="$(git config --get gitmirror.remoteName)" | ||
81 | local remote_url | ||
82 | remote_url="$(git config --get gitmirror.remoteUrl)" | ||
83 | if [[ -z "${remote}" || -z "${remote_url}" ]]; then | ||
84 | msg "FATAL: ${repository} not configured for mirroring" >&2 | ||
85 | exit 2 | ||
86 | fi | ||
71 | msg "Synchronizing ${repository} with ${remote}" | 87 | msg "Synchronizing ${repository} with ${remote}" |
72 | pushd "${repository}" > /dev/null | 88 | git config "remote.${remote}.url" "${remote_url}" |
73 | 89 | ||
74 | msg "Remote tracking branches before fetch" | 90 | msg "Remote tracking branches before fetch" |
75 | # shellcheck disable=SC2034 | 91 | # shellcheck disable=SC2034 |
@@ -82,7 +98,8 @@ sync_mirror() { | |||
82 | read_branches "heads" local_branches | 98 | read_branches "heads" local_branches |
83 | 99 | ||
84 | msg "Fetch from ${remote}" | 100 | msg "Fetch from ${remote}" |
85 | git fetch --tags "${remote}" | 101 | git fetch --tags --force --progress -- "${remote_url}" \ |
102 | "+refs/heads/*:refs/remotes/${remote}/*" | ||
86 | 103 | ||
87 | msg "Remote tracking branches after fetch" | 104 | msg "Remote tracking branches after fetch" |
88 | local -A new_tracking | 105 | local -A new_tracking |
@@ -93,6 +110,11 @@ sync_mirror() { | |||
93 | local branch_name | 110 | local branch_name |
94 | 111 | ||
95 | for branch_name in "${!new_tracking[@]}"; do | 112 | for branch_name in "${!new_tracking[@]}"; do |
113 | if [[ "${branch_name}" =~ ^patch-for/.* ]]; then | ||
114 | msg "FATAL: Refusing to pull patch ${branch_name} from remote" >&2 | ||
115 | exit 2 | ||
116 | fi | ||
117 | |||
96 | local new_tip="${new_tracking["${branch_name}"]}" | 118 | local new_tip="${new_tracking["${branch_name}"]}" |
97 | local old_tip | 119 | local old_tip |
98 | get_or_empty old_tracking "${branch_name}" old_tip | 120 | get_or_empty old_tracking "${branch_name}" old_tip |
@@ -109,7 +131,6 @@ sync_mirror() { | |||
109 | if [[ -z "${local_tip}" ]]; then | 131 | if [[ -z "${local_tip}" ]]; then |
110 | msg "Create local branch ${branch_name} at ${new_tip}" | 132 | msg "Create local branch ${branch_name} at ${new_tip}" |
111 | git branch --no-track "${branch_name}" "${patch_tip:-"${new_tip}"}" | 133 | git branch --no-track "${branch_name}" "${patch_tip:-"${new_tip}"}" |
112 | git branch --set-upstream-to="${remote}/${branch_name}" "${branch_name}" | ||
113 | if [[ -n "${patch_tip}" ]]; then | 134 | if [[ -n "${patch_tip}" ]]; then |
114 | msg "Will patch newly created local branch ${branch_name}" | 135 | msg "Will patch newly created local branch ${branch_name}" |
115 | to_patch+=("${branch_name}") | 136 | to_patch+=("${branch_name}") |
@@ -131,14 +152,15 @@ sync_mirror() { | |||
131 | fi | 152 | fi |
132 | done | 153 | done |
133 | 154 | ||
134 | local job_name="${repository}-apply-patch" | 155 | local job_name |
156 | # shellcheck disable=2059 | ||
157 | job_name="$(printf "${MERGE_JOB_FORMAT}" "${project}")" | ||
135 | 158 | ||
136 | for branch_name in "${to_patch[@]}"; do | 159 | for branch_name in "${to_patch[@]}"; do |
137 | msg "Triggering Jenkings job ${job_name} for branch ${branch_name}" | 160 | msg "Triggering Jenkings job ${job_name} for branch ${branch_name}" |
138 | # TODO | 161 | ssh -p "${JENKINS_SSH_PORT}" "${JENKINS_SSH_HOST}" \ |
162 | build "${job_name}" -p "BRANCH=${branch_name}" | ||
139 | done | 163 | done |
140 | |||
141 | popd > /dev/null | ||
142 | } | 164 | } |
143 | 165 | ||
144 | if [[ "$0" == "${BASH_SOURCE[0]}" ]]; then | 166 | if [[ "$0" == "${BASH_SOURCE[0]}" ]]; then |