diff options
author | Kristóf Marussy <kristof@marussy.com> | 2021-01-28 19:33:10 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2021-01-28 19:33:10 +0100 |
commit | 3dc333574e9ecb661861a5e55f7f0e119629a4f1 (patch) | |
tree | f6e4b0c1b06fc800a8754b24c7495da68383f3c3 | |
parent | Initial commit (diff) | |
download | gitmirror-3dc333574e9ecb661861a5e55f7f0e119629a4f1.tar.gz gitmirror-3dc333574e9ecb661861a5e55f7f0e119629a4f1.tar.zst gitmirror-3dc333574e9ecb661861a5e55f7f0e119629a4f1.zip |
Add script to merge remote patches
-rwxr-xr-x | gitmirror-merge.sh | 79 | ||||
-rwxr-xr-x | gitmirror.sh | 12 |
2 files changed, 84 insertions, 7 deletions
diff --git a/gitmirror-merge.sh b/gitmirror-merge.sh new file mode 100755 index 0000000..d346602 --- /dev/null +++ b/gitmirror-merge.sh | |||
@@ -0,0 +1,79 @@ | |||
1 | #!/usr/bin/env bash | ||
2 | set -Eeuo pipefail | ||
3 | |||
4 | MIRROR_REMOTE="gitmirror_remote" | ||
5 | CLONE_URL_PATTERN="file://${PWD}/%s" | ||
6 | |||
7 | msg() { | ||
8 | echo "==> $1" | ||
9 | } | ||
10 | |||
11 | msg2() { | ||
12 | echo "--> $1" | ||
13 | } | ||
14 | |||
15 | deepened_fetch() { | ||
16 | local fetch_spec="$1" | ||
17 | local exclude="$2" | ||
18 | if git fetch "${MIRROR_REMOTE}" "${fetch_spec}" \ | ||
19 | --shallow-exclude="${exclude}"; then | ||
20 | msg2 "Deepen fetch by 1" | ||
21 | git fetch "${MIRROR_REMOTE}" "${fetch_spec}" --deepen=1 | ||
22 | else | ||
23 | msg2 "Shallow request possibly empty, retry with depth=1" | ||
24 | git fetch "${MIRROR_REMOTE}" "${fetch_spec}" --depth=1 | ||
25 | fi | ||
26 | } | ||
27 | |||
28 | merge_patches() { | ||
29 | if (( $# != 3 )); then | ||
30 | echo "Usage: $0 REPOSITORY REMOTE BRANCH" >&2 | ||
31 | exit 1 | ||
32 | fi | ||
33 | |||
34 | local repository="$1" | ||
35 | local remote="$2" | ||
36 | local branch="$3" | ||
37 | |||
38 | if [[ "${remote}" == "${MIRROR_REMOTE}" ]]; then | ||
39 | msg "REMOTE must not be ${MIRROR_REMOTE}" >&2 | ||
40 | exit 1 | ||
41 | fi | ||
42 | |||
43 | local copy_name="${repository}-patch-${remote}-${branch}" | ||
44 | msg "Create working copy ${copy_name}" | ||
45 | rm -rf "${copy_name}" | ||
46 | mkdir "${copy_name}" | ||
47 | pushd "${copy_name}" > /dev/null | ||
48 | git init | ||
49 | local clone_url | ||
50 | # shellcheck disable=2059 | ||
51 | clone_url="$(printf "${CLONE_URL_PATTERN}" "${repository}")" | ||
52 | git remote add "${MIRROR_REMOTE}" "${clone_url}" | ||
53 | |||
54 | local patch_branch="patch-for/${branch}" | ||
55 | msg "Fetch patch at ${patch_branch}" | ||
56 | local upstream_branch="${remote}/${branch}" | ||
57 | local remote_upstream_ref="refs/remotes/${upstream_branch}" | ||
58 | deepened_fetch "${patch_branch}" "${remote_upstream_ref}" | ||
59 | |||
60 | msg "Fetch upstream at ${upstream_branch}" | ||
61 | local local_upstream_ref="refs/remotes/${MIRROR_REMOTE}/${upstream_branch}" | ||
62 | deepened_fetch "${remote_upstream_ref}:${local_upstream_ref}" \ | ||
63 | "refs/heads/${patch_branch}" | ||
64 | |||
65 | msg "Merge ${patch_branch} into ${upstream_branch}" | ||
66 | git checkout -b "${branch}" --no-track "${local_upstream_ref}" | ||
67 | git merge "${MIRROR_REMOTE}/${patch_branch}" --no-ff \ | ||
68 | -m "Merge branch '${patch_branch}' into ${upstream_branch}" | ||
69 | |||
70 | msg "Push ${branch} to ${MIRROR_REMOTE}" | ||
71 | git push --force "${MIRROR_REMOTE}" "${branch}:${branch}" | ||
72 | |||
73 | popd > /dev/null | ||
74 | rm -rf "${copy_name}" | ||
75 | } | ||
76 | |||
77 | if [[ "$0" == "${BASH_SOURCE[0]}" ]]; then | ||
78 | merge_patches "$@" | ||
79 | fi | ||
diff --git a/gitmirror.sh b/gitmirror.sh index 29503d9..53433dc 100755 --- a/gitmirror.sh +++ b/gitmirror.sh | |||
@@ -22,6 +22,8 @@ read_branches() { | |||
22 | done < <(git for-each-ref "${prefix}**" \ | 22 | done < <(git for-each-ref "${prefix}**" \ |
23 | --format=$'%(refname)\t%(objectname)' \ | 23 | --format=$'%(refname)\t%(objectname)' \ |
24 | --color=never) | 24 | --color=never) |
25 | |||
26 | local branch_name | ||
25 | for branch_name in "${!read_branches_target[@]}"; do | 27 | for branch_name in "${!read_branches_target[@]}"; do |
26 | printf " %*s = %s\n" \ | 28 | printf " %*s = %s\n" \ |
27 | $((-max_length)) \ | 29 | $((-max_length)) \ |
@@ -55,18 +57,14 @@ archive_if_needed() { | |||
55 | } | 57 | } |
56 | 58 | ||
57 | sync_mirror() { | 59 | sync_mirror() { |
58 | if (( $# == 0 || $# > 2 )); then | 60 | if (( $# != 2 )); then |
59 | echo "Usage: $0 REPOSITORY [REMOTE]" >&2 | 61 | echo "Usage: $0 REPOSITORY [REMOTE]" >&2 |
60 | exit 1 | 62 | exit 1 |
61 | fi | 63 | fi |
62 | 64 | ||
63 | local repository="$1" | 65 | local repository="$1" |
64 | local remote | 66 | local remote="$2" |
65 | if (( $# == 1 )); then | 67 | |
66 | remote="origin" | ||
67 | else | ||
68 | remote="$2" | ||
69 | fi | ||
70 | local archive_prefix | 68 | local archive_prefix |
71 | archive_prefix="$(TZ='UTC' printf 'archives/%(%Y%m%dT%H%M%S)TZ/')" | 69 | archive_prefix="$(TZ='UTC' printf 'archives/%(%Y%m%dT%H%M%S)TZ/')" |
72 | 70 | ||