Login

update-django: Update your django git branches.

Author:
telenieko
Posted:
November 5, 2008
Language:
HTML/template
Version:
Not specified
Score:
1 (after 1 ratings)

This is the (revamped) bash script I use to keep my git branches up-to-date with SVN to make my life a lot easier, just save it in a text file and read the instructions at the top!

Hope it's useful to somebody else than me ;)

  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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/sh
# vim: foldmethod=marker
# DJANGO GIT UPDATING SCRIPT, PLEASE READ THOSE INSTRUCTIONS.
# This script expects you to have the following structure:
#   * Some branches that match the $TRUNK regex which are *clean*
#     copies of SVN trunk or branches.
#   * A master branch coming from every of those $TRUNK branches, aka: master
#   * Any other topic branches will pull from one of those masters (or not)
# The script expects the "pull" configuration to be set correctly for every branch.
# Imagine the following scenarios:
#   * A master branch follows trunk:
#     git config branch.master.remote .
#     git config branch.master.merge trunk
#   * A master-1.0.X branch follows trunk-1.0.X
#     git config branch.master-1.0.X.remote .
#     git config branch.master-1.0.X.merge trunk-1.0.X
#   * A 6735-views branch that follows master
#     git config branch.6735-views.remote .
#     git config branch.6735-views.merge master
# By doing this configuration you tell git where to merge from when running
# "git pull" on a branch.
# If you wonder how to setup GIT to understand Django's SVN structure ask Marc Fargas
# at [email protected]

# Base directory of you django git repo.
DJANGO=$HOME/dev/python/django

# Regex to find local branches that represent *clean* copies of svn trunk or branches.
# We will run "git svn rebase" on this branch, and then pull from those.
# NEVER PUBLISH THOSE BRANCHES ON THE INTERNET. THANKS.
TRUNK="trunk.*"

# Regex to find the "master" branches, those are the ones that pull from $TRUNK
# and are never rebased so can be published safely.
MASTER="master.*"

# Regex of branches to ignore, in case you have some branches you
# do not want to automatically update.
IGNORE='private/.*'

## CONFIG END ##

DIR=`pwd`

# What branches do we have?
cd $DJANGO/.git/refs/heads
BRANCHES=`find -type f`
cd $DJANGO

# {{{ match_branch: Check if the branch in $1 matches the regex in $2
# Takes the branch name as $1, and the ignore list as $2
# Return value 0 if no match
# Return value 1 if matches
match_branch() {
    br=$1
    ignore=$2
    if [ `expr match "$br" "$ignore"` -gt 0 ]; then
        return 1
    elif [ "$br" = "=" ]; then
        return 1
    fi
    return 0
}
# }}}

# {{{ ignore_branch: Check if the branch in $1 has to be ignored
# (aka: not in $TRUNK, $MASTER, $IGNORE
# Takes the branch name as $1
# Return value 0 if ok.
# Return value 1 if should be ignored.
ignore_branch() {
    br=$1
    if [ `expr match "$br" "$TRUNK"` -gt 0 ]; then
        return 1
    elif [ `expr match "$br" "$MASTER"` -gt 0 ]; then
        return 1
    elif [ `expr match "$br" "$IGNORE"` -gt 0 ]; then
        return 1
    fi
    return 0
}
# }}}

# {{{ update_branch: Merge, from branch $1 the branch $MASTER.
# On failure we "git reset --hard", print a message and return 1.
update_branch() {
    echo "Merging $1 with its master"
    git checkout $1
    git pull
    RES=$?
    if ! [ $RES -eq 0 ]
    then
        echo "MERGE FAILED FOR $1 DO IT BY HAND"
        git reset --hard
        echo "MERGE FAILED FOR $1 DO IT BY HAND"
        return 1
    fi
}
# }}}

# {{{ update_svn: Update our $TRUNK branches with code from svn.
# That runs "svn fetch", "svn rebase" and merges $TRUNK into $MASTER.
update_svn() {
    cd $DJANGO
    git svn fetch
    for BR in $BRANCHES
    do
        BR=${BR:2}
        match_branch $BR  $TRUNK 
        RES=$?
        if [ $RES -eq 1 ]
        then
            echo "Merging $BR with SVN"
            git checkout $BR
            git svn rebase
            RES=$?
            if ! [ $RES -eq 0 ]
            then
                echo "COULD NOT MERGE $BR"
                return 1
            fi
        fi
    done
}
# }}}

# {{{ update_master: Update our $MASTER branches with code from our $TRUNK.
# That runs "git pull" on those branches.
update_master() {
    cd $DJANGO
    for BR in $BRANCHES
    do
        BR=${BR:2}
        match_branch $BR  $MASTER 
        RES=$?
        if [ $RES -eq 1 ]
        then
            echo "Merging $BR with trunk branch"
            git checkout $BR
            git pull
            RES=$?
            if ! [ $RES -eq 0 ]
            then
                echo "COULD NOT MERGE $BR"
                return 1
            fi
        fi
    done
}
# }}}

update_svn
RES=$?
if ! [ $RES -eq 0 ]; then
    echo "Got trouble updating svn, aborting."
    cd $DIR ; exit 1
fi

update_master
RES=$?
if ! [ $RES -eq 0 ]; then
    echo "Got trouble updating master branches, aborting."
    cd $DIR ; exit 1
fi


for BR in $BRANCHES
do
    BR=${BR:2}
    ignore_branch $BR
    RES=$?
    if [ $RES -eq 0 ]
    then
        update_branch $BR
    else
        echo "Not merging $BR with anyone."
    fi
done

# Go back to the directory we were before running.
cd $DIR

More like this

  1. Bootstrap Accordian by Netplay4 5 years, 10 months ago
  2. Bootstrap theme for django-endless-pagination? by se210 8 years, 10 months ago
  3. Bootstrap theme for django-endless-pagination? by se210 8 years, 10 months ago
  4. Reusable form template with generic view by roldandvg 8 years, 11 months ago
  5. Pagination Django with Boostrap by guilegarcia 9 years, 1 month ago

Comments

Please login first before commenting.