feat(sterni/git-only-push): support force pushing

This is occasionally necessary. --force-with-lease should also be
supported in the future, unfortunately getopts(1) doesn't have --long
option support.

Change-Id: Ib054009f48585b1a52ed041a51bcaf7e32dca1b3
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12904
Autosubmit: sterni <sternenseemann@systemli.org>
Reviewed-by: sterni <sternenseemann@systemli.org>
Tested-by: BuildkiteCI
This commit is contained in:
sterni 2024-12-22 14:05:07 +01:00 committed by clbot
parent 8459ad94fa
commit a2a33f1d06

View file

@ -25,7 +25,7 @@ die() {
usage() { usage() {
printf '%s\n' \ printf '%s\n' \
"git only-push [-n] [-x] [-b <rev>] -r <remote> -t <refspec> [--] <commit>..." \ "git only-push [-n] [-f] [-x] [-b <rev>] -r <remote> -t <refspec> [--] <commit>..." \
>&2 >&2
} }
@ -33,7 +33,7 @@ base=refs/remotes/origin/HEAD
dry=false dry=false
# TODO(sterni): non-interactive mode, e.g. clean up also on cherry-pick failure # TODO(sterni): non-interactive mode, e.g. clean up also on cherry-pick failure
while getopts "b:r:t:nxh" opt; do while getopts "b:r:t:nxfh" opt; do
case $opt in case $opt in
# TODO(sterni): it is probably too close to --branch? # TODO(sterni): it is probably too close to --branch?
b) b)
@ -51,6 +51,10 @@ while getopts "b:r:t:nxh" opt; do
x) x)
cherry_pick_x=true cherry_pick_x=true
;; ;;
f)
# TODO(sterni): support --force-with-lease
push_f=true
;;
h|?) h|?)
usage usage
# TODO(sterni): add man page # TODO(sterni): add man page
@ -60,6 +64,7 @@ while getopts "b:r:t:nxh" opt; do
\t-t <refspec>\tTarget ref to push to. \t-t <refspec>\tTarget ref to push to.
\t-b <rev>\tOptional: Base revision to cherry-pick commits onto. Defaults to refs/remotes/origin/HEAD. \t-b <rev>\tOptional: Base revision to cherry-pick commits onto. Defaults to refs/remotes/origin/HEAD.
\t-x\t\tUse `git cherry-pick -x` for creating cherry-picks. \t-x\t\tUse `git cherry-pick -x` for creating cherry-picks.
\t-f\-\tForce push to remote ref.
\t-n\t\tDry run. \t-n\t\tDry run.
' '
[ "$opt" = "h" ] && exit 0 || exit 100 [ "$opt" = "h" ] && exit 0 || exit 100
@ -124,5 +129,5 @@ done
if $dry; then if $dry; then
printf 'Would push resulting HEAD to %s on %s\n' "$to" "$remote" >&2 printf 'Would push resulting HEAD to %s on %s\n' "$to" "$remote" >&2
else else
git push "$remote" "HEAD:$to" git push ${push_f:+-f} "$remote" "HEAD:$to"
fi fi