git-rebase.shon commit rebase: allow rebasing onto different base. (e646c9c)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Junio C Hamano.
   4#
   5
   6USAGE='[--onto <newbase>] <upstream> [<branch>]'
   7LONG_USAGE='If <branch> is specified, switch to that branch first.  Then,
   8extract commits in the current branch that are not in <upstream>,
   9and reconstruct the current on top of <upstream>, discarding the original
  10development history.  If --onto <newbase> is specified, the history is
  11reconstructed on top of <newbase>, instead of <upstream>.  For example,
  12while on "topic" branch:
  13
  14          A---B---C topic
  15         /
  16    D---E---F---G master
  17
  18        $ '"$0"' --onto master~1 master topic
  19
  20would rewrite the history to look like this:
  21
  22
  23              A'\''--B'\''--C'\'' topic
  24             /
  25    D---E---F---G master
  26'
  27
  28. git-sh-setup
  29
  30unset newbase
  31while case "$#" in 0) break ;; esac
  32do
  33        case "$1" in
  34        --onto)
  35                test 2 -le "$#" || usage
  36                newbase="$2"
  37                shift
  38                ;;
  39        -*)
  40                usage
  41                ;;
  42        *)
  43                break
  44                ;;
  45        esac
  46        shift
  47done
  48
  49# Make sure we do not have .dotest
  50if mkdir .dotest
  51then
  52        rmdir .dotest
  53else
  54        echo >&2 '
  55It seems that I cannot create a .dotest directory, and I wonder if you
  56are in the middle of patch application or another rebase.  If that is not
  57the case, please rm -fr .dotest and run me again.  I am stopping in case
  58you still have something valuable there.'
  59        exit 1
  60fi
  61
  62# The tree must be really really clean.
  63git-update-index --refresh || exit
  64diff=$(git-diff-index --cached --name-status -r HEAD)
  65case "$diff" in
  66?*)     echo "$diff"
  67        exit 1
  68        ;;
  69esac
  70
  71# The upstream head must be given.  Make sure it is valid.
  72upstream_name="$1"
  73upstream=`git rev-parse --verify "${upstream_name}^0"` ||
  74    die "invalid upsteram $upstream_name"
  75
  76# If the branch to rebase is given, first switch to it.
  77case "$#" in
  782)
  79        branch_name="$2"
  80        git-checkout "$2" || usage
  81        ;;
  82*)
  83        branch_name=`git symbolic-ref HEAD` || die "No current branch"
  84        branch_name=`expr "$branch_name" : 'refs/heads/\(.*\)'`
  85        ;;
  86esac
  87branch=$(git-rev-parse --verify "${branch_name}^0") || exit
  88
  89# Make sure the branch to rebase onto is valid.
  90onto_name=${newbase-"$upstream_name"}
  91onto=$(git-rev-parse --verify "${onto_name}^0") || exit
  92
  93# Now we are rebasing commits $upstream..$branch on top of $onto
  94
  95# Check if we are already based on $onto, but this should be
  96# done only when upstream and onto are the same.
  97if test "$upstream" = "onto"
  98then
  99        mb=$(git-merge-base "$onto" "$branch")
 100        if test "$mb" = "$onto"
 101        then
 102                echo >&2 "Current branch $branch_name is up to date."
 103                exit 0
 104        fi
 105fi
 106
 107# Rewind the head to "$onto"; this saves our current head in ORIG_HEAD.
 108git-reset --hard "$onto"
 109
 110# If the $onto is a proper descendant of the tip of the branch, then
 111# we just fast forwarded.
 112if test "$mb" = "$onto"
 113then
 114        echo >&2 "Fast-forwarded $branch to $newbase."
 115        exit 0
 116fi
 117
 118git-format-patch -k --stdout --full-index "$upstream" ORIG_HEAD |
 119git am --binary -3 -k