about summary refs log tree commit diff
path: root/Completion/Unix/Command/_hg
blob: 7b72605b396ae98bfa50ea71e9fff0133265df8d (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
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
#compdef hg

# Zsh completion script for mercurial.  Rename this file to _hg and copy
# it into your zsh function path (/usr/share/zsh/site-functions for
# instance)
#
# If you do not want to install it globally, you can copy it somewhere
# else and add that directory to $fpath. This must be done before
# compinit is called. If the file is copied to ~/.zsh.d, your ~/.zshrc
# file could look like this:
#
# fpath=("$HOME/.zsh.d" $fpath)
# autoload -U compinit
# compinit
#
# Copyright (C) 2005, 2006 Steve Borho <steve@borho.org>
# Copyright (C) 2006-9 Brendan Cully <brendan@kublai.com>
#
# Permission is hereby granted, without written agreement and without
# licence or royalty fees, to use, copy, modify, and distribute this
# software and to distribute modified versions of this software for any
# purpose, provided that the above copyright notice and the following
# two paragraphs appear in all copies of this software.
#
# In no event shall the authors be liable to any party for direct,
# indirect, special, incidental, or consequential damages arising out of
# the use of this software and its documentation, even if the authors
# have been advised of the possibility of such damage.
#
# The authors specifically disclaim any warranties, including, but not
# limited to, the implied warranties of merchantability and fitness for
# a particular purpose.  The software provided hereunder is on an "as
# is" basis, and the authors have no obligation to provide maintenance,
# support, updates, enhancements, or modifications.

emulate -LR zsh
setopt extendedglob

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

_hg() {
  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 -S : $_hg_global_opts \
    ':mercurial 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 -S : $_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 hg hg debugcomplete -v | 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 'mercurial command' _hg_cmd_list
}

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

_hg_tags_internal() {
  local -a expl
  typeset -a hgtags
  hgtags=( ${(f)"$(_hg_cmd tags -q 2>/dev/null)"} )
  _wanted tags expl 'tags' compadd -a - hgtags
}

_hg_bookmarks_internal() {
  local -a expl
  typeset -a hgbookmarks
  hgbookmarks=( ${(f)"$(_hg_cmd bookmarks -q 2>/dev/null)"} )
  _wanted bookmarks expl 'bookmarks' compadd -a - hgbookmarks
}

_hg_branches_internal() {
  local -a expl
  typeset -a hgbranches
  hgbranches=( ${(f)"$(_hg_cmd branches -q 2>/dev/null)"} )
  _wanted branches expl 'branches' compadd -a - hgbranches
}

_hg_tags() {
  _alternative \
    'bookmarks:bookmark:_hg_bookmarks_internal' \
    'tags:tag:_hg_tags_internal'
}

# likely merge candidates
_hg_mergerevs() {
  typeset -a heads
  local revset='sort(head() and not ., -rev)'

  heads=(${(f)"$(_hg_cmd log -r '$revset' --template '{rev}\\n')"})
  (( $#heads )) && _describe -t heads 'heads' heads
}

_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_committable() {
  typeset -a status_files
  # A file is a candidate for `hg commit` if it is:
  # - modified (m), or
  # - added (a), or
  # - removed (r)
  _hg_status mar
  _wanted files expl 'committable files' _multi_parts / status_files
}

_hg_resolve() {
  local rstate rpath

  [[ -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_internal_merge_tools=(
  \\:dump \\:fail \\:local \\:merge \\:merge-local \\:merge-other \\:merge3
  \\:other \\:prompt \\:tagmerge \\:union
)

_hg_merge_tools() {
  typeset -a external_tools
  _describe -t internal_tools 'internal merge tools' _hg_internal_merge_tools
  external_tools=(${(f)"$(_hg_cmd showconfig merge-tools | cut -d . -f 2)"})
  (( $#external_tools )) && _describe -t external_tools 'external merge tools' external_tools
}

_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 or name of overlay bundle file]:repository:_files -/'
    '--cwd=[change working directory]:new working directory:_files -/'
    '(--noninteractive -y)'{-y,--noninteractive}'[do not prompt, automatically pick the first choice for all prompts]'
    '(--verbose -v)'{-v,--verbose}'[enable additional output]'
    '*--config[set/override config option]:defined config items:_hg_config'
    '(--quiet -q)'{-q,--quiet}'[suppress output]'
    '(--help -h)'{-h,--help}'[display help and exit]'
    '--debug[enable debugging output]'
    '--debugger[start debugger]'
    '--encoding=[set the charset encoding]:encoding'
    '--encodingmode=[set the charset encoding mode]:encoding mode'
    '--traceback[always print a traceback on exception]'
    '--time[time how long the command takes]'
    '--profile[print command execution profile]'
    '--version[output version information and exit]'
    '--hidden[consider hidden changesets]'
)

_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[omit dates from diff headers]'
)

_hg_mergetool_opts=(
  '(--tool -t)'{-t+,--tool=}'[specify merge tool]:merge tool:_hg_merge_tools'
)

_hg_dryrun_opts=(
  '(--dry-run -n)'{-n,--dry-run}'[do not perform actions, just print output]')

_hg_template_opts=(
  '--template[display with template]: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]:command'
  '--remotecmd=[specify hg command to run on the remote side]:remote command'
  '--insecure[do not verify server certificate (ignoring web.cacerts config)]'
)

_hg_subrepos_opts=(
  '(--subrepos -S)'{-S,--subrepos}'[recurse into subrepositories]'
)

_hg_cmd() {
  _call_program hg hg --config ui.verbose=0 --config defaults."$1"= \
    "$_hg_cmd_globals[@]" "$@" 2> /dev/null
}

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

_hg_cmd_addremove() {
  _arguments -s -S : $_hg_global_opts $_hg_pat_opts $_hg_dryrun_opts $_hg_subrepos_opts \
  '(--similarity -s)'{-s+,--similarity=}'[guess renamed files by similarity (0<=s<=100)]:similarity' \
  '*:unknown or missing files:_hg_addremove'
}

_hg_cmd_annotate() {
  _arguments -s -S : $_hg_global_opts $_hg_pat_opts \
  '(--rev -r)'{-r+,--rev=}'[annotate the specified revision]:revision:_hg_tags' \
  "--no-follow[don't follow copies and renames]" \
  '(--text -a)'{-a,--text}'[treat all files as text]' \
  '(--user -u)'{-u,--user}'[list the author (long with -v)]' \
  '(--file -f)'{-f,--file}'[list the filename]' \
  '(--date -d)'{-d,--date}'[list the date (short with -q)]' \
  '(--number -n)'{-n,--number}'[list the revision number (default)]' \
  '(--changeset -c)'{-c,--changeset}'[list the changeset]' \
  '(--line-number -l)'{-l,--line-number}'[show line number at the first appearance]' \
  '*:files:_hg_files'
}

_hg_cmd_archive() {
  _arguments -s -S : $_hg_global_opts $_hg_pat_opts $_hg_subrepos_opts \
  '--no-decode[do not pass files through decoders]' \
  '(--prefix -p)'{-p+,--prefix=}'[directory prefix for files in archive]:prefix' \
  '(--rev -r)'{-r+,--rev=}'[revision to distribute]:revision:_hg_tags' \
  '(--type -t)'{-t+,--type=}'[type of distribution to create]:archive type:(files tar tbz2 tgz uzip zip)' \
  '*:destination:_files'
}

_hg_cmd_backout() {
  _arguments -s -S : $_hg_global_opts $_hg_mergetool_opts $_hg_pat_opts \
    '--merge[merge with old dirstate parent after backout]' \
    '(--date -d)'{-d+,--date=}'[record the specified date as commit date]:date' \
    '--parent[parent to choose when backing out merge]' \
    '(--user -u)'{-u+,--user=}'[record the specified user as committer]:user' \
    '(--rev -r)'{-r+,--rev=}'[revision]:revision:_hg_tags' \
    '(--message -m)'{-m+,--message=}'[specify commit message]:text' \
    '(--logfile -l)'{-l+,--logfile=}'[read commit message from specified file]:log file:_files'
}

_hg_cmd_bisect() {
  _arguments -s -S : $_hg_global_opts \
  '(-)'{-r,--reset}'[reset bisect state]' \
  '(--extend -e)'{-e,--extend}'[extend the bisect range]' \
  '(--good -g --bad -b --skip -s --reset -r)'{-g,--good}'[mark changeset good]'::revision:_hg_tags \
  '(--good -g --bad -b --skip -s --reset -r)'{-b,--bad}'[mark changeset bad]'::revision:_hg_tags \
  '(--good -g --bad -b --skip -s --reset -r)'{-s,--skip}'[skip testing changeset]' \
  '(--command -c --noupdate -U)'{-c+,--command=}'[use command to check changeset state]':commands:_command_names \
  '(--command -c --noupdate -U)'{-U,--noupdate}'[do not update to target]'
}

_hg_cmd_bookmarks() {
  _arguments -s -S : $_hg_global_opts \
  '(--force -f)'{-f,--force}'[force]' \
  '(--rev -r --delete -d --rename -m)'{-r+,--rev=}'[set bookmark at revision]:revision:_hg_tags' \
  '(--rev -r --delete -d --rename -m)'{-d,--delete}'[delete a given bookmark]' \
  '(--rev -r --delete -d --rename -m)'{-m+,--rename=}'[rename given bookmark]:bookmark:_hg_bookmarks_internal' \
  '(--inactive -i)'{-i,--inactive}'[mark a bookmark inactive]' \
  ':bookmark:_hg_bookmarks_internal'
}

_hg_cmd_branch() {
  _arguments -s -S : $_hg_global_opts \
  '(--force -f)'{-f,--force}'[set branch name even if it shadows an existing branch]' \
  '(--clean -C)'{-C,--clean}'[reset branch name to parent branch name]'
}

_hg_cmd_branches() {
  _arguments -s -S : $_hg_global_opts \
  '(--closed -c)'{-c,--closed}'[show normal and closed branches]'
}

_hg_cmd_bundle() {
  _arguments -s -S : $_hg_global_opts $_hg_remote_opts \
  '(--force -f)'{-f,--force}'[run even when the destination is unrelated]' \
  '(2)*--base[a base changeset assumed to be available at the destination]:revision:_hg_tags' \
  '*'{-b+,--branch=}'[a specific branch you would like to bundle]:branch:_hg_branches_internal' \
  '*'{-r+,--rev=}'[a changeset intended to be added to the destination]:revision:_hg_tags' \
  '--all[bundle all changesets in the repository]' \
  '--type[bundle compression type to use (default: bzip2)]:bundle type' \
  ':output file:_files' \
  ':destination repository:_files -/'
}

_hg_cmd_cat() {
  _arguments -s -S : $_hg_global_opts $_hg_pat_opts \
  '(--output -o)'{-o+,--output=}'[print output to file with formatted name]:format string' \
  '(--rev -r)'{-r+,--rev=}'[revision]:revision:_hg_tags' \
  '--decode[apply any matching decode filter]' \
  '*:file:_hg_files'
}

_hg_cmd_clone() {
  _arguments -s -S : $_hg_global_opts $_hg_remote_opts \
  '(--noupdate -U)'{-U,--noupdate}'[do not update the new working directory]' \
  '(--rev -r)'{-r+,--rev=}'[include the specified changeset]:revision' \
  '--uncompressed[use uncompressed transfer (fast over LAN)]' \
  ':source repository:_hg_remote' \
  ':destination:_hg_clone_dest'
}

_hg_cmd_commit() {
  _arguments -s -S : $_hg_global_opts $_hg_pat_opts $_hg_subrepos_opts \
  '(--addremove -A)'{-A,--addremove}'[mark new/missing files as added/removed before committing]' \
  '(--message -m)'{-m+,--message=}'[specify commit message]:text' \
  '(--logfile -l)'{-l+,--logfile=}'[read commit message from specified file]:log file:_files' \
  '(--date -d)'{-d+,--date=}'[record the specified date as commit date]:date' \
  '(--user -u)'{-u+,--user=}'[record the specified user as committer]:user' \
  '--amend[amend the parent of the working directory]' \
  '--close-branch[mark a branch head as closed]' \
  '(--interactive -i)'{-i,--interactive}'[use interactive mode]' \
  '(--secret -s)'{-s,--secret}'[use the secret phase for committing]' \
  '*:file:_hg_committable'
}

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

_hg_cmd_diff() {
  local context state state_descr line ret=1
  typeset -A opt_args

  _arguments -s -S : $_hg_global_opts $_hg_pat_opts $_hg_diff_opts $_hg_subrepos_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' && ret=0

  if [[ $state == 'diff_files' ]]
  then
    if [[ -n ${opt_args[(I)-r|--rev]} ]]
    then
      _hg_files && ret=0
    else
      _hg_modified && ret=0
    fi
  fi

  return ret
}

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

_hg_cmd_forget() {
  _arguments -s -S : $_hg_global_opts $_hg_pat_opts \
  '*:file:_hg_files'
}

_hg_cmd_grep() {
  _arguments -s -S : $_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 -S : $_hg_global_opts $_hg_template_opts \
  '(--topo -t)'{-t,--topo}'[show topological heads only]' \
  '(--closed -c)'{-c,--closed}'[show normal and closed branch heads]' \
  '(--rev -r)'{-r+,--rev=}'[show only heads which are descendants of rev]:revision:_hg_tags'
}

_hg_cmd_help() {
  _arguments -s -S : $_hg_global_opts \
  '(--extension -e)'{-e,--extension}'[show only help for extensions]' \
  '(--command -c)'{-c,--command}'[show only help for commands]' \
  '(--keyword -k)'{-k,--keyword}'[show topics matching keyword]' \
  '*:mercurial command:_hg_commands'
}

_hg_cmd_identify() {
  _arguments -s -S : $_hg_global_opts \
  '(--rev -r)'{-r+,--rev=}'[identify the specified rev]:revision:_hg_tags' \
  '(--num -n)'{-n,--num}'[show local revision number]' \
  '(--id -i)'{-i,--id}'[show global revision id]' \
  '(--branch -b)'{-b,--branch}'[show branch]' \
  '(--tags -t)'{-t,--tags}'[show tags]' \
  '(--bookmarks -B)'{-B,--bookmarks}'[show bookmarks]'
}

_hg_cmd_import() {
  _arguments -s -S : $_hg_global_opts \
  '(--strip -p)'{-p+,--strip=}'[directory strip option for patch (default: 1)]:count' \
  '(--message -m)'{-m+,--message=}'[use <text> as commit message]:text:' \
  '(--force -f)'{-f,--force}'[skip check for outstanding uncommitted changes]' \
  '--bypass[apply patch without touching the working directory]' \
  '--no-commit[do not commit, just update the working directory]' \
  '--partial[commit even if some hunks fail]' \
  '--exact[abort if patch would apply lossily]' \
  '--import-branch[use any branch information in patch (implied by --exact)]' \
  '(--date -d)'{-d+,--date=}'[record the specified date as commit date]:date' \
  '(--user -u)'{-u+,--user=}'[record the specified user as committer]:user' \
  '(--similarity -s)'{-s+,--similarity=}'[guess renamed files by similarity (0<=s<=100)]:similarity' \
  '*:patch:_files'
}

_hg_cmd_incoming() {
  _arguments -s -S : $_hg_global_opts $_hg_remote_opts $_hg_template_opts $_hg_subrepos_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]' \
  '*'{-r+,--rev=}'[a remote changeset intended to be added]: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 -S : $_hg_global_opts $_hg_remote_opts \
  ':dir:_files -/'
}

_hg_cmd_locate() {
  _arguments -s -S : $_hg_global_opts $_hg_pat_opts \
  '(--rev -r)'{-r+,--rev=}'[search repository as it stood at revision]:revision:_hg_tags' \
  '(--print0 -0)'{-0,--print0}'[end filenames with NUL, for use with xargs]' \
  '(--fullpath -f)'{-f,--fullpath}'[print complete paths from the filesystem root]' \
  '*:search pattern:_hg_files'
}

_hg_cmd_log() {
  _arguments -s -S : $_hg_global_opts $_hg_pat_opts $_hg_template_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]' \
  '*'{-k+,--keyword=}'[search for a keyword]:keyword' \
  '(--limit -l)'{-l+,--limit=}'[limit number of changes displayed]:limit' \
  '*'{-r+,--rev=}'[show the specified revision or revset]: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]' \
  '*'{-P+,--prune=}'[do not display revision or any of its ancestors]:revision:_hg_tags' \
  '*:files:_hg_files'
}

_hg_cmd_manifest() {
  _arguments -s -S : $_hg_global_opts \
  '--all[list files from all revisions]' \
  ':revision:_hg_tags'
}

_hg_cmd_merge() {
  _arguments -s -S : $_hg_global_opts $_hg_mergetool_opts \
  '(--force -f)'{-f,--force}'[force a merge with outstanding changes]' \
  '(--rev -r 1)'{-r+,--rev=}'[revision to merge]:revision:_hg_mergerevs' \
  '(--preview -P)'{-P,--preview}'[review revisions to merge (no merge is performed)]' \
  ':revision:_hg_mergerevs'
}

_hg_cmd_outgoing() {
  _arguments -s -S : $_hg_global_opts $_hg_remote_opts $_hg_template_opts $_hg_subrepos_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]' \
  '*'{-r+,--rev=}'[a changeset intended to be included in the destination]:revision:_hg_tags' \
  '(--newest-first -n)'{-n,--newest-first}'[show newest record first]' \
  ':destination:_hg_remote'
}

_hg_cmd_parents() {
  _arguments -s -S : $_hg_global_opts $_hg_template_opts \
  '(--rev -r)'{-r+,--rev=}'[show parents of the specified rev]:revision:_hg_tags' \
  ':last modified file:_hg_files'
}

_hg_cmd_paths() {
  _arguments -s -S : $_hg_global_opts \
  ':path:_hg_paths'
}

_hg_cmd_phase() {
  _arguments -s -S : $_hg_global_opts \
  '(--public -p --draft -d --secret -s)'{-p,--public}'[set changeset phase to public]' \
  '(--public -p --draft -d --secret -s)'{-d,--draft}'[set changeset phase to draft]' \
  '(--public -p --draft -d --secret -s)'{-s,--secret}'[set changeset phase to secret]' \
  '(--force -f)'{-f,--force}'[allow to move boundary backward]' \
  '*'{-r+,--rev=}'[target revision]:revision:_hg_tags' \
  '*:revision:_hg_tags'
}

_hg_cmd_pull() {
  _arguments -s -S : $_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 -S : $_hg_global_opts $_hg_remote_opts \
  '(--force -f)'{-f,--force}'[force push]' \
  '*'{-r+,--rev=}'[a changeset intended to be included in the destination]:revision:_hg_tags' \
  '*'{-B,--bookmark=}'[bookmark to push]:bookmark:_hg_bookmarks_internal' \
  '*'{-b,--branch=}'[branch to push]:branch:_hg_branches_internal' \
  '--new-branch[allow pushing a new branch]' \
  ':destination:_hg_remote'
}

_hg_cmd_remove() {
  _arguments -s -S : $_hg_global_opts $_hg_pat_opts $_hg_subrepos_opts \
  '(--after -A)'{-A,--after}'[record delete for missing files]' \
  '(--force -f)'{-f,--force}'[forget added files, delete modified files]' \
  '*:file:_hg_files'
}

_hg_cmd_rename() {
  _arguments -s -S : $_hg_global_opts $_hg_pat_opts $_hg_dryrun_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_resolve() {
  local context state state_descr line ret=1
  typeset -A opt_args

  _arguments -s -S : $_hg_global_opts $_hg_mergetool_opts \
  '(--all -a)'{-a,--all}'[select all unresolved files]' \
  '(--no-status -n)'{-n,--no-status}'[hide status prefix]' \
  '(--list -l --mark -m --unmark -u)'{-l,--list}'[list state of files needing merge]:*:merged files:->resolve_files' \
  '(--mark -m --list -l --unmark -u)'{-m,--mark}'[mark files as resolved]:*:unresolved files:_hg_unresolved' \
  '(--unmark -u --list -l --mark -m)'{-u,--unmark}'[mark files as unresolved]:*:resolved files:_hg_resolved' \
  '*:file:_hg_unresolved' && ret=0

  if [[ $state == 'resolve_files' ]]
  then
    _alternative 'files:resolved files:_hg_resolved' \
      'files:unresolved files:_hg_unresolved' && ret=0
  fi

  return ret
}

_hg_cmd_revert() {
  local context state state_descr line ret=1
  typeset -A opt_args

  _arguments -s -S : $_hg_global_opts $_hg_pat_opts $_hg_dryrun_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 -C)'{-C,--no-backup}'[do not save backup copies of files]' \
  '(--date -d)'{-d+,--date=}'[tipmost revision matching date]:date' \
  '*:file:->diff_files' && ret=0

  if [[ $state == 'diff_files' ]]
  then
    if [[ -n ${opt_args[(I)-r|--rev]} ]]
    then
      _hg_files && ret=0
    else
      typeset -a status_files
      _hg_status mard
      _wanted files expl 'modified, added, removed or deleted file' _multi_parts / status_files && ret=0
    fi
  fi

  return ret
}

_hg_cmd_serve() {
  _arguments -s -S : $_hg_global_opts $_hg_subrepos_opts \
  '(--accesslog -A)'{-A+,--accesslog=}'[name of access log file to write to]:log file:_files' \
  '(--errorlog -E)'{-E+,--errorlog=}'[name of error log file to write to]:log file:_files' \
  '(--daemon -d)'{-d,--daemon}'[run server in background]' \
  '(--port -p)'{-p+,--port=}'[port to listen on (default: 8000)]:listen port' \
  '(--address -a)'{-a+,--address=}'[address to listen on (default: all interfaces)]:interface address' \
  '--prefix[prefix path to serve from (default: server root)]:prefix' \
  '(--name -n)'{-n+,--name=}'[name to show in web pages (default: working directory)]:repository name' \
  '--web-conf=[name of the hgweb config file]:config file:_files' \
  '--pid-file=[name of file to write process ID to]:pid file:_files' \
  '--cmdserver[for remote clients]' \
  '(--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]' \
  '--certificate=[SSL certificate file]:certificate file:_files'
}

_hg_cmd_showconfig() {
  _arguments -s -S : $_hg_global_opts \
  '(--untrusted -u)'{-u,--untrusted}'[show untrusted configuration options]' \
  '(--edit -e)'{-e,--edit}'[edit user config]' \
  '(--local -l --global -g)'{-l,--local}'[edit repository config]' \
  '(--local -l --global -g)'{-g,--global}'[edit global config]' \
  ':config item:_hg_config'
}

_hg_cmd_status() {
  _arguments -s -S : $_hg_global_opts $_hg_pat_opts $_hg_subrepos_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 (not tracked) 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' \
  '--change=[list the changed files of a revision]:revision:_hg_tags' \
  '*:files:_files'
}

_hg_cmd_summary() {
  _arguments -s -S : $_hg_global_opts \
  '--remote[check for push and pull]'
}

_hg_cmd_tag() {
  _arguments -s -S : $_hg_global_opts \
  '(--local -l)'{-l,--local}'[make the tag local]' \
  '(--message -m)'{-m+,--message=}'[message for tag commit log entry]:message' \
  '(--date -d)'{-d+,--date=}'[record the specified date as commit date]:date' \
  '(--user -u)'{-u+,--user=}'[record the specified user as committer]:user' \
  '(--rev -r)'{-r+,--rev=}'[revision to tag]:revision:_hg_tags' \
  '(--force -f)'{-f,--force}'[force tag]' \
  '--remove[remove a tag]' \
  ':tag name:'
}

_hg_cmd_tip() {
  _arguments -s -S : $_hg_global_opts $_hg_template_opts \
  '(--patch -p)'{-p,--patch}'[show patch]'
}

_hg_cmd_unbundle() {
  _arguments -s -S : $_hg_global_opts \
  '(--update -u)'{-u,--update}'[update to new tip if changesets were unbundled]' \
  '*:files:_files'
}

_hg_cmd_update() {
  _arguments -s -S : $_hg_global_opts $_hg_mergetool_opts \
  '(--clean -C)'{-C,--clean}'[discard uncommitted changes (no backup)]' \
  '(--check -c)'{-c,--check}'[require clean working directory]' \
  '(--date -d)'{-d+,--date=}'[tipmost revision matching date]:date' \
  '(--rev -r)'{-r+,--rev=}'[revision]:revision:_hg_tags' \
  ':revision:_hg_tags'
}

# HGK
_hg_cmd_view() {
  _arguments -s -S : $_hg_global_opts \
  '(--limit -l)'{-l+,--limit=}'[limit number of changes displayed]:limit' \
  ':revision range:_hg_tags'
}

# MQ
_hg_qseries() {
  typeset -a patches
  patches=(${(f)"$(_hg_cmd qseries 2> /dev/null)"})
  (( $#patches )) && _describe -t hg-patches 'patches' patches
}

_hg_qapplied() {
  typeset -a patches
  patches=(${(f)"$(_hg_cmd qapplied 2> /dev/null)"})
  if (( $#patches ))
  then
    patches+=(qbase qtip)
    _describe -t hg-applied-patches 'applied patches' patches
  fi
}

_hg_qunapplied() {
  typeset -a patches
  patches=(${(f)"$(_hg_cmd qunapplied 2> /dev/null)"})
  (( $#patches )) && _describe -t hg-unapplied-patches 'unapplied patches' patches
}

# unapplied, including guarded patches
_hg_qdeletable() {
  typeset -a unapplied
  unapplied=(${(f)"$(_hg_cmd qseries 2> /dev/null)"})
  for p in $(_hg_cmd qapplied)
  do
    unapplied=(${unapplied:#$p})
  done

  (( $#unapplied )) && _describe -t hg-allunapplied-patches 'all unapplied patches' unapplied
}

_hg_qguards() {
  typeset -a guards
  local guard
  compset -P "+|-"
  _hg_cmd qselect -s 2> /dev/null | while read guard
  do
    guards+=(${guard#(+|-)})
  done
  (( $#guards )) && _describe -t hg-guards 'guards' guards
}

_hg_qseries_opts=(
  '(--summary -s)'{-s,--summary}'[print first line of patch header]')

_hg_cmd_qapplied() {
  _arguments -s -S : $_hg_global_opts $_hg_qseries_opts
}

_hg_cmd_qdelete() {
  _arguments -s -S : $_hg_global_opts \
  '(--keep -k)'{-k,--keep}'[keep patch file]' \
  '*'{-r+,--rev=}'[stop managing a revision]:applied patch:_hg_revrange' \
  '*:unapplied patch:_hg_qdeletable'
}

_hg_cmd_qdiff() {
  _arguments -s -S : $_hg_global_opts $_hg_pat_opts \
  '*:pattern:_hg_files'
}

_hg_cmd_qfold() {
  _arguments -s -S : $_hg_global_opts $_h_commit_opts \
  '(--keep -k)'{-k,--keep}'[keep folded patch files]' \
  '*:unapplied patch:_hg_qunapplied'
}

_hg_cmd_qgoto() {
  _arguments -s -S : $_hg_global_opts \
  '(--force -f)'{-f,--force}'[overwrite any local changes]' \
  ':patch:_hg_qseries'
}

_hg_cmd_qguard() {
  _arguments -s -S : $_hg_global_opts \
  '(--list -l)'{-l,--list}'[list all patches and guards]' \
  '(--none -n)'{-n,--none}'[drop all guards]' \
  ':patch:_hg_qseries' \
  '*:guards:_hg_qguards'
}

_hg_cmd_qheader() {
  _arguments -s -S : $_hg_global_opts \
  ':patch:_hg_qseries'
}

_hg_cmd_qimport() {
  _arguments -s -S : $_hg_global_opts \
  '(--existing -e)'{-e,--existing}'[import file in patch dir]' \
  '(--name -n 2)'{-n+,--name=}'[patch file name]:name:' \
  '(--force -f)'{-f,--force}'[overwrite existing files]' \
  '*'{-r+,--rev=}'[place existing revisions under mq control]:revision:_hg_revrange' \
  '*:patch:_files'
}

_hg_cmd_qnew() {
  _arguments -s -S : $_hg_global_opts $_hg_commit_opts \
  '(--force -f)'{-f,--force}'[import uncommitted changes into patch]' \
  ':patch:'
}

_hg_cmd_qnext() {
  _arguments -s -S : $_hg_global_opts $_hg_qseries_opts
}

_hg_cmd_qpop() {
  _arguments -s -S : $_hg_global_opts \
  '(--all -a :)'{-a,--all}'[pop all patches]' \
  '(--name -n)'{-n+,--name=}'[queue name to pop]:' \
  '(--force -f)'{-f,--force}'[forget any local changes]' \
  ':patch:_hg_qapplied'
}

_hg_cmd_qprev() {
  _arguments -s -S : $_hg_global_opts $_hg_qseries_opts
}

_hg_cmd_qpush() {
  _arguments -s -S : $_hg_global_opts \
  '(--all -a :)'{-a,--all}'[apply all patches]' \
  '(--list -l)'{-l,--list}'[list patch name in commit text]' \
  '(--merge -m)'{-m+,--merge=}'[merge from another queue]:' \
  '(--name -n)'{-n+,--name=}'[merge queue name]:' \
  '(--force -f)'{-f,--force}'[apply if the patch has rejects]' \
  ':patch:_hg_qunapplied'
}

_hg_cmd_qrefresh() {
  _arguments -s -S : $_hg_global_opts $_hg_pat_opts $_hg_commit_opts \
  '(--git -g)'{-g,--git}'[use git extended diff format]' \
  '(--short -s)'{-s,--short}'[short refresh]' \
  '*:files:_hg_files'
}

_hg_cmd_qrename() {
  _arguments -s -S : $_hg_global_opts \
  ':patch:_hg_qseries' \
  ':destination:'
}

_hg_cmd_qselect() {
  _arguments -s -S : $_hg_global_opts \
  '(--none -n :)'{-n,--none}'[disable all guards]' \
  '(--series -s :)'{-s,--series}'[list all guards in series file]' \
  '--pop[pop to before first guarded applied patch]' \
  '--reapply[pop and reapply patches]' \
  '*:guards:_hg_qguards'
}

_hg_cmd_qseries() {
  _arguments -s -S : $_hg_global_opts $_hg_qseries_opts \
  '(--missing -m)'{-m,--missing}'[print patches not in series]'
}

_hg_cmd_qunapplied() {
  _arguments -s -S : $_hg_global_opts $_hg_qseries_opts
}

_hg_cmd_qtop() {
  _arguments -s -S : $_hg_global_opts $_hg_qseries_opts
}

_hg_cmd_strip() {
  _arguments -s -S : $_hg_global_opts \
  '*'{-r+,--rev=}'[revision]:revision:_hg_tags' \
  '(--force -f)'{-f,--force}'[force removal of changesets, discard uncommitted changes (no backup)]' \
  '--no-backup[no backups]' \
  '(--keep -k)'{-k,--keep}'[do not modify working directory during strip]' \
  '*'{-B+,--bookmark=}'[remove revs only reachable from given bookmark]:bookmark:_hg_bookmarks_internal' \
  '*:revision:_hg_tags'
}

_hg "$@"