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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
## vim:ft=zsh
## git support by: Frank Terbeck <ft@bewatermyfriend.org>
## Distributed under the same BSD-ish license as zsh itself.
setopt localoptions extendedglob NO_shwordsplit
local gitdir gitbase gitbranch gitaction gitunstaged gitstaged gitsha1 gitmisc
local -i querystaged queryunstaged
local -a git_patches_applied git_patches_unapplied
local -A hook_com
(( ${+functions[VCS_INFO_git_getaction]} )) ||
VCS_INFO_git_getaction () {
local gitdir=$1
local tmp
for tmp in "${gitdir}/rebase-apply" \
"${gitdir}/rebase" \
"${gitdir}/../.dotest" ; do
if [[ -d ${tmp} ]] ; then
if [[ -f "${tmp}/rebasing" ]] ; then
gitaction="rebase"
elif [[ -f "${tmp}/applying" ]] ; then
gitaction="am"
else
gitaction="am/rebase"
fi
return 0
fi
done
for tmp in "${gitdir}/rebase-merge/interactive" \
"${gitdir}/.dotest-merge/interactive" ; do
if [[ -f "${tmp}" ]] ; then
gitaction="rebase-i"
return 0
fi
done
for tmp in "${gitdir}/rebase-merge" \
"${gitdir}/.dotest-merge" ; do
if [[ -d "${tmp}" ]] ; then
gitaction="rebase-m"
return 0
fi
done
if [[ -f "${gitdir}/MERGE_HEAD" ]] ; then
gitaction="merge"
return 0
fi
if [[ -f "${gitdir}/BISECT_LOG" ]] ; then
gitaction="bisect"
return 0
fi
if [[ -f "${gitdir}/CHERRY_PICK_HEAD" ]] ; then
if [[ -d "${gitdir}/sequencer" ]] ; then
gitaction=cherry-seq
else
gitaction=cherry
fi
return 0
fi
if [[ -d "${gitdir}/sequencer" ]] ; then
gitaction="cherry-or-revert"
return 0
fi
return 1
}
(( ${+functions[VCS_INFO_git_getbranch]} )) ||
VCS_INFO_git_getbranch () {
local gitdir=$1 tmp actiondir
local gitsymref="${vcs_comm[cmd]} symbolic-ref HEAD"
actiondir=''
for tmp in "${gitdir}/rebase-apply" \
"${gitdir}/rebase" \
"${gitdir}/../.dotest"; do
if [[ -d ${tmp} ]]; then
actiondir=${tmp}
break
fi
done
if [[ -n ${actiondir} ]]; then
gitbranch="$(${(z)gitsymref} 2> /dev/null)"
[[ -z ${gitbranch} ]] && [[ -r ${actiondir}/head-name ]] \
&& gitbranch="$(< ${actiondir}/head-name)"
[[ -z ${gitbranch} || ${gitbranch} == 'detached HEAD' ]] && [[ -r ${actiondir}/onto ]] \
&& gitbranch="$(< ${actiondir}/onto)"
elif [[ -f "${gitdir}/MERGE_HEAD" ]] ; then
gitbranch="$(${(z)gitsymref} 2> /dev/null)"
[[ -z ${gitbranch} ]] && gitbranch="$(< ${gitdir}/ORIG_HEAD)"
elif [[ -d "${gitdir}/rebase-merge" ]] ; then
gitbranch="$(< ${gitdir}/rebase-merge/head-name)"
if [[ $gitbranch == 'detached HEAD' ]]; then
# get a sha1
gitbranch="$(< ${gitdir}/rebase-merge/orig-head)"
fi
elif [[ -d "${gitdir}/.dotest-merge" ]] ; then
gitbranch="$(< ${gitdir}/.dotest-merge/head-name)"
elif gitbranch="$(${(z)gitsymref} 2> /dev/null)" ; then
elif gitbranch="refs/tags/$(${vcs_comm[cmd]} describe --all --exact-match HEAD 2>/dev/null)" ; then
elif gitbranch="$(${vcs_comm[cmd]} describe --contains HEAD 2>/dev/null)" ; then
## Commented out because we don't know of a case in which 'describe --contains' fails and 'name-rev --tags' succeeds.
#elif gitbranch="$(${vcs_comm[cmd]} name-rev --name-only --no-undefined --tags HEAD 2>/dev/null)" ; then
elif gitbranch="$(${vcs_comm[cmd]} name-rev --name-only --no-undefined --always HEAD 2>/dev/null)" ; then
fi
if [[ -z ${gitbranch} ]]
then
gitbranch="${${"$(< $gitdir/HEAD)"}[1,7]}..."
fi
return 0
}
(( ${+functions[VCS_INFO_git_handle_patches]} )) ||
VCS_INFO_git_handle_patches () {
local git_applied_s git_unapplied_s gitmsg
# All callers populate $git_patches_applied and $git_patches_unapplied in
# order, but the hook requires us to reverse $git_patches_applied.
git_patches_applied=(${(Oa)git_patches_applied})
VCS_INFO_set-patch-format 'git_patches_applied' 'git_applied_s' \
'git_patches_unapplied' 'git_unapplied_s' \
":vcs_info:${vcs}:${usercontext}:${rrn}" gitmsg \
'' '' ''
gitmisc=$REPLY
}
gitdir=${vcs_comm[gitdir]}
VCS_INFO_git_getbranch ${gitdir}
gitbase=${vcs_comm[basedir]}
if [[ -z ${gitbase} ]]; then
# Bare repository
gitbase=${gitdir:P}
fi
rrn=${gitbase:t}
if zstyle -t ":vcs_info:${vcs}:${usercontext}:${rrn}" get-revision ; then
gitsha1=$(${vcs_comm[cmd]} rev-parse --quiet --verify HEAD)
else
gitsha1=''
fi
gitbranch="${gitbranch##refs/[^/]##/}"
if [[ -z ${gitdir} ]] || [[ -z ${gitbranch} ]] ; then
return 1
fi
if zstyle -t ":vcs_info:${vcs}:${usercontext}:${rrn}" "check-for-changes" ; then
querystaged=1
queryunstaged=1
elif zstyle -t ":vcs_info:${vcs}:${usercontext}:${rrn}" "check-for-staged-changes" ; then
querystaged=1
fi
if (( querystaged || queryunstaged )) && \
[[ "$(${vcs_comm[cmd]} rev-parse --is-inside-work-tree 2> /dev/null)" == 'true' ]] ; then
# Default: off - these are potentially expensive on big repositories
if (( queryunstaged )) ; then
${vcs_comm[cmd]} diff --no-ext-diff --ignore-submodules=dirty --quiet --exit-code 2> /dev/null ||
gitunstaged=1
fi
if (( querystaged )) ; then
if ${vcs_comm[cmd]} rev-parse --quiet --verify HEAD &> /dev/null ; then
${vcs_comm[cmd]} diff-index --cached --quiet --ignore-submodules=dirty HEAD 2> /dev/null
(( $? && $? != 128 )) && gitstaged=1
else
# empty repository (no commits yet)
# 4b825dc642cb6eb9a060e54bf8d69288fbee4904 is the git empty tree.
${vcs_comm[cmd]} diff-index --cached --quiet --ignore-submodules=dirty 4b825dc642cb6eb9a060e54bf8d69288fbee4904 2>/dev/null
(( $? && $? != 128 )) && gitstaged=1
fi
fi
fi
VCS_INFO_adjust
VCS_INFO_git_getaction ${gitdir}
local patchdir=${gitdir}/patches/${gitbranch}
if [[ -d $patchdir ]] && [[ -f $patchdir/applied ]] \
&& [[ -f $patchdir/unapplied ]]
then
# stgit
git_patches_applied=(${(f)"$(< "${patchdir}/applied")"})
git_patches_unapplied=(${(f)"$(< "${patchdir}/unapplied")"})
VCS_INFO_git_handle_patches
elif [[ -d "${gitdir}/rebase-merge" ]]; then
# 'git rebase -i'
patchdir="${gitdir}/rebase-merge"
local p
(( ${+functions[VCS_INFO_git_map_rebase_line_to_hash_and_subject]} )) ||
VCS_INFO_git_map_rebase_line_to_hash_and_subject() {
local p=$1
unset REPLY
# pick/edit/fixup/squash/reword: Add "$hash $subject" to $git_patches_applied.
# exec: Add "exec ${command}" to $git_patches_applied.
# (anything else): As 'exec'.
case $p in
([#]*)
# Comment line. Skip.
return 0
;;
(''(p|pick|e|edit|r|reword|f|fixup|s|squash)' '*)
# The line is of the form "pick $hash $subject".
# Just strip the verb and we're good to go.
p=${p#* }
# Special case: in an interactive rebase, if the user wrote "p $shorthash\n"
# in the editor (without a description after the hash), then the .../done
# file will contain "p $longhash $shorthash\n" (git 2.11.0) or "pick $longhash\n"
# (git 2.19.0).
if [[ $p != *\ * ]]; then
# The line is of the form "pick $longhash\n"
#
# Mark the log message subject as unknown.
# TODO: Can we performantly obtain the subject?
p+=" ?"
elif (( ${#${p//[^ ]}} == 1 )) && [[ ${p%% *} == ${p#* }* ]]; then
# The line is of the form "p $longhash $shorthash\n"
#
# The shorthash is superfluous, so discard it, and mark
# the log message subject as unknown.
# TODO: Can we performantly obtain the subject?
p="${p%% *} ?"
fi
;;
(''(x|exec) *)
# The line is of the form 'exec foo bar baz' where 'foo bar
# baz' is a shell command. There's no way to map _that_ to
# "$hash $subject", but I hope this counts as making an effort.
p=${p/#x /exec }
;;
(*)
# Forward compatibility with not-yet-existing 'git rebase -i' verbs.
if [[ $p != *\ * ]]; then
p+=" ?"
fi
esac
REPLY=$p
}
if [[ -f "${patchdir}/done" ]] ; then
for p in ${(f)"$(< "${patchdir}/done")"}; do
VCS_INFO_git_map_rebase_line_to_hash_and_subject "$p"
(( $+REPLY )) && git_patches_applied+=( "$REPLY" )
done
fi
if [[ -f "${patchdir}/git-rebase-todo" ]] ; then
for p in ${(f)"$(< "${patchdir}/git-rebase-todo")"}; do
VCS_INFO_git_map_rebase_line_to_hash_and_subject "$p"
(( $+REPLY )) && git_patches_unapplied+=( "$REPLY" )
done
fi
VCS_INFO_git_handle_patches
elif [[ -d "${gitdir}/rebase-apply" ]]; then
# 'git rebase' without -i, or 'git am'
patchdir="${gitdir}/rebase-apply"
local next="${patchdir}/next"
local this_patch_file
if [[ -f $next ]]; then
local cur=$(< $next)
local p subject
# Compute patch names for patches "before" the current patch
if [[ -r ${patchdir}/rewritten ]]; then
if zstyle -t ":vcs_info:${vcs}:${usercontext}:${rrn}" use-simple; then
git_patches_applied=( ${${(f)"$(<${patchdir}/rewritten)"}// */' ?'} )
else
git_patches_applied=(
${(f)"$(${vcs_comm[cmd]} log --no-walk=unsorted --pretty='%h %s' ${${(f)"$(<${patchdir}/rewritten)"}%% *} --)"}
)
fi
else
# Compatibility with old git. In 2.11 and 2.24, at least,
# (( cur == 1 )), so the loop body would never run. However, both
# of these versions have original-commit and orig-head and would
# take the 'if' branch, rather than this 'else' branch.
for ((p = 1; p < cur; p++)); do
printf -v this_patch_file "%s/%04d" "${patchdir}" "${p}"
if [[ -f $this_patch_file ]]; then
VCS_INFO_patch2subject "${this_patch_file}"
git_patches_applied+=( "$p $REPLY" )
else
git_patches_applied+=( "$p ?" )
fi
done
fi
# Set $subject to the info for the current patch
if [[ -f "${patchdir}/msg-clean" ]]; then
subject="${$(< "${patchdir}/msg-clean")[(f)1]}"
elif [[ -f "${patchdir}/final-commit" ]]; then
# This value is not rfc2047-encoded. It's also available via
# "${patchdir}/info".
subject="${$(< "${patchdir}/final-commit")[(f)1]}"
elif printf -v this_patch_file "%s/%04d" "${patchdir}" "${cur}"
[[ -f $this_patch_file ]]
then
# This branch is last for several reasons:
#
# - The "Subject" header will be MIME-encoded (rfc2047).
#
# - If the mail has full rfc822 headers (including "Received" and
# so on), we won't find the "Subject:" header, since
# VCS_INFO_patch2subject only checks the first few lines.
#
# - In --scissors mode, we may find the outer "Subject:" header,
# whereas the inner one (after the scissors line) will be used,
# if present.
() {
local REPLY
VCS_INFO_patch2subject "${this_patch_file}"
subject=$REPLY
}
fi
subject=${subject:-'?'}
if [[ -f "${patchdir}/original-commit" ]]; then
git_patches_applied+=("$(< ${patchdir}/original-commit) $subject")
elif [[ -f "${patchdir}/next" ]]; then
git_patches_applied+=("$(< ${patchdir}/next) $subject")
else
git_patches_applied+=("? $subject")
fi
# Handle patches scheduled for after the current patch, if instructed to.
if zstyle -t ":vcs_info:${vcs}:${usercontext}:${rrn}" get-unapplied; then
local last="$(< "${patchdir}/last")"
if [[ -r ${patchdir}/original-commit && -r ${patchdir}/orig-head ]] &&
! zstyle -t ":vcs_info:${vcs}:${usercontext}:${rrn}" use-simple
then
git_patches_unapplied+=( ${(f)"$(${vcs_comm[cmd]} log --reverse --pretty="%h %s" "$(<${patchdir}/original-commit)..$(<${patchdir}/orig-head)")"} )
else
for ((p = cur+1; p <= last; p++)); do
printf -v this_patch_file "%s/%04d" "${patchdir}" "${p}"
if [[ -f $this_patch_file ]]; then
VCS_INFO_patch2subject "${this_patch_file}"
git_patches_unapplied+=( "$p $REPLY" )
else
git_patches_unapplied+=( "$p ?" )
fi
done
fi
fi
fi
unset this_patch_file
VCS_INFO_git_handle_patches
elif [[ -f "${gitdir}/MERGE_HEAD" ]]; then
# This is 'git merge --no-commit'
local -a heads=( ${(@f)"$(<"${gitdir}/MERGE_HEAD")"} )
local subject;
# TODO: maybe read up to the first blank line
IFS='' read -r subject < "${gitdir}/MERGE_MSG"
# $subject is the subject line of the would-be commit
# Maybe we can get the subject lines of MERGE_HEAD's commits cheaply?
local p
for p in $heads[@]; do
git_patches_applied+=("$p $subject")
done
unset p
# Not touching git_patches_unapplied
VCS_INFO_git_handle_patches
elif [[ -f "${gitdir}/CHERRY_PICK_HEAD" ]]; then
# 'git cherry-pick' without -n, that conflicted. (With -n, git doesn't
# record the CHERRY_PICK_HEAD information anywhere, as of git 2.6.2.)
#
# ### 'git cherry-pick foo bar baz' only records the "remaining" part of
# ### the queue in the .git dir: if 'bar' has a conflict, the .git dir
# ### has a record of 'baz' being queued, but no record of 'foo' having been
# ### part of the queue as well. Therefore, the %n/%c applied/unapplied
# ### expandos will be memoryless: the "applied" counter will always
# ### be "1". The %u/%c tuple will assume the values [(1,2), (1,1), (1,0)],
# ### whereas the correct sequence would be [(1,2), (2,1), (3,0)].
local subject
# TODO: maybe read up to the first blank line
IFS='' read -r subject < "${gitdir}/MERGE_MSG"
git_patches_applied=( "$(<${gitdir}/CHERRY_PICK_HEAD) ${subject}" )
if [[ -f "${gitdir}/sequencer/todo" ]]; then
# Get the next patches, and remove the one that's in CHERRY_PICK_HEAD.
git_patches_unapplied=( ${${(M)${(f)"$(<"${gitdir}/sequencer/todo")"}:#pick *}#pick } )
git_patches_unapplied[1]=()
else
git_patches_unapplied=()
fi
VCS_INFO_git_handle_patches
else
gitmisc=''
fi
backend_misc[patches]="${gitmisc}"
VCS_INFO_formats "${gitaction}" "${gitbranch}" "${gitbase}" "${gitstaged}" "${gitunstaged}" "${gitsha1}" "${gitmisc}"
return 0
|