summaryrefslogtreecommitdiff
path: root/dot/local/bin/git-ll-remote
blob: 28b996fa4362c9efb59796d7c020a80f9583e180 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/bin/bash

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}

QUIET=y
SHOW=all
[ "$OPT_a" ] && SHOW=ahead
[ "$OPT_A" ] && SHOW=all
[ "$OPT_b" ] && SHOW=behind
[ "$OPT_u" ] && SHOW=upto
[ "$OPT_d" ] && SHOW=diverged
[ "$OPT_q" ] && QUIET=y
[ "$OPT_v" ] && QUIET=
[ "$OPT_n" ] && NS_ONLY=y
[ "$OPT_N" ] && NS_ONLY=

show_message()
{
    show_${SHOW}_message
}

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

EOF
}

show_diverged_message()
{
    cat >&2 <<EOF
Showing remote branches that have commits ahead of yours
AND where you have commits ahead of theirs.

EOF
}

show_diverged_none_message()
{
    cat >&2 <<EOF
No remote branches are diverged from your HEAD.  $1 other remote
branches were not listed.

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
}

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

    VERDICT=$(verdict "$hash")
    if [ "${SHOW%-*}" = all -o "$VERDICT" = "$SHOW" ]
    then
        if [ ! "$QUIET" -a "$listed" -eq 0 ]
        then
            show_message
        fi
        more=$(git show -s "$hash" --pretty='%ar:%cn') || exit
        IFS=: read when author <<< "$more"
        when=${when% ago}
        subj=$(git show -s "$hash" --pretty='%s')

        author_size=13
        author=${author:0:$author_size}

        subj_size=20
        subj_orig_size=${#subj}
        subj=${subj:0:$subj_size}
        if [ "$subj_orig_size" -ge "$subj_size" ]
        then
            # The length of the subject is allowed to be one longer only if it's truncated.
            # The visual protrusion makes truncation unambiguous even in case literal "..." included in subject field.
            subj=${subj%??}...
        fi

        case "$SEEN_HASH" in
            *" $hash "*) subj= author= when= ;;
        esac
        SEEN_HASH="$SEEN_HASH $hash "

        printf "%-42s %21s  %-${author_size}s   %-$((subj_size + 1))s  %10s %s\n" "$hash" "$when" "$author" "$subj" "($VERDICT)" "$ref"
        listed=$((listed + 1))
    else
        skipped=$((skipped + 1))
    fi
}

read_hashes()
{
    local SEEN_HASH=
    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)