#!/usr/bin/env bash
function kernel_prepare_bare_repo_decide_shallow_or_full() {
declare decision="not_yet_decided" decision_why="unknown"
declare -i ask_for_user_confirmation=0
declare cache_git_bare_dir="${SRC}/cache/git-bare"
declare FULL_kernel_git_bare_tree="${cache_git_bare_dir}/kernel"
declare FULL_marker="git-bare/kernel/"
declare SHALLOW_kernel_git_bare_tree="${cache_git_bare_dir}/shallow-kernel-${KERNEL_MAJOR_MINOR}"
git_bundles_dir="${SRC}/cache/git-bundles/kernel"
run_host_command_logged mkdir -p "${cache_git_bare_dir}"
[[ -z "${kernel_work_dir}" ]] && exit_with_error "kernel_work_dir is not set"
[[ -z "${bare_tree_done_marker_file}" ]] && exit_with_error "bare_tree_done_marker_file is not set"
declare device_backing_dir
device_backing_dir="$(findmnt --nofsroot -n -o SOURCE --target "${cache_git_bare_dir}")"
declare -i device_backing_dir_is_slow=0
if [[ "${device_backing_dir}" == *"mmc"* ]]; then
display_alert "Device backing ${cache_git_bare_dir} is eMMC/SD/etc" "${device_backing_dir}" "git"
device_backing_dir_is_slow=1
fi
display_alert "Device backing ${cache_git_bare_dir}" "${device_backing_dir}" "git"
declare -i free_space_mib
free_space_mib="$(df -BM --output=avail "${cache_git_bare_dir}" | tail -n 1 | sed 's/M//')"
display_alert "Free space on ${cache_git_bare_dir}" "${free_space_mib} MiB" "git"
if [[ "${decision}" == "not_yet_decided" ]]; then
if [[ -f "${kernel_work_dir}/.git" ]]; then
display_alert "Worktree .git file already exists" "${kernel_work_dir}/.git" "git"
if grep -q "${FULL_marker}" "${kernel_work_dir}/.git"; then
display_alert "Worktree .git file indicates full version bare" "${kernel_work_dir}/.git" "git"
decision="full"
decision_why="existing worktree points to full"
else
display_alert "Worktree .git file does NOT indicate full bare" "${kernel_work_dir}/.git" "git"
decision="shallow"
decision_why="existing worktree points to shallow"
fi
else
display_alert "Worktree .git file does NOT exist" "${kernel_work_dir}/.git" "git"
fi
fi
if [[ "${decision}" == "not_yet_decided" ]]; then
if [[ -d "${FULL_kernel_git_bare_tree}" && -f "${FULL_kernel_git_bare_tree}/${bare_tree_done_marker_file}" ]]; then
display_alert "Full version bare tree exists and is ready to go" "${FULL_kernel_git_bare_tree}" "git"
decision="full"
decision_why="full version bare tree exists and is ready to go"
else
display_alert "Full version bare tree does NOT exist or is NOT ready to go" "${FULL_kernel_git_bare_tree}" "git"
fi
fi
declare forced_decision="${KERNEL_GIT:-"none"}"
if [[ "${decision}" == "not_yet_decided" ]]; then
case "${forced_decision,,}" in
shallow)
display_alert "Forced shallow via" "KERNEL_GIT=shallow" "git"
decision="shallow"
decision_why="forced by KERNEL_GIT=shallow"
;;
full)
display_alert "Forced full via" "KERNEL_GIT=full" "git"
decision="full"
decision_why="forced by KERNEL_GIT=full"
;;
esac
elif [[ "${forced_decision}" != "none" && "${forced_decision}" != "${decision}" ]]; then
display_alert "Can't change Kernel git from '${decision}' to '${forced_decision}'" "${decision_why}" "warn"
countdown_and_continue_if_not_aborted 3
fi
if [[ "${decision}" == "not_yet_decided" ]]; then
if [[ -d "${SHALLOW_kernel_git_bare_tree}" && -f "${SHALLOW_kernel_git_bare_tree}/${bare_tree_done_marker_file}" ]]; then
display_alert "Shallow bare tree for {KERNEL_MAJOR_MINOR} exists and is ready to go" "${SHALLOW_kernel_git_bare_tree}" "git"
decision="shallow"
decision_why="shallow ${KERNEL_MAJOR_MINOR} bare tree exists and is ready to go"
else
display_alert "Shallow bare tree for ${KERNEL_MAJOR_MINOR} does NOT exist or is NOT ready to go" "${SHALLOW_kernel_git_bare_tree}" "git"
fi
fi
if [[ "${decision}" == "not_yet_decided" ]]; then
ask_for_user_confirmation=1
if [[ ${free_space_mib} -lt 32768 ]] || [[ ${device_backing_dir_is_slow} -eq 1 ]]; then
decision="shallow"
decision_why="slow storage device (${device_backing_dir}) or low disk space (${free_space_mib} MiB)"
else
decision="full"
decision_why="fast storage device (${device_backing_dir}) and enough disk space (${free_space_mib} MiB)"
fi
fi
display_alert "Using ${decision} Kernel bare tree for ${KERNEL_MAJOR_MINOR}" "${decision_why}" "info"
declare base_oras_ref="${GIT_ORAS_TARBALLS_SHALLOW_BASE_REF:-"${GHCR_SOURCE}/armbian/shallow"}"
declare estimated_dl_size_mib=0 benefits="" cons=""
case "${decision}" in
shallow)
kernel_git_bare_tree="${SHALLOW_kernel_git_bare_tree}"
git_kernel_ball_fn="linux-shallow-${KERNEL_MAJOR_MINOR}.git.tar"
git_kernel_oras_ref="${base_oras_ref}/kernel-git-shallow-${KERNEL_MAJOR_MINOR}:latest"
estimated_dl_size_mib=300
benefits="smaller download, less disk space consumed"
cons="less useful over time, no history, not shared across versions"
;;
*)
kernel_git_bare_tree="${FULL_kernel_git_bare_tree}"
git_kernel_ball_fn="linux-complete.git.tar"
git_kernel_oras_ref="${base_oras_ref}/kernel-git:latest"
estimated_dl_size_mib=2700
benefits="more useful over time, full history, single tree across all versions"
cons="bigger download, more disk space consumed"
;;
esac
display_alert "kernel_git_bare_tree" "${kernel_git_bare_tree}" "git"
display_alert "git_kernel_ball_fn" "${git_kernel_ball_fn}" "git"
display_alert "git_kernel_oras_ref" "${git_kernel_oras_ref}" "git"
display_alert "estimated_dl_size_mib" "${estimated_dl_size_mib}" "git"
if [[ ${ask_for_user_confirmation} -eq 1 && -t 0 ]]; then
echo "--------------------------------------------------------------------------------------------------------------------" >&2
display_alert "Warning: no Kernel bare tree exists for version ${KERNEL_MAJOR_MINOR} - about to start downloading." "" "wrn"
display_alert "Armbian is going to use a '${decision}' git tree, which is around" "${estimated_dl_size_mib}MiB" ""
display_alert "This was decided due to" "${decision_why}" ""
display_alert "Benefits of using a '${decision}' git tree" "${benefits}" "info"
display_alert "Cons of using a '${decision}' git tree" "${cons}" "wrn"
display_alert "You can abort now, and pass either KERNEL_GIT=full or KERNEL_GIT=shallow to force." "" "wrn"
display_alert "If you want to abort" "press Ctrl+C before the countdown ends in 60 seconds" "wrn"
display_alert "If you agree with the decision to use a '${decision}' git tree" "press any other key to continue" ""
countdown_and_continue_if_not_aborted 60
fi
return 0
}
function kernel_prepare_bare_repo_from_oras_gitball() {
if [[ -z "${kernel_git_bare_tree}" ]]; then
exit_with_error "kernel_git_bare_tree is not set"
fi
if [[ -z "${bare_tree_done_marker_file}" ]]; then
exit_with_error "bare_tree_done_marker_file is not set"
fi
git_oras_tree_prepare_from_gitball \
"Kernel" "${kernel_git_bare_tree}" "${bare_tree_done_marker_file}" \
"${git_kernel_oras_ref}" "${git_bundles_dir}" "${git_kernel_ball_fn}" \
"${SRC}/cache/sources/linux-kernel-worktree"
return 0
}