summaryrefslogtreecommitdiff
path: root/dot/local/bin/git-ll-remote
blob: a0e689b5c151a6d54a281b829b25e0a93e5e6627 (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
#!/bin/sh

while [ $# -gt 0 ]
do
    case "$1" in
        --) shift; break;;
        -?) eval "OPT_${1#-}=y"
            shift
            ;;
        --*=*) kv=${1#--}
            k=${kv##=*}
            v=${kv%*=}
            eval "OPT_${k}=\$1" "$v"
            shift
            ;;
        -*) exit 1;;
        *) break;;
    esac
done

remote=${1:-origin}

have_ref()
{
    git rev-list --quiet "$1" 2>/dev/null
}

show_all_message()
{
    >&2 printf '%s\n' \
        'Showing all remote branches:' \
        ''

}

show_normal_message()
{
    >&2 printf '%s\n' \
        'Showing remote branches that have pushed onto your HEAD:' \
        ''
}

show_reverse_message()
{
    >&2 printf '%s\n' \
           'Showing branches unmerged on the REMOTE side (THEY need YOUR changes)' \
           'because "-r" (reverse) was specified:' \
           ''
}

handle_hash_ref()
{
    local hash="$1" ref="$2"
    if ! have_ref $hash
    then
        git fetch $remote $ref;
    fi
    if [ -z "$OPT_r" ]
    then
        cmd="git merge-base --is-ancestor HEAD $hash"
    else
        cmd="git merge-base --is-ancestor $hash HEAD"
    fi
    if [ "$OPT_a" ] || $cmd
    then
        if ! [ "$OPT_q" ]
        then
            if [ "$OPT_a" ]
            then show_all_message
            elif [ "$OPT_r" -a "$listed" -eq 0 ]
            then show_reverse_message
            else show_normal_message
            fi
        fi

        >&2 printf 'remote: %s\n' "$ref"
        git show $hash | sed '/^$/q'
        listed=$((listed + 1))
    else
        skipped=$((skipped + 1))
    fi
}

read_hashes()
{
    while read hash ref; do
        case $ref in
            refs/namespaces/*) handle_hash_ref "$hash" "$ref" ;;
        esac
    done
}

finalize()
{
    if [ "$listed" -eq 0 ]
    then
        if [ "$OPT_r" ]
        then
            >&2 printf 'Already up-to-date.  (No remotes are ancestors of your HEAD; %d remotes up-to-date.)\n' "$skipped"
        else
            >&2 printf 'Already up-to-date.  (No remotes have pushed onto your HEAD; %d remotes up-to-date.)\n' "$skipped"
        fi
    fi
}

skipped=0
listed=0
git ls-remote $remote | (read_hashes && finalize)