#!/bin/sh

# Usage: git-up
#        git-reup
#
# Like git-pull but show a short and sexy log of changes
# immediately after merging (git-up) or rebasing (git-reup).
#
# Inspired by Kyle Neath's `git up' alias:
# http://gist.github.com/249223
# Stolen from Ryan Tomayko
# http://github.com/rtomayko/dotfiles/blob/rtomayko/bin/git-up

set -e

if [ "$(basename $0)" = "git-reup" ]; then
    # when invoked as git-reup, run as `git pull --rebase ...'
    PULL_ARGS="--rebase"
    FETCH_ARGS=""
else
    # when invoked as git-up, run as `git pull ...`
    PULL_ARGS="--ff-only"
    if [ " $@" = " " ]; then
        # when invoked without a repo/head, fetch (and prune) all remotes
        FETCH_ARGS="--all --prune"
    fi
fi

HEAD_OLD=$(git rev-parse HEAD)
git pull $PULL_ARGS $FETCH_ARGS $@
HEAD_NEW=$(git rev-parse HEAD)

if [ "$HEAD_OLD" != "$HEAD_NEW" ]; then
    # if HEAD was actually updated...
    if [ "$(basename $0)" = "git-reup" ]; then
        # if we're pulling with --rebase, show diffstat of all changes.
        # not sure why git-pull only does this when merging.
        echo "Diff:"
        git --no-pager diff --color --stat ${HEAD_OLD}.. | sed 's/^/ /'
    fi

    # show an abbreviated commit log of stuff that was just merged.
    echo "Log:"
    git log --color --pretty=oneline --abbrev-commit ${HEAD_OLD}.. | sed 's/^/  /'
fi