-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.sh
More file actions
executable file
·145 lines (127 loc) · 3.68 KB
/
sync.sh
File metadata and controls
executable file
·145 lines (127 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash
############################################################
# Help #
############################################################
show_help()
{
echo "Synchronize downstream repositories from upstream"
echo
echo "Syntax: sync.sh [-h|-d|-y] TARGET"
echo "Options:"
echo " -h Print this help."
echo " -d Dry run (do not push to remote downstream)."
echo " -y Yes-mode (non-interactive: proceed without asking)."
echo
echo "Arguments:"
echo " TARGET Target downstream branch"
echo
echo "Example:"
echo " ./sync.sh release-1.12"
echo
}
# Reset in case getopts has been used previously in the shell.
OPTIND=1
dry_run=0
yes_mode=0
repos=(operator ebpf-agent flowlogs-pipeline console-plugin cli)
cp_variants=(pf4 pf5)
while getopts "h?dy" opt; do
case "$opt" in
h|\?)
show_help
exit 0
;;
d)
dry_run=1
;;
y)
yes_mode=1
;;
esac
done
shift $((OPTIND-1))
[ "${1:-}" = "--" ] && shift
if [ "$#" == "0" ]; then
echo "Missing argument: TARGET"
show_help
exit 1
fi
if [ "$#" != "1" ]; then
echo "Too many arguments: $@"
show_help
exit 1
fi
dry_run_text=""
if [[ $dry_run == 1 ]]; then
echo "DRY RUN: remote will not be updated"
dry_run_text=" (dry run)"
fi
target="$1"
echo "Synchronizing \"downstream/$target\" with \"upstream/main${dry_run_text}\". A temporary local branch named \"tmp-$target\" will be created/overwritten."
if [[ $yes_mode != 1 ]]; then
read -p "Continue? [yN] " yn
echo
if [[ ! $yn =~ ^[Yy]$ ]] ; then
exit 1
fi
fi
warnings=()
print_warnings() {
for warning in "${warnings[@]}"; do
echo "WARNING: $warning"
done
}
merge_and_push() {
local repo=$1
local downstream_branch=$2
local upstream_branch=$3
local tmp_branch="tmp-$downstream_branch"
git checkout -B $tmp_branch downstream/$downstream_branch
git reset --hard downstream/$downstream_branch
git merge upstream/$upstream_branch
if [[ "$?" != "0" ]]; then
warnings+=("Merge failed in \"$repo\", branch \"$downstream_branch\"; resolve conflicts, merge and push manually.")
elif [[ $dry_run == 1 ]]; then
echo "DRY RUN: skip push $tmp_branch to downstream/$downstream_branch. You can push manually if you wish."
else
if [[ $yes_mode != 1 ]]; then
read -p "Merge done. Proceed with push? [yN] " yn
echo
if [[ ! $yn =~ ^[Yy]$ ]] ; then
return
fi
fi
# Proceed with push
git push downstream HEAD:$downstream_branch
fi
}
for repo in "${repos[@]}"; do
echo -e "\n\033[1mProcessing $repo\033[0m"
pushd $repo
git fetch upstream
git fetch downstream
git ls-remote --exit-code --heads downstream refs/heads/$target
if [[ "$?" != "0" ]]; then
echo "Branch downstream/$target not found. Create the branches before running sync.sh. You can use new-branches.sh."
exit 1
fi
merge_and_push $repo $target main
if [[ "$repo" == "console-plugin" ]]; then
for variant in "${cp_variants[@]}"; do
echo -e "\n\033[1mVariant: $variant\033[0m"
git diff HEAD --exit-code
if [[ "$?" != "0" ]]; then
warnings+=("Sounds like console-plugin previous merge failed, cannot proceed with this variant. Run again the script after resolving conflicts for syncing next variant.")
else
git ls-remote --exit-code --heads downstream refs/heads/$target-$variant
if [[ "$?" != "0" ]]; then
echo "Branch downstream/$target-$variant not found. Create the branches before running sync.sh. You can use new-branches.sh."
exit 1
fi
merge_and_push $repo $target-$variant main-$variant
fi
done
fi
popd
done
print_warnings