Source

thg / contrib / _hgtk

  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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#compdef hgtk

# Zsh completion script for hgtk.
# copy it into your zsh function path (/usr/share/zsh/site-functions)
#
# Copyright (C) 2009 Steve Borho
#
# This is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your
# option) any later version.

emulate -LR zsh
setopt extendedglob

local curcontext="$curcontext" state line
typeset -A _hg_cmd_globals

_hgtk() {
  local cmd _hg_root
  integer i=2
  _hg_cmd_globals=()

  while (( i < $#words ))
  do
    case "$words[$i]" in
      -R|--repository)
        eval _hg_root="$words[$i+1]"
        _hg_cmd_globals+=("$words[$i]" "$_hg_root")
        (( i += 2 ))
        continue
      ;;
      -R*)
        _hg_cmd_globals+="$words[$i]"
        eval _hg_root="${words[$i]#-R}"
       (( i++ ))
       continue
      ;;
      --cwd|--config)
        # pass along arguments to hg completer
        _hg_cmd_globals+=("$words[$i]" "$words[$i+1]")
        (( i += 2 ))
        continue
      ;;
      -*)
        # skip option
        (( i++ ))
        continue
      ;;
    esac
    if [[ -z "$cmd" ]]
    then
      cmd="$words[$i]"
      words[$i]=()
      (( CURRENT-- ))
    fi
    (( i++ ))
  done

  if [[ -z "$cmd" ]]
  then
    _arguments -s -w : $_hg_global_opts \
    ':hgtk command:_hg_commands'
    return
  fi

  # resolve abbreviations and aliases
  if ! (( $+functions[_hg_cmd_${cmd}] ))
  then
    local cmdexp
    (( $#_hg_cmd_list )) || _hg_get_commands

    cmdexp=$_hg_cmd_list[(r)${cmd}*]
    if [[ $cmdexp == $_hg_cmd_list[(R)${cmd}*] ]]
    then
      # might be nice to rewrite the command line with the expansion
      cmd="$cmdexp"
    fi
    if [[ -n $_hg_alias_list[$cmd] ]]
    then
      cmd=$_hg_alias_list[$cmd]
    fi
  fi

  curcontext="${curcontext%:*:*}:hg-${cmd}:"

  zstyle -s ":completion:$curcontext:" cache-policy update_policy

  if [[ -z "$update_policy" ]]
  then
    zstyle ":completion:$curcontext:" cache-policy _hg_cache_policy
  fi

  if (( $+functions[_hg_cmd_${cmd}] ))
  then
    _hg_cmd_${cmd}
  else
    # complete unknown commands normally
    _arguments -s -w : $_hg_global_opts \
      '*:files:_hg_files'
  fi
}

_hg_cache_policy() {
  typeset -a old

  # cache for a minute
  old=( "$1"(mm+10) )
  (( $#old )) && return 0

  return 1
}

_hg_get_commands() {
  typeset -ga _hg_cmd_list
  typeset -gA _hg_alias_list
  local hline cmd cmdalias

  _call_program hgtk hgtk debugcomplete -v 2>/dev/null | while read -A hline
  do
    cmd=$hline[1]
    _hg_cmd_list+=($cmd)

    for cmdalias in $hline[2,-1]
    do
      _hg_cmd_list+=($cmdalias)
      _hg_alias_list+=($cmdalias $cmd)
    done
  done
}

_hg_commands() {
  (( $#_hg_cmd_list )) || _hg_get_commands
  _describe -t commands 'hgtk command' _hg_cmd_list
}

_hg_revrange() {
  compset -P 1 '*:'
  _hg_tags "$@"
}

_hg_tags() {
  typeset -a tags
  local tag rev

  _hg_cmd tags 2> /dev/null | while read tag
  do
    tags+=(${tag/ #    [0-9]#:*})
  done
  (( $#tags )) && _describe -t tags 'tags' tags
}

_hg_files() {
  if [[ -n "$_hg_root" ]]
  then
    [[ -d "$_hg_root/.hg" ]] || return
    case "$_hg_root" in
      /*)
        _files -W $_hg_root
      ;;
      *)
        _files -W $PWD/$_hg_root
      ;;
    esac
  else
    _files
  fi
}

_hg_status() {
  [[ -d $PREFIX ]] || PREFIX=$PREFIX:h
  status_files=(${(ps:\0:)"$(_hg_cmd status -0n$1 ./$PREFIX 2>/dev/null)"})
}

_hg_unknown() {
  typeset -a status_files
  _hg_status u
  _wanted files expl 'unknown files' _multi_parts / status_files
}

_hg_missing() {
  typeset -a status_files
  _hg_status d
  _wanted files expl 'missing files' _multi_parts / status_files
}

_hg_modified() {
  typeset -a status_files
  _hg_status m
  _wanted files expl 'modified files' _multi_parts / status_files
}

_hg_resolve() {
  local rstate rpah

  [[ -d $PREFIX ]] || PREFIX=$PREFIX:h

  _hg_cmd resolve -l ./$PREFIX 2> /dev/null | while read rstate rpath
  do
    [[ $rstate == 'R' ]] && resolved_files+=($rpath)
    [[ $rstate == 'U' ]] && unresolved_files+=($rpath)
  done
}

_hg_resolved() {
  typeset -a resolved_files unresolved_files
  _hg_resolve
  _wanted files expl 'resolved files' _multi_parts / resolved_files
}

_hg_unresolved() {
  typeset -a resolved_files unresolved_files
  _hg_resolve
  _wanted files expl 'unresolved files' _multi_parts / unresolved_files
}

_hg_config() {
    typeset -a items
    items=(${${(%f)"$(_call_program hg hg showconfig)"}%%\=*})
    (( $#items )) && _describe -t config 'config item' items
}

_hg_addremove() {
  _alternative 'files:unknown files:_hg_unknown' \
    'files:missing files:_hg_missing'
}

_hg_ssh_urls() {
  if [[ -prefix */ ]]
  then
    if zstyle -T ":completion:${curcontext}:files" remote-access
    then
      local host=${PREFIX%%/*}
      typeset -a remdirs
      compset -p $(( $#host + 1 ))
      local rempath=${(M)PREFIX##*/}
      local cacheid="hg:${host}-${rempath//\//_}"
      cacheid=${cacheid%[-_]}
      compset -P '*/'
      if _cache_invalid "$cacheid" || ! _retrieve_cache "$cacheid"
      then
        remdirs=(${${(M)${(f)"$(_call_program files ssh -a -x $host ls -1FL "${(q)rempath}" 2> /dev/null)"}##*/}%/})
        _store_cache "$cacheid" remdirs
      fi
      _describe -t directories 'remote directory' remdirs -S/
    else
      _message 'remote directory'
    fi
  else
    if compset -P '*@'
    then
      _hosts -S/
    else
      _alternative 'hosts:remote host name:_hosts -S/' \
        'users:user:_users -S@'
    fi
  fi
}

_hg_urls() {
  if compset -P bundle://
  then
    _files
  elif compset -P ssh://
  then
    _hg_ssh_urls
  elif [[ -prefix *: ]]
  then
    _urls
  else
    local expl
    compset -S '[^:]*'
    _wanted url-schemas expl 'URL schema' compadd -S '' - \
      http:// https:// ssh:// bundle://
  fi
}

_hg_paths() {
  typeset -a paths pnames
  _hg_cmd paths 2> /dev/null | while read -A pnames
  do
    paths+=($pnames[1])
  done
  (( $#paths )) && _describe -t path-aliases 'repository alias' paths
}

_hg_remote() {
  _alternative 'path-aliases:repository alias:_hg_paths' \
    'directories:directory:_files -/' \
    'urls:URL:_hg_urls'
}

_hg_clone_dest() {
  _alternative 'directories:directory:_files -/' \
    'urls:URL:_hg_urls'
}

# Common options
_hg_global_opts=(
    '(--repository -R)'{-R+,--repository}'[repository root directory]:repository:_files -/'
    '(--verbose -v)'{-v,--verbose}'[enable additional output]'
    '(--quiet -q)'{-q,--quiet}'[suppress output]'
    '(--help -h)'{-h,--help}'[display help and exit]'
    '--debugger[start debugger]'
)

_hg_pat_opts=(
  '*'{-I+,--include}'[include names matching the given patterns]:dir:_files -W $(_hg_cmd root) -/'
  '*'{-X+,--exclude}'[exclude names matching the given patterns]:dir:_files -W $(_hg_cmd root) -/')

_hg_diff_opts=(
  '(--text -a)'{-a,--text}'[treat all files as text]'
  '(--git -g)'{-g,--git}'[use git extended diff format]'
  "--nodates[don't include dates in diff headers]")

_hg_style_opts=(
  '--style[display using template map file]:'
  '--template[display with template]:')

_hg_commit_opts=(
  '(-m --message -l --logfile --edit -e)'{-e,--edit}'[edit commit message]'
  '(-e --edit -l --logfile --message -m)'{-m+,--message}'[use <text> as commit message]:message:'
  '(-e --edit -m --message --logfile -l)'{-l+,--logfile}'[read the commit message from <file>]:log file:_files')

_hg_remote_opts=(
  '(--ssh -e)'{-e+,--ssh}'[specify ssh command to use]:'
  '--remotecmd[specify hg command to run on the remote side]:')

_hg_cmd() {
  _call_program hg hg "$_hg_cmd_globals[@]" "$@"
}

_hg_cmd_add() {
  _arguments -s -w : $_hg_global_opts $_hg_pat_opts \
  '*:unknown files:_hg_unknown'
}

_hg_cmd_annotate() {
  _arguments -s -w : $_hg_global_opts $_hg_pat_opts \
  '(--rev -r)'{-r+,--rev}'[annotate the specified revision]:revision:_hg_tags' \
  '(--follow -f)'{-f,--follow}'[follow file copies and renames]' \
  '(--text -a)'{-a,--text}'[treat all files as text]' \
  '(--user -u)'{-u,--user}'[list the author]' \
  '(--date -d)'{-d,--date}'[list the date]' \
  '(--number -n)'{-n,--number}'[list the revision number (default)]' \
  '(--changeset -c)'{-c,--changeset}'[list the changeset]' \
  '*:files:_hg_files'
}

_hg_cmd_clone() {
  _arguments -s -w : $_hg_global_opts $_hg_remote_opts \
  '(--noupdate -U)'{-U,--noupdate}'[do not update the new working directory]' \
  '(--rev -r)'{-r+,--rev}'[a changeset you would like to have after cloning]:' \
  '--uncompressed[use uncompressed transfer (fast over LAN)]' \
  ':source repository:_hg_remote' \
  ':destination:_hg_clone_dest'
}

_hg_cmd_commit() {
  _arguments -s -w : $_hg_global_opts $_hg_pat_opts \
  '(--addremove -A)'{-A,--addremove}'[mark new/missing files as added/removed before committing]' \
  '(--message -m)'{-m+,--message}'[use <text> as commit message]:text:' \
  '(--logfile -l)'{-l+,--logfile}'[read commit message from <file>]:log file:_files -g \*.txt' \
  '(--date -d)'{-d+,--date}'[record datecode as commit date]:date code:' \
  '(--user -u)'{-u+,--user}'[record user as commiter]:user:' \
  '*:file:_hg_files'
}

_hg_cmd_vdiff() {
  typeset -A opt_args
  _arguments -s -w : $_hg_global_opts $_hg_pat_opts $_hg_diff_opts \
  '*'{-r,--rev}'+[revision]:revision:_hg_revrange' \
  '(--show-function -p)'{-p,--show-function}'[show which function each change is in]' \
  '(--ignore-all-space -w)'{-w,--ignore-all-space}'[ignore white space when comparing lines]' \
  '(--ignore-space-change -b)'{-b,--ignore-space-change}'[ignore changes in the amount of white space]' \
  '(--ignore-blank-lines -B)'{-B,--ignore-blank-lines}'[ignore changes whose lines are all blank]' \
  '*:file:->diff_files'

  if [[ $state == 'diff_files' ]]
  then
    if [[ -n $opt_args[-r] ]]
    then
      _hg_files
    else
      _hg_modified
    fi
  fi
}

_hg_cmd_export() {
  _arguments -s -w : $_hg_global_opts $_hg_diff_opts \
  '(--outout -o)'{-o+,--output}'[print output to file with formatted name]:filespec:' \
  '--switch-parent[diff against the second parent]' \
  '*:revision:_hg_tags'
}

_hg_cmd_grep() {
  _arguments -s -w : $_hg_global_opts $_hg_pat_opts \
  '(--print0 -0)'{-0,--print0}'[end filenames with NUL]' \
  '--all[print all revisions with matches]' \
  '(--follow -f)'{-f,--follow}'[follow changeset or file history]' \
  '(--ignore-case -i)'{-i,--ignore-case}'[ignore case when matching]' \
  '(--files-with-matches -l)'{-l,--files-with-matches}'[print only filenames and revs that match]' \
  '(--line-number -n)'{-n,--line-number}'[print matching line numbers]' \
  '*'{-r+,--rev}'[search in given revision range]:revision:_hg_revrange' \
  '(--user -u)'{-u,--user}'[print user who committed change]' \
  '1:search pattern:' \
  '*:files:_hg_files'
}

_hg_cmd_heads() {
  _arguments -s -w : $_hg_global_opts $_hg_style_opts \
  '(--rev -r)'{-r+,--rev}'[show only heads which are descendants of rev]:revision:_hg_tags'
}

_hg_cmd_help() {
  _arguments -s -w : $_hg_global_opts \
  '*:hgtk command:_hg_commands'
}

_hg_cmd_incoming() {
  _arguments -s -w : $_hg_global_opts $_hg_remote_opts $_hg_style_opts \
  '(--no-merges -M)'{-M,--no-merges}'[do not show merge revisions]' \
  '(--force -f)'{-f,--force}'[run even when the remote repository is unrelated]' \
  '(--patch -p)'{-p,--patch}'[show patch]' \
  '(--rev -r)'{-r+,--rev}'[a specific revision up to which you would like to pull]:revision:_hg_tags' \
  '(--newest-first -n)'{-n,--newest-first}'[show newest record first]' \
  '--bundle[file to store the bundles into]:bundle file:_files' \
  ':source:_hg_remote'
}

_hg_cmd_init() {
  _arguments -s -w : $_hg_global_opts $_hg_remote_opts \
  ':dir:_files -/'
}

_hg_cmd_log() {
  _arguments -s -w : $_hg_global_opts $_hg_pat_opts $_hg_style_opts \
  '(--follow --follow-first -f)'{-f,--follow}'[follow changeset or history]' \
  '(-f --follow)--follow-first[only follow the first parent of merge changesets]' \
  '(--copies -C)'{-C,--copies}'[show copied files]' \
  '(--keyword -k)'{-k+,--keyword}'[search for a keyword]:' \
  '(--limit -l)'{-l+,--limit}'[limit number of changes displayed]:' \
  '*'{-r,--rev}'[show the specified revision or range]:revision:_hg_revrange' \
  '(--no-merges -M)'{-M,--no-merges}'[do not show merges]' \
  '(--only-merges -m)'{-m,--only-merges}'[show only merges]' \
  '(--patch -p)'{-p,--patch}'[show patch]' \
  '(--prune -P)'{-P+,--prune}'[do not display revision or any of its ancestors]:revision:_hg_tags' \
  '*:files:_hg_files'
}

_hg_cmd_outgoing() {
  _arguments -s -w : $_hg_global_opts $_hg_remote_opts $_hg_style_opts \
  '(--no-merges -M)'{-M,--no-merges}'[do not show merge revisions]' \
  '(--force -f)'{-f,--force}'[run even when the remote repository is unrelated]' \
  '(--patch -p)'{-p,--patch}'[show patch]' \
  '(--rev -r)'{-r+,--rev}'[a specific revision you would like to push]' \
  '(--newest-first -n)'{-n,--newest-first}'[show newest record first]' \
  ':destination:_hg_remote'
}

_hg_cmd_pull() {
  _arguments -s -w : $_hg_global_opts $_hg_remote_opts \
  '(--force -f)'{-f,--force}'[run even when the remote repository is unrelated]' \
  '(--update -u)'{-u,--update}'[update to new tip if changesets were pulled]' \
  '(--rev -r)'{-r+,--rev}'[a specific revision up to which you would like to pull]:revision:' \
  ':source:_hg_remote'
}

_hg_cmd_push() {
  _arguments -s -w : $_hg_global_opts $_hg_remote_opts \
  '(--force -f)'{-f,--force}'[force push]' \
  '(--rev -r)'{-r+,--rev}'[a specific revision you would like to push]:revision:_hg_tags' \
  ':destination:_hg_remote'
}

_hg_cmd_remove() {
  _arguments -s -w : $_hg_global_opts $_hg_pat_opts \
  '(--after -A)'{-A,--after}'[record remove that has already occurred]' \
  '(--force -f)'{-f,--force}'[remove file even if modified]' \
  '*:file:_hg_files'
}

_hg_cmd_rename() {
  _arguments -s -w : $_hg_global_opts $_hg_pat_opts \
  '(--after -A)'{-A,--after}'[record a rename that has already occurred]' \
  '(--force -f)'{-f,--force}'[forcibly copy over an existing managed file]' \
  '*:file:_hg_files'
}

_hg_cmd_revert() {
  local context state line
  typeset -A opt_args

  _arguments -s -w : $_hg_global_opts $_hg_pat_opts \
  '(--all -a :)'{-a,--all}'[revert all changes when no arguments given]' \
  '(--rev -r)'{-r+,--rev}'[revision to revert to]:revision:_hg_tags' \
  '--no-backup[do not save backup copies of files]' \
  '*:file:->diff_files'

  if [[ $state == 'diff_files' ]]
  then
    if [[ -n $opt_args[-r] ]]
    then
      _hg_files
    else
      typeset -a status_files
      _hg_status mard
      _wanted files expl 'modified, added, removed or deleted file' _multi_parts / status_files
    fi
  fi
}

_hg_cmd_serve() {
  _arguments -s -w : $_hg_global_opts \
  '(--accesslog -A)'{-A+,--accesslog}'[name of access log file]:log file:_files' \
  '(--errorlog -E)'{-E+,--errorlog}'[name of error log file]:log file:_files' \
  '(--daemon -d)'{-d,--daemon}'[run server in background]' \
  '(--port -p)'{-p+,--port}'[listen port]:listen port:' \
  '(--address -a)'{-a+,--address}'[interface address]:interface address:' \
  '(--name -n)'{-n+,--name}'[name to show in web pages]:repository name:' \
  '(--templates -t)'{-t,--templates}'[web template directory]:template dir:_files -/' \
  '--style[web template style]:style' \
  '--stdio[for remote clients]' \
  '(--ipv6 -6)'{-6,--ipv6}'[use IPv6 in addition to IPv4]'
}

_hg_cmd_status() {
  _arguments -s -w : $_hg_global_opts $_hg_pat_opts \
  '(--all -A)'{-A,--all}'[show status of all files]' \
  '(--modified -m)'{-m,--modified}'[show only modified files]' \
  '(--added -a)'{-a,--added}'[show only added files]' \
  '(--removed -r)'{-r,--removed}'[show only removed files]' \
  '(--deleted -d)'{-d,--deleted}'[show only deleted (but tracked) files]' \
  '(--clean -c)'{-c,--clean}'[show only files without changes]' \
  '(--unknown -u)'{-u,--unknown}'[show only unknown files]' \
  '(--ignored -i)'{-i,--ignored}'[show ignored files]' \
  '(--no-status -n)'{-n,--no-status}'[hide status prefix]' \
  '(--copies -C)'{-C,--copies}'[show source of copied files]' \
  '(--print0 -0)'{-0,--print0}'[end filenames with NUL, for use with xargs]' \
  '--rev[show difference from revision]:revision:_hg_tags' \
  '*:files:_files'
}

_hg_cmd_update() {
  _arguments -s -w : $_hg_global_opts \
  '(--clean -C)'{-C,--clean}'[overwrite locally modified files]' \
  '(--rev -r)'{-r+,--rev}'[revision]:revision:_hg_tags' \
  ':revision:_hg_tags'
}

_hgtk "$@"