blob: 304cbb11c2ea24ee26cf4f0cfd732b56dfe34f3e (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
#!/bin/sh
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: Copyright (c) 2024 by sterni
#
# WARNING: This script is not well tested and may find a way to eat your commits.
#
# git only-push lets you push a specific range or list of commits to a remote
# ref based on a given revision (defaults to refs/remotes/origin/HEAD). This can
# be useful to push a subset of commits (that are ready for review) from a local
# commit chain to a PR branch (or gerrit style review ref).
#
# This is achieved by cherry-picking the relevant commits onto the base revision
# in a temporary worktree. For this the commits need to apply independently of
# prior commits not included in the selection, of course.
#
# git only-push is to be considered experimental. Its command line interface is
# janky and may be revised.
set -eu
die() {
printf '%s: %s\n' "$(basename "$0")" "$2"
exit "$1"
}
usage() {
printf '%s\n' \
"git only-push [-n] [-f] [-x] [-b <rev>] -r <remote> -t <refspec> [--] <commit>..." \
>&2
}
base=refs/remotes/origin/HEAD
dry=false
# TODO(sterni): non-interactive mode, e.g. clean up also on cherry-pick failure
while getopts "b:r:t:nxfh" opt; do
case $opt in
# TODO(sterni): it is probably too close to --branch?
b)
base="$OPTARG"
;;
t)
to="$OPTARG"
;;
r)
remote="$OPTARG"
;;
n)
dry=true
;;
x)
cherry_pick_x=true
;;
f)
# TODO(sterni): support --force-with-lease
push_f=true
;;
h|?)
usage
# TODO(sterni): add man page
# shellcheck disable=SC2016
[ "$opt" = "h" ] && printf '
\t-r <remote>\tRemote 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-x\t\tUse `git cherry-pick -x` for creating cherry-picks.
\t-f\-\tForce push to remote ref.
\t-n\t\tDry run.
'
[ "$opt" = "h" ] && exit 0 || exit 100
;;
esac
done
shift $((OPTIND - 1))
if [ -z "${to:-}" ]; then
usage
die 100 "Missing -t flag"
fi
if [ -z "${remote:-}" ]; then
usage
die 100 "Missing -r flag"
fi
if [ "$#" -eq 0 ]; then
usage
die 100 "Missing commits"
fi
worktree=
cleanup() {
cd "$repo"
test -n "$worktree" && test -e "$worktree" \
&& git worktree remove "$worktree"
}
trap cleanup EXIT
# Resolve ranges, get them into chronological order
revs="$(git rev-list --no-walk "$@" | tac)"
repo="$(git rev-parse --show-toplevel)"
if $dry; then
printf 'Would create worktree and checkout %s\n' "$base" >&2
else
worktree="$(mktemp -d)"
git worktree add "$worktree" "$base"
cd "$worktree"
fi
for rev in $revs; do
if $dry; then
printf 'Would cherry pick %s\n' "$rev" >&2
else
no_cherry_pick=false
git cherry-pick ${cherry_pick_x:+-x} "$rev" || no_cherry_pick=true
if $no_cherry_pick; then
tmp="$worktree"
# Prevent cleanup from removing the worktree
worktree=""
die 101 "Could not cherry pick $rev. Please manually fixup worktree at $tmp"
fi
fi
done
if $dry; then
printf 'Would push resulting HEAD to %s on %s\n' "$to" "$remote" >&2
else
git push ${push_f:+-f} "$remote" "HEAD:$to"
fi
|