summaryrefslogtreecommitdiff
path: root/dot/local/bin/git-ll-remote
blob: 17a50d4c69d2a4cafb5e2c380ffec8aafe1e470f (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
#!/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()
{
    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()
{
    x=$(cat <<EOF)
No remote branches are ahead of your HEAD. %d remote branches were not listed.
EOF
    printf "$x\n" "$1" >&2

}

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()
{
    x=$(cat <<EOF)
No remotes branches are behind your HEAD. %d other remote branches were not
listed.
EOF
    printf "$x\n" "$1" >&2

}

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

EOF
}

show_upto_none_message()
{
x=$(cat <<EOF)
No remote branches are even with your HEAD. %d other remote branches were not
listed.
EOF
    printf "$x\n" "$1" >&2
}

SHOW=ahead
[ "$OPT_a" ] && SHOW=all
[ "$OPT_r" ] && SHOW=behind
[ "$OPT_u" ] && SHOW=upto

show_message()
{
    show_${SHOW}_message
}

git_fetch()
{
    local hash="$1" ref="$2"
    have_ref "$hash" || git fetch --depth=1 "$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 [ ! "$OPT_q" -a "$listed" -eq 0 ]
        then
            show_message
        fi

        if [ "$OPT_a" ]
        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
        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)