Skip to content

Instantly share code, notes, and snippets.

@fallroot
Last active February 27, 2019 08:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fallroot/ec5889d09134bf8495e41b5956e594f8 to your computer and use it in GitHub Desktop.
Save fallroot/ec5889d09134bf8495e41b5956e594f8 to your computer and use it in GitHub Desktop.
Diff two branch what was not cherry-picked and cherry-pick with -c option Raw

Git Cherry Diff

Git Cherry

git cherry [-v] [<upstream> [<head> [<limit>]]]

git-cherry - Find commits yet to be applied to upstream

Bash Function Definition

[function] <function_name> { 
    <statements>
}

Bash while Loop

while <expression>
do
    <statements>
done

while <expression>; do
    <statements>
done

https://www.gnu.org/software/bash/manual/bashref.html#Looping-Constructs

Bash if Statement

if <expression>
then
    <statements>
fi

if <expression>; then
    <statements>
fi

https://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs

Bash case Statement

case <expression> in
    <case1>) <statements>;;
    <case2>) <statements>;;
    ...
    *) <statements>;;
esac

getopts

Small getopts tutorial [Bash Hackers Wiki]

shift $((OPTIND-1))

while getopts <options>
do
    <statements>
done

Bash Positional parameters

# 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
$@

Differences between $* and $@

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."

Commands Substitution

https://www.gnu.org/software/bash/manual/bashref.html#Command-Substitution

$(command)
`command`

Arithmetic Evaluation

$((command))
$[command]
$ echo 1 + 2
$ echo $((1 + 2))
$ echo $[1 + 2]

grep

https://www.gnu.org/software/grep/manual/grep.html

-v, --invert-match Selected lines are those not matching any of the specified patterns.

sed

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/ .*//"

Final Code

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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment