Skip to content

Instantly share code, notes, and snippets.

@pswaminathan
Created April 2, 2020 15:18
Show Gist options
  • Save pswaminathan/b15229ce57fcf456bbdb4e9c51e2b04d to your computer and use it in GitHub Desktop.
Save pswaminathan/b15229ce57fcf456bbdb4e9c51e2b04d to your computer and use it in GitHub Desktop.
Git stack
#!/bin/sh
#
# git-stack: Push this commit to a branch specified in its
# commit description.
#
# -- IMPORTANT -- this script is for macOS (using BSD sed)
#
# Taken from https://wchargin.github.io/posts/managing-dependent-pull-requests/
# Copyright (c) 2017 William Chargin. Released under the MIT license.
set -eu
DIRECTIVE='stacked-branch' # any regex metacharacters should be escaped
BRANCH_PREFIX="${USER}/stacked/"
target_branch() {
directive="$( \
git show --pretty='%B' \
| sed -n -E 's/^'"${DIRECTIVE}"': ([A-Za-z0-9_.-]+)$/\1/p' \
; )"
if [ -z "${directive}" ]; then
printf >&2 'error: missing "%s" directive\n' "${DIRECTIVE}"
return 1
fi
if [ "$(printf '%s\n' "${directive}" | wc -l)" -gt 1 ]; then
printf >&2 'error: multiple "%s" directives\n' "${DIRECTIVE}"
return 1
fi
printf '%s%s\n' "${BRANCH_PREFIX}" "${directive}"
}
main() {
if [ "${1:-}" = "--query" ]; then
target_branch
return
fi
remote="${1:-origin}"
branch="$(target_branch)"
set -x
git push --force-with-lease "${remote}" HEAD:refs/heads/"${branch}"
}
main "$@"
@colinrgodsey
Copy link

Script needs to be marked executable and placed somewhere on the PATH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment