blob: 086b60b250b1192f4570438e046f4dedac0b2e49 [file] [log] [blame]
Quentin Monnet164585e2021-11-12 00:57:35 +00001#!/usr/bin/env bash
Quentin Monnet1e2278e2021-11-12 00:56:33 +00002
3usage () {
Quentin Monnet9ba94fd2022-01-16 00:04:19 +00004 echo "USAGE: ./sync-kernel.sh <bpftool-repo> <kernel-repo>"
5 echo ""
6 echo "This script synchronizes the mirror with upstream bpftool sources from the kernel repository."
7 echo "It performs the following steps:"
8 echo " - Update the libbpf submodule, commit, and use its new checkpoints as target commits for bpftool."
9 echo " - Cherry-pick commits from the bpf-next branch, up to the bpf-next target commit."
10 echo " - Cherry-pick commits from the bpf branch, up to the bpf target commit."
Quentin Monnet9ba94fd2022-01-16 00:04:19 +000011 echo " - Create a new commit with the updated version and checkpoints."
12 echo " - Check consistency."
Quentin Monnet1e2278e2021-11-12 00:56:33 +000013 echo ""
Quentin Monnet164585e2021-11-12 00:57:35 +000014 echo "Set BPF_NEXT_BASELINE to override bpf-next tree commit, otherwise read from <bpftool-repo>/CHECKPOINT-COMMIT."
15 echo "Set BPF_BASELINE to override bpf tree commit, otherwise read from <bpftool-repo>/BPF-CHECKPOINT-COMMIT."
Quentin Monnet9ba94fd2022-01-16 00:04:19 +000016 echo "Set BPF_NEXT_TIP_COMMIT to override bpf-next tree target commit, otherwise read from <bpftool-repo>/libbpf/CHECKPOINT-COMMIT, after libbpf update."
17 echo "Set BPF_TIP_COMMIT to override bpf tree target commit, otherwise read from <bpftool-repo>/libbpf/BPF-CHECKPOINT-COMMIT, after libbpf update."
18 echo "Set SKIP_LIBBPF_UPDATE to 1 to avoid updating libbpf automatically."
19 echo "Set MANUAL_MODE to 1 to manually control every cherry-picked commit."
Quentin Monnet1e2278e2021-11-12 00:56:33 +000020 exit 1
21}
22
23set -eu
24
Quentin Monnet164585e2021-11-12 00:57:35 +000025BPFTOOL_REPO=${1-""}
Quentin Monnet1e2278e2021-11-12 00:56:33 +000026LINUX_REPO=${2-""}
Quentin Monnet1e2278e2021-11-12 00:56:33 +000027
Quentin Monnet164585e2021-11-12 00:57:35 +000028if [ -z "${BPFTOOL_REPO}" ] || [ -z "${LINUX_REPO}" ]; then
29 echo "Error: bpftool or linux repos are not specified"
Quentin Monnet1e2278e2021-11-12 00:56:33 +000030 usage
31fi
Quentin Monnet164585e2021-11-12 00:57:35 +000032
Quentin Monnet8e1d7262022-09-28 18:39:14 +010033BASELINE_COMMIT=${BPF_NEXT_BASELINE:-$(cat "${BPFTOOL_REPO}"/CHECKPOINT-COMMIT)}
34BPF_BASELINE_COMMIT=${BPF_BASELINE:-$(cat "${BPFTOOL_REPO}"/BPF-CHECKPOINT-COMMIT)}
Quentin Monnet164585e2021-11-12 00:57:35 +000035
Quentin Monnet1e2278e2021-11-12 00:56:33 +000036if [ -z "${BASELINE_COMMIT}" ] || [ -z "${BPF_BASELINE_COMMIT}" ]; then
37 echo "Error: bpf or bpf-next baseline commits are not provided"
38 usage
39fi
40
41SUFFIX=$(date --utc +%Y-%m-%dT%H-%M-%S.%3NZ)
42WORKDIR=$(pwd)
43TMP_DIR=$(mktemp -d)
44
Quentin Monnet8e1d7262022-09-28 18:39:14 +010045# shellcheck disable=SC2064
Quentin Monnet1e2278e2021-11-12 00:56:33 +000046trap "cd ${WORKDIR}; exit" INT TERM EXIT
47
Quentin Monnet164585e2021-11-12 00:57:35 +000048BPFTOOL_SRC_DIR="tools/bpf/bpftool"
49
Quentin Monnet1e2278e2021-11-12 00:56:33 +000050declare -A PATH_MAP
51PATH_MAP=( \
Quentin Monnet164585e2021-11-12 00:57:35 +000052 [${BPFTOOL_SRC_DIR}]=src \
53 [${BPFTOOL_SRC_DIR}/bash-completion]=bash-completion \
54 [${BPFTOOL_SRC_DIR}/Documentation]=docs \
55 [kernel/bpf/disasm.c]=src/kernel/bpf/disasm.c \
56 [kernel/bpf/disasm.h]=src/kernel/bpf/disasm.h \
Quentin Monnetcc5caf62022-08-19 11:56:06 +010057 [tools/include/tools/dis-asm-compat.h]=include/tools/dis-asm-compat.h \
Quentin Monnet164585e2021-11-12 00:57:35 +000058 [tools/include/uapi/asm-generic/bitsperlong.h]=include/uapi/asm-generic/bitsperlong.h \
Quentin Monnet1e2278e2021-11-12 00:56:33 +000059 [tools/include/uapi/linux/bpf_common.h]=include/uapi/linux/bpf_common.h \
60 [tools/include/uapi/linux/bpf.h]=include/uapi/linux/bpf.h \
61 [tools/include/uapi/linux/btf.h]=include/uapi/linux/btf.h \
Quentin Monnet164585e2021-11-12 00:57:35 +000062 [tools/include/uapi/linux/const.h]=include/uapi/linux/const.h \
Quentin Monneta7f0e7f2022-01-27 20:29:45 +000063 [tools/include/uapi/linux/if_link.h]=include/uapi/linux/if_link.h \
James Hilliardd5a59b22022-02-15 15:02:53 -070064 [tools/include/uapi/linux/netlink.h]=include/uapi/linux/netlink.h \
Quentin Monnet164585e2021-11-12 00:57:35 +000065 [tools/include/uapi/linux/perf_event.h]=include/uapi/linux/perf_event.h \
Quentin Monnet1e2278e2021-11-12 00:56:33 +000066 [tools/include/uapi/linux/pkt_cls.h]=include/uapi/linux/pkt_cls.h \
67 [tools/include/uapi/linux/pkt_sched.h]=include/uapi/linux/pkt_sched.h \
Quentin Monnet164585e2021-11-12 00:57:35 +000068 [tools/include/uapi/linux/tc_act/tc_bpf.h]=include/uapi/linux/tc_act/tc_bpf.h \
Quentin Monnet1e2278e2021-11-12 00:56:33 +000069)
70
Quentin Monnet8e1d7262022-09-28 18:39:14 +010071BPFTOOL_PATHS=( "${!PATH_MAP[@]}" )
72BPFTOOL_VIEW_PATHS=( "${PATH_MAP[@]}" )
Quentin Monneta1c7c112021-11-12 01:00:33 +000073BPFTOOL_VIEW_EXCLUDE_REGEX='^(docs/\.gitignore|src/Makefile\.(feature|include))$'
Quentin Monnet164585e2021-11-12 00:57:35 +000074LINUX_VIEW_EXCLUDE_REGEX='^$'
Quentin Monnet1e2278e2021-11-12 00:56:33 +000075
Quentin Monnet164585e2021-11-12 00:57:35 +000076# Deal with tools/bpf/bpftool first, because once we've mkdir-ed src/, command
77# "git mv" doesn't move bpftool _as_ src but _into_ src/.
78BPFTOOL_TREE_FILTER="mkdir __bpftool && "$'\\\n'
79BPFTOOL_TREE_FILTER+="git mv -kf ${BPFTOOL_SRC_DIR} __bpftool/${PATH_MAP[${BPFTOOL_SRC_DIR}]} && "$'\\\n'
80
81# Extract bash-completion and Documentation from src/.
82BPFTOOL_TREE_FILTER+="git mv -kf __bpftool/src/bash-completion __bpftool/bash-completion && "$'\\\n'
83BPFTOOL_TREE_FILTER+="git mv -kf __bpftool/src/Documentation __bpftool/docs && "$'\\\n'
84
Quentin Monnet66320922022-09-28 11:11:37 +010085BPFTOOL_TREE_FILTER+="mkdir -p __bpftool/include/tools __bpftool/include/uapi/asm-generic __bpftool/include/uapi/linux/tc_act __bpftool/src/kernel/bpf && "$'\\\n'
Quentin Monnet1e2278e2021-11-12 00:56:33 +000086for p in "${!PATH_MAP[@]}"; do
Quentin Monnet164585e2021-11-12 00:57:35 +000087 case ${p} in
88 ${BPFTOOL_SRC_DIR}*)
89 continue;;
90 esac
91 BPFTOOL_TREE_FILTER+="git mv -kf ${p} __bpftool/${PATH_MAP[${p}]} && "$'\\\n'
Quentin Monnet1e2278e2021-11-12 00:56:33 +000092done
Quentin Monnet164585e2021-11-12 00:57:35 +000093BPFTOOL_TREE_FILTER+="true >/dev/null"
Quentin Monnet1e2278e2021-11-12 00:56:33 +000094
95cd_to()
96{
Quentin Monnet8e1d7262022-09-28 18:39:14 +010097 cd "${WORKDIR}" && cd "$1"
Quentin Monnet1e2278e2021-11-12 00:56:33 +000098}
99
100# Output brief single-line commit description
101# $1 - commit ref
102commit_desc()
103{
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100104 git log -n1 --pretty='%h ("%s")' "$1"
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000105}
106
107# Create commit single-line signature, which consists of:
108# - full commit subject
109# - author date in ISO8601 format
110# - full commit body with newlines replaced with vertical bars (|)
111# - shortstat appended at the end
112# The idea is that this single-line signature is good enough to make final
113# decision about whether two commits are the same, across different repos.
114# $1 - commit ref
115# $2 - paths filter
116commit_signature()
117{
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100118 # shellcheck disable=SC2086
119 git show --pretty='("%s")|%aI|%b' --shortstat "$1" -- ${2-.} | tr '\n' '|'
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000120}
121
Quentin Monnet164585e2021-11-12 00:57:35 +0000122# Cherry-pick commits touching bpftool-related files
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000123# $1 - baseline_tag
124# $2 - tip_tag
125cherry_pick_commits()
126{
127 local manual_mode=${MANUAL_MODE:-0}
128 local baseline_tag=$1
129 local tip_tag=$2
130 local new_commits
131 local signature
132 local should_skip
133 local synced_cnt
134 local manual_check
Quentin Monnet164585e2021-11-12 00:57:35 +0000135 local bpftool_conflict_cnt
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000136 local desc
137
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100138 # shellcheck disable=SC2068
139 new_commits=$(git rev-list --no-merges --topo-order --reverse "${baseline_tag}".."${tip_tag}" ${BPFTOOL_PATHS[@]})
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000140 for new_commit in ${new_commits}; do
Quentin Monnet164585e2021-11-12 00:57:35 +0000141 if [[ "${baseline_tag}" == "${BPF_BASELINE_TAG}" ]]; then
142 if git merge-base --is-ancestor "${new_commit}" "${BASELINE_COMMIT}"; then
143 echo "Commit ${new_commit::12} from bpf is already in bpf-next branch, skipping."
144 continue
145 fi
146 fi
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100147 desc="$(commit_desc "${new_commit}")"
148 signature="$(commit_signature "${new_commit}" "${BPFTOOL_PATHS[*]}")"
149 # shellcheck disable=SC2126
150 synced_cnt=$(grep -F "${signature}" "${TMP_DIR}"/bpftool_commits.txt | wc -l)
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000151 manual_check=0
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100152 if (("${synced_cnt}" > 0)); then
Quentin Monnet164585e2021-11-12 00:57:35 +0000153 # commit with the same subject is already in bpftool, but it's
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000154 # not 100% the same commit, so check with user
Quentin Monnet164585e2021-11-12 00:57:35 +0000155 echo "Commit '${desc}' is synced into bpftool as:"
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100156 grep -F "${signature}" "${TMP_DIR}"/bpftool_commits.txt | \
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000157 cut -d'|' -f1 | sed -e 's/^/- /'
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100158 if (("${manual_mode}" != 1 && "${synced_cnt}" == 1)); then
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000159 echo "Skipping '${desc}' due to unique match..."
160 continue
161 fi
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100162 if (("${synced_cnt}" > 1)); then
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000163 echo "'${desc} matches multiple commits, please, double-check!"
164 manual_check=1
165 fi
166 fi
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100167 if (("${manual_mode}" == 1 || "${manual_check}" == 1)); then
168 read -rp "Do you want to skip '${desc}'? [y/N]: " should_skip
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000169 case "${should_skip}" in
170 "y" | "Y")
171 echo "Skipping '${desc}'..."
172 continue
173 ;;
174 esac
175 fi
Quentin Monnet164585e2021-11-12 00:57:35 +0000176 # commit hasn't been synced into bpftool yet
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000177 echo "Picking '${desc}'..."
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100178 if ! git cherry-pick "${new_commit}" &>/dev/null; then
Quentin Monnet164585e2021-11-12 00:57:35 +0000179 echo "Warning! Cherry-picking '${desc} failed, checking if it's non-bpftool files causing problems..."
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100180 # shellcheck disable=SC2068
Quentin Monnet164585e2021-11-12 00:57:35 +0000181 bpftool_conflict_cnt=$(git diff --name-only --diff-filter=U -- ${BPFTOOL_PATHS[@]} | wc -l)
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000182 conflict_cnt=$(git diff --name-only | wc -l)
183 prompt_resolution=1
184
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100185 if (("${bpftool_conflict_cnt}" == 0)); then
Quentin Monnet164585e2021-11-12 00:57:35 +0000186 echo "Looks like only non-bpftool files have conflicts, ignoring..."
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100187 if (("${conflict_cnt}" == 0)); then
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000188 echo "Empty cherry-pick, skipping it..."
189 git cherry-pick --abort
190 continue
191 fi
192
193 git add .
194 # GIT_EDITOR=true to avoid editor popping up to edit commit message
195 if ! GIT_EDITOR=true git cherry-pick --continue &>/dev/null; then
196 echo "Error! That still failed! Please resolve manually."
197 else
198 echo "Success! All cherry-pick conflicts were resolved for '${desc}'!"
199 prompt_resolution=0
200 fi
201 fi
202
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100203 if (("${prompt_resolution}" == 1)); then
204 read -rp "Error! Cherry-picking '${desc}' failed, please fix manually and press <return> to proceed..."
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000205 fi
206 fi
207 # Append signature of just cherry-picked commit to avoid
208 # potentially cherry-picking the same commit twice later when
209 # processing bpf tree commits. At this point we don't know yet
Quentin Monnet164585e2021-11-12 00:57:35 +0000210 # the final commit sha in bpftool repo, so we record Linux SHA
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000211 # instead as LINUX_<sha>.
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100212 echo "LINUX_$(git log --pretty='%h' -n1) ${signature}" >> "${TMP_DIR}"/bpftool_commits.txt
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000213 done
214}
215
216cleanup()
217{
218 echo "Cleaning up..."
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100219 rm -r -- "${TMP_DIR}"
220 cd_to "${LINUX_REPO}"
221 git checkout "${TIP_SYM_REF}"
222 git branch -D "${BASELINE_TAG}" "${TIP_TAG}" "${BPF_BASELINE_TAG}" "${BPF_TIP_TAG}" \
223 "${SQUASH_BASE_TAG}" "${SQUASH_TIP_TAG}" || true
224 # shellcheck disable=SC2015
225 git show-ref --verify --quiet refs/heads/"${VIEW_TAG}" && \
226 git branch -D "${VIEW_TAG}" || true
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000227
228 cd_to .
229 echo "DONE."
230}
231
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100232cd_to "${BPFTOOL_REPO}"
233BPFTOOL_SYNC_TAG="bpftool-sync-${SUFFIX}"
234git checkout -b "${BPFTOOL_SYNC_TAG}"
Quentin Monnet9ba94fd2022-01-16 00:04:19 +0000235
236# Update libbpf
237if [[ "${SKIP_LIBBPF_UPDATE:-0}" -ne 1 ]]; then
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100238 cd_to "${BPFTOOL_REPO}"/libbpf
Quentin Monnet9ba94fd2022-01-16 00:04:19 +0000239 git pull origin master
240 LIBBPF_VERSION=$(grep -oE '^LIBBPF_([0-9.]+)' src/libbpf.map | sort -rV | head -n1 | cut -d'_' -f2)
241 LIBBPF_COMMIT=$(git rev-parse HEAD)
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100242 cd_to "${BPFTOOL_REPO}"
Quentin Monnet9ba94fd2022-01-16 00:04:19 +0000243 if [[ -n "$(git status --porcelain --untracked-files=no)" ]]; then
244 git add libbpf
Quentin Monnetd10e1e12022-11-16 17:43:38 +0000245 git commit --signoff -m 'sync: Update libbpf submodule' \
Quentin Monnet9ba94fd2022-01-16 00:04:19 +0000246 -m "\
247Pull latest libbpf from mirror.
248Libbpf version: ${LIBBPF_VERSION}
249Libbpf commit: ${LIBBPF_COMMIT}" \
250 -- libbpf
251 fi
252fi
253
254# Use libbpf's new checkpoints as tips
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100255TIP_COMMIT=${BPF_NEXT_TIP_COMMIT:-$(cat "${BPFTOOL_REPO}"/libbpf/CHECKPOINT-COMMIT)}
256BPF_TIP_COMMIT=${BPF_TIP_COMMIT:-$(cat "${BPFTOOL_REPO}"/libbpf/BPF-CHECKPOINT-COMMIT)}
Quentin Monnet9ba94fd2022-01-16 00:04:19 +0000257if [ -z "${TIP_COMMIT}" ] || [ -z "${BPF_TIP_COMMIT}" ]; then
258 echo "Error: bpf or bpf-next tip commits are not provided"
259 usage
260fi
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000261
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100262cd_to "${BPFTOOL_REPO}"
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000263GITHUB_ABS_DIR=$(pwd)
Quentin Monnet164585e2021-11-12 00:57:35 +0000264echo "Dumping existing bpftool commit signatures..."
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000265for h in $(git log --pretty='%h' -n500); do
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100266 echo "$h" "$(commit_signature "$h")" >> "${TMP_DIR}"/bpftool_commits.txt
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000267done
268
269# Use current kernel repo HEAD as a source of patches
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100270cd_to "${LINUX_REPO}"
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000271LINUX_ABS_DIR=$(pwd)
272TIP_SYM_REF=$(git symbolic-ref -q --short HEAD || git rev-parse HEAD)
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100273BASELINE_TAG="bpftool-baseline-${SUFFIX}"
274TIP_TAG="bpftool-tip-${SUFFIX}"
275BPF_BASELINE_TAG="bpftool-bpf-baseline-${SUFFIX}"
276BPF_TIP_TAG="bpftool-bpf-tip-${SUFFIX}"
277VIEW_TAG="bpftool-view-${SUFFIX}"
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000278
279# Squash state of kernel repo at baseline into single commit
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100280SQUASH_BASE_TAG="bpftool-squash-base-${SUFFIX}"
281SQUASH_TIP_TAG="bpftool-squash-tip-${SUFFIX}"
282SQUASH_COMMIT=$(git commit-tree "${BASELINE_COMMIT}^{tree}" -m "BASELINE SQUASH ${BASELINE_COMMIT}")
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000283
284echo "WORKDIR: ${WORKDIR}"
285echo "LINUX REPO: ${LINUX_REPO}"
Quentin Monnet164585e2021-11-12 00:57:35 +0000286echo "BPFTOOL REPO: ${BPFTOOL_REPO}"
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000287echo "TEMP DIR: ${TMP_DIR}"
288echo "SUFFIX: ${SUFFIX}"
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100289echo "BASE COMMIT: '$(commit_desc "${BASELINE_COMMIT}")'"
290echo "TIP COMMIT: '$(commit_desc "${TIP_COMMIT}")'"
291echo "BPF BASE COMMIT: '$(commit_desc "${BPF_BASELINE_COMMIT}")'"
292echo "BPF TIP COMMIT: '$(commit_desc "${BPF_TIP_COMMIT}")'"
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000293echo "SQUASH COMMIT: ${SQUASH_COMMIT}"
294echo "BASELINE TAG: ${BASELINE_TAG}"
295echo "TIP TAG: ${TIP_TAG}"
296echo "BPF BASELINE TAG: ${BPF_BASELINE_TAG}"
297echo "BPF TIP TAG: ${BPF_TIP_TAG}"
298echo "SQUASH BASE TAG: ${SQUASH_BASE_TAG}"
299echo "SQUASH TIP TAG: ${SQUASH_TIP_TAG}"
300echo "VIEW TAG: ${VIEW_TAG}"
Quentin Monnet164585e2021-11-12 00:57:35 +0000301echo "BPFTOOL SYNC TAG: ${BPFTOOL_SYNC_TAG}"
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000302echo "PATCHES: ${TMP_DIR}/patches"
303
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100304git branch "${BASELINE_TAG}" "${BASELINE_COMMIT}"
305git branch "${TIP_TAG}" "${TIP_COMMIT}"
306git branch "${BPF_BASELINE_TAG}" "${BPF_BASELINE_COMMIT}"
307git branch "${BPF_TIP_TAG}" "${BPF_TIP_COMMIT}"
308git branch "${SQUASH_BASE_TAG}" "${SQUASH_COMMIT}"
309git checkout -b "${SQUASH_TIP_TAG}" "${SQUASH_COMMIT}"
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000310
311# Cherry-pick new commits onto squashed baseline commit
Quentin Monnet164585e2021-11-12 00:57:35 +0000312echo "Cherry-pick for bpf-next..."
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100313cherry_pick_commits "${BASELINE_TAG}" "${TIP_TAG}"
Quentin Monnet164585e2021-11-12 00:57:35 +0000314echo "Cherry-pick for bpf..."
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100315cherry_pick_commits "${BPF_BASELINE_TAG}" "${BPF_TIP_TAG}"
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000316
Quentin Monnet164585e2021-11-12 00:57:35 +0000317# Move all bpftool files into __bpftool directory.
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100318FILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch --prune-empty -f --tree-filter "${BPFTOOL_TREE_FILTER}" "${SQUASH_TIP_TAG}" "${SQUASH_BASE_TAG}"
Quentin Monnet164585e2021-11-12 00:57:35 +0000319# Make __bpftool a new root directory
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100320FILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch --prune-empty -f --subdirectory-filter __bpftool "${SQUASH_TIP_TAG}" "${SQUASH_BASE_TAG}"
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000321
Quentin Monnet164585e2021-11-12 00:57:35 +0000322# If there are no new commits with bpftool-related changes, bail out
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100323COMMIT_CNT=$(git rev-list --count "${SQUASH_BASE_TAG}".."${SQUASH_TIP_TAG}")
324if (("${COMMIT_CNT}" <= 0)); then
325 echo "No new changes to apply, we are done!"
326 cleanup
327 exit 2
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000328fi
329
330# Exclude baseline commit and generate nice cover letter with summary
Quentin Monnetd10e1e12022-11-16 17:43:38 +0000331git format-patch --no-signature "${SQUASH_BASE_TAG}".."${SQUASH_TIP_TAG}" --cover-letter -o "${TMP_DIR}"/patches
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000332
Quentin Monnet164585e2021-11-12 00:57:35 +0000333# Now is time to re-apply bpftool-related linux patches to bpftool repo
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100334cd_to "${BPFTOOL_REPO}"
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000335
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100336# shellcheck disable=SC2012
337for patch in $(ls -1 "${TMP_DIR}"/patches | tail -n +2); do
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000338 if ! git am --3way --committer-date-is-author-date "${TMP_DIR}/patches/${patch}"; then
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100339 read -rp "Applying ${TMP_DIR}/patches/${patch} failed, please resolve manually and press <return> to proceed..."
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000340 fi
341done
342
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000343# Use generated cover-letter as a template for "sync commit" with
344# baseline and checkpoint commits from kernel repo (and leave summary
345# from cover letter intact, of course)
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100346echo "${TIP_COMMIT}" > CHECKPOINT-COMMIT && \
347echo "${BPF_TIP_COMMIT}" > BPF-CHECKPOINT-COMMIT && \
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000348git add CHECKPOINT-COMMIT && \
349git add BPF-CHECKPOINT-COMMIT && \
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100350awk '/\*\*\* BLURB HERE \*\*\*/ {p=1} p' "${TMP_DIR}"/patches/0000-cover-letter.patch | \
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000351sed "s/\*\*\* BLURB HERE \*\*\*/\
Quentin Monnet164585e2021-11-12 00:57:35 +0000352sync: Pull latest bpftool changes from kernel\n\
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000353\n\
Quentin Monnet164585e2021-11-12 00:57:35 +0000354Syncing latest bpftool commits from kernel repository.\n\
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000355Baseline bpf-next commit: ${BASELINE_COMMIT}\n\
356Checkpoint bpf-next commit: ${TIP_COMMIT}\n\
357Baseline bpf commit: ${BPF_BASELINE_COMMIT}\n\
Quentin Monnet4dfcd142022-02-16 15:22:20 +0000358Checkpoint bpf commit: ${BPF_TIP_COMMIT}/" | \
Quentin Monnetd10e1e12022-11-16 17:43:38 +0000359git commit --signoff --file=-
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000360
361echo "SUCCESS! ${COMMIT_CNT} commits synced."
362
Quentin Monnet164585e2021-11-12 00:57:35 +0000363echo "Verifying Linux's and Github's bpftool state"
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000364
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100365cd_to "${LINUX_REPO}"
366git checkout -b "${VIEW_TAG}" "${TIP_COMMIT}"
367FILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch -f --tree-filter "${BPFTOOL_TREE_FILTER}" "${VIEW_TAG}"^.."${VIEW_TAG}"
368FILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch -f --subdirectory-filter __bpftool "${VIEW_TAG}"^.."${VIEW_TAG}"
369# shellcheck disable=SC2068
370git ls-files -- ${BPFTOOL_VIEW_PATHS[@]} | grep -v -E "${LINUX_VIEW_EXCLUDE_REGEX}" > "${TMP_DIR}"/linux-view.ls
Quentin Monnet7ee73da2022-01-27 22:05:28 +0000371# Before we compare each file, try to apply to the mirror a patch containing the
372# expected differences between the two repositories; this is to avoid checking
373# "known" differences visually and taking the risk of missing a new, relevant
374# differences.
375echo "Patching to account for expected differences..."
376patch -d "${LINUX_ABS_DIR}" -p0 -f --reject-file=- --no-backup-if-mismatch < "${GITHUB_ABS_DIR}/scripts/sync-kernel-expected-diff.patch" || true
377git add -u
378git commit -m 'tmp: apply expected differences to compare github/kernel repos' || true
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000379
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100380cd_to "${BPFTOOL_REPO}"
381# shellcheck disable=SC2068
382git ls-files -- ${BPFTOOL_VIEW_PATHS[@]} | grep -v -E "${BPFTOOL_VIEW_EXCLUDE_REGEX}" > "${TMP_DIR}"/github-view.ls
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000383
384echo "Comparing list of files..."
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100385diff -u "${TMP_DIR}"/linux-view.ls "${TMP_DIR}"/github-view.ls
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000386echo "Comparing file contents..."
387CONSISTENT=1
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100388while IFS= read -r F; do
Quentin Monnet164585e2021-11-12 00:57:35 +0000389 if ! diff -u --color "${LINUX_ABS_DIR}/${F}" "${GITHUB_ABS_DIR}/${F}"; then
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000390 echo "${LINUX_ABS_DIR}/${F} and ${GITHUB_ABS_DIR}/${F} are different!"
391 CONSISTENT=0
392 fi
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100393done < "${TMP_DIR}"/linux-view.ls
Quentin Monnet7ee73da2022-01-27 22:05:28 +0000394echo ""
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100395if (("${CONSISTENT}" == 1)); then
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000396 echo "Great! Content is identical!"
397else
398 ignore_inconsistency=n
399 echo "Unfortunately, there are some inconsistencies, please double check."
Quentin Monnet7ee73da2022-01-27 22:05:28 +0000400 echo "Some of them may come from patches in bpf tree but absent from bpf-next."
401 echo "Note: I applied scripts/sync-kernel-expected-diff.patch before checking,"
402 echo "to account for expected changes. If this patch needs an update,"
403 echo "you can do it now with:"
404 echo "------"
405 echo " (cd \"${LINUX_ABS_DIR}\" && git -c advice.detachedHead=false checkout HEAD~)"
406 echo " for f in \$(cat \"${TMP_DIR}/linux-view.ls\"); do"
407 echo " diff -u --label \"\${f}\" --label \"\${f}\" \\"
408 echo " \"${LINUX_ABS_DIR}/\${f}\" \\"
409 echo " \"${GITHUB_ABS_DIR}/\${f}\""
410 echo " done > \"${GITHUB_ABS_DIR}/scripts/sync-kernel-expected-diff.patch\""
411 echo "------"
Quentin Monnet8e1d7262022-09-28 18:39:14 +0100412 read -rp "Does everything look good? [y/N]: " ignore_inconsistency
Quentin Monnet1e2278e2021-11-12 00:56:33 +0000413 case "${ignore_inconsistency}" in
414 "y" | "Y")
415 echo "Ok, proceeding..."
416 ;;
417 *)
418 echo "Oops, exiting with error..."
419 exit 4
420 esac
421fi
422
423cleanup