Change release version from 21.4 to 22.1 throughout.
[bpt/emacs.git] / lisp / net / tramp-vc.el
CommitLineData
fb7933a3
KG
1;;; tramp-vc.el --- Version control integration for TRAMP.el
2
38c65fca 3;; Copyright (C) 2000, 2001, 2002, 2003, 2004 by Free Software Foundation, Inc.
fb7933a3
KG
4
5;; Author: Daniel Pittman <daniel@danann.net>
6;; Keywords: comm, processes
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
24
25;;; Commentary:
26
27;; See the main module, 'tramp.el' for discussion of the purpose of TRAMP.
28;; This module provides integration between remote files accessed by TRAMP and
29;; the Emacs version control system.
30
31;;; Code:
32
33(eval-when-compile
34 (require 'cl))
35(require 'vc)
36;; Old VC defines vc-rcs-release in vc.el, new VC requires extra module.
37(unless (boundp 'vc-rcs-release)
38 (require 'vc-rcs))
39(require 'tramp)
40
38c65fca
KG
41;; Avoid byte-compiler warnings if the byte-compiler supports this.
42;; Currently, XEmacs supports this.
43(eval-when-compile
44 (when (fboundp 'byte-compiler-options)
45 (let (unused-vars) ; Pacify Emacs byte-compiler
46 (defalias 'warnings 'identity) ; Pacify Emacs byte-compiler
47 (byte-compiler-options (warnings (- unused-vars))))))
48
fb7933a3
KG
49;; -- vc --
50
51;; This used to blow away the file-name-handler-alist and reinstall
52;; TRAMP into it. This was intended to let VC work remotely. It didn't,
53;; at least not in my XEmacs 21.2 install.
bf247b6e 54;;
fb7933a3
KG
55;; In any case, tramp-run-real-handler now deals correctly with disabling
56;; the things that should be, making this a no-op.
57;;
58;; I have removed it from the tramp-file-name-handler-alist because the
59;; shortened version does nothing. This is for reference only now.
60;;
61;; Daniel Pittman <daniel@danann.net>
62;;
63;; (defun tramp-handle-vc-registered (file)
64;; "Like `vc-registered' for tramp files."
65;; (tramp-run-real-handler 'vc-registered (list file)))
66
67;; `vc-do-command'
68;; This function does not deal well with remote files, so we define
69;; our own version and make a backup of the original function and
70;; call our version for tramp files and the original version for
71;; normal files.
72
73;; The following function is pretty much copied from vc.el, but
74;; the part that actually executes a command is changed.
75;; CCC: this probably works for Emacs 21, too.
76(defun tramp-vc-do-command (buffer okstatus command file last &rest flags)
77 "Like `vc-do-command' but invoked for tramp files.
78See `vc-do-command' for more information."
79 (save-match-data
07dfe738 80 (and file (setq file (expand-file-name file)))
fb7933a3
KG
81 (if (not buffer) (setq buffer "*vc*"))
82 (if vc-command-messages
83 (message "Running `%s' on `%s'..." command file))
84 (let ((obuf (current-buffer)) (camefrom (current-buffer))
85 (squeezed nil)
86 (olddir default-directory)
87 vc-file status)
07dfe738 88 (let* ((v (tramp-dissect-file-name (expand-file-name file)))
fb7933a3
KG
89 (multi-method (tramp-file-name-multi-method v))
90 (method (tramp-file-name-method v))
91 (user (tramp-file-name-user v))
92 (host (tramp-file-name-host v))
7432277c 93 (localname (tramp-file-name-localname v)))
fb7933a3
KG
94 (set-buffer (get-buffer-create buffer))
95 (set (make-local-variable 'vc-parent-buffer) camefrom)
96 (set (make-local-variable 'vc-parent-buffer-name)
97 (concat " from " (buffer-name camefrom)))
98 (setq default-directory olddir)
bf247b6e 99
fb7933a3
KG
100 (erase-buffer)
101
102 (mapcar
103 (function
104 (lambda (s) (and s (setq squeezed (append squeezed (list s))))))
105 flags)
106 (if (and (eq last 'MASTER) file
107 (setq vc-file (vc-name file)))
108 (setq squeezed
109 (append squeezed
7432277c 110 (list (tramp-file-name-localname
fb7933a3
KG
111 (tramp-dissect-file-name vc-file))))))
112 (if (and file (eq last 'WORKFILE))
113 (progn
114 (let* ((pwd (expand-file-name default-directory))
115 (preflen (length pwd)))
116 (if (string= (substring file 0 preflen) pwd)
117 (setq file (substring file preflen))))
118 (setq squeezed (append squeezed (list file)))))
119 ;; Unless we (save-window-excursion) the layout of windows in
120 ;; the current frame changes. This is painful, at best.
121 ;;
122 ;; As a point of note, (save-excursion) is still here only because
123 ;; it preserves (point) in the current buffer. (save-window-excursion)
124 ;; does not, at least under XEmacs 21.2.
125 ;;
126 ;; I trust that the FSF support this as well. I can't find useful
127 ;; documentation to check :(
128 ;;
129 ;; Daniel Pittman <daniel@danann.net>
130 (save-excursion
131 (save-window-excursion
132 ;; Actually execute remote command
01917a18
MA
133 ;; `shell-command' cannot be used; it isn't magic in XEmacs.
134 (tramp-handle-shell-command
fb7933a3
KG
135 (mapconcat 'tramp-shell-quote-argument
136 (cons command squeezed) " ") t)
137 ;;(tramp-wait-for-output)
138 ;; Get status from command
139 (tramp-send-command multi-method method user host "echo $?")
140 (tramp-wait-for-output)
141 ;; Make sure to get status from last line of output.
142 (goto-char (point-max)) (forward-line -1)
143 (setq status (read (current-buffer)))
144 (message "Command %s returned status %d." command status)))
145 (goto-char (point-max))
146 (set-buffer-modified-p nil)
147 (forward-line -1)
4007ba5b
KG
148 (if (or (not (integerp status))
149 (and (integerp okstatus) (< okstatus status)))
fb7933a3
KG
150 (progn
151 (pop-to-buffer buffer)
152 (goto-char (point-min))
153 (shrink-window-if-larger-than-buffer)
154 (error "Running `%s'...FAILED (%s)" command
155 (if (integerp status)
156 (format "status %d" status)
157 status))
158 )
159 (if vc-command-messages
160 (message "Running %s...OK" command))
161 )
162 (set-buffer obuf)
163 status))
164 ))
165
166;; Following code snarfed from Emacs 21 vc.el and slightly tweaked.
167(defun tramp-vc-do-command-new (buffer okstatus command file &rest flags)
168 "Like `vc-do-command' but for TRAMP files.
169This function is for the new VC which comes with Emacs 21.
170Since TRAMP doesn't do async commands yet, this function doesn't, either."
171 (and file (setq file (expand-file-name file)))
172 (if vc-command-messages
173 (message "Running %s on %s..." command file))
174 (save-current-buffer
38c65fca
KG
175 (unless (eq buffer t)
176 ; Pacify byte-compiler
177 (funcall (symbol-function 'vc-setup-buffer) buffer))
fb7933a3
KG
178 (let ((squeezed nil)
179 (inhibit-read-only t)
180 (status 0))
181 (let* ((v (when file (tramp-dissect-file-name file)))
182 (multi-method (when file (tramp-file-name-multi-method v)))
183 (method (when file (tramp-file-name-method v)))
184 (user (when file (tramp-file-name-user v)))
185 (host (when file (tramp-file-name-host v)))
7432277c 186 (localname (when file (tramp-file-name-localname v))))
fb7933a3
KG
187 (setq squeezed (delq nil (copy-sequence flags)))
188 (when file
4007ba5b
KG
189 (setq squeezed (append squeezed (list (file-relative-name
190 file default-directory)))))
fb7933a3
KG
191 (let ((w32-quote-process-args t))
192 (when (eq okstatus 'async)
193 (message "Tramp doesn't do async commands, running synchronously."))
01917a18
MA
194 ;; `shell-command' cannot be used; it isn't magic in XEmacs.
195 (setq status (tramp-handle-shell-command
fb7933a3
KG
196 (mapconcat 'tramp-shell-quote-argument
197 (cons command squeezed) " ") t))
4007ba5b
KG
198 (when (or (not (integerp status))
199 (and (integerp okstatus) (< okstatus status)))
fb7933a3
KG
200 (pop-to-buffer (current-buffer))
201 (goto-char (point-min))
202 (shrink-window-if-larger-than-buffer)
203 (error "Running %s...FAILED (%s)" command
204 (if (integerp status) (format "status %d" status) status))))
205 (if vc-command-messages
206 (message "Running %s...OK" command))
38c65fca
KG
207 ; Pacify byte-compiler
208 (funcall (symbol-function 'vc-exec-after)
209 `(run-hook-with-args
210 'vc-post-command-functions ',command ',localname ',flags))
fb7933a3
KG
211 status))))
212
213
214;; The context for a VC command is the current buffer.
215;; That makes a test on the buffers file more reliable than a test on the
216;; arguments.
217;; This is needed to handle remote VC correctly - else we test against the
218;; local VC system and get things wrong...
219;; Daniel Pittman <daniel@danann.net>
220;;-(if (fboundp 'vc-call-backend)
221;;- () ;; This is the new VC for which we don't have an appropriate advice yet
0457dd55 222(unless (fboundp 'process-file)
fb7933a3
KG
223(if (fboundp 'vc-call-backend)
224 (defadvice vc-do-command
225 (around tramp-advice-vc-do-command
226 (buffer okstatus command file &rest flags)
227 activate)
228 "Invoke tramp-vc-do-command for tramp files."
229 (let ((file (symbol-value 'file))) ;pacify byte-compiler
230 (if (or (and (stringp file) (tramp-tramp-file-p file))
231 (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name))))
232 (setq ad-return-value
bf247b6e 233 (apply 'tramp-vc-do-command-new buffer okstatus command
fb7933a3
KG
234 file ;(or file (buffer-file-name))
235 flags))
236 ad-do-it)))
237 (defadvice vc-do-command
238 (around tramp-advice-vc-do-command
239 (buffer okstatus command file last &rest flags)
240 activate)
241 "Invoke tramp-vc-do-command for tramp files."
242 (let ((file (symbol-value 'file))) ;pacify byte-compiler
243 (if (or (and (stringp file) (tramp-tramp-file-p file))
244 (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name))))
245 (setq ad-return-value
bf247b6e 246 (apply 'tramp-vc-do-command buffer okstatus command
fb7933a3 247 (or file (buffer-file-name)) last flags))
0457dd55 248 ad-do-it)))))
fb7933a3
KG
249;;-)
250
251
252;; XEmacs uses this to do some of its work. Like vc-do-command, we
253;; need to enhance it to make VC work via TRAMP-mode.
254;;
255;; Like the previous function, this is a cut-and-paste job from the VC
256;; file. It's based on the vc-do-command code.
257;; CCC: this isn't used in Emacs 21, so do as before.
258(defun tramp-vc-simple-command (okstatus command file &rest args)
259 ;; Simple version of vc-do-command, for use in vc-hooks only.
260 ;; Don't switch to the *vc-info* buffer before running the
261 ;; command, because that would change its default directory
262 (save-match-data
07dfe738 263 (let* ((v (tramp-dissect-file-name (expand-file-name file)))
fb7933a3
KG
264 (multi-method (tramp-file-name-multi-method v))
265 (method (tramp-file-name-method v))
266 (user (tramp-file-name-user v))
267 (host (tramp-file-name-host v))
7432277c 268 (localname (tramp-file-name-localname v)))
fb7933a3
KG
269 (save-excursion (set-buffer (get-buffer-create "*vc-info*"))
270 (erase-buffer))
271 (let ((exec-path (append vc-path exec-path)) exec-status
272 ;; Add vc-path to PATH for the execution of this command.
273 (process-environment
274 (cons (concat "PATH=" (getenv "PATH")
275 path-separator
276 (mapconcat 'identity vc-path path-separator))
277 process-environment)))
278 ;; Call the actual process. See tramp-vc-do-command for discussion of
279 ;; why this does both (save-window-excursion) and (save-excursion).
280 ;;
281 ;; As a note, I don't think that the process-environment stuff above
282 ;; has any effect on the remote system. This is a hard one though as
283 ;; there is no real reason to expect local and remote paths to be
284 ;; identical...
285 ;;
286 ;; Daniel Pittman <daniel@danann.net>
287 (save-excursion
288 (save-window-excursion
289 ;; Actually execute remote command
01917a18
MA
290 ;; `shell-command' cannot be used; it isn't magic in XEmacs.
291 (tramp-handle-shell-command
fb7933a3 292 (mapconcat 'tramp-shell-quote-argument
7432277c 293 (append (list command) args (list localname)) " ")
fb7933a3
KG
294 (get-buffer-create"*vc-info*"))
295 ;(tramp-wait-for-output)
296 ;; Get status from command
297 (tramp-send-command multi-method method user host "echo $?")
298 (tramp-wait-for-output)
299 (setq exec-status (read (current-buffer)))
300 (message "Command %s returned status %d." command exec-status)))
bf247b6e 301
4007ba5b
KG
302 ;; Maybe okstatus can be `async' here. But then, maybe the
303 ;; async thing is new in Emacs 21, but this function is only
304 ;; used in Emacs 20.
fb7933a3
KG
305 (cond ((> exec-status okstatus)
306 (switch-to-buffer (get-file-buffer file))
307 (shrink-window-if-larger-than-buffer
308 (display-buffer "*vc-info*"))
309 (error "Couldn't find version control information")))
310 exec-status))))
311
312;; This function does not exist any more in Emacs-21's VC
313(defadvice vc-simple-command
314 (around tramp-advice-vc-simple-command
315 (okstatus command file &rest args)
316 activate)
317 "Invoke tramp-vc-simple-command for tramp files."
318 (let ((file (symbol-value 'file))) ;pacify byte-compiler
319 (if (or (and (stringp file) (tramp-tramp-file-p file))
320 (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name))))
321 (setq ad-return-value
bf247b6e 322 (apply 'tramp-vc-simple-command okstatus command
fb7933a3
KG
323 (or file (buffer-file-name)) args))
324 ad-do-it)))
325
326
327;; `vc-workfile-unchanged-p'
328;; This function does not deal well with remote files, so we do the
329;; same as for `vc-do-command'.
330
331;; `vc-workfile-unchanged-p' checks the modification time, we cannot
332;; do that for remote files, so here's a version which relies on diff.
333;; CCC: this one probably works for Emacs 21, too.
334(defun tramp-vc-workfile-unchanged-p
335 (filename &optional want-differences-if-changed)
336 (if (fboundp 'vc-backend-diff)
337 ;; Old VC. Call `vc-backend-diff'.
338 (let ((status (funcall (symbol-function 'vc-backend-diff)
339 filename nil nil
340 (not want-differences-if-changed))))
341 (zerop status))
342 ;; New VC. Call `vc-default-workfile-unchanged-p'.
38c65fca
KG
343 (funcall (symbol-function 'vc-default-workfile-unchanged-p)
344 (vc-backend filename) filename)))
fb7933a3
KG
345
346(defadvice vc-workfile-unchanged-p
347 (around tramp-advice-vc-workfile-unchanged-p
348 (filename &optional want-differences-if-changed)
349 activate)
350 "Invoke tramp-vc-workfile-unchanged-p for tramp files."
351 (if (and (stringp filename)
352 (tramp-tramp-file-p filename)
353 (not
354 (let ((v (tramp-dissect-file-name filename)))
90f8dc03
KG
355 ;; The following check is probably to test whether
356 ;; file-attributes returns correct last modification
357 ;; times. This check needs to be changed.
fb7933a3
KG
358 (tramp-get-remote-perl (tramp-file-name-multi-method v)
359 (tramp-file-name-method v)
360 (tramp-file-name-user v)
361 (tramp-file-name-host v)))))
362 (setq ad-return-value
363 (tramp-vc-workfile-unchanged-p filename want-differences-if-changed))
364 ad-do-it))
365
366
367;; Redefine a function from vc.el -- allow tramp files.
368;; `save-match-data' seems not to be required -- it isn't in
369;; the original version, either.
370;; CCC: this might need some work -- how does the Emacs 21 version
371;; work, anyway? Does it work over ange-ftp? Hm.
372(if (not (fboundp 'vc-backend-checkout))
373 () ;; our replacement won't work and is unnecessary anyway
374(defun vc-checkout (filename &optional writable rev)
375 "Retrieve a copy of the latest version of the given file."
376 ;; If ftp is on this system and the name matches the ange-ftp format
377 ;; for a remote file, the user is trying something that won't work.
378 (funcall (symbol-function 'vc-backend-checkout) filename writable rev)
379 (vc-resynch-buffer filename t t))
380)
381
382
383;; Do we need to advise the vc-user-login-name function anyway?
bf247b6e 384;; This will return the correct login name for the owner of a
fb7933a3
KG
385;; file. It does not deal with the default remote user name...
386;;
bf247b6e 387;; That is, when vc calls (vc-user-login-name), we return the
fb7933a3 388;; local login name, something that may be different to the remote
bf247b6e 389;; default.
fb7933a3
KG
390;;
391;; The remote VC operations will occur as the user that we logged
392;; in with however - not always the same as the local user.
393;;
bf247b6e 394;; In the end, I did advise the function. This is because, well,
fb7933a3
KG
395;; the thing didn't work right otherwise ;)
396;;
397;; Daniel Pittman <daniel@danann.net>
398
399(defun tramp-handle-vc-user-login-name (&optional uid)
400 "Return the default user name on the remote machine.
401Whenever VC calls this function, `file' is bound to the file name
402in question. If no uid is provided or the uid is equal to the uid
403owning the file, then we return the user name given in the file name.
404
405This should only be called when `file' is bound to the
406filename we are thinking about..."
407 ;; Pacify byte-compiler; this symbol is bound in the calling
408 ;; function. CCC: Maybe it would be better to move the
409 ;; boundness-checking into this function?
38c65fca
KG
410 (let* ((file (symbol-value 'file))
411 (remote-uid
bf247b6e 412 ;; With Emacs 22.1, `file-attributes' has got an optional parameter
38c65fca
KG
413 ;; ID-FORMAT. Handle this case backwards compatible.
414 (if (and (functionp 'subr-arity)
415 (= 2 (cdr (funcall (symbol-function 'subr-arity)
416 (symbol-function 'file-attributes)))))
417 (nth 2 (file-attributes file 'integer))
418 (nth 2 (file-attributes file)))))
c951aecb 419 (if (and uid (/= uid remote-uid))
fb7933a3 420 (error "tramp-handle-vc-user-login-name cannot map a uid to a name")
07dfe738 421 (let* ((v (tramp-dissect-file-name (expand-file-name file)))
fb7933a3
KG
422 (u (tramp-file-name-user v)))
423 (cond ((stringp u) u)
424 ((vectorp u) (elt u (1- (length u))))
425 ((null u) (user-login-name))
426 (t (error "tramp-handle-vc-user-login-name cannot cope!")))))))
427
428
429(defadvice vc-user-login-name
430 (around tramp-vc-user-login-name activate)
431 "Support for files on remote machines accessed by TRAMP."
432 ;; We rely on the fact that `file' is bound when this is called.
433 ;; This appears to be the case everywhere in vc.el and vc-hooks.el
434 ;; as of Emacs 20.5.
435 ;;
436 ;; CCC TODO there should be a real solution! Talk to Andre Spiegel
437 ;; about this.
438 (let ((file (when (boundp 'file)
439 (symbol-value 'file)))) ;pacify byte-compiler
440 (or (and (stringp file)
441 (tramp-tramp-file-p file) ; tramp file
442 (setq ad-return-value
443 (save-match-data
444 (tramp-handle-vc-user-login-name uid)))) ; get the owner name
445 ad-do-it))) ; else call the original
446
bf247b6e 447
fb7933a3
KG
448;; Determine the name of the user owning a file.
449(defun tramp-file-owner (filename)
450 "Return who owns FILE (user name, as a string)."
bf247b6e 451 (let ((v (tramp-dissect-file-name
07dfe738
KG
452 (expand-file-name filename))))
453 (if (not (file-exists-p filename))
fb7933a3
KG
454 nil ; file cannot be opened
455 ;; file exists, find out stuff
456 (save-excursion
457 (tramp-send-command
458 (tramp-file-name-multi-method v) (tramp-file-name-method v)
459 (tramp-file-name-user v) (tramp-file-name-host v)
460 (format "%s -Lld %s"
461 (tramp-get-ls-command (tramp-file-name-multi-method v)
462 (tramp-file-name-method v)
463 (tramp-file-name-user v)
464 (tramp-file-name-host v))
7432277c 465 (tramp-shell-quote-argument (tramp-file-name-localname v))))
fb7933a3
KG
466 (tramp-wait-for-output)
467 ;; parse `ls -l' output ...
468 ;; ... file mode flags
469 (read (current-buffer))
470 ;; ... number links
471 (read (current-buffer))
472 ;; ... uid (as a string)
473 (symbol-name (read (current-buffer)))))))
474
475;; Wire ourselves into the VC infrastructure...
476;; This function does not exist any more in Emacs-21's VC
477;; CCC: it appears that no substitute is needed for Emacs 21.
478(defadvice vc-file-owner
479 (around tramp-vc-file-owner activate)
480 "Support for files on remote machines accessed by TRAMP."
481 (let ((filename (ad-get-arg 0)))
482 (or (and (tramp-file-name-p filename) ; tramp file
483 (setq ad-return-value
484 (save-match-data
485 (tramp-file-owner filename)))) ; get the owner name
486 ad-do-it))) ; else call the original
487
488
489;; We need to make the version control software backend version
490;; information local to the current buffer. This is because each TRAMP
491;; buffer can (theoretically) have a different VC version and I am
492;; *way* too lazy to try and push the correct value into each new
493;; buffer.
494;;
495;; Remote VC costs will just have to be paid, at least for the moment.
496;; Well, at least, they will right until I feel guilty about doing a
497;; botch job here and fix it. :/
498;;
499;; Daniel Pittman <daniel@danann.net>
500;; CCC: this is probably still needed for Emacs 21.
501(defun tramp-vc-setup-for-remote ()
502 "Make the backend release variables buffer local.
503This makes remote VC work correctly at the cost of some processing time."
504 (when (and (buffer-file-name)
505 (tramp-tramp-file-p (buffer-file-name)))
506 (make-local-variable 'vc-rcs-release)
507 (setq vc-rcs-release nil)))
508(add-hook 'find-file-hooks 'tramp-vc-setup-for-remote t)
509
510;; No need to load this again if anyone asks.
511(provide 'tramp-vc)
512
ab5796a9 513;;; arch-tag: 27cc42ce-da19-468d-ad5c-a2690558db60
fb7933a3 514;;; tramp-vc.el ends here