-
Notifications
You must be signed in to change notification settings - Fork 1
/
vc-hgcmd.el
1559 lines (1382 loc) · 64.7 KB
/
vc-hgcmd.el
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
;;; vc-hgcmd.el --- VC mercurial backend that uses hg command server -*- lexical-binding: t -*-
;; Copyright (C) 2018-2021 Free Software Foundation, Inc.
;; Author: Andrii Kolomoiets <andreyk.mad@gmail.com>
;; Keywords: vc
;; URL: https://github.com/muffinmad/emacs-vc-hgcmd
;; Package-Version: 1.14.1
;; Package-Requires: ((emacs "25.1"))
;; This file is NOT part of GNU Emacs.
;; This program 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 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Functions implementation status
;;
;; FUNCTION NAME STATUS
;; BACKEND PROPERTIES
;; * revision-granularity OK
;; - update-on-retrieve-tag OK
;; STATE-QUERYING FUNCTIONS
;; * registered (file) OK
;; * state (file) OK
;; - dir-status-files (dir files update-function) OK
;; - dir-extra-headers (dir) OK
;; - dir-printer (fileinfo) OK
;; - status-fileinfo-extra (file) OK
;; * working-revision (file) OK
;; * checkout-model (files) OK
;; - mode-line-string (file) OK
;; STATE-CHANGING FUNCTIONS
;; * create-repo (backend) OK
;; * register (files &optional comment) OK
;; - responsible-p (file) OK
;; - receive-file (file rev) NO no specific actions
;; - unregister (file) OK
;; * checkin (files comment &optional rev) OK
;; * find-revision (file rev buffer) OK
;; * checkout (file &optional rev) OK
;; * revert (file &optional contents-done) OK
;; - merge-file (file rev1 rev2) NO not needed
;; - merge-branch () OK
;; - merge-news (file) NO not needed
;; - pull (prompt) OK
;; - steal-lock (file &optional revision) NO not needed
;; - modify-change-comment (files rev comment) NO hg can modify only last comment
;; - mark-resolved (files) OK
;; - find-admin-dir (file) NO is this .hg dir?
;; HISTORY FUNCTIONS
;; * print-log (files buffer &optional shortlog start-revision limit) OK
;; * log-outgoing (backend remote-location) OK
;; * log-incoming (backend remote-location) OK
;; - log-search (buffer pattern) OK
;; - log-view-mode () OK
;; - show-log-entry (revision) OK
;; - comment-history (file) NO
;; - update-changelog (files) NO
;; * diff (files &optional rev1 rev2 buffer async) OK
;; - revision-completion-table (files) OK branches and tags instead of revisions
;; - annotate-command (file buf &optional rev) OK
;; - annotate-time () OK
;; - annotate-current-time () NO
;; - annotate-extract-revision-at-line () OK
;; - region-history (FILE BUFFER LFROM LTO) OK experimental option "line-range" added in mercurial 4.4
;; - region-history-mode () NO
;; - mergebase (rev1 &optional rev2) TODO
;; TAG SYSTEM
;; - create-tag (dir name branchp) OK
;; - retrieve-tag (dir name update) OK
;; MISCELLANEOUS
;; - make-version-backups-p (file) NO
;; - root (file) OK
;; - ignore (file &optional directory) OK find-ignore-file
;; - ignore-completion-table OK find-ignore-file
;; - previous-revision (file rev) OK
;; - next-revision (file rev) OK
;; - log-edit-mode () OK
;; - check-headers () NO
;; - delete-file (file) OK
;; - rename-file (old new) OK
;; - find-file-hook () OK
;; - extra-menu () OK shelve, addremove
;; - extra-dir-menu () OK extra-status-menu same as extra-menu
;; - conflicted-files (dir) OK with no respect to DIR
;; - repository-url (file-or-dir) OK
;;
;; VC backend to work with hg repositories through hg command server.
;; https://www.mercurial-scm.org/wiki/CommandServer
;;
;; The main advantage compared to vc-hg is speed.
;; Because communicating with hg over pipe is much faster than starting hg for each command.
;;
;; Also there are some other improvements and differences:
;;
;; - graph log is used for branch or root log
;;
;; - Conflict status for a file
;; Files with unresolved merge conflicts have appropriate status in `vc-dir'.
;; Also you can use `vc-find-conflicted-file' to find next file with unresolved merge conflict.
;; Files with resolved merge conflicts have extra file info in `vc-dir'.
;;
;; - hg summary as `vc-dir' extra headers
;; hg summary command gives useful information about commit, update and phase states.
;;
;; - Current branch is displayed on mode line.
;; It's not customizable yet.
;;
;; - Amend and close branch commits
;; While editing commit message you can toggle --amend and --close-branch flags.
;;
;; - Merge branch
;; vc-hgcmd will ask for branch name to merge.
;;
;; - Default pull arguments
;; You can customize default hg pull command arguments.
;; By default it's --update. You can change it for particular pull by invoking `vc-pull' with prefix argument.
;;
;; - Branches and tags as revision completion table
;; Instead of list of all revisions of file vc-hgcmd provides list of named branches and tags.
;; It's very useful on `vc-retrieve-tag'.
;; You can specify -C to run hg update with -C flag and discard all uncommitted changes.
;;
;; - Filenames in vc-annotate buffer is omitted
;; They are mostly useless in annotate buffer.
;; To find out right filename to annotate vc-hgcmd uses `status --rev <rev> -C file'.
;;
;; - `previous-revision' and `next-revision' respect files
;; Keys `p' and `n' in annotation buffer works correctly.
;;
;; - Create tag
;; vc-hgcmd creates tag on `vc-create-tag'
;; If `vc-create-tag' is invoked with prefix argument then named branch will be created.
;;
;; - Predefined commit message
;; While committing merge changes commit message will be set to 'merged <branch>' if
;; different branch was merged or to 'merged <node>'.
;;
;; Additionally predefined commit message passed to custom function
;; `vc-hgcmd-log-edit-message-function' so one can change it.
;; For example, to include current task in commit message:
;;
;; (defun my/hg-commit-message (original-message)
;; (if org-clock-current-task
;; (concat org-clock-current-task " " original-message)
;; original-message))
;;
;; (custom-set-variables
;; '(vc-hgcmd-log-edit-message-function 'my/hg-commit-message))
;;
;; - Interactive command `vc-hgcmd-runcommand' that allow to run custom hg commands
;;
;; - It is possible to answer to hg questions, e.g. pick action during merge
;;
;; - Option to display shelves in `vc-dir'
;;
;; - View changes made by revision; diff to parents
;; Additional bindings in `log-view-mode':
;; - `c c' view change made by revision at point (-c option to hg diff command)
;; - `c 1' view diff between revision at point and its first parent
;; - `c 2' view diff between revision at point and its second parent
;; `C c', `C 1' and `C 2' shows corresponding diffs for whole changeset.
;;
;; - View log for revset
;; Command `vc-hgcmd-print-log-revset' allows to print log for
;; revset, e.g. "branch(branch1) or branch(branch2)"
;;; Code:
(require 'bindat)
(require 'cl-lib)
(require 'diff-mode)
(require 'seq)
(require 'subr-x)
(require 'vc)
(require 'vc-dir)
;;;; Customization
(defgroup vc-hgcmd nil
"Settings for VC mercurial commandserver backend."
:group 'vc
:prefix "vc-hgcmd-")
(defcustom vc-hgcmd-hg-executable "hg"
"Hg executable."
:type '(string))
(defcustom vc-hgcmd-cmdserver-config-options '("ui.interactive=True"
"ui.editor=emacsclient -a emacs"
"extensions.shelve=")
"Config options for command server.
Specify options in form <option>=<value>. It will be passed to hg with --config argument."
:type '(repeat string))
(defcustom vc-hgcmd-cmdserver-process-environment nil
"Environment variables for hg command server process."
:type '(repeat string))
(defcustom vc-hgcmd-pull-args "--update"
"Arguments for pull command.
This arguments will be used for each pull command.
You can edit this arguments for specific pull command by invoke `vc-pull' with prefix argument."
:type '(string))
(defcustom vc-hgcmd-push-alternate-args "--new-branch"
"Initial value for hg push arguments when asked."
:type '(string))
(defcustom vc-hgcmd-log-edit-message-function nil
"Function to return string that will be used as initial value for log edit.
First param will be commit message computed by backend: 'merged <branch>' if
named branch was merged to current branch or 'merged <node>' if two heads on
same branch was merged."
:type '(choice
(function)
(const :tag "Default commit message" nil)))
(defcustom vc-hgcmd-dir-show-shelve t
"Show current shelves in `vc-dir' buffer."
:type '(boolean))
(defcustom vc-hgcmd-short-log-graph t
"Use graph in short log."
:type '(boolean))
(defcustom vc-hgcmd-short-log-format
'("{rev}:{branch}: {author|person} {date|shortdate} {desc|firstline}\\n"
"^\\(?:[-o@_x*+|: /\\]*\\)\\(%s\\):\\([^:]+\\): \\(.*?\\) \
\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\)"
((1 'log-view-message)
(2 'change-log-file)
(3 'change-log-name)
(4 'change-log-date)))
"Log template when viewing short log.
This should be a list (TEMPLATE REGEXP KEYWORDS), where TEMPLATE
is the \"--template\" argument string to pass to \"hg log\",
REGEXP is a regular expression matching the resulting
output, and KEYWORDS is a list of `font-lock-keywords' for
highlighting the Log View buffer.
REGEXP will be used to find revision with specific
number in log view, so write \"%s\" instead of \"[0-9]+\"
as revision number.
Note that revision number must be group 1."
:type '(list string regexp (repeat sexp)))
(defcustom vc-hgcmd-checkin-switches t
"A string or list of strings specifying extra switches for checkin.
These are passed to the \"hg commit\" command."
:type '(choice (const :tag "Unspecified" nil)
(const :tag "None" t)
(string :tag "Argument String")
(repeat :tag "Argument List" :value ("") string)))
(defcustom vc-hgcmd-checkout-switches t
"A string or list of strings specifying extra switches for checkout.
These are passed to the \"hg cat\" command."
:type '(choice (const :tag "Unspecified" nil)
(const :tag "None" t)
(string :tag "Argument String")
(repeat :tag "Argument List" :value ("") string)))
(defcustom vc-hgcmd-register-switches t
"A string or list of strings; extra switches for registering a file.
These are passed to the \"hg add\" command."
:type '(choice (const :tag "Unspecified" nil)
(const :tag "None" t)
(string :tag "Argument String")
(repeat :tag "Argument List" :value ("") string)))
(defcustom vc-hgcmd-diff-switches t
"A string or list of strings specifying switches for diff.
These are passed to the \"hg diff\" command."
:type '(choice (const :tag "Unspecified" nil)
(const :tag "None" t)
(string :tag "Argument String")
(repeat :tag "Argument List" :value ("") string)))
(defcustom vc-hgcmd-annotate-switches t
"A string or list of strings specifying switches for annotate.
These are passed to the \"hg annotate\" command."
:type '(choice (const :tag "Unspecified" nil)
(const :tag "None" t)
(string :tag "Argument String")
(repeat :tag "Argument List" :value ("") string)))
;;;; Modes
(define-derived-mode vc-hgcmd-process-mode nil "Hgcmd process"
"Major mode for hg cmdserver process"
(hack-dir-local-variables-non-file-buffer)
(set-buffer-multibyte nil)
(setq
buffer-undo-list t
list-buffers-directory (abbreviate-file-name default-directory)
buffer-read-only t))
(defvar vc-hgcmd-output-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map special-mode-map)
(define-key map "g" nil)
map))
(define-derived-mode vc-hgcmd-output-mode special-mode "Hgcmd output"
"Major mode for hg output"
(hack-dir-local-variables-non-file-buffer)
(set (make-local-variable 'window-point-insertion-type) t)
(setq
buffer-undo-list t
list-buffers-directory (abbreviate-file-name default-directory)))
;;;; cmdserver communication
(defvar vc-hgcmd--process-buffers-by-dir (make-hash-table :test #'equal))
(cl-defstruct (vc-hgcmd--command (:copier nil)) command output-buffer skip-error result-code wait callback callback-args)
(defvar-local vc-hgcmd--current-command nil
"Current running hgcmd command.")
(put 'vc-hgcmd--current-command 'permanent-local t)
(defvar-local vc-hgcmd--encoding 'utf-8
"Encoding that used for cmdserver communication.")
(put 'vc-hgcmd--encoding 'permanent-local t)
(defun vc-hgcmd--project-name (dir)
"Get project name based on DIR."
(file-name-nondirectory (directory-file-name dir)))
(defun vc-hgcmd--read-output ()
"Parse process output in current buffer."
;; When some hg extension like hghooks 0.7.0 use 'print' then we will recieve output without channel.
;; So let's find '<channel><length>' pattern first and all output before it send to 'o' channel.
;; Suppose that length value will be less 2 ** 24 so there are always be \0 after channel.
(goto-char 1)
(when (search-forward-regexp "[oedrLI]\0\\(.\\|\n\\)\\{3\\}" nil t)
(if (> (point) 6)
(let ((data (decode-coding-string (buffer-substring-no-properties 1 (- (point) 5)) vc-hgcmd--encoding))
(inhibit-read-only t))
(delete-region 1 (- (point) 5))
(cons ?o data))
(let* ((data (bindat-unpack '((c byte) (d u32)) (buffer-substring-no-properties 1 6)))
(channel (bindat-get-field data 'c))
(size (bindat-get-field data 'd)))
(cond ((memq channel '(?o ?e ?d ?r))
(when (> (point-max) (+ 5 size))
(let ((data (buffer-substring-no-properties 6 (+ 6 size)))
(inhibit-read-only t))
(delete-region 1 (+ 6 size))
(cons channel (if (eq channel ?r)
(bindat-get-field (bindat-unpack `((f u32)) data) 'f)
(decode-coding-string data vc-hgcmd--encoding))))))
((memq channel '(?I ?L))
(let ((inhibit-read-only t))
(delete-region 1 6))
(cons channel size)))))))
(defun vc-hgcmd--data-for-tty (data)
"Prepare binary DATA to be sent to tty process."
(mapconcat #'identity (mapcar (lambda (c) (concat "\x16" (char-to-string c))) data) ""))
(defun vc-hgcmd--cmdserver-process-filter (process output)
"Filter OUTPUT for hg cmdserver PROCESS.
Insert output to process buffer and check if amount of data is enought to parse it to output buffer."
(let ((buffer (process-buffer process)))
(when (buffer-live-p buffer)
(with-current-buffer buffer
(goto-char (point-max))
(let ((inhibit-read-only t)) (insert output))
(let ((current-command (or vc-hgcmd--current-command
(error "Hgcmd process output without command: %s" output))))
(while
(let ((data (vc-hgcmd--read-output)))
(when data
(let ((channel (car data))
(data (cdr data)))
(cond ((memq channel '(?o ?e ?d))
(unless (and (eq channel ?e) (vc-hgcmd--command-skip-error current-command))
(let ((output-buffer (vc-hgcmd--command-output-buffer current-command)))
(when (or (stringp output-buffer) (buffer-live-p output-buffer))
(with-current-buffer output-buffer
(let ((inhibit-read-only t))
(goto-char (point-max))
(insert data)))))))
((eq channel ?r)
(setf (vc-hgcmd--command-result-code current-command) data)
(setq vc-hgcmd--current-command nil)
(let ((output-buffer (vc-hgcmd--command-output-buffer current-command))
(callback (vc-hgcmd--command-callback current-command))
(args (vc-hgcmd--command-callback-args current-command)))
(when (or (stringp output-buffer) (buffer-live-p output-buffer))
(with-current-buffer output-buffer
(setq mode-line-process nil)
(when callback
(if args (funcall callback args) (funcall callback)))))))
((eq channel ?L)
(let ((output-buffer (vc-hgcmd--command-output-buffer current-command)))
(when (or (stringp output-buffer) (buffer-live-p output-buffer))
(display-buffer output-buffer)
(let ((tty (process-tty-name process))
(answer (let ((inhibit-quit t))
(prog1
(with-local-quit
(read-string "Hgcmd interactive input: "))
(setq quit-flag nil)))))
(when answer
(with-current-buffer output-buffer
(let ((inhibit-read-only t))
(goto-char (point-max))
(insert answer "\n"))))
(when (process-live-p process)
(process-send-string
process
(let* ((to-send (when answer (concat (vc-hgcmd--encode-command-arg answer) "\n")))
(binary-data (concat (bindat-pack '((l u32)) `((l . ,(length to-send)))) to-send)))
(if tty
(vc-hgcmd--data-for-tty binary-data)
binary-data)))
(when tty
(process-send-eof process)))))))
;; What is I channel for?
(t (error (format "Hgcmd unhandled channel %c" channel)))))
t))))))))
(defun vc-hgcmd--cmdserver-process-sentinel (process _event)
"Will listen for PROCESS events and kill process buffer if process killed."
(unless (process-live-p process)
(let ((buffer (process-buffer process)))
(when (buffer-live-p buffer)
(with-current-buffer buffer
(when vc-hgcmd--current-command
;; process has died but command waits for output
(vc-hgcmd--cmdserver-process-filter process (bindat-pack
'((c byte) (l u32) (v u32))
'((c . ?r) (l . 4) (v . 255))))))
(kill-buffer buffer)))))
(defun vc-hgcmd--repo-dir ()
"Get repo dir."
(abbreviate-file-name (or (vc-hgcmd-root default-directory) default-directory)))
(defun vc-hgcmd--process-buffer ()
"Get hg cmdserver process buffer for repo in `default-directory'."
(let ((dir (vc-hgcmd--repo-dir)))
(or
(let ((buffer (gethash dir vc-hgcmd--process-buffers-by-dir)))
(when (buffer-live-p buffer) buffer))
(puthash
dir
(with-current-buffer (generate-new-buffer (concat " *hgcmd process: " (vc-hgcmd--project-name dir) "*"))
(setq default-directory dir)
(vc-hgcmd-process-mode)
(let* ((process-environment (append '("LANGUAGE=C") vc-hgcmd-cmdserver-process-environment process-environment))
(process-connection-type nil)
(process
(condition-case nil
(apply
#'start-file-process
(concat "vc-hgcmd process: " (vc-hgcmd--project-name default-directory))
(current-buffer)
vc-hgcmd-hg-executable
(nconc (mapcan (lambda (option) (list "--config" option)) vc-hgcmd-cmdserver-config-options) (list "serve" "--cmdserver" "pipe")))
(error nil))))
;; process will be nil if hg executable not found
(when (process-live-p process)
(set-process-sentinel process #'ignore)
(set-process-query-on-exit-flag process nil)
(set-process-coding-system process 'no-conversion 'no-conversion)
;; read hello message
;; check process again because it can be tramp sh process with output like "env: hg not found"
(let ((output (vc-hgcmd--read-output)))
(while (and (process-live-p process) (or (not output) (not (string-prefix-p "capabilities: " (cdr output)))))
(accept-process-output process 0.1 nil t)
(setq output (vc-hgcmd--read-output)))
(when (process-live-p process)
(let* ((output (cdr output))
(encoding (when (string-match "\\bencoding: \\(.+\\)" output)
(intern (downcase (match-string 1 output))))))
(when encoding
(setq vc-hgcmd--encoding (if (eq encoding 'ascii) 'us-ascii encoding))))
(set-process-filter process #'vc-hgcmd--cmdserver-process-filter)
(set-process-sentinel process #'vc-hgcmd--cmdserver-process-sentinel)))))
(current-buffer))
vc-hgcmd--process-buffers-by-dir))))
(defun vc-hgcmd--output-buffer (command)
"Get and display hg output buffer for COMMAND."
(let* ((dir (vc-hgcmd--repo-dir))
(buffer
(or (seq-find (lambda (buffer)
(with-current-buffer buffer
(and (eq major-mode 'vc-hgcmd-output-mode)
(equal (abbreviate-file-name default-directory) dir))))
(buffer-list))
(let ((buffer (generate-new-buffer (concat "*hgcmd output: " (vc-hgcmd--project-name dir) "*"))))
(with-current-buffer buffer
(setq default-directory dir)
(vc-hgcmd-output-mode))
buffer))))
(let ((window (display-buffer buffer)))
(with-current-buffer buffer
(let ((inhibit-read-only t))
(goto-char (point-max))
(unless (eq (point) (point-min)) (insert "\f\n"))
(set-window-start window (point))
(insert (concat "Running \"" (mapconcat #'identity command " ") "\"...\n")))))
buffer))
(defun vc-hgcmd--encode-command-arg (arg)
"Encode command ARG."
(encode-coding-string arg vc-hgcmd--encoding))
(defun vc-hgcmd--run-command (cmd)
"Run hg CMD."
(let* ((buffer (vc-hgcmd--process-buffer))
(process (get-buffer-process buffer)))
(with-current-buffer buffer
(when vc-hgcmd--current-command
(user-error "Hg command \"%s\" is active" (car (vc-hgcmd--command-command vc-hgcmd--current-command))))
(when (process-live-p process)
(let* ((tty (process-tty-name process))
(command (vc-hgcmd--command-command cmd))
(output-buffer (or (vc-hgcmd--command-output-buffer cmd)
(setf (vc-hgcmd--command-output-buffer cmd) (vc-hgcmd--output-buffer command)))))
(setq vc-hgcmd--current-command cmd)
(when (or (stringp output-buffer) (buffer-live-p output-buffer))
(with-current-buffer output-buffer
(setq mode-line-process
(propertize (format " [running %s...]" (car command))
'face 'mode-line-emphasis
'help-echo
"A command is in progress in this buffer"))))
(process-send-string
process
(concat
"runcommand\n"
(let* ((args (mapconcat #'vc-hgcmd--encode-command-arg
(append '("--config" "ui.report_untrusted=0")
command)
"\0"))
(binary-data (bindat-pack '((l u32)) `((l . ,(length args))))))
(concat (if tty
(vc-hgcmd--data-for-tty binary-data)
binary-data)
args))))
(when tty
(process-send-eof process)))
(when (vc-hgcmd--command-wait cmd)
(while vc-hgcmd--current-command
(accept-process-output process 0.1 nil t)))
t))))
(defun vc-hgcmd--command (skip-error &rest command)
"Run hg COMMAND and return it's output. If SKIP-ERROR is non nil data on error channel will be omited."
(with-temp-buffer
(let ((cmd (make-vc-hgcmd--command :command command :output-buffer (current-buffer) :wait t :skip-error skip-error)))
(when (vc-hgcmd--run-command cmd)
(let ((result (string-trim-right (buffer-string))))
;; TODO min result code for each command that is not error
(if (= (vc-hgcmd--command-result-code cmd) 255)
(with-current-buffer (vc-hgcmd--output-buffer command)
(goto-char (point-max))
(let ((inhibit-read-only t))
(insert (concat result "\n")))
nil)
(when (> (length result) 0)
result)))))))
(defun vc-hgcmd-command (&rest command)
"Run hg COMMAND and return it's output."
(apply #'vc-hgcmd--command (nconc (list nil) command)))
(defun vc-hgcmd-command-to-buffer (buffer &rest command)
"Send output of COMMAND to BUFFER and wait COMMAND to finish."
(vc-setup-buffer buffer)
(vc-hgcmd--run-command (make-vc-hgcmd--command :command command :output-buffer buffer :wait t)))
(defun vc-hgcmd--update-callback (buffer)
"Update BUFFER where was command called from after command finished."
(when (buffer-live-p buffer)
(with-current-buffer buffer
(cond
((derived-mode-p 'vc-dir-mode)
(vc-dir-refresh))
((derived-mode-p 'dired-mode)
(revert-buffer))))))
(defun vc-hgcmd-command-update-callback (command)
"Run COMMAND and update current buffer after command finished."
(vc-hgcmd--run-command
(make-vc-hgcmd--command
:command command
:callback #'vc-hgcmd--update-callback
:callback-args (current-buffer))))
(defconst vc-hgcmd--translation-status '((?C . up-to-date)
(?= . up-to-date)
(?A . added)
(?I . ignored)
(?R . removed)
(?! . missing)
(?? . unregistered)
(?M . edited)
(?\s . origin))
"Translation for status command output.")
(defconst vc-hgcmd--translation-resolve '((?U . conflict)
(?R . edited))
"Translation for resolve command output.")
(defun vc-hgcmd--branches ()
"Return branches list."
(split-string (vc-hgcmd-command "branches" "-q") "\n"))
(defun vc-hgcmd--tags ()
"Return tags list."
(split-string (vc-hgcmd-command "tags" "-q") "\n"))
(defun vc-hgcmd--parents (template &optional revision)
"Return parents of REVISION formatted by TEMPLATE string."
(let* ((revision (or revision ""))
(parents (vc-hgcmd-command
"log"
"-r"
(format "p1(%s)+p2(%s)" revision revision)
"--template"
(concat template "\\n"))))
(when parents (split-string parents "\n"))))
(defun vc-hgcmd--file-relative-name (file)
"Return FILE file name relative to vc root."
(file-relative-name file (vc-hgcmd-root file)))
;;;; VC backend
(autoload 'vc-switches "vc")
(defun vc-hgcmd--switches (operation)
"Return switch list for OPERATION."
(let ((switches (vc-switches 'hgcmd operation)))
(when switches
(mapcan #'split-string-and-unquote switches))))
(defun vc-hgcmd-revision-granularity ()
"Per-repository revision number."
'repository)
(defun vc-hgcmd-update-on-retrieve-tag ()
"No buffers update on retrieve tag."
nil)
;;;###autoload (defun vc-hgcmd-registered (file)
;;;###autoload (when (vc-find-root file ".hg")
;;;###autoload (load "vc-hgcmd" nil t)
;;;###autoload (vc-hgcmd-registered file)))
(defun vc-hgcmd-registered (file)
"Is file FILE is registered."
(when (vc-hgcmd-root file)
(or (file-directory-p file)
;; vc-registered is called for buffer-file-name and
;; shortly then after for truename. Update default-dir so
;; 'hg state' will be called in right repo
(let ((state
(let ((default-directory (file-name-directory (expand-file-name file))))
(vc-hgcmd-state file))))
(and state (not (memq state '(ignored unregistered))))))))
(defun vc-hgcmd-state (file)
"State for FILE."
(let ((out (vc-hgcmd--command t "status" "-A" (vc-hgcmd--file-relative-name file))))
(when out
(let ((state (cdr (assoc (aref out 0) vc-hgcmd--translation-status))))
(if (and (eq state 'edited) (vc-hgcmd--file-unresolved-p file))
'conflict
state)))))
(cl-defstruct (vc-hgcmd-extra-fileinfo
(:copier nil)
(:constructor vc-hgcmd-create-extra-fileinfo (status &optional origin))
(:conc-name vc-hgcmd-extra-fileinfo->))
status ;; copied, renamed or resolved
origin ;; origin file in case copied or renamed
)
(defun vc-hgcmd-dir-printer (info)
"Pretty print INFO."
(vc-default-dir-printer 'Hgcmd info)
(let ((extra (vc-dir-fileinfo->extra info)))
(when extra
(insert (propertize
(format " (%s)"
(pcase (vc-hgcmd-extra-fileinfo->status extra)
('resolved "resolved conflict")
('copied (format "copied from %s" (vc-hgcmd-extra-fileinfo->origin extra)))
('renamed-from (format "renamed from %s" (vc-hgcmd-extra-fileinfo->origin extra)))
('renamed-to (format "renamed to %s" (vc-hgcmd-extra-fileinfo->origin extra)))))
'face 'font-lock-comment-face)))))
(defun vc-hgcmd--dir-status-callback (update-function)
"Call UPDATE-FUNCTION with result of status command."
(let* ((conflicted (vc-hgcmd-conflicted-files))
(result (mapcar (lambda (file)
(list (car file) (cdr file) (when (eq (cdr file) 'edited) (vc-hgcmd-create-extra-fileinfo 'resolved))))
conflicted))
(conflicted (mapcar #'car conflicted)))
(goto-char (point-min))
(while (not (eobp))
(let ((status (cdr (assoc (char-after) vc-hgcmd--translation-status))))
(when status
(let ((file (buffer-substring-no-properties (+ (point) 2) (line-end-position))))
(unless (or (member file conflicted) (eq status 'origin))
(push (list
file
status
(pcase status
('added (save-excursion
(forward-line)
(when (and (point-at-bol)
(eq 'origin (cdr (assoc (char-after) vc-hgcmd--translation-status))))
(let ((origin (buffer-substring-no-properties (+ (point) 2) (line-end-position))))
(vc-hgcmd-create-extra-fileinfo
(if (re-search-forward (concat "^R " (regexp-quote origin) "$") nil t)
'renamed-from
'copied)
origin)))))
('removed (save-excursion
(when (re-search-backward (concat "^ " (regexp-quote file) "$") nil t)
(forward-line -1)
(vc-hgcmd-create-extra-fileinfo
'renamed-to
(buffer-substring-no-properties (+ (point) 2) (line-end-position))))))))
result)))))
(forward-line))
(funcall update-function result)))
(defun vc-hgcmd-dir-status-files (dir files update-function)
"Call UPDATE-FUNCTION with status for files in DIR or FILES."
(let ((command (if files
(nconc (list "status" "-A") (mapcar #'vc-hgcmd--file-relative-name files))
(list "status" "-C" (vc-hgcmd--file-relative-name dir)))))
(vc-hgcmd--run-command
(make-vc-hgcmd--command
:command command
:output-buffer (current-buffer)
:callback #'vc-hgcmd--dir-status-callback
:callback-args update-function
:skip-error t))))
(defvar vc-hgcmd-extra-menu-map
(let ((map (make-sparse-keymap)))
(define-key map [hgcmd-sl]
'(menu-item "Create shelve..." vc-hgcmd-shelve
:help "Shelve changes"))
(define-key map [hgcmd-uc]
'(menu-item "Continue unshelve" vc-hgcmd-shelve-unshelve-continue
:help "Continue unshelve"))
(define-key map [hgcmd-ua]
'(menu-item "Abort unshelve" vc-hgcmd-shelve-unshelve-abort
:help "Abort unshelve"))
(define-key map [hgcmd-ar]
'(menu-item "Addremove" vc-hgcmd-addremove
:help "Add new and remove missing files"))
map))
(defun vc-hgcmd-extra-menu ()
"Return a menu keymap with additional hg commands."
vc-hgcmd-extra-menu-map)
(defun vc-hgcmd-extra-status-menu ()
"Return a menu keymap with additional hg commands."
vc-hgcmd-extra-menu-map)
(defvar vc-hgcmd-shelve-map
(let ((map (make-sparse-keymap)))
;; Turn off vc-dir marking
(define-key map [mouse-2] 'ignore)
(define-key map [down-mouse-3] #'vc-hgcmd-shelve-menu)
(define-key map "\C-k" #'vc-hgcmd-shelve-delete-at-point)
(define-key map "=" #'vc-hgcmd-shelve-show-at-point)
(define-key map "\C-m" #'vc-hgcmd-shelve-show-at-point)
(define-key map "A" 'vc-hgcmd-shelve-apply-at-point)
(define-key map "P" 'vc-hgcmd-shelve-pop-at-point)
map))
(defvar vc-hgcmd-shelve-menu-map
(let ((map (make-sparse-keymap "Hg shelve")))
(define-key map [de]
'(menu-item "Delete shelve" vc-hgcmd-shelve-delete-at-point
:help "Delete the current shelve"))
(define-key map [ap]
'(menu-item "Unshelve and keep shelve" vc-hgcmd-shelve-apply-at-point
:help "Apply the current shelve and keep it in the shelve list"))
(define-key map [po]
'(menu-item "Unshelve and remove shelve" vc-hgcmd-shelve-pop-at-point
:help "Apply the current shelve and remove it"))
(define-key map [sh]
'(menu-item "Show shelve" vc-hgcmd-shelve-show-at-point
:help "Show the contents of the current shelve"))
map))
(defun vc-hgcmd-dir-extra-headers (_dir)
"Return summary command for DIR output as dir extra headers."
(concat
(with-temp-buffer
(when (vc-hgcmd--run-command (make-vc-hgcmd--command :command (list "summary") :output-buffer (current-buffer) :wait t))
(mapconcat
#'identity
(let (result)
(goto-char (point-min))
(while (not (eobp))
(push
(let ((entry (if (looking-at "\\([^ ].*\\):\\s-+\\(.*\\)")
(cons (capitalize (match-string 1)) (match-string 2))
(cons "" (buffer-substring (point) (line-end-position))))))
(concat
(propertize (format "%-11s: " (car entry)) 'face 'font-lock-type-face)
(propertize (cdr entry) 'face 'font-lock-variable-name-face)))
result)
(forward-line))
(nreverse result))
"\n")))
(when vc-hgcmd-dir-show-shelve
(let ((shelves (vc-hgcmd-shelve-list)))
(when shelves
(concat
"\n"
(propertize "Shelve :\n" 'face 'font-lock-type-face)
(with-temp-buffer
(when (vc-hgcmd--run-command (make-vc-hgcmd--command :command (list "shelve" "-l") :output-buffer (current-buffer) :wait t))
(goto-char (point-min))
(mapconcat
(lambda (shelve)
(prog1
(propertize
(concat
" "
(propertize shelve 'face 'font-lock-variable-name-face)
(propertize
(replace-regexp-in-string
(concat "^" (regexp-quote shelve) "\s*")
" "
(buffer-substring-no-properties (line-beginning-position) (line-end-position)))
'face 'font-lock-comment-face))
'mouse-face 'highlight
'help-echo "mouse-3: Show shelve menu\nRET: Show shelve\nA: Unshelve and keep\nP: Unshelve and remove\nC-k: Delete shelve"
'keymap vc-hgcmd-shelve-map
'vc-hgcmd--shelve-name shelve)
(forward-line)))
shelves "\n")))))))))
(defun vc-hgcmd-shelve-list ()
"Return shelve list."
(let ((shelves (vc-hgcmd-command "shelve" "-l" "-q")))
(when shelves (split-string shelves "\n"))))
(defun vc-hgcmd-shelve-name-at-point ()
"Return shelve name at point."
(or (get-text-property (point) 'vc-hgcmd--shelve-name)
(error "Cannot find shelve at point")))
(defun vc-hgcmd-shelve-delete-at-point ()
"Delete shelve at point."
(interactive)
(let ((shelve (vc-hgcmd-shelve-name-at-point)))
(when (y-or-n-p (format "Delete shelve %s? " shelve))
(vc-hgcmd-command "shelve" "-d" shelve)
(vc-dir-refresh))))
(defun vc-hgcmd-shelve-read (prompt)
"Read shelve name with PROMPT."
(let ((name (completing-read prompt (letrec ((table (lazy-completion-table table (lambda () (vc-hgcmd-shelve-list)))))) nil t)))
(when (not (string-equal "" name))
name)))
(defun vc-hgcmd-shelve-apply (name)
"Unshelve and keep shelve with NAME."
(interactive (list (vc-hgcmd-shelve-read "Apply and keep shelve: ")))
(when name
(vc-hgcmd-command "unshelve" "-k" name)
(vc-dir-refresh)))
(defun vc-hgcmd-shelve-apply-at-point ()
"Unshelve and keep shelve at point."
(interactive)
(vc-hgcmd-shelve-apply (vc-hgcmd-shelve-name-at-point)))
(defun vc-hgcmd-shelve-pop (name)
"Unshelve and remove shelve with NAME."
(interactive (list (vc-hgcmd-shelve-read "Apply and remove shelve: ")))
(when name
(vc-hgcmd-command "unshelve" name)
(vc-dir-refresh)))
(defun vc-hgcmd-shelve-pop-at-point ()
"Unshelve and remove shelve at point."
(interactive)
(vc-hgcmd-shelve-pop (vc-hgcmd-shelve-name-at-point)))
(defun vc-hgcmd-shelve-show-at-point ()
"Show shelve at point."
(interactive)
(let ((shelve (vc-hgcmd-shelve-name-at-point))
(buffer (get-buffer-create "*vc-hgcmd-shelve*")))
(vc-setup-buffer buffer)
(with-current-buffer
(when (vc-hgcmd--run-command (make-vc-hgcmd--command :command (list "shelve" "-p" shelve) :output-buffer (current-buffer) :wait t))
(diff-mode)
(setq buffer-read-only t)
(pop-to-buffer (current-buffer))))))
(defun vc-hgcmd-shelve (name)
"Create shelve named NAME."
(interactive "sShelve name: ")
(if (string-equal "" name)
(vc-hgcmd-command "shelve")
(vc-hgcmd-command "shelve" "-n" name))
(vc-dir-refresh))
(defun vc-hgcmd-shelve-unshelve-continue ()
"Continue unshelve."
(interactive)
(vc-hgcmd-command "unshelve" "--continue")
(vc-dir-refresh))
(defun vc-hgcmd-shelve-unshelve-abort ()
"Abort unshelve."
(interactive)
(vc-hgcmd-command "unshelve" "--abort")
(vc-dir-refresh))
(defun vc-hgcmd-shelve-menu (event)
"Popup shelve menu on EVENT."
(interactive "e")
(vc-dir-at-event event (popup-menu vc-hgcmd-shelve-menu-map event)))
(defun vc-hgcmd-addremove ()
"Add new and remove missing files."
(interactive)
(vc-hgcmd-command "addremove")
(vc-dir-refresh))
(defun vc-hgcmd-working-revision (file)
"Working revision of FILE. Result is revision of FILE up to repository revision."
(let* ((reporev (car-safe (split-string (vc-hgcmd-command "id" "-n") "+")))
(filerev (when file
(vc-hgcmd-command
"log"
"-r" (format "limit(reverse(follow('%s') and ..%s), 1)"
(vc-hgcmd--file-relative-name file)
reporev)
"--template" "{rev}"))))
(or filerev reporev)))
(defun vc-hgcmd-checkout-model (_files)
"Files are always writable."
'implicit)
(defun vc-hgcmd-mode-line-string (file)
"Return a string for `vc-mode-line' to put in the mode line for FILE."
(let* ((state (vc-state file))
(state-echo nil)
(face nil)
;; TODO allow to customize it.
(branch (vc-hgcmd-command "branch")))
(propertize
(concat
"Hgcmd"
(cond
((eq state 'up-to-date)
(setq state-echo "Up to date file")
(setq face 'vc-up-to-date-state)
"-")
((eq state 'added)
(setq state-echo "Locally added file")
(setq face 'vc-locally-added-state)
"@")