Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 "$@"
@pswaminathan

This comment has been minimized.

Copy link
Owner Author

@pswaminathan pswaminathan commented Sep 17, 2020

Oh interesting! All I needed to do was to have this on my $PATH:

prasanna at Coxmate in ~
$ ll bin; echo $PATH
total 51664
drwxr-xr-x    5 prasanna  staff   160B Nov 26  2019 .
drwxr-xr-x+ 100 prasanna  staff   3.1K Sep 17 10:08 ..
-rwxr-xr-x    1 prasanna  staff   1.0K Apr  2 14:14 git-stack
-rwxr-xr-x    1 prasanna  staff    25M Nov 26  2019 ngrok
/Users/prasanna/bin:/Users/prasanna/go/bin:/Users/prasanna/.local/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Users/prasanna/.yadr/bin:/Users/prasanna/.yadr/bin/yadr:/Users/prasanna/Library/Android/sdk/platform-tools

@colinrgodsey

This comment has been minimized.

Copy link

@colinrgodsey colinrgodsey commented Sep 17, 2020

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
You can’t perform that action at this time.