summaryrefslogtreecommitdiff
path: root/dot/local/bin/git-ll-remote
blob: 000e41dd9bdf6e4858d7138bf211947b16ebf4d4 (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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/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}

SHOW=ahead
[ "$OPT_a" ] && SHOW=all
[ "$OPT_r" ] && SHOW=behind
[ "$OPT_u" ] && SHOW=upto
QUIET=
[ "$OPT_q" ] && QUIET=y
[ "$OPT_n" ] && NS_ONLY=y
[ "$OPT_N" ] && NS_ONLY=

show_message()
{
    show_${SHOW}_message
}

show_all_message()
{
    cat >&2 <<EOF
Showing all remote branches:

EOF
}

show_all_none_message()
{
    cat >&2 <<EOF
No remote branches.
EOF
}

show_ahead_message()
{
    cat >&2 <<EOF
Showing remote branches that are ahead (they have pushed onto your HEAD):

EOF
}

show_ahead_none_message()
{
    cat >&2 <<EOF
No remote branches are ahead of your HEAD.  $1 other remote branches were not
listed.

EOF
}

show_behind_message()
{
    cat >&2 <<EOF
Showing remote branches that are behind (your HEAD has commits on top of
the remote branch):

EOF
}

show_behind_none_message()
{
    cat >&2 <<EOF
No remotes branches are behind your HEAD.  $1 other remote branches were not
listed.

EOF

}

show_upto_message()
{
    cat >&2 <<EOF
Showing remote branches that are even with your HEAD.

EOF
}

show_upto_none_message()
{
    cat >&2 <<EOF
No remote branches are even with your HEAD.  $1 other remote branches were not
listed.

EOF
}

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

git_fetch()
{
    local hash="$1" ref="$2"
    have_ref "$hash" || git fetch "$remote" "$ref"
}

is_same_ref()
{
    [ "$1" = "$2 " ] || [ "$(git rev-parse "$1")" = "$(git rev-parse "$2")" ]
}

is_ancestor()
{
    if is_same_ref "$1" "$2"
    then false
    else git merge-base --is-ancestor "$1" "$2"
    fi
}

verdict()
{
    if is_same_ref HEAD "$1"
    then echo even
    elif is_ancestor "$1" HEAD
    then echo behind
    elif is_ancestor HEAD "$1"
    then echo ahead
    else echo diverged
    fi
}

show_this_remote_branch()
{
    local hash="$1"
    case "$SHOW" in
        all) VERDICT=$(verdict "$hash") ;;
        behind) is_ancestor "$hash" HEAD ;;
        ahead) is_ancestor HEAD "$hash" ;;
        upto) is_same_ref HEAD "$hash" ;;
    esac
}

handle_hash_ref()
{
    local hash="$1" ref="$2"
    git_fetch "$hash" "$ref" || exit

    if show_this_remote_branch "$hash"
    then
        if [ ! "$QUIET" -a "$listed" -eq 0 ]
        then
            show_message
        fi

        if [ "$SHOW" = all ]
        then
            >&2 printf 'remote: %s (%s)\n' "$ref" "$VERDICT"
        else
            >&2 printf 'remote: %s\n' "$ref"
        fi

        git show "$hash" | sed '/^$/q'
        listed=$((listed + 1))
    else
        skipped=$((skipped + 1))
    fi
}

read_hashes()
{
    while read hash ref; do
        [ "$hash" != From ] || continue
        case "$ref" in
            refs/namespaces/*) ;;
            *) [ ! "$NS_ONLY" ] || continue ;;
        esac
        handle_hash_ref "$hash" "$ref"
    done
}

finalize()
{
    if [ "$listed" -eq 0 ]
    then
        show_${SHOW}_none_message "$skipped"
    fi
}

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