about summary refs log tree commit diff
path: root/Functions/VCS_Info/Backends/VCS_INFO_get_data_hg
blob: 1c103a541f4a2b456dd97ab5a2b1f183b73ed37b (plain) (blame)
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
## vim:ft=zsh
## mercurial support by: Frank Terbeck <ft@bewatermyfriend.org>
## Distributed under the same BSD-ish license as zsh itself.

setopt localoptions NO_shwordsplit
local file hgbranch hgbranch_name hgbase hghash hglrev hgmqstring \
    r_branch hgchanges revformat bookmarks r_bmhash r_bmname hgbmstring
local -i getbookmarks
local -a hgbm mqpatches hgmisc_args
local -xA hook_com

hgbase=${vcs_comm[basedir]}
rrn=${hgbase:t}

file="${hgbase}/.hg/branch"
if [[ -r ${file} ]] ; then
    hgbranch_name=$(< ${file})
else
    hgbranch_name="default"
fi

hghash=''
hglrev=''
hgbm=()
bookmarks="${hgbase}/.hg/bookmarks"
if zstyle -t ":vcs_info:${vcs}:${usercontext}:${rrn}" get-revision ; then
    # Calling the 'hg' program is quite a bit too slow for prompts.
    # If there's a way around that, I'd be interested.
    # Disabled by default anyway, so no harm done.
    local HGRCPATH

    if zstyle -t ":vcs_info:${vcs}:${usercontext}:${rrn}" \
            "check-for-changes" ; then

        HGRCPATH="/dev/null" ${vcs_comm[cmd]} id --debug -i -n -b \
        | read -r hghash hglrev r_branch

        # Are there uncommitted-changes?
        if [[ $hglrev[-1] == + ]] ; then
            hgchanges=1
        fi

        # Remove uncommitted-changes marker, if any
        hglrev=${hglrev/+/}
        hghash=${hghash/+/}
    else
        HGRCPATH="/dev/null" ${vcs_comm[cmd]} \
        parents --template="{node} {rev} {branches}\n" \
        | read -r hghash hglrev r_branch
    fi

    if zstyle -t ":vcs_info:${vcs}:${usercontext}:${rrn}" "get-bookmarks" \
            && getbookmarks=1 || getbookmarks=0

    if (( getbookmarks )) && [[ -r "${bookmarks}" ]] ; then
        while read -r r_bmhash r_bmname ; do
            if [[ $hghash == $r_bmhash ]] ; then
                hgbm=( "$r_bmname" ${hgbm} )
            fi
        done < ${bookmarks}
    fi

    if [[ -n ${hglrev} ]] ; then
        zstyle -s ":vcs_info:${vcs}:${usercontext}:${rrn}" hgrevformat revformat || revformat="%r:%h"
        hook_com=( localrev "${hglrev}" "hash" "${hghash}" )
        if VCS_INFO_hook 'set-hgrev-format' "${revformat}"; then
            zformat -f hglrev "${revformat}" "r:${hook_com[localrev]}" "h:${hook_com[hash]}"
        else
            hglrev=${hook_com[rev-replace]}
        fi
        hook_com=()
        if (( getbookmarks )) ; then
            if VCS_INFO_hook 'gen-hg-bookmark-string' "${hgbm[@]}"; then
                hgbmstring=${(j.;.)hgbm}
            else
                hgbmstring=${hook_com[hg-bookmark-string]}
            fi
            hook_com=()
        fi
        zstyle -s ":vcs_info:${vcs}:${usercontext}:${rrn}" branchformat hgbranch || hgbranch="%b:%r"
        hook_com=( branch "${hgbranch_name}" revision "${hglrev}" )
        if VCS_INFO_hook 'set-branch-format' "${hgbranch}"; then
            zformat -f hgbranch "${hgbranch}" "b:${hook_com[branch]}" "r:${hook_com[revision]}"
        else
            hgbranch=${hook_com[branch-replace]}
        fi
        hook_com=()
    fi
else
    hgbranch="${hgbranch_name}"
fi

local patchdir=${hgbase}/.hg/patches/

if [[ -d $patchdir ]] ; then
    local -a mqpatches
    if [[ -e "${patchdir}/status" ]]; then
        mqpatches=( ${${(f)"$(< "${patchdir}/status")"}/(#s)[a-f0-9]##:/} )
        mqpatches=( ${(Oa)mqpatches} )
    else
        mqpatches=( )
    fi

    if VCS_INFO_hook 'gen-mq-patch-string' "${mqpatches[@]}"; then
        if (( ${#mqpatches} )); then
            hgmqstring=${mqpatches[1]}
        else
            hgmqstring="no patch applied"
        fi
    else
        hgbmstring=${hook_com[hg-mqpatch-string]}
    fi
    hook_com=()
else
    hgmqstring=''
fi

if [[ -z "${hgmqstring}" ]] && [[ -z "${hgbmstring}" ]]; then
    hgmisc_args=( '' ) # make sure there's at least *one* misc argument
elif [[ -z "${hgmqstring}" ]]; then
    hgmisc_args=( "${hgmqstring}" )
elif [[ -z "${hgbmstring}" ]]; then
    hgmisc_args=( "${hgbmstring}" )
else
    hgmisc_args=( "${hgmqstring}" "${hgbmstring}" )
fi
VCS_INFO_formats '' "${hgbranch}" "${hgbase}" '' "${hgchanges}" "${hglrev}" "${hgmisc_args[@]}"
return 0