summaryrefslogtreecommitdiff
path: root/bin/git-pull-safe
blob: ab3ac25158a453ff7de217cdccd2136456ddc6bc (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
#!/usr/bin/env 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 this 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
# note that `git remote update` may not fetch all tags
git fetch --all --tags --prune
for branch in $branches; do
    remote="$(branch_remote $branch 2>/dev/null || true)"
    if [ "$(branch_prefix $branch)" != "dgit"  ] && [ "$remote" != "" ]; then
        # echo "I: attempting to fast-forward local branch $branch"
        if ! git branch --contains "$branch@{upstream}" \
                | grep -qE " $branch$"; 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
            git merge-ff "$branch"
        fi
    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"
        [ -z "$source" ] \
            && source=$(git cat-file blob dgit/dgit/$suite:debian/control \
                            | awk -F': ' '$1 ~ /Source/ { print $2 }')
        dgit -p$source fetch $suite
    fi
done