summaryrefslogtreecommitdiff
path: root/bin/git-pull-safe
blob: c0bfe33e60d784c3fb169a1d012392c15b781617 (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
#!/bin/bash

# Update all remote-tracking branches, and as many local branches that
# we can fast-forward.  Additionally, if the current branch looks like
# a dgit suite-tracking branch, merge into it the current state of the
# package in the Debian archive

# This script is based on the value of git_update set by the myrepos
# dgit plugin.  That library uses an unconditional `git pull` to match
# global myrepos conventions.  The main reason for having that script
# is to override that unconditional `git pull`

set -e

function branch_prefix() {
    local branch="$1"
    echo $branch | cut -s -d/ -f1
}

function branch_remote() {
    local branch="$1"
    git config branch.$branch.remote
}

current_branch="$(git rev-parse --abbrev-ref HEAD)"
branches="$(git for-each-ref --format='%(refname:short)' refs/heads/)"

# (1) normal git branches
git remote update
for branch in $branches; do
    remote="$(branch_remote $branch 2>/dev/null || true)"
    if [ "$(branch_prefix $branch)" != "dgit"  ] && [ "$remote" != "" ]; then
        # if the branch cannot be fast-forwarded, git-merge-ff will
        # exit non-zero, so myrepos will report that the update
        # failed, so user intervention is required
        # echo "I: attempting to fast-forward local branch $branch"
        git merge-ff "$branch"
    fi
done

# (2) dgit branches
if [ "$(branch_prefix $current_branch)" = "dgit" ]; then
    # echo "I: current branch is $current_branch; doing \`dgit pull\`"
    dgit pull
fi
for suite in $(git show-ref | grep refs/remotes/dgit/dgit | cut -d/ -f5 ); do
    # skip if we just called `dgit pull` on this suite
    if ! [ "$current_branch" = "dgit/$suite" ]; then
        # echo "I: fetching dgit suite $suite"
        dgit fetch $suite
    fi
done