Skip to content

Instantly share code, notes, and snippets.

@RichardBronosky
Created April 10, 2022 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardBronosky/e9bf247089fe36f5eda01c89d23bb798 to your computer and use it in GitHub Desktop.
Save RichardBronosky/e9bf247089fe36f5eda01c89d23bb798 to your computer and use it in GitHub Desktop.
CLI tool for merging dependabot PRs
#! /usr/bin/env bash
set -eu
_usage(){
cat<<EOF
Usage:
./dependabot-merge.sh get_branches | ./dependabot-merge.sh checkout_merge_push_delete
EOF
_funcs | sed $'1i Functions:\n; s/^/ /'
}
function get_branches(){
git branch --all | sed '/^ *remotes\/origin\/dependabot\//!d; s/ *remotes\/origin\///'
}
function checkout(){
_branches_from_stdin_or_args "$@"
for branch in "${branches[@]}"; do
branch="$1"
git fetch origin
git checkout -b $branch origin/$branch
#git merge master # the github suggested way
git merge --no-edit -s recursive -Xtheirs master
#git rebase master # github doesn't make the connection to the PR this way
done
}
function merge_and_push(){
_branches_from_stdin_or_args "$@"
for branch in "${branches[@]}"; do
git checkout master
git merge --no-edit --no-ff $branch # the github suggested way
#git merge --ff-only $branch # github doesn't make the connection to the PR this way
git push origin master
done
}
function delete(){
_branches_from_stdin_or_args "$@"
for branch in "${branches[@]}"; do
git branch --delete --force $branch
done
}
function delete_remote(){
_branches_from_stdin_or_args "$@"
for branch in "${branches[@]}"; do
git branch --delete --remotes $branch
git push --delete origin $branch
done
}
function checkout_merge_push_delete(){
_branches_from_stdin_or_args "$@"
echo "branches2: ${branches[@]}"
for branch in "${branches[@]}"; do
for cmd in checkout merge_and_push delete delete_remote; do
$cmd "$branch"
done
done
}
_branches_from_stdin_or_args(){
if [[ -n "${branches[@]}" ]]; then
return
elif [[ -p /dev/stdin ]] || ! [[ -t 0 ]]; then
echo "pipe"
mapfile -t branches
else
echo "args"
branches=("$@")
fi
echo "branches1: ${branches[@]}"
}
_funcs(){
declare -F | awk '$2=="-f" && $3~/^[^_]/{print $3}'
}
_main(){
if [[ -n "$@" ]]; then
cmd="$1"; shift
$cmd "$@"
else
_usage
fi
}
_main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment