17c2aad20Sopenharmony_ci#!/bin/bash
27c2aad20Sopenharmony_ci
37c2aad20Sopenharmony_ciusage () {
47c2aad20Sopenharmony_ci	echo "USAGE: ./sync-kernel.sh <libbpf-repo> <kernel-repo> <bpf-branch>"
57c2aad20Sopenharmony_ci	echo ""
67c2aad20Sopenharmony_ci	echo "Set BPF_NEXT_BASELINE to override bpf-next tree commit, otherwise read from <libbpf-repo>/CHECKPOINT-COMMIT."
77c2aad20Sopenharmony_ci	echo "Set BPF_BASELINE to override bpf tree commit, otherwise read from <libbpf-repo>/BPF-CHECKPOINT-COMMIT."
87c2aad20Sopenharmony_ci	echo "Set MANUAL_MODE to 1 to manually control every cherry-picked commits."
97c2aad20Sopenharmony_ci	exit 1
107c2aad20Sopenharmony_ci}
117c2aad20Sopenharmony_ci
127c2aad20Sopenharmony_ciset -eu
137c2aad20Sopenharmony_ci
147c2aad20Sopenharmony_ciLIBBPF_REPO=${1-""}
157c2aad20Sopenharmony_ciLINUX_REPO=${2-""}
167c2aad20Sopenharmony_ciBPF_BRANCH=${3-""}
177c2aad20Sopenharmony_ciBASELINE_COMMIT=${BPF_NEXT_BASELINE:-$(cat ${LIBBPF_REPO}/CHECKPOINT-COMMIT)}
187c2aad20Sopenharmony_ciBPF_BASELINE_COMMIT=${BPF_BASELINE:-$(cat ${LIBBPF_REPO}/BPF-CHECKPOINT-COMMIT)}
197c2aad20Sopenharmony_ci
207c2aad20Sopenharmony_ciif [ -z "${LIBBPF_REPO}" ] || [ -z "${LINUX_REPO}" ]; then
217c2aad20Sopenharmony_ci	echo "Error: libbpf or linux repos are not specified"
227c2aad20Sopenharmony_ci	usage
237c2aad20Sopenharmony_cifi
247c2aad20Sopenharmony_ciif [ -z "${BPF_BRANCH}" ]; then
257c2aad20Sopenharmony_ci	echo "Error: linux's bpf tree branch is not specified"
267c2aad20Sopenharmony_ci	usage
277c2aad20Sopenharmony_cifi
287c2aad20Sopenharmony_ciif [ -z "${BASELINE_COMMIT}" ] || [ -z "${BPF_BASELINE_COMMIT}" ]; then
297c2aad20Sopenharmony_ci	echo "Error: bpf or bpf-next baseline commits are not provided"
307c2aad20Sopenharmony_ci	usage
317c2aad20Sopenharmony_cifi
327c2aad20Sopenharmony_ci
337c2aad20Sopenharmony_ciSUFFIX=$(date --utc +%Y-%m-%dT%H-%M-%S.%3NZ)
347c2aad20Sopenharmony_ciWORKDIR=$(pwd)
357c2aad20Sopenharmony_ciTMP_DIR=$(mktemp -d)
367c2aad20Sopenharmony_ci
377c2aad20Sopenharmony_citrap "cd ${WORKDIR}; exit" INT TERM EXIT
387c2aad20Sopenharmony_ci
397c2aad20Sopenharmony_cideclare -A PATH_MAP
407c2aad20Sopenharmony_ciPATH_MAP=(									\
417c2aad20Sopenharmony_ci	[tools/lib/bpf]=src							\
427c2aad20Sopenharmony_ci	[tools/include/uapi/linux/bpf_common.h]=include/uapi/linux/bpf_common.h	\
437c2aad20Sopenharmony_ci	[tools/include/uapi/linux/bpf.h]=include/uapi/linux/bpf.h		\
447c2aad20Sopenharmony_ci	[tools/include/uapi/linux/btf.h]=include/uapi/linux/btf.h		\
457c2aad20Sopenharmony_ci	[tools/include/uapi/linux/fcntl.h]=include/uapi/linux/fcntl.h		\
467c2aad20Sopenharmony_ci	[tools/include/uapi/linux/openat2.h]=include/uapi/linux/openat2.h	\
477c2aad20Sopenharmony_ci	[tools/include/uapi/linux/if_link.h]=include/uapi/linux/if_link.h	\
487c2aad20Sopenharmony_ci	[tools/include/uapi/linux/if_xdp.h]=include/uapi/linux/if_xdp.h		\
497c2aad20Sopenharmony_ci	[tools/include/uapi/linux/netdev.h]=include/uapi/linux/netdev.h		\
507c2aad20Sopenharmony_ci	[tools/include/uapi/linux/netlink.h]=include/uapi/linux/netlink.h	\
517c2aad20Sopenharmony_ci	[tools/include/uapi/linux/pkt_cls.h]=include/uapi/linux/pkt_cls.h	\
527c2aad20Sopenharmony_ci	[tools/include/uapi/linux/pkt_sched.h]=include/uapi/linux/pkt_sched.h	\
537c2aad20Sopenharmony_ci	[include/uapi/linux/perf_event.h]=include/uapi/linux/perf_event.h	\
547c2aad20Sopenharmony_ci	[Documentation/bpf/libbpf]=docs						\
557c2aad20Sopenharmony_ci)
567c2aad20Sopenharmony_ci
577c2aad20Sopenharmony_ciLIBBPF_PATHS=("${!PATH_MAP[@]}" ":^tools/lib/bpf/Makefile" ":^tools/lib/bpf/Build" ":^tools/lib/bpf/.gitignore" ":^tools/include/tools/libc_compat.h")
587c2aad20Sopenharmony_ciLIBBPF_VIEW_PATHS=("${PATH_MAP[@]}")
597c2aad20Sopenharmony_ciLIBBPF_VIEW_EXCLUDE_REGEX='^src/(Makefile|Build|test_libbpf\.c|bpf_helper_defs\.h|\.gitignore)$|^docs/(\.gitignore|api\.rst|conf\.py)$|^docs/sphinx/.*'
607c2aad20Sopenharmony_ciLINUX_VIEW_EXCLUDE_REGEX='^include/tools/libc_compat.h$'
617c2aad20Sopenharmony_ci
627c2aad20Sopenharmony_ciLIBBPF_TREE_FILTER="mkdir -p __libbpf/include/uapi/linux __libbpf/include/tools && "$'\\\n'
637c2aad20Sopenharmony_cifor p in "${!PATH_MAP[@]}"; do
647c2aad20Sopenharmony_ci	LIBBPF_TREE_FILTER+="git mv -kf ${p} __libbpf/${PATH_MAP[${p}]} && "$'\\\n'
657c2aad20Sopenharmony_cidone
667c2aad20Sopenharmony_ciLIBBPF_TREE_FILTER+="git rm --ignore-unmatch -f __libbpf/src/{Makefile,Build,test_libbpf.c,.gitignore} >/dev/null"
677c2aad20Sopenharmony_ci
687c2aad20Sopenharmony_cicd_to()
697c2aad20Sopenharmony_ci{
707c2aad20Sopenharmony_ci	cd ${WORKDIR} && cd "$1"
717c2aad20Sopenharmony_ci}
727c2aad20Sopenharmony_ci
737c2aad20Sopenharmony_ci# Output brief single-line commit description
747c2aad20Sopenharmony_ci# $1 - commit ref
757c2aad20Sopenharmony_cicommit_desc()
767c2aad20Sopenharmony_ci{
777c2aad20Sopenharmony_ci	git log -n1 --pretty='%h ("%s")' $1
787c2aad20Sopenharmony_ci}
797c2aad20Sopenharmony_ci
807c2aad20Sopenharmony_ci# Create commit single-line signature, which consists of:
817c2aad20Sopenharmony_ci# - full commit subject
827c2aad20Sopenharmony_ci# - author date in ISO8601 format
837c2aad20Sopenharmony_ci# - full commit body with newlines replaced with vertical bars (|)
847c2aad20Sopenharmony_ci# - shortstat appended at the end
857c2aad20Sopenharmony_ci# The idea is that this single-line signature is good enough to make final
867c2aad20Sopenharmony_ci# decision about whether two commits are the same, across different repos.
877c2aad20Sopenharmony_ci# $1 - commit ref
887c2aad20Sopenharmony_ci# $2 - paths filter
897c2aad20Sopenharmony_cicommit_signature()
907c2aad20Sopenharmony_ci{
917c2aad20Sopenharmony_ci	local ref=$1
927c2aad20Sopenharmony_ci	shift
937c2aad20Sopenharmony_ci	git show --pretty='("%s")|%aI|%b' --shortstat $ref -- "${@-.}" | tr '\n' '|'
947c2aad20Sopenharmony_ci}
957c2aad20Sopenharmony_ci
967c2aad20Sopenharmony_ci# Cherry-pick commits touching libbpf-related files
977c2aad20Sopenharmony_ci# $1 - baseline_tag
987c2aad20Sopenharmony_ci# $2 - tip_tag
997c2aad20Sopenharmony_cicherry_pick_commits()
1007c2aad20Sopenharmony_ci{
1017c2aad20Sopenharmony_ci	local manual_mode=${MANUAL_MODE:-0}
1027c2aad20Sopenharmony_ci	local baseline_tag=$1
1037c2aad20Sopenharmony_ci	local tip_tag=$2
1047c2aad20Sopenharmony_ci	local new_commits
1057c2aad20Sopenharmony_ci	local signature
1067c2aad20Sopenharmony_ci	local should_skip
1077c2aad20Sopenharmony_ci	local synced_cnt
1087c2aad20Sopenharmony_ci	local manual_check
1097c2aad20Sopenharmony_ci	local libbpf_conflict_cnt
1107c2aad20Sopenharmony_ci	local desc
1117c2aad20Sopenharmony_ci
1127c2aad20Sopenharmony_ci	new_commits=$(git rev-list --no-merges --topo-order --reverse ${baseline_tag}..${tip_tag} -- "${LIBBPF_PATHS[@]}")
1137c2aad20Sopenharmony_ci	for new_commit in ${new_commits}; do
1147c2aad20Sopenharmony_ci		desc="$(commit_desc ${new_commit})"
1157c2aad20Sopenharmony_ci		signature="$(commit_signature ${new_commit} "${LIBBPF_PATHS[@]}")"
1167c2aad20Sopenharmony_ci		synced_cnt=$(grep -F "${signature}" ${TMP_DIR}/libbpf_commits.txt | wc -l)
1177c2aad20Sopenharmony_ci		manual_check=0
1187c2aad20Sopenharmony_ci		if ((${synced_cnt} > 0)); then
1197c2aad20Sopenharmony_ci			# commit with the same subject is already in libbpf, but it's
1207c2aad20Sopenharmony_ci			# not 100% the same commit, so check with user
1217c2aad20Sopenharmony_ci			echo "Commit '${desc}' is synced into libbpf as:"
1227c2aad20Sopenharmony_ci			grep -F "${signature}" ${TMP_DIR}/libbpf_commits.txt | \
1237c2aad20Sopenharmony_ci				cut -d'|' -f1 | sed -e 's/^/- /'
1247c2aad20Sopenharmony_ci			if ((${manual_mode} != 1 && ${synced_cnt} == 1)); then
1257c2aad20Sopenharmony_ci				echo "Skipping '${desc}' due to unique match..."
1267c2aad20Sopenharmony_ci				continue
1277c2aad20Sopenharmony_ci			fi
1287c2aad20Sopenharmony_ci			if ((${synced_cnt} > 1)); then
1297c2aad20Sopenharmony_ci				echo "'${desc} matches multiple commits, please, double-check!"
1307c2aad20Sopenharmony_ci				manual_check=1
1317c2aad20Sopenharmony_ci			fi
1327c2aad20Sopenharmony_ci		fi
1337c2aad20Sopenharmony_ci		if ((${manual_mode} == 1 || ${manual_check} == 1)); then
1347c2aad20Sopenharmony_ci			read -p "Do you want to skip '${desc}'? [y/N]: " should_skip
1357c2aad20Sopenharmony_ci			case "${should_skip}" in
1367c2aad20Sopenharmony_ci				"y" | "Y")
1377c2aad20Sopenharmony_ci					echo "Skipping '${desc}'..."
1387c2aad20Sopenharmony_ci					continue
1397c2aad20Sopenharmony_ci					;;
1407c2aad20Sopenharmony_ci			esac
1417c2aad20Sopenharmony_ci		fi
1427c2aad20Sopenharmony_ci		# commit hasn't been synced into libbpf yet
1437c2aad20Sopenharmony_ci		echo "Picking '${desc}'..."
1447c2aad20Sopenharmony_ci		if ! git cherry-pick ${new_commit} &>/dev/null; then
1457c2aad20Sopenharmony_ci			echo "Warning! Cherry-picking '${desc} failed, checking if it's non-libbpf files causing problems..."
1467c2aad20Sopenharmony_ci			libbpf_conflict_cnt=$(git diff --name-only --diff-filter=U -- "${LIBBPF_PATHS[@]}" | wc -l)
1477c2aad20Sopenharmony_ci			conflict_cnt=$(git diff --name-only | wc -l)
1487c2aad20Sopenharmony_ci			prompt_resolution=1
1497c2aad20Sopenharmony_ci
1507c2aad20Sopenharmony_ci			if ((${libbpf_conflict_cnt} == 0)); then
1517c2aad20Sopenharmony_ci				echo "Looks like only non-libbpf files have conflicts, ignoring..."
1527c2aad20Sopenharmony_ci				if ((${conflict_cnt} == 0)); then
1537c2aad20Sopenharmony_ci					echo "Empty cherry-pick, skipping it..."
1547c2aad20Sopenharmony_ci					git cherry-pick --abort
1557c2aad20Sopenharmony_ci					continue
1567c2aad20Sopenharmony_ci				fi
1577c2aad20Sopenharmony_ci
1587c2aad20Sopenharmony_ci				git add .
1597c2aad20Sopenharmony_ci				# GIT_EDITOR=true to avoid editor popping up to edit commit message
1607c2aad20Sopenharmony_ci				if ! GIT_EDITOR=true git cherry-pick --continue &>/dev/null; then
1617c2aad20Sopenharmony_ci					echo "Error! That still failed! Please resolve manually."
1627c2aad20Sopenharmony_ci				else
1637c2aad20Sopenharmony_ci					echo "Success! All cherry-pick conflicts were resolved for '${desc}'!"
1647c2aad20Sopenharmony_ci					prompt_resolution=0
1657c2aad20Sopenharmony_ci				fi
1667c2aad20Sopenharmony_ci			fi
1677c2aad20Sopenharmony_ci
1687c2aad20Sopenharmony_ci			if ((${prompt_resolution} == 1)); then
1697c2aad20Sopenharmony_ci				read -p "Error! Cherry-picking '${desc}' failed, please fix manually and press <return> to proceed..."
1707c2aad20Sopenharmony_ci			fi
1717c2aad20Sopenharmony_ci		fi
1727c2aad20Sopenharmony_ci		# Append signature of just cherry-picked commit to avoid
1737c2aad20Sopenharmony_ci		# potentially cherry-picking the same commit twice later when
1747c2aad20Sopenharmony_ci		# processing bpf tree commits. At this point we don't know yet
1757c2aad20Sopenharmony_ci		# the final commit sha in libbpf repo, so we record Linux SHA
1767c2aad20Sopenharmony_ci		# instead as LINUX_<sha>.
1777c2aad20Sopenharmony_ci		echo LINUX_$(git log --pretty='%h' -n1) "${signature}" >> ${TMP_DIR}/libbpf_commits.txt
1787c2aad20Sopenharmony_ci	done
1797c2aad20Sopenharmony_ci}
1807c2aad20Sopenharmony_ci
1817c2aad20Sopenharmony_cicleanup()
1827c2aad20Sopenharmony_ci{
1837c2aad20Sopenharmony_ci	echo "Cleaning up..."
1847c2aad20Sopenharmony_ci	rm -r ${TMP_DIR}
1857c2aad20Sopenharmony_ci	cd_to ${LINUX_REPO}
1867c2aad20Sopenharmony_ci	git checkout ${TIP_SYM_REF}
1877c2aad20Sopenharmony_ci	git branch -D ${BASELINE_TAG} ${TIP_TAG} ${BPF_BASELINE_TAG} ${BPF_TIP_TAG} \
1887c2aad20Sopenharmony_ci		      ${SQUASH_BASE_TAG} ${SQUASH_TIP_TAG} ${VIEW_TAG} || true
1897c2aad20Sopenharmony_ci
1907c2aad20Sopenharmony_ci	cd_to .
1917c2aad20Sopenharmony_ci	echo "DONE."
1927c2aad20Sopenharmony_ci}
1937c2aad20Sopenharmony_ci
1947c2aad20Sopenharmony_ci
1957c2aad20Sopenharmony_cicd_to ${LIBBPF_REPO}
1967c2aad20Sopenharmony_ciGITHUB_ABS_DIR=$(pwd)
1977c2aad20Sopenharmony_ciecho "Dumping existing libbpf commit signatures..."
1987c2aad20Sopenharmony_cifor h in $(git log --pretty='%h' -n500); do
1997c2aad20Sopenharmony_ci	echo $h "$(commit_signature $h)" >> ${TMP_DIR}/libbpf_commits.txt
2007c2aad20Sopenharmony_cidone
2017c2aad20Sopenharmony_ci
2027c2aad20Sopenharmony_ci# Use current kernel repo HEAD as a source of patches
2037c2aad20Sopenharmony_cicd_to ${LINUX_REPO}
2047c2aad20Sopenharmony_ciLINUX_ABS_DIR=$(pwd)
2057c2aad20Sopenharmony_ciTIP_SYM_REF=$(git symbolic-ref -q --short HEAD || git rev-parse HEAD)
2067c2aad20Sopenharmony_ciTIP_COMMIT=$(git rev-parse HEAD)
2077c2aad20Sopenharmony_ciBPF_TIP_COMMIT=$(git rev-parse ${BPF_BRANCH})
2087c2aad20Sopenharmony_ciBASELINE_TAG=libbpf-baseline-${SUFFIX}
2097c2aad20Sopenharmony_ciTIP_TAG=libbpf-tip-${SUFFIX}
2107c2aad20Sopenharmony_ciBPF_BASELINE_TAG=libbpf-bpf-baseline-${SUFFIX}
2117c2aad20Sopenharmony_ciBPF_TIP_TAG=libbpf-bpf-tip-${SUFFIX}
2127c2aad20Sopenharmony_ciVIEW_TAG=libbpf-view-${SUFFIX}
2137c2aad20Sopenharmony_ciLIBBPF_SYNC_TAG=libbpf-sync-${SUFFIX}
2147c2aad20Sopenharmony_ci
2157c2aad20Sopenharmony_ci# Squash state of kernel repo at baseline into single commit
2167c2aad20Sopenharmony_ciSQUASH_BASE_TAG=libbpf-squash-base-${SUFFIX}
2177c2aad20Sopenharmony_ciSQUASH_TIP_TAG=libbpf-squash-tip-${SUFFIX}
2187c2aad20Sopenharmony_ciSQUASH_COMMIT=$(git commit-tree ${BASELINE_COMMIT}^{tree} -m "BASELINE SQUASH ${BASELINE_COMMIT}")
2197c2aad20Sopenharmony_ci
2207c2aad20Sopenharmony_ciecho "WORKDIR:          ${WORKDIR}"
2217c2aad20Sopenharmony_ciecho "LINUX REPO:       ${LINUX_REPO}"
2227c2aad20Sopenharmony_ciecho "LIBBPF REPO:      ${LIBBPF_REPO}"
2237c2aad20Sopenharmony_ciecho "TEMP DIR:         ${TMP_DIR}"
2247c2aad20Sopenharmony_ciecho "SUFFIX:           ${SUFFIX}"
2257c2aad20Sopenharmony_ciecho "BASE COMMIT:      '$(commit_desc ${BASELINE_COMMIT})'"
2267c2aad20Sopenharmony_ciecho "TIP COMMIT:       '$(commit_desc ${TIP_COMMIT})'"
2277c2aad20Sopenharmony_ciecho "BPF BASE COMMIT:  '$(commit_desc ${BPF_BASELINE_COMMIT})'"
2287c2aad20Sopenharmony_ciecho "BPF TIP COMMIT:   '$(commit_desc ${BPF_TIP_COMMIT})'"
2297c2aad20Sopenharmony_ciecho "SQUASH COMMIT:    ${SQUASH_COMMIT}"
2307c2aad20Sopenharmony_ciecho "BASELINE TAG:     ${BASELINE_TAG}"
2317c2aad20Sopenharmony_ciecho "TIP TAG:          ${TIP_TAG}"
2327c2aad20Sopenharmony_ciecho "BPF BASELINE TAG: ${BPF_BASELINE_TAG}"
2337c2aad20Sopenharmony_ciecho "BPF TIP TAG:      ${BPF_TIP_TAG}"
2347c2aad20Sopenharmony_ciecho "SQUASH BASE TAG:  ${SQUASH_BASE_TAG}"
2357c2aad20Sopenharmony_ciecho "SQUASH TIP TAG:   ${SQUASH_TIP_TAG}"
2367c2aad20Sopenharmony_ciecho "VIEW TAG:         ${VIEW_TAG}"
2377c2aad20Sopenharmony_ciecho "LIBBPF SYNC TAG:  ${LIBBPF_SYNC_TAG}"
2387c2aad20Sopenharmony_ciecho "PATCHES:          ${TMP_DIR}/patches"
2397c2aad20Sopenharmony_ci
2407c2aad20Sopenharmony_cigit branch ${BASELINE_TAG} ${BASELINE_COMMIT}
2417c2aad20Sopenharmony_cigit branch ${TIP_TAG} ${TIP_COMMIT}
2427c2aad20Sopenharmony_cigit branch ${BPF_BASELINE_TAG} ${BPF_BASELINE_COMMIT}
2437c2aad20Sopenharmony_cigit branch ${BPF_TIP_TAG} ${BPF_TIP_COMMIT}
2447c2aad20Sopenharmony_cigit branch ${SQUASH_BASE_TAG} ${SQUASH_COMMIT}
2457c2aad20Sopenharmony_cigit checkout -b ${SQUASH_TIP_TAG} ${SQUASH_COMMIT}
2467c2aad20Sopenharmony_ci
2477c2aad20Sopenharmony_ci# Cherry-pick new commits onto squashed baseline commit
2487c2aad20Sopenharmony_cicherry_pick_commits ${BASELINE_TAG} ${TIP_TAG}
2497c2aad20Sopenharmony_cicherry_pick_commits ${BPF_BASELINE_TAG} ${BPF_TIP_TAG}
2507c2aad20Sopenharmony_ci
2517c2aad20Sopenharmony_ci# Move all libbpf files into __libbpf directory.
2527c2aad20Sopenharmony_ciFILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch --prune-empty -f --tree-filter "${LIBBPF_TREE_FILTER}" ${SQUASH_TIP_TAG} ${SQUASH_BASE_TAG}
2537c2aad20Sopenharmony_ci# Make __libbpf a new root directory
2547c2aad20Sopenharmony_ciFILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch --prune-empty -f --subdirectory-filter __libbpf ${SQUASH_TIP_TAG} ${SQUASH_BASE_TAG}
2557c2aad20Sopenharmony_ci
2567c2aad20Sopenharmony_ci# If there are no new commits with  libbpf-related changes, bail out
2577c2aad20Sopenharmony_ciCOMMIT_CNT=$(git rev-list --count ${SQUASH_BASE_TAG}..${SQUASH_TIP_TAG})
2587c2aad20Sopenharmony_ciif ((${COMMIT_CNT} <= 0)); then
2597c2aad20Sopenharmony_ci    echo "No new changes to apply, we are done!"
2607c2aad20Sopenharmony_ci    cleanup
2617c2aad20Sopenharmony_ci    exit 2
2627c2aad20Sopenharmony_cifi
2637c2aad20Sopenharmony_ci
2647c2aad20Sopenharmony_ci# Exclude baseline commit and generate nice cover letter with summary
2657c2aad20Sopenharmony_cigit format-patch --no-signature ${SQUASH_BASE_TAG}..${SQUASH_TIP_TAG} --cover-letter -o ${TMP_DIR}/patches
2667c2aad20Sopenharmony_ci
2677c2aad20Sopenharmony_ci# Now is time to re-apply libbpf-related linux patches to libbpf repo
2687c2aad20Sopenharmony_cicd_to ${LIBBPF_REPO}
2697c2aad20Sopenharmony_cigit checkout -b ${LIBBPF_SYNC_TAG}
2707c2aad20Sopenharmony_ci
2717c2aad20Sopenharmony_cifor patch in $(ls -1 ${TMP_DIR}/patches | tail -n +2); do
2727c2aad20Sopenharmony_ci	if ! git am -3 --committer-date-is-author-date "${TMP_DIR}/patches/${patch}"; then
2737c2aad20Sopenharmony_ci		if ! patch -p1 --merge < "${TMP_DIR}/patches/${patch}"; then
2747c2aad20Sopenharmony_ci			read -p "Applying ${TMP_DIR}/patches/${patch} failed, please resolve manually and press <return> to proceed..."
2757c2aad20Sopenharmony_ci		fi
2767c2aad20Sopenharmony_ci		git am --continue
2777c2aad20Sopenharmony_ci	fi
2787c2aad20Sopenharmony_cidone
2797c2aad20Sopenharmony_ci
2807c2aad20Sopenharmony_ci# Generate bpf_helper_defs.h and commit, if anything changed
2817c2aad20Sopenharmony_ci# restore Linux tip to use bpf_doc.py
2827c2aad20Sopenharmony_cicd_to ${LINUX_REPO}
2837c2aad20Sopenharmony_cigit checkout ${TIP_TAG}
2847c2aad20Sopenharmony_ci# re-generate bpf_helper_defs.h
2857c2aad20Sopenharmony_cicd_to ${LIBBPF_REPO}
2867c2aad20Sopenharmony_ci"${LINUX_ABS_DIR}/scripts/bpf_doc.py" --header					      \
2877c2aad20Sopenharmony_ci	--file include/uapi/linux/bpf.h > src/bpf_helper_defs.h
2887c2aad20Sopenharmony_ci# if anything changed, commit it
2897c2aad20Sopenharmony_cihelpers_changes=$(git status --porcelain src/bpf_helper_defs.h | wc -l)
2907c2aad20Sopenharmony_ciif ((${helpers_changes} == 1)); then
2917c2aad20Sopenharmony_ci	git add src/bpf_helper_defs.h
2927c2aad20Sopenharmony_ci	git commit -s -m "sync: auto-generate latest BPF helpers
2937c2aad20Sopenharmony_ci
2947c2aad20Sopenharmony_ciLatest changes to BPF helper definitions.
2957c2aad20Sopenharmony_ci" -- src/bpf_helper_defs.h
2967c2aad20Sopenharmony_cifi
2977c2aad20Sopenharmony_ci
2987c2aad20Sopenharmony_ci# Use generated cover-letter as a template for "sync commit" with
2997c2aad20Sopenharmony_ci# baseline and checkpoint commits from kernel repo (and leave summary
3007c2aad20Sopenharmony_ci# from cover letter intact, of course)
3017c2aad20Sopenharmony_ciecho ${TIP_COMMIT} > CHECKPOINT-COMMIT &&					      \
3027c2aad20Sopenharmony_ciecho ${BPF_TIP_COMMIT} > BPF-CHECKPOINT-COMMIT &&				      \
3037c2aad20Sopenharmony_cigit add CHECKPOINT-COMMIT &&							      \
3047c2aad20Sopenharmony_cigit add BPF-CHECKPOINT-COMMIT &&						      \
3057c2aad20Sopenharmony_ciawk '/\*\*\* BLURB HERE \*\*\*/ {p=1} p' ${TMP_DIR}/patches/0000-cover-letter.patch | \
3067c2aad20Sopenharmony_cised "s/\*\*\* BLURB HERE \*\*\*/\
3077c2aad20Sopenharmony_cisync: latest libbpf changes from kernel\n\
3087c2aad20Sopenharmony_ci\n\
3097c2aad20Sopenharmony_ciSyncing latest libbpf commits from kernel repository.\n\
3107c2aad20Sopenharmony_ciBaseline bpf-next commit:   ${BASELINE_COMMIT}\n\
3117c2aad20Sopenharmony_ciCheckpoint bpf-next commit: ${TIP_COMMIT}\n\
3127c2aad20Sopenharmony_ciBaseline bpf commit:        ${BPF_BASELINE_COMMIT}\n\
3137c2aad20Sopenharmony_ciCheckpoint bpf commit:      ${BPF_TIP_COMMIT}/" |				      \
3147c2aad20Sopenharmony_cigit commit -s --file=-
3157c2aad20Sopenharmony_ci
3167c2aad20Sopenharmony_ciecho "SUCCESS! ${COMMIT_CNT} commits synced."
3177c2aad20Sopenharmony_ci
3187c2aad20Sopenharmony_ciecho "Verifying Linux's and Github's libbpf state"
3197c2aad20Sopenharmony_ci
3207c2aad20Sopenharmony_cicd_to ${LINUX_REPO}
3217c2aad20Sopenharmony_cigit checkout -b ${VIEW_TAG} ${TIP_COMMIT}
3227c2aad20Sopenharmony_ciFILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch -f --tree-filter "${LIBBPF_TREE_FILTER}" ${VIEW_TAG}^..${VIEW_TAG}
3237c2aad20Sopenharmony_ciFILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch -f --subdirectory-filter __libbpf ${VIEW_TAG}^..${VIEW_TAG}
3247c2aad20Sopenharmony_cigit ls-files -- "${LIBBPF_VIEW_PATHS[@]}" | grep -v -E "${LINUX_VIEW_EXCLUDE_REGEX}" > ${TMP_DIR}/linux-view.ls
3257c2aad20Sopenharmony_ci
3267c2aad20Sopenharmony_cicd_to ${LIBBPF_REPO}
3277c2aad20Sopenharmony_cigit ls-files -- "${LIBBPF_VIEW_PATHS[@]}" | grep -v -E "${LIBBPF_VIEW_EXCLUDE_REGEX}" > ${TMP_DIR}/github-view.ls
3287c2aad20Sopenharmony_ci
3297c2aad20Sopenharmony_ciecho "Comparing list of files..."
3307c2aad20Sopenharmony_cidiff -u ${TMP_DIR}/linux-view.ls ${TMP_DIR}/github-view.ls
3317c2aad20Sopenharmony_ciecho "Comparing file contents..."
3327c2aad20Sopenharmony_ciCONSISTENT=1
3337c2aad20Sopenharmony_cifor F in $(cat ${TMP_DIR}/linux-view.ls); do
3347c2aad20Sopenharmony_ci	if ! diff -u "${LINUX_ABS_DIR}/${F}" "${GITHUB_ABS_DIR}/${F}"; then
3357c2aad20Sopenharmony_ci		echo "${LINUX_ABS_DIR}/${F} and ${GITHUB_ABS_DIR}/${F} are different!"
3367c2aad20Sopenharmony_ci		CONSISTENT=0
3377c2aad20Sopenharmony_ci	fi
3387c2aad20Sopenharmony_cidone
3397c2aad20Sopenharmony_ciif ((${CONSISTENT} == 1)); then
3407c2aad20Sopenharmony_ci	echo "Great! Content is identical!"
3417c2aad20Sopenharmony_cielse
3427c2aad20Sopenharmony_ci	ignore_inconsistency=n
3437c2aad20Sopenharmony_ci	echo "Unfortunately, there are some inconsistencies, please double check."
3447c2aad20Sopenharmony_ci	read -p "Does everything look good? [y/N]: " ignore_inconsistency
3457c2aad20Sopenharmony_ci	case "${ignore_inconsistency}" in
3467c2aad20Sopenharmony_ci		"y" | "Y")
3477c2aad20Sopenharmony_ci			echo "Ok, proceeding..."
3487c2aad20Sopenharmony_ci			;;
3497c2aad20Sopenharmony_ci		*)
3507c2aad20Sopenharmony_ci			echo "Oops, exiting with error..."
3517c2aad20Sopenharmony_ci			exit 4
3527c2aad20Sopenharmony_ci	esac
3537c2aad20Sopenharmony_cifi
3547c2aad20Sopenharmony_ci
3557c2aad20Sopenharmony_cicleanup
356