#!/usr/bin/env bash # Update solus package and infra repos current_date=$(date -I) pr_dirs=() src_dir=$HOME/kde/src # Function to display script usage UPDATE usage() { echo "Usage: $0" echo " -h, --help Display this help message" echo "This script takes no arguments." echo '' echo 'It will check each directory under' echo ' $HOME/kde/src and print any which are not on master.' } # Silence output for these to avoid cli spam pushd () { command pushd "$@" > /dev/null } popd () { command popd "$@" > /dev/null } # TODO add fix variable --fix / -f and only run fix function if passed get_top_branch () { top_branch_untrimmed=$(git log --oneline --graph --decorate | grep -o 'HEAD -> .*' | awk '/>\s/{ print $3 }') top_branch="${top_branch_untrimmed%?}" } get_branch_per_dir () { for dir in $(find ${src_dir} -maxdepth 1 -type d); do cd ${dir} if [[ "${dir}" == *"src/log" || "${dir}" == "${src_dir}" ]]; then printf "Skipping directory %s\n" ${dir} continue fi dir_branch=$(git rev-parse --abbrev-ref HEAD) get_top_branch if [[ ! "${dir_branch}" == "${top_branch}" ]]; then printf "%s not on its main branch\n" "${dir}" pr_dirs+=("${dir}") fi cd ${src_dir} done echo "Done checking branches" } print_array () { a=("$@") for b in "${a[@]}"; do echo "$b" done } fix_branches () { a=("$@") for b in "${a[@]}"; do echo "Fixing $b" cd ${b} get_top_branch git reset --hard git switch ${top_branch} git fetch && git pull done } if [[ ( $@ == "--help") || $@ == "-h" ]]; then usage exit 0 fi printf "Getting KDE source directory branches...\n" get_branch_per_dir if [[ ${#pr_dirs[@]} -eq 0 ]]; then echo "[INFO] No directories need to switch branches" exit 0 else printf "\n[INFO] Directories not on the top branch:\n" print_array "${pr_dirs[@]}" printf "\n[INFO] Switching branches to top branch\n" fix_branches "${pr_dirs[@]}" fi printf "[INFO] Directories are up to date"