aboutsummaryrefslogtreecommitdiff
path: root/scripts/git/git-resquash.sh
blob: e0f26ecdc427a3b3c56e7bdcf12ef89302570bc3 (plain)
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
#!/bin/sh
#
# Provides a convenient alias for "git rebase -i --autosquash --keep-root"
# on gits that have it, and a replacement on gits that don't.

set -e

PARENT="$1"

if test "x$PARENT" = "x"; then
    echo "You must specify the parent branch."
    exit 1
fi

# Can we use git rebase --keep-base?  Detect the git version to find out.
GITVER=$(git version)
if test "$(echo "$GITVER"|cut -d ' ' -f 1-2)" = "git version"; then
    # --keep-base was added in git 2.24.  Detect if we have that version.
    GITVER=$(echo "$GITVER" | cut -d ' ' -f 3)
    major=$(echo "$GITVER" | cut -d . -f 1)
    minor=$(echo "$GITVER" | cut -d . -f 2)
    if test "$major" -lt 2; then
        USE_KEEP_BASE=0
    elif test "$major" -eq 2 && test "$minor" -lt 24; then
        USE_KEEP_BASE=0
    else
        USE_KEEP_BASE=1
    fi
else
    # This isn't a git that reports its version in a way recognize; assume that
    # --keep-base will work
    USE_KEEP_BASE=1
fi

if test "x$USE_KEEP_BASE" = "x1" ; then
    exec git rebase -i --autosquash --keep-base "${PARENT}"
else
    REV=$(git log --reverse --format='%H' "${PARENT}..HEAD" | head -1)

    if test "x${REV}" = "x"; then
        echo "No changes here since ${PARENT}"
        exit 1
    fi

    exec git rebase -i --autosquash "${REV}^"
fi