-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsvn.el
2243 lines (2035 loc) · 78.6 KB
/
dsvn.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
;;; dsvn.el --- Subversion interface
;; Copyright 2006-2010 Virtutech AB
;; Copyright 2010 Intel
;; Author: David KÃ¥gedal <davidk@lysator.liu.se>
;; Mattias Engdegård <mattiase@acm.org>
;; Maintainer: Mattias Engdegård <mattiase@acm.org>
;; Created: 27 Jan 2006
;; Keywords: docs
;; 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 2 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, write to the Free Software
;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
;; USA
;;; Commentary:
;;
;; This is an interface for managing Subversion working copies. It
;; can show you an up-to-date view of the current status, and commit
;; changes. If also helps you do other tasks such as updating,
;; switching, diffing and more.
;;
;; To get you started, add this line to your startup file:
;;
;; (autoload 'svn-status "dsvn" "Run `svn status'." t)
;; (autoload 'svn-update "dsvn" "Run `svn update'." t)
;;
;; This file integrates well with vc-svn, so you might want to do this
;; as well:
;;
;; (require 'vc-svn)
;;
;; To get the status view, type
;;
;; M-x svn-status
;;
;; and select a directory where you have a checked-out Subversion
;; working copy. A buffer will be created that shows what files you
;; have modified, and any unknown files. The file list corresponds
;; closely to that produced by "svn status", only slightly
;; reformatted.
;;
;; Navigate through the file list using "n" and "p", for next and
;; previous file, respectively.
;;
;; You can get a summary of available commands by typing "?".
;;
;; Some commands operate on files, and can either operate on the file
;; under point, or on a group of files that have been marked. The
;; commands used for marking a file are the following:
;;
;; m mark and go down
;; DEL unmark and go up
;; u unmark and go down
;; SPC toggle mark
;; M-DEL unmark all
;;
;; The commands that operate on files are:
;;
;; f Visit the file under point (does not use marks)
;; o Visit the file under point in another window (does not use marks)
;; = Show diff of uncommitted changes. This does not use marks
;; unless you give a prefix argument (C-u)
;; c Commit files
;; a Add files
;; r Remove files
;; R Resolve conflicts
;; M Rename/move files
;; U Revert files
;; P View or edit properties of the file or directory under point
;; (does not use marks)
;; l Show log of file or directory under point (does not use marks)
;;
;; These commands update what is shown in the status buffer:
;;
;; g Rerun "svn status" to update the list. Use a prefix
;; argument (C-u) to clear the list first to make sure that
;; it is correct.
;; s Update status of selected files
;; S Show status of specific file or directory
;; x Expunge unchanged files from the list
;;
;; To update the working copy:
;;
;; M-u Run "svn update". If a prefix argument is given (C-u),
;; you will be prompted for a revision to update to.
;; M-s Switch working copy to another branch.
;; M-m Merge in changes using "svn merge".
;;
;; To view the Subversion log, type "M-x svn-log".
;;
;; Bugs and missing features:
;;
;; - Annotate (blame).
;; - Log, with a useful log mode where the user can easily view any revision
;; as a diff or visit a revision of a file in a buffer.
;; - Integration with ediff or similar to resolve conflicts.
(require 'vc)
(require 'log-edit)
(require 'uniquify)
(defconst svn-status-msg-col 1)
(defconst svn-status-flags-col 11)
(defconst svn-status-mark-col 18)
(defconst svn-status-file-col 20)
(defgroup dsvn nil
"Settings for dsvn."
:group 'tools)
(defcustom svn-program "svn"
"*The svn program to run"
:type 'string
:group 'dsvn)
(defcustom svn-restore-windows nil
"*Non-nil means that the window configuration is restored after commit"
:type 'boolean
:group 'dsvn)
(defcustom svn-diff-args '("-x" "-p")
"*Additional arguments used for all invocations of `svn diff'."
:type '(repeat string)
:group 'dsvn)
;; start-file-process and process-file are needed for tramp but only appeared
;; in Emacs 23 and 22 respectively.
(setq svn-start-file-process
(if (fboundp 'start-file-process) 'start-file-process 'start-process))
(setq svn-process-file
(if (fboundp 'process-file) 'process-file 'call-process))
;; Run svn with default (US-English) messages, since we are going to
;; parse them.
(setq svn-process-environment '("LC_MESSAGES=C"))
(defun svn-call-in-svn-environment (func)
;; Dynamic rebinding of process-environment
(let ((process-environment
(append svn-process-environment process-environment)))
(funcall func)))
(defun svn-start-svn-process (buffer args)
"Start an svn process associated to BUFFER, with command-line
arguments ARGS. Return the process object for it."
(svn-call-in-svn-environment
(lambda ()
(apply svn-start-file-process "svn" buffer svn-program args))))
(defun svn-call-svn (infile buffer display args)
"Call svn synchronously. Arguments are like process-file."
(svn-call-in-svn-environment
(lambda ()
(apply svn-process-file svn-program infile buffer display args))))
(defun svn-call-process (buffer args)
"Run svn and wait for it to finish.
Argument BUFFER is the buffer in which to insert output.
Optional argument ARGS are the arguments to svn."
(let ((proc (svn-start-svn-process buffer args)))
(set-process-coding-system proc 'utf-8)
(set-process-filter proc 'svn-output-filter)
(while (eq (process-status proc) 'run)
(accept-process-output proc 5)
(sit-for 0))))
(defun svn-run-with-output (subcommand &optional args mode)
"Run 'svn' with output to another window.
Argument SUBCOMMAND is the command to execute.
Optional argument ARGS is a list of the arguments to the command.
Optional argument MODE is the major mode to use for the output buffer.
Return non-NIL if there was any output."
(let ((buf (get-buffer-create "*svn output*"))
(dir default-directory)
(inhibit-read-only t))
(with-current-buffer buf
(erase-buffer)
(if mode
(funcall mode)
(fundamental-mode))
(setq default-directory dir)
(setq buffer-read-only t)
(let ((proc (svn-start-svn-process buf (cons subcommand args))))
(set-process-coding-system proc 'utf-8)
(set-process-filter proc 'svn-output-filter)
(while (eq (process-status proc) 'run)
(accept-process-output proc 5)
(sit-for 0)))
(if (= (point-min) (point-max))
nil
(save-selected-window
(select-window (display-buffer buf))
(goto-char (point-min)))
t))))
(defun svn-run-hidden (command args)
"Run 'svn' without showing output.
Argument COMMAND is the command to run.
Optional argument ARGS is a list of arguments.
Returns the buffer that holds the output from 'svn'."
(let ((buf (get-buffer-create " *svn*"))
(dir default-directory))
(with-current-buffer buf
(erase-buffer)
(setq default-directory dir))
(svn-call-svn nil buf nil (cons (symbol-name command) args))
buf))
(defun svn-run-for-stdout (command args)
"Run `svn', and return standard output as a string, discarding stderr.
Argument COMMAND is the svn subcommand to run.
Optional argument ARGS is a list of arguments."
(let ((output-buffer (generate-new-buffer "*svn-stdout*")))
(svn-call-svn nil (list output-buffer nil) nil
(cons (symbol-name command) args))
(let ((stdout (with-current-buffer output-buffer (buffer-string))))
(kill-buffer output-buffer)
stdout)))
(defun svn-output-filter (proc str)
"Output filter for svn output.
Argument PROC is the process object.
Argument STR is the output string."
(save-excursion
(set-buffer (process-buffer proc))
(goto-char (process-mark proc))
;; put point back where it was to avoid scrolling for long output
;; (e.g., large diffs)
(let ((p (point))
(inhibit-read-only t))
(insert-before-markers str)
(goto-char p))))
(defvar svn-todo-queue '()
"A queue of commands to run when the current command finishes.")
(make-variable-buffer-local 'svn-todo-queue)
(defun svn-current-url ()
"Get the repository URL."
(with-current-buffer (svn-run-hidden 'info ())
(if (re-search-backward "^URL: \\(.*\\)$" nil t)
(match-string 1)
(error "Couldn't find the current URL"))))
(defun svn-repository-root ()
"Get the repository root."
(with-current-buffer (svn-run-hidden 'info ())
(if (re-search-backward "^Repository Root: \\(.*\\)$" nil t)
(match-string 1)
(error "Couldn't find the repository root"))))
(defconst svn-noninteractive-blacklist
'(add revert resolved)
"Subversion commands that don't accept the --non-interactive option.
This is only important for svn 1.4, as 1.5 accepts this option for all
commands.")
(defun svn-run (command args &optional description)
"Run subversion command COMMAND with ARGS.
Optional third argument DESCRIPTION is a string used in the status
buffer to describe what is going on."
;; Clean up old output
(let ((inhibit-read-only t))
(delete-region svn-output-marker (point-max)))
(let* ((command-s (symbol-name command))
(filter-func (intern (concat "svn-" command-s "-filter")))
(sentinel-func (intern (concat "svn-" command-s "-sentinel")))
proc)
;; The command status-v is interpreted as status -v
(when (eq command 'status-v)
(setq command-s "status"
args (cons "-v" args)))
(unless (memq command svn-noninteractive-blacklist)
(setq args (cons "--non-interactive" args)))
(setq proc (svn-start-svn-process (current-buffer) (cons command-s args)))
(if (fboundp filter-func)
(set-process-filter proc filter-func)
(set-process-filter proc 'svn-default-filter))
(if (fboundp sentinel-func)
(set-process-sentinel proc sentinel-func)
(set-process-sentinel proc 'svn-default-sentinel))
(setq svn-running (list description proc))
(set-svn-process-status 'running)
proc))
(defun svn-check-running ()
(when svn-running
(error "Can't run two svn processes from the same buffer")))
(defun svn-run-async (command args &optional file-filter)
"Run subversion command COMMAND with ARGS, possibly at a later time.
Optional third argument FILE-FILTER is the file filter to be in effect
during the run."
(if svn-running
(setq svn-todo-queue
(nconc svn-todo-queue
(list (list command args file-filter))))
(progn
(set (make-local-variable 'svn-file-filter) file-filter)
(svn-run command args))))
;; This could be used to debug filter functions
(defvar svn-output-queue nil)
(defvar svn-in-output-filter nil)
(defun svn-filter-queue (proc str)
(setq svn-output-queue (nconc svn-output-queue (list str)))
(unless svn-in-output-filter
(let ((svn-in-output-filter t))
(while svn-output-queue
(svn-status-filter proc (car svn-output-queue))
(setq svn-output-queue (cdr svn-output-queue))))))
(defun svn-default-filter (proc str)
(save-excursion
(set-buffer (process-buffer proc))
(let ((inhibit-read-only t))
(goto-char (point-max))
(insert str))))
(defun svn-default-sentinel (proc reason)
(with-current-buffer (process-buffer proc)
(when (and svn-running
(eq proc (cadr svn-running)))
(setq svn-running nil)
(if (/= (process-exit-status proc) 0)
(set-svn-process-status 'failed)
(set-svn-process-status 'finished))
(move-to-column goal-column))
(when svn-todo-queue
(let ((cmd-info (car svn-todo-queue)))
(setq svn-todo-queue (cdr svn-todo-queue))
(let ((command (car cmd-info))
(args (cadr cmd-info))
(file-filter (car (cddr cmd-info))))
(set (make-local-variable 'svn-file-filter) file-filter)
(svn-run command args))))))
(defun svn-diff (arg)
"Run `svn diff'.
Argument ARG are the command line arguments."
(interactive "ssvn diff arguments: ")
(svn-run-with-output "diff"
(append svn-diff-args (split-string arg))
'diff-mode))
(defun svn-add-unversioned-files-p (files)
"Ask the user whether FILES should be added; return the answer."
(let ((buf (get-buffer-create "*svn-unversioned-files*")))
(with-current-buffer buf
(setq buffer-read-only nil)
(erase-buffer)
(insert (mapconcat (lambda (f) f) files "\n"))
(setq buffer-read-only t)
(goto-char (point-min))
(save-selected-window
(pop-to-buffer buf)
(shrink-window-if-larger-than-buffer)
(let* ((n (length files))
(add-them (y-or-n-p
(if (= n 1)
"Add this item first? "
(format "Add these %d items first? " n)))))
(let ((win (get-buffer-window buf)))
(if win
(condition-case nil
(delete-window win)
(error nil))))
(bury-buffer buf)
add-them)))))
(defun svn-commit ()
"Commit changes to one or more files."
(interactive)
(save-some-buffers)
(let ((unversioned-files (svn-action-files
(lambda (pos) (eq (svn-file-status pos) ?\?)))))
(if unversioned-files
(if (svn-add-unversioned-files-p unversioned-files)
(progn
(message "Adding unversioned items. Please re-commit when ready.")
(svn-run 'add unversioned-files "Adding files"))
(message "Files not added; nothing committed."))
(let ((status-buf (current-buffer))
(commit-buf (get-buffer-create "*svn commit*"))
(window-conf (and svn-restore-windows
(current-window-configuration)))
(listfun (lambda () (with-current-buffer log-edit-parent-buffer
(svn-action-files)))))
(log-edit 'svn-confirm-commit t
(if (< emacs-major-version 23)
listfun
(list (cons 'log-edit-listfun listfun)))
commit-buf)
(set (make-local-variable 'saved-window-configuration) window-conf)))))
(defun svn-confirm-commit ()
"Commit changes with the current buffer as commit message."
(interactive)
(let ((files (with-current-buffer log-edit-parent-buffer
(svn-action-files)))
(commit-buf (current-buffer))
(status-buf log-edit-parent-buffer)
(window-conf saved-window-configuration)
;; XEmacs lacks make-temp-file but has make-temp-name + temp-directory
(msg-file (if (fboundp 'make-temp-file)
(make-temp-file "svn-commit")
(make-temp-name (expand-file-name "svn-commit"
(temp-directory))))))
;; Ensure final newline
(goto-char (point-max))
(unless (bolp)
(newline))
(write-region (point-min) (point-max) msg-file)
(when (boundp 'vc-comment-ring)
;; insert message into comment ring, unless identical to the previous
(let ((comment (buffer-string)))
(when (or (ring-empty-p vc-comment-ring)
(not (equal comment (ring-ref vc-comment-ring 0))))
(ring-insert vc-comment-ring comment))))
(kill-buffer commit-buf)
(with-current-buffer status-buf
(make-local-variable 'svn-commit-msg-file)
(make-local-variable 'svn-commit-files)
(setq svn-commit-msg-file msg-file)
(setq svn-commit-files files)
(svn-run 'commit (append (list "-N" "-F" msg-file) files)))
(if window-conf
(set-window-configuration window-conf))))
(defun svn-commit-filter (proc str)
"Output filter function for `svn commit'."
(save-excursion
(set-buffer (process-buffer proc))
(let ((inhibit-read-only t)
(nomore))
(goto-char (point-max))
(insert str)
(goto-char svn-output-marker)
(while (not nomore)
(cond ((looking-at
"\\(Sending\\|Adding\\|Transmitting file\\|Deleting\\) .*\n")
;; Ignore these expected and uninteresting messages
(delete-region (match-beginning 0)
(match-end 0)))
((looking-at "Committed revision \\([0-9]+\\).\n")
(svn-update-label svn-revision-label (match-string 1))
(forward-line 1))
((looking-at ".*\n")
;; Unexpected output is left in the buffer
(forward-line 1))
(t
(setq nomore t)))))))
(defun svn-commit-sentinel (proc reason)
"Sentinel function for `svn commit'."
(with-current-buffer (process-buffer proc)
(when (= (process-exit-status proc) 0)
(while svn-commit-files
(let* ((file (car svn-commit-files))
(path (concat default-directory file))
(pos (svn-file-pos file))
(file-buffer (get-file-buffer path))
(inhibit-read-only t))
(when pos
(svn-update-status-flag pos ?\ ?\ )
(svn-update-status-msg pos "Committed"))
(when (and file-buffer (fboundp 'vc-svn-workfile-version))
(with-current-buffer file-buffer
;; Use buffer-file-name instead of path to get the
;; canonical file name used by vc
;; TODO: use the version number written by the commit command
(vc-file-setprop buffer-file-name 'vc-workfile-version
(vc-svn-workfile-version buffer-file-name))
(vc-mode-line buffer-file-name))))
(setq svn-commit-files (cdr svn-commit-files))))
(delete-file svn-commit-msg-file))
(svn-default-sentinel proc reason))
;;; Svn log
(defun svn-file-log (pos)
"List the change log of the selected file or directory."
(interactive "d")
(let ((file (or (svn-getprop pos 'file)
(svn-getprop pos 'dir))))
(unless file
(error "No file or directory on this line"))
(svn-log (list file))))
(defun svn-log (arg)
"Run `svn log'.
Argument ARG is the command-line arguments, as a string or a list."
(interactive "ssvn log arguments: ")
(when (stringp arg)
(setq arg (split-string arg)))
(svn-run-with-output "log" arg
'svn-log-mode))
(defvar svn-log-mode-map nil
"Keymap for `svn-log-mode'.")
(unless svn-log-mode-map
(setq svn-log-mode-map (make-sparse-keymap))
(define-key svn-log-mode-map "\r" 'svn-log-show-diff)
(define-key svn-log-mode-map "n" 'svn-log-next)
(define-key svn-log-mode-map "p" 'svn-log-prev)
(define-key svn-log-mode-map "e" 'svn-log-edit)
(define-key svn-log-mode-map "+" 'svn-log-expand)
(define-key svn-log-mode-map "-" 'svn-log-compact)
(define-key svn-log-mode-map "=" 'svn-log-diff)
)
(defun svn-update-log-entry (verbose-p)
"Update the log entry under point, using verbose output if
VERBOSE-P."
(save-excursion
(end-of-line)
(re-search-backward svn-log-entry-start-re nil t)
(let ((start (point)))
(unless (re-search-forward "^r\\([0-9]+\\) |" nil t)
(error "Found no commit"))
(let* ((commit-id (string-to-int (match-string 1)))
(new (svn-run-hidden 'log
(append (and verbose-p '("-v"))
'("-r")
(list (int-to-string commit-id)))))
(text (with-current-buffer new
(goto-char (point-min))
(unless (re-search-forward svn-log-entry-start-re nil t)
(error "Failed finding log entry start"))
(unless (re-search-forward svn-log-entry-start-re nil t)
(error "Failed finding log entry end"))
(buffer-substring (point-min) (match-beginning 0))))
(inhibit-read-only t))
(re-search-forward svn-log-entry-start-re nil 'limit)
(goto-char (match-beginning 0))
(delete-region start (point))
(insert text)))))
(defun svn-log-expand ()
"Show verbose log entry information."
(interactive)
(svn-update-log-entry t))
(defun svn-log-compact ()
"Show compact log entry information."
(interactive)
(svn-update-log-entry nil))
(defun svn-log-mode ()
"Major mode for viewing Subversion logs.
\\{svn-log-mode-map}"
(interactive)
(kill-all-local-variables)
(setq major-mode 'svn-log-mode
mode-name "Svn log")
(use-local-map svn-log-mode-map)
(setq paragraph-start "^commit"))
(defconst svn-log-entry-start-re "^-\\{72\\}$")
(defun svn-log-find-revision (commit-id)
(let (found start)
(save-excursion
(goto-char (point-min))
(while (and (re-search-forward svn-log-entry-start-re nil t)
(setq start (point))
(re-search-forward "^r\\([0-9]+\\) |" nil t)
(if (/= (string-to-int (match-string 1)) commit-id)
t
(setq found t)
nil))))
(when found
(goto-char start)
(beginning-of-line)
t)))
(defun svn-log-current-commit ()
(save-excursion
(end-of-line)
(re-search-backward svn-log-entry-start-re nil t)
(unless (re-search-forward "^r\\([0-9]+\\) |" nil t)
(error "Found no commit"))
(string-to-int (match-string 1))))
(defun svn-log-show-diff ()
"Show the changes introduced by the changeset under point."
(interactive)
(let ((commit-id (svn-log-current-commit))
(diff-buf (get-buffer-create "*svn diff*"))
(dir default-directory)
(inhibit-read-only t))
(display-buffer diff-buf)
(with-current-buffer diff-buf
(diff-mode)
(setq buffer-read-only t)
(erase-buffer)
(setq default-directory dir)
(svn-call-process diff-buf
(append (list "diff" "-r" (format "%d:%d" (1- commit-id) commit-id))
svn-diff-args)))))
(defun svn-log-edit-files (commit-id)
(let ((root (svn-repository-root))
result)
(with-current-buffer
(svn-run-hidden 'log (list "-v" "-r"
(int-to-string commit-id)
root))
(goto-char (point-min))
(unless (re-search-forward "^Changed paths:" nil t)
(error "Cannot find list of changes"))
(while (re-search-forward
"^ \\(\\S-+\\)\\s-+\\(.*?\\)\\( (from .*)$\\)?$"
nil t)
(let ((how (match-string 1))
(file (match-string 2))
(tail (match-string 3)))
(when (string-match "\\([^/]*/\\)?\\([^/]*\\)$" file)
(setq file (match-string 0 file)))
(setq result (cons (concat how " " file) result)))))
(nreverse result)))
(defun svn-log-diff ()
"Run `svn diff' for the current log entry."
(interactive)
(let ((commit-id (svn-log-current-commit)))
(svn-run-with-output "diff"
(append svn-diff-args
(list "-c" (number-to-string commit-id)))
'diff-mode)))
(defun svn-log-edit ()
"Edit the log message for the revision under point."
(interactive)
(let* ((commit-id (svn-log-current-commit))
(log (svn-propget commit-id "svn:log"))
(cwd default-directory)
(parent-buffer (current-buffer))
(buffer (get-buffer-create (format "*svn log message of r%d*"
commit-id))))
(log-edit 'svn-log-edit-done t
`(lambda () (svn-log-edit-files ,commit-id))
buffer)
(insert log)
(set (make-local-variable 'svn-commit-id) commit-id)
(set (make-local-variable 'svn-directory) cwd)
(set (make-local-variable 'svn-parent-buffer) parent-buffer)
(setq default-directory cwd)
(message (substitute-command-keys
"Press \\[log-edit-done] when you are done editing."))))
(defun svn-log-edit-done ()
(interactive)
(setq default-directory svn-directory) ; just in case the user cd'd
(message "Changing log message...")
(let ((commit-id svn-commit-id))
(svn-propset commit-id "svn:log" (buffer-string))
(when (buffer-name svn-parent-buffer)
(save-excursion
(set-buffer svn-parent-buffer)
(when (svn-log-find-revision commit-id)
(svn-update-log-entry nil)))))
(kill-buffer nil)
(message "Changing log message... done"))
(defun svn-log-next ()
"Move to the next changeset in the log."
(interactive)
(end-of-line)
(unless (re-search-forward svn-log-entry-start-re nil t)
(error "Found no commit"))
(beginning-of-line)
(svn-log-show-diff))
(defun svn-log-prev ()
"Move to the previous changeset in the log."
(interactive)
(beginning-of-line)
(unless (re-search-backward svn-log-entry-start-re nil t)
(error "Found no commit"))
(svn-log-show-diff))
(defun svn-new-label (&optional pos)
(unless pos (setq pos (point)))
(let ((start (make-marker))
(stop (make-marker)))
(set-marker start pos)
(set-marker stop pos)
(list start stop)))
(defun svn-update-label (label str)
(let ((start (car label))
(stop (cadr label))
(inhibit-read-only t))
(delete-region start stop)
(set-marker-insertion-type stop t)
(save-excursion
(goto-char start)
(insert str))))
;;; Svn propedit
(defun svn-prop-args (file-or-rev)
"Returns a list of arguments to the 'svn prop...' commands, to
make them act on FILE-OR-REV (a file name or a revision number)."
(if (integerp file-or-rev)
(list "--revprop" "-r" (int-to-string file-or-rev))
(list file-or-rev)))
(defun svn-prop-description (file-or-rev)
"Returns a human-readable description of FILE-OR-REV (a file
name or revision number)."
(if (integerp file-or-rev)
(format "revision %d" file-or-rev)
file-or-rev))
(defun svn-propget (file-or-rev propname)
"Return the Subversion property PROPNAME of FILE-OR-REV (file
name or revision number)."
(with-current-buffer
(svn-run-hidden 'propget
(cons propname
(svn-prop-args file-or-rev)))
(substring (buffer-string) 0 -1))) ; trim final newline added by svn
(defun svn-get-props (file)
"Return an alist containing the properties of FILE"
;; First retrieve the property names, and then the value of each.
;; We can't use proplist -v because is output is ambiguous when values
;; consist of multiple lines.
(if (string-equal (svn-run-for-stdout 'info (list file)) "")
(error "%s is not under version control" file))
(let (propnames)
(with-current-buffer (svn-run-hidden 'proplist (list file))
(goto-char (point-min))
(when (looking-at "Properties on ")
(forward-line 1)
(while (looking-at " \\(.+\\)$")
(setq propnames (cons (match-string 1) propnames))
(forward-line 1))))
(mapcar (lambda (propname)
(cons propname (svn-propget file propname)))
propnames)))
(defun svn-propedit (file)
"Edit properties of FILE."
(interactive (list (expand-file-name
(or (svn-getprop (point) 'file)
(read-file-name "Edit properties of file: "
default-directory
nil t
(svn-getprop (point) 'dir))))))
(let ((local-file (svn-local-file-name file)))
(when (string-equal local-file "")
(setq local-file ".")
(setq file (file-name-as-directory file)))
(svn-check-running)
(let ((buf-name (format "*propedit %s*" local-file)))
(if (get-buffer buf-name)
(kill-buffer buf-name))
(let ((prop-alist (svn-get-props local-file))
(propedit-buf (get-buffer-create buf-name)))
(switch-to-buffer-other-window propedit-buf)
(svn-propedit-mode)
(insert
"# Properties of " local-file "\n"
"#\n"
"# Lines are on the form PROPNAME: VALUE for single-line values,\n"
"# or just PROPNAME: followed by one or more lines starting with > for\n"
"# multi-line values. Lines starting with # are ignored.\n"
"#\n"
"# Change, add, delete or rename properties just by editing this\n"
"# buffer; then press "
(substitute-command-keys "\\[svn-propedit-done]")
" to save changes.\n\n")
(mapc (lambda (prop)
(let* ((value (cdr prop))
(lines nil)
(len (length value))
(ofs 0))
;; Split value in lines - we can't use split-string because
;; its behaviour is not consistent across Emacs versions.
(while (<= ofs len)
(let* ((nl (or (string-match "\n" value ofs) len)))
(setq lines (cons (substring value ofs nl) lines))
(setq ofs (+ nl 1))))
(setq lines (nreverse lines))
;; The lines list now contains one string per line, and
;; an empty list at the end if the string finished in a \n.
(insert (car prop) ":")
(if (> (length lines) 1)
(progn
(insert "\n")
(mapc (lambda (line) (insert ">" line "\n"))
lines))
(insert " " (or (car lines) "") "\n"))))
(sort prop-alist #'(lambda (a b) (string< (car a) (car b)))))
(make-local-variable 'svn-propedit-file)
(setq svn-propedit-file file)
(setq default-directory (file-name-directory file))
(message
(substitute-command-keys
"Press \\[svn-propedit-done] when you are done editing."))))))
(defvar svn-propedit-mode-map nil
"Keymap for `svn-propedit-mode'.")
(unless svn-propedit-mode-map
(setq svn-propedit-mode-map (make-sparse-keymap))
(define-key svn-propedit-mode-map "\C-c\C-c" 'svn-propedit-done))
(defun svn-propedit-mode ()
"Major mode for editing Subversion properties."
(interactive)
(kill-all-local-variables)
(setq major-mode 'svn-propedit-mode
mode-name "Svn propedit")
(use-local-map svn-propedit-mode-map)
(setq font-lock-defaults
'((("^#.*$" ;comment
. 'font-lock-comment-face)
("^\\([^ \t\n#>][^ \t\n]*\\):" ;property name
. (1 'bold))
("^[^ \t\n#>][^ \t\n]*: *\\(.*\\)$" ;property value
. (1 'font-lock-function-name-face))
("^>" ;multi-line marker
. 'bold)
("^>\\(.*\\)$" ;property value (continued)
. (1 'font-lock-function-name-face))
)
nil ;keywords-only
nil ;case-fold
;; syntax-alist: don't fontify quotes specially in any way
((?\" . "."))
nil ;syntax-begin
))
(font-lock-mode))
(defun svn-props-from-buffer ()
"Parse the current propedit buffer and return an alist of the properties."
(save-excursion
(let (prop-alist)
(goto-char (point-min))
(while (not (eobp))
(cond ((looking-at "^\\([^ \t\n#>][^ \t\n]*\\): *\\(.*\\)$")
(let ((prop-name (match-string 1))
(value (match-string 2)))
(set-text-properties 0 (length prop-name) nil prop-name)
(set-text-properties 0 (length value) nil value)
(when (assoc prop-name prop-alist)
(error "Duplicated property %s" prop-name))
(setq prop-alist (cons (cons prop-name value) prop-alist))))
((looking-at "^>\\(.*\\)$")
(let ((extra-line (match-string 1)))
(set-text-properties 0 (length extra-line) nil extra-line)
(when (null prop-alist)
(error "Continued line not preceded by property name"))
(let ((old-value (cdar prop-alist)))
(setcdr (car prop-alist)
(concat old-value "\n" extra-line))))))
(forward-line 1))
;; Remove the extra leading newline from multi-line values
(mapcar (lambda (prop)
(let ((name (car prop))
(value (cdr prop)))
(if (and (not (equal value ""))
(equal (substring value 0 1) "\n"))
(cons name (substring value 1))
prop)))
prop-alist))))
(defun svn-propdel (file prop-name)
"Delete FILE's property PROP-NAME."
(svn-run-hidden 'propdel (list prop-name file)))
(defun svn-propset (file-or-rev prop-name prop-value)
"Set the property PROP-NAME to PROP-VALUE for FILE-OR-REV (a
file name or revision number)."
(let ((buf (svn-run-hidden 'propset (append (list prop-name prop-value)
(svn-prop-args file-or-rev)))))
(unless
(with-current-buffer buf
(goto-char (point-min))
(looking-at "^property '.*' set on "))
(switch-to-buffer buf)
(error "Failed setting property %s of %s"
prop-name
(svn-prop-description file-or-rev)))))
(defun svn-propedit-done ()
"Apply property changes to the file."
(interactive)
(let ((wc-props (svn-get-props svn-propedit-file))
(new-props (svn-props-from-buffer))
(changes 0))
;; first remove properties that the user deleted from the buffer
(mapc (lambda (wc-prop)
(let ((prop-name (car wc-prop)))
(when (not (assoc prop-name new-props))
(message "Deleting property %s" prop-name)
(svn-propdel svn-propedit-file prop-name)
(setq changes (1+ changes)))))
wc-props)
;; then set the properties that have changed or are new
(mapc (lambda (new-prop)
(let* ((prop-name (car new-prop))
(wc-prop (assoc prop-name wc-props)))
(unless (equal new-prop wc-prop)
(message "Setting property %s" prop-name)
(svn-propset svn-propedit-file prop-name (cdr new-prop))
(setq changes (1+ changes)))))
new-props)
(cond
((> changes 1) (message "Changed %d properties." changes))
((= changes 0) (message "No properties changed."))))
(svn-foreach-svn-buffer
svn-propedit-file
(lambda (local-file-name file-pos)
(svn-refresh-item local-file-name nil)))
(kill-buffer (current-buffer)))
;;; Svn buffer
(defvar svn-files-start nil)
(defvar svn-files-stop nil)
(defvar svn-url-label nil)
(defvar svn-revision-label nil)
(defvar svn-running-label nil)
(defvar svn-output-marker nil)
(defvar svn-running nil)
(defun create-svn-buffer (dir)
"Create a buffer for showing svn status.
Argument DIR is the directory to run svn in."
(let ((status-buf (create-file-buffer (concat dir "*svn*")))
(inhibit-read-only t))
(with-current-buffer status-buf
(setq default-directory dir)
(svn-status-mode)
(make-local-variable 'svn-url-label)
(make-local-variable 'svn-revision-label)
(make-local-variable 'svn-running-label)
(make-local-variable 'svn-output-marker)
(insert "Svn status for " dir) (newline)
(insert "URL: ") (setq svn-url-label (svn-new-label))
(insert " revision " ) (setq svn-revision-label (svn-new-label))
(newline)
(newline)
(insert "---- ") (setq svn-running-label (svn-new-label))
(newline)
(setq svn-files-start (point-marker))
(set-marker-insertion-type svn-files-start nil)
(setq svn-last-inserted-marker (point-marker))
(set-marker-insertion-type svn-last-inserted-marker nil)
(insert "----")
(newline)
(setq svn-output-marker (point-marker))
(set-marker-insertion-type svn-output-marker nil)
;; Do this after inserting stuff
(setq svn-files-stop (copy-marker svn-files-start t))
(setq buffer-read-only t))
status-buf))
(defun switch-to-svn-buffer (dir)
"Switch to a (possibly new) buffer displaying status for DIR"
(setq dir (file-name-as-directory dir))
(let ((buffers (buffer-list)))
(while (and buffers
(not (with-current-buffer (car buffers)
(and (eq major-mode 'svn-status-mode)
(string= default-directory dir)))))
(setq buffers (cdr buffers)))
(switch-to-buffer (if buffers
(car buffers)
(create-svn-buffer dir)))))