Skip to content

Instantly share code, notes, and snippets.

@marc-h38
Created March 11, 2021 19:02
Show Gist options
  • Save marc-h38/a87e8b786b03af11ba60e4434a32e65f to your computer and use it in GitHub Desktop.
Save marc-h38/a87e8b786b03af11ba60e4434a32e65f to your computer and use it in GitHub Desktop.
#!/bin/sh
set -e
# Prints all the git branch --set-upstream-to= commands that must be run
# after a remote changed the name of one of its branches.
# set -x
# default values, use environement to override
: "${REMOTE_URL:=https://github.com/zephyrproject-rtos/zephyr}"
: "${old_branch_name:=master}"
: "${new_branch_name:=main}"
main()
(
# shellcheck disable=SC2039
local rem rem_url brnch br_rem tracked_branch
# Assumes people don't use whitespace in their remote names (otherwise
# should do a bash while .. done <(git remote)
for rem in $(git remote); do
rem_url=$(git config --get remote."$rem".url)
test "$rem_url" = ${REMOTE_URL} ||
test "$rem_url" = ${REMOTE_URL}.git ||
continue
printf 'Found remote: %s %s\n' "$rem" "$rem_url"
# no whitespace either, see above
for brnch in $(git branch); do
br_rem=$(git config --get branch."$brnch".remote) || true
test "$br_rem" = "$rem" ||
continue
tracked_branch=$(git config --get branch."$brnch".merge) || true
test "$tracked_branch" = refs/heads/"$old_branch_name" ||
continue
printf 'Please run:\n\tgit branch --set-upstream-to=%s %s\n' \
"$rem/$new_branch_name" "$brnch"
done
done
)
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment