fix: Get parent branch the right way

This commit is contained in:
Tracey Clark 2025-12-07 17:25:28 -06:00
commit 7156b7ff01

View file

@ -3,6 +3,7 @@
current_date=$(date -I)
pr_dirs=()
src_dir=$HOME/kde/src
# Function to display script usage UPDATE
usage() {
@ -23,27 +24,32 @@ popd () {
command popd "$@" > /dev/null
}
# TODO add fix variable --fix / -f and only run fix function if passed
# TODO add fix variable --fix / -f and function
# get_branch()
# {
# $(git rev-parse --abbrev-ref HEAD)
# }
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 $(ls $HOME/kde/src);
for dir in $(find ${src_dir} -maxdepth 1 -type d);
do
# cd or
pushd .
# printf "Checking directory %s\n" ${dir}
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)
if [[ "${dir_branch}" -ne "master" ]]; then
printf "%s not on master\n" "${dir}"
get_top_branch
if [[ ! "${dir_branch}" == "${top_branch}" ]]; then
printf "%s not on its main branch\n" "${dir}"
pr_dirs+=("${dir}")
fi
popd
cd ${src_dir}
done
echo "Done checking branches"
}
print_array () {
@ -54,22 +60,35 @@ print_array () {
done
}
# TODO
# fix_branches () {
# a=("$@")
# for b in "${a[@]}";
# do
# echo "Fixing $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
if [[ ( $@ == "--help") || $@ == "-h" ]]; then
usage
exit 0
fi
printf "Getting KDE source directory branches...\n"
get_branch_per_dir
printf "\nNot on master:\n"
print_array "${pr_dirs[@]}"
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"