git cherry [-v] [<upstream> [<head> [<limit>]]]
git-cherry - Find commits yet to be applied to upstream
[function] <function_name> {
<statements>
}
while <expression>
do
<statements>
done
while <expression>; do
<statements>
done
https://www.gnu.org/software/bash/manual/bashref.html#Looping-Constructs
if <expression>
then
<statements>
fi
if <expression>; then
<statements>
fi
https://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs
case <expression> in
<case1>) <statements>;;
<case2>) <statements>;;
...
*) <statements>;;
esac
Small getopts tutorial [Bash Hackers Wiki]
shift $((OPTIND-1))
while getopts <options>
do
<statements>
done
# positional parameters
$0, $1, ..., $9, ${10}
# number of arguments
$#
# all of the positional parameters as a single word
$*
# all of the positional parameters as a separate word
$@
index=1 # Initialize count.
echo "Listing args with \"\$*\":"
for arg in "$*" # Doesn't work properly if "$*" isn't quoted.
do
echo "Arg #$index = $arg"
let "index+=1"
done # $* sees all arguments as single word.
echo "Entire arg list seen as single word."
echo
index=1 # Reset count.
# What happens if you forget to do this?
echo "Listing args with \"\$@\":"
for arg in "$@"
do
echo "Arg #$index = $arg"
let "index+=1"
done # $@ sees arguments as separate words.
echo "Arg list seen as separate words."
https://www.gnu.org/software/bash/manual/bashref.html#Command-Substitution
$(command)
`command`
$((command))
$[command]
$ echo 1 + 2
$ echo $((1 + 2))
$ echo $[1 + 2]
https://www.gnu.org/software/grep/manual/grep.html
-v, --invert-match
Selected lines are those not matching any of the specified patterns.
https://www.gnu.org/software/sed/manual/sed.html
$ seq 3 | sed 2q
$ seq 3 | sed 2d
$ seq 3 | sed -n 2p
$ seq 6 | sed 'n;n;s/./x/'
$ seq 6 | sed '0~3s/./x/'
grep "^+" | sed "s/^+/#/"
sed "/^[^+]/d;s/^+/#/"
grep -v "^#" | sed "s/ .*//"
sed "/^#/d;s/ .*//"
cherry_diff() {
while getopts "c" opt
do
case $opt in
c)
do_cherry_pick=true
;;
esac
done
shift $(($OPTIND - 1))
if [ $# -lt 1 ]
then
echo "Usage: $0 [-c] [upstream_branch] working_branch"
return
fi
if [ $# -eq 1 ]
then
upstream_branch=$(git symbolic-ref --short HEAD)
working_branch=$1
else
upstream_branch=$1
working_branch=$2
fi
commits=$(git cherry -v $upstream_branch $working_branch | grep "^+" | sed "s/^+/#/" | $EDITOR)
if [ "$do_cherry_pick" = true ]
then
if [ $# -gt 1 ]
then
git checkout $upstream_branch
fi
echo "$commits" | grep -v "^#" | sed "s/ .*//" | xargs git cherry-pick
fi
}