summary refs log tree commit diff
path: root/Functions/VCS_Info/VCS_INFO_quilt
blob: 6adf0a358be4f48ea2470f0f6a06b76431238704 (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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
## vim:ft=zsh:foldmethod=marker

function VCS_INFO_quilt-match() {
    emulate -L zsh
    setopt extendedglob
    local d mode="$1" param="$2"
    local -a list

    case ${mode} in
        (assoc) list=( ${(kOP)param} );;
        (array) : "${foo[@]}" ${(t)foo}; list=( ${(OP)param} );;
        (*) return 1;;
    esac
    for d in "${list[@]}"; do
        if [[ ${PWD} == ${d%/##}(|/*) ]]; then
            print "$d"
            return 0
        fi
    done
    return 1
}

function VCS_INFO_quilt-standalone-detect() {
    emulate -L zsh
    setopt extendedglob
    local param
    local -i ret

    zstyle -s "${context}" quilt-standalone param || return 1
    [[ "${param}" == 'never' ]] && return 1
    [[ "${param}" == 'always' ]] && return 0

    if (( ${+functions[$param]} )); then
        ${param}
        return $?
    fi

    case ${(Pt)param} in
    *association*)
        local m
        local -A A
        m="$(VCS_INFO_quilt-match assoc ${param})"
        A=(${(kvP)param})
        (( $? == 0 )) && [[ ${A[$m]} == "true" ]] && return 0
        return 1
        ;;
    *array*)
        typeset -gU ${param}
        VCS_INFO_quilt-match array ${param} > /dev/null
        return $?
        ;;
    *scalar*)
        [[ "${(P)param}" == 'always' ]] && return 0
        [[ "${(P)param}" == 'never' ]] && return 1
        ;;
    esac
    # If nothing hit yet, it just wasn't meant to be.
    return 1
}

function VCS_INFO_quilt-dirfind() {
    # This is a wrapper around VCS_INFO_bydir_detect(). It makes sure
    # that $vcs_comm[] is unchanged. Currently, changing anything in it
    # should not be an issue, but this makes sure the code can safely
    # be called elsewhere, too - if needed.
    emulate -L zsh
    setopt extendedglob
    local dir="$1" file="$2"; shift $#
    local ret oldfile olddir

    olddir=${vcs_comm[basedir]}
    vcs_comm[basedir]=''
    oldfile=${vcs_comm[detect_need_file]}
    vcs_comm[detect_need_file]=${file}
    VCS_INFO_bydir_detect ${dir}
    ret=$?
    vcs_comm[detect_need_file]=${oldfile}
    printf '%s' ${vcs_comm[basedir]}
    vcs_comm[basedir]="${olddir}"
    return ${ret}
}

# This function takes as an argument a filename of a patch and sets $REPLY to
# a single-line "subject", or unsets it if no subject could be extracted.
function VCS_INFO_quilt-patch2subject() {
    integer i
    integer -r LIMIT=10
    local -a lines
    local needle
    if [[ -f "$1" ]]; then
        # Extract the first LIMIT lines, or up to the first empty line or the start of the unidiffs,
        # whichever comes first.
        while (( i++ < LIMIT )); do
            IFS= read -r "lines[$i]"
            if [[ -z ${lines[$i]} ]] || [[ ${lines[$i]} == (#b)(---|Index:)* ]]; then
                lines[$i]=()
                break
            fi
        done < "$1"
        
        if needle=${lines[(i)Subject:*]}; (( needle <= $#lines )); then
            # "Subject: foo" line, plus rfc822 whitespace unfolding.
            #
            # Example: 'git format-patch' patches.
            REPLY=${lines[needle]}
            REPLY=${REPLY#*: }
            REPLY=${REPLY#\[PATCH\] }
            while [[ ${${lines[++needle]}[1]} == ' ' ]]; do
                REPLY+=${lines[needle]}
            done
        elif needle=${lines[(r)Description:*]}; [[ -n $needle ]]; then
            # "Description: foo" line.
            #
            # Example: DEP-3 patches.
            REPLY=${needle#*: }
        elif [[ ${lines[1]} == '# HG changeset patch' ]] && { needle=${${lines:#([#]*)}[1]}; [[ -n $needle ]] }; then
            # Mercurial patch
            REPLY=$needle
        elif (( ${+lines[1]} )); then
            # The first line of the file is not part of the diff.
            REPLY=${lines[1]}
        else
            # The patch has no subject.
            unset REPLY
            return 0
        fi
    else
        # The patch cannot be examined, or invalid arguments.
        unset REPLY
        return 1
    fi
}

function VCS_INFO_quilt() {
    emulate -L zsh
    setopt extendedglob
    local mode="$1"
    local patches pc tmp qstring root
    local -i ret
    local context
    local -a applied unapplied all applied_string unapplied_string quiltcommand quilt_env
    local -A hook_com

    context=":vcs_info:${vcs}.quilt-${mode}:${usercontext}:${rrn}"
    zstyle -t "${context}" use-quilt || return 1

    case ${mode} in
    (standalone)
        VCS_INFO_quilt-standalone-detect || return 1
        ;;
    (addon)
        ;;
    (*)
        printf 'Invalid mode: `%s'\''\n' "$1"
        return 2
        ;;
    esac

    pc="$(VCS_INFO_quilt-dirfind .pc .version)"
    ret=$?
    if (( ret == 0 )); then
        [[ ${quiltmode} == 'standalone' ]] && root=${pc}
        pc=${pc}/.pc
        if [[ -e ${pc}/applied-patches ]]; then
            applied=( ${(f)"$(<$pc/applied-patches)"} )
            # throw away empty entries
            applied=( ${applied:#} )
            applied=( ${(Oa)applied} )
        else
            applied=()
        fi
        patches=$(<$pc/.quilt_patches)
        patches=`builtin cd -q "${pc:h}" && print -r - ${patches:P}`
    fi
    if zstyle -t "${context}" get-unapplied; then
        # This zstyle call needs to be moved further up if `quilt' needs
        # to be run in more places than this one.
        zstyle -s "${context}" quiltcommand quiltcommand || quiltcommand='quilt'
        quilt_env=(env)
        if [ -z "$patches" ]; then
            zstyle -s "${context}" quilt-patch-dir patches || patches="${QUILT_PATCHES}"
            if [[ "${patches}" != /* ]]; then
                tmp=${patches:-patches}
                patches="$(VCS_INFO_quilt-dirfind "${tmp}")"
                ret=$?
                (( ret )) && return ${ret}
                patches=${patches}/${tmp}
            else
                [[ -d ${patches} ]] || return 1
            fi
            quilt_env+=(QUILT_PATCHES="$patches")
        fi
        unapplied=( ${(f)"$(${quilt_env[@]} $quiltcommand --quiltrc /dev/null unapplied 2> /dev/null)"} )
        unapplied=( ${unapplied:#} )
    else
        unapplied=()
    fi

    if [[ -n $patches ]]; then
      () {
        local i
        for ((i=1; i<=$#applied; i++)); do
          if VCS_INFO_quilt-patch2subject "$patches/$applied[$i]" && (( $+REPLY ))
          then
            applied[$i]+=" $REPLY"
          else
            applied[$i]+=" ?"
          fi
        done
        for ((i=1; i<=$#unapplied; i++)); do
          if VCS_INFO_quilt-patch2subject "$patches/$unapplied[$i]" && (( $+REPLY ))
          then
            unapplied[$i]+=" $REPLY"
          else
            unapplied[$i]+=" ?"
          fi
        done
      }
    fi

    all=( ${(Oa)applied} ${unapplied} )

    if VCS_INFO_hook 'gen-applied-string' "${applied[@]}"; then
        if (( ${#applied} )); then
            applied_string=${applied[1]}
        else
            applied_string=""
        fi
    else
        applied_string=${hook_com[applied-string]}
    fi
    hook_com=()
    if VCS_INFO_hook 'gen-unapplied-string' "${unapplied[@]}"; then
        unapplied_string="${#unapplied}"
    else
        unapplied_string=${hook_com[unapplied-string]}
    fi

    if (( ${#applied} )); then
        zstyle -s "${context}" patch-format qstring || qstring="%p (%n applied)"
    else
        zstyle -s "${context}" nopatch-format qstring || qstring="no patch applied"
    fi
    hook_com=( applied "${applied_string}" unapplied "${unapplied_string}"
               applied-n ${#applied}       unapplied-n ${#unapplied}       all-n ${#all} )
    if VCS_INFO_hook 'set-patch-format' ${qstring}; then
        zformat -f qstring "${qstring}" "p:${hook_com[applied]}" "u:${hook_com[unapplied]}" \
                                        "n:${#applied}" "c:${#unapplied}" "a:${#all}"
    else
        qstring=${hook_com[patch-replace]}
    fi
    hook_com=()

    case ${mode} in
    (standalone)
        VCS_INFO_formats '' '' "${root}" '' '' '' "${qstring}"
        VCS_INFO_set
        ;;
    (addon)
        # When VCS_INFO_quilt() is called with "addon" a "local REPLY" variable
        # should be in place. That variable can be unset after it's being used.
        REPLY="${qstring}"
        ;;
    esac

    VCS_INFO_hook 'post-quilt' ${mode} ${patches} ${pc:-\\-nopc-}
}
VCS_INFO_quilt "$@"