summaryrefslogtreecommitdiffstats
path: root/git-rebase-subtree/git-rebase-subtree.in
blob: 60efcffd9461e65193a1f26598cea54d7be76ac3 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/sh
# git-rebase-subtree - rebase "subtree of branches" in one go
# Copyright (C) 2007 Hans Ulrich Niedermann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

unset CDPATH
SED="${SED-sed}"

self=`basename "$0"`
selfdir=`dirname "$0"`
selfdir=`cd "$selfdir" && pwd`
. "$selfdir/git-ndim-sh"

SUBDIRECTORY_OK="yes"
USAGE="<command> [params...]"

LONG_USAGE="\
Let a local subtree of branches follow a remote origin without too much merging.
For more details, see the ${self}(1) man page."
. git-sh-setup

require_work_tree

# Abort on error
set -e

#dry_run=:
dry_run=false

cmd() {
    echo "CMD>" "$@"
    "$dry_run" || "$@"
}


gf_init() {
    echo "$self: Examining configuration"
    config_found=no
    for configvar in "rebase-subtree.rebase" "rebase-subtree.subtree"
    do
        if test -n "$(git config --get-all "$configvar")"
        then
            if test "x$configvar" = "xrebase-subtree.subtree"; then
                echo "$self: WARNING: Using deprecated config variable rebase-subtree.subtree."
                echo "       Better use rebase-subtree.rebase lines instead."
            fi
            git config --get-all "$configvar" | while read from to restofline; do
                if test "x#" = "x$(echo "$from" | sed -n '1s/^\(.\).*/\1/p')"; then continue; fi
                echo ""
                echo "  From: $from"
                echo "    To: $to"
            done
            config_found=yes
            break
        fi
    done
    test "x$config_found" = "xyes" || die "Unset or empty git config var \"rebase-subtree.rebase\""
}

gf_branch() {
    branch="$1"
    newbranch="follow-old/$branch"
    if git rev-parse --verify "$branch" > /dev/null 2>&1; then
	# valid branch
	if git rev-parse --verify "$newbranch" > /dev/null 2>&1; then
	    : # branch already exists, do nothing
	else
	    cmd git branch "${newbranch}" "${branch}"
	fi
    else
	echo "$self: gf_branch called with invalid branch \"$branch\"" >&2
	exit 1
    fi
}


gf_rmbranch() {
    branch="$1"
    newbranch="follow-old/$branch"
    if git rev-parse --verify "$branch" > /dev/null 2>&1; then
	if git rev-parse --verify "$newbranch" > /dev/null 2>&1; then
	    cmd git-branch -D "${newbranch}"
	fi
    fi
}


gf_rebase_tree() {
    reverse="no"
    case "$1" in
	--reverse) reverse=yes; shift ;;
    esac
    newroot="$1"
    test "x$newroot" = "x" && die "Need <newroot> parameter"
    oldroot="$2"
    test "x$oldroot" = "x" && die "Need <oldroot> parameter"
    echo
    echo "CAUTION: ${self} does not handle merge errors yet - that will mess up your repo!"
    echo "Press Ctrl-C to abort. Or, if you feel adventurous, press ENTER."
    read
    echo
    echo "$self: Preparing subtree rebase"
    git config --get-all "$configvar" | while read from to restofline; do
	if test "x#" = "x$(echo "$from" | sed -n '1s/^\(.\).*/\1/p')"; then continue; fi
	gf_branch "$from"
	gf_branch "$to"
    done
    echo
    echo "$self: Executing subtree rebase"
    git config --get-all "$configvar" | while read from to restofline; do
	if test "x#" = "x$(echo "$from" | sed -n '1s/^\(.\).*/\1/p')"; then continue; fi
	if test "x$reverse" = "xno" && test "x$from" = "x$oldroot"; then
	    cmd git-rebase "$newroot" "$oldroot"
	    cmd git-rebase --onto "$newroot" "follow-old/$from" "$to"
	elif test "x$reverse" = "xyes" && test "x$from" = "x$newroot"; then
	    cmd git-rebase --onto "$newroot" "$oldroot" "$to"
	else
	    cmd git-rebase --onto "$from" "follow-old/$from" "$to"
	fi
    done
    echo
    echo "$self: Cleaning up after subtree rebase"
    git config --get-all "$configvar" | while read from to restofline; do
	if test "x#" = "x$(echo "$from" | sed -n '1s/^\(.\).*/\1/p')"; then continue; fi
	gf_rmbranch "$from"
	gf_rmbranch "$to"
    done
}

gf_init
gf_rebase_tree "$@"

# End of file.