]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-vc.el
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-660
[gnu-emacs] / lisp / net / tramp-vc.el
1 ;;; tramp-vc.el --- Version control integration for TRAMP.el
2
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004 by Free Software Foundation, Inc.
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
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
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.
54 ;;
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.
78 See `vc-do-command' for more information."
79 (save-match-data
80 (and file (setq file (expand-file-name file)))
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)
88 (let* ((v (tramp-dissect-file-name (expand-file-name file)))
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))
93 (localname (tramp-file-name-localname v)))
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)
99
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
110 (list (tramp-file-name-localname
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
133 (shell-command
134 (mapconcat 'tramp-shell-quote-argument
135 (cons command squeezed) " ") t)
136 ;;(tramp-wait-for-output)
137 ;; Get status from command
138 (tramp-send-command multi-method method user host "echo $?")
139 (tramp-wait-for-output)
140 ;; Make sure to get status from last line of output.
141 (goto-char (point-max)) (forward-line -1)
142 (setq status (read (current-buffer)))
143 (message "Command %s returned status %d." command status)))
144 (goto-char (point-max))
145 (set-buffer-modified-p nil)
146 (forward-line -1)
147 (if (or (not (integerp status))
148 (and (integerp okstatus) (< okstatus status)))
149 (progn
150 (pop-to-buffer buffer)
151 (goto-char (point-min))
152 (shrink-window-if-larger-than-buffer)
153 (error "Running `%s'...FAILED (%s)" command
154 (if (integerp status)
155 (format "status %d" status)
156 status))
157 )
158 (if vc-command-messages
159 (message "Running %s...OK" command))
160 )
161 (set-buffer obuf)
162 status))
163 ))
164
165 ;; Following code snarfed from Emacs 21 vc.el and slightly tweaked.
166 (defun tramp-vc-do-command-new (buffer okstatus command file &rest flags)
167 "Like `vc-do-command' but for TRAMP files.
168 This function is for the new VC which comes with Emacs 21.
169 Since TRAMP doesn't do async commands yet, this function doesn't, either."
170 (and file (setq file (expand-file-name file)))
171 (if vc-command-messages
172 (message "Running %s on %s..." command file))
173 (save-current-buffer
174 (unless (eq buffer t)
175 ; Pacify byte-compiler
176 (funcall (symbol-function 'vc-setup-buffer) buffer))
177 (let ((squeezed nil)
178 (inhibit-read-only t)
179 (status 0))
180 (let* ((v (when file (tramp-dissect-file-name file)))
181 (multi-method (when file (tramp-file-name-multi-method v)))
182 (method (when file (tramp-file-name-method v)))
183 (user (when file (tramp-file-name-user v)))
184 (host (when file (tramp-file-name-host v)))
185 (localname (when file (tramp-file-name-localname v))))
186 (setq squeezed (delq nil (copy-sequence flags)))
187 (when file
188 (setq squeezed (append squeezed (list (file-relative-name
189 file default-directory)))))
190 (let ((w32-quote-process-args t))
191 (when (eq okstatus 'async)
192 (message "Tramp doesn't do async commands, running synchronously."))
193 (setq status (shell-command
194 (mapconcat 'tramp-shell-quote-argument
195 (cons command squeezed) " ") t))
196 (when (or (not (integerp status))
197 (and (integerp okstatus) (< okstatus status)))
198 (pop-to-buffer (current-buffer))
199 (goto-char (point-min))
200 (shrink-window-if-larger-than-buffer)
201 (error "Running %s...FAILED (%s)" command
202 (if (integerp status) (format "status %d" status) status))))
203 (if vc-command-messages
204 (message "Running %s...OK" command))
205 ; Pacify byte-compiler
206 (funcall (symbol-function 'vc-exec-after)
207 `(run-hook-with-args
208 'vc-post-command-functions ',command ',localname ',flags))
209 status))))
210
211
212 ;; The context for a VC command is the current buffer.
213 ;; That makes a test on the buffers file more reliable than a test on the
214 ;; arguments.
215 ;; This is needed to handle remote VC correctly - else we test against the
216 ;; local VC system and get things wrong...
217 ;; Daniel Pittman <daniel@danann.net>
218 ;;-(if (fboundp 'vc-call-backend)
219 ;;- () ;; This is the new VC for which we don't have an appropriate advice yet
220 (unless (fboundp 'process-file)
221 (if (fboundp 'vc-call-backend)
222 (defadvice vc-do-command
223 (around tramp-advice-vc-do-command
224 (buffer okstatus command file &rest flags)
225 activate)
226 "Invoke tramp-vc-do-command for tramp files."
227 (let ((file (symbol-value 'file))) ;pacify byte-compiler
228 (if (or (and (stringp file) (tramp-tramp-file-p file))
229 (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name))))
230 (setq ad-return-value
231 (apply 'tramp-vc-do-command-new buffer okstatus command
232 file ;(or file (buffer-file-name))
233 flags))
234 ad-do-it)))
235 (defadvice vc-do-command
236 (around tramp-advice-vc-do-command
237 (buffer okstatus command file last &rest flags)
238 activate)
239 "Invoke tramp-vc-do-command for tramp files."
240 (let ((file (symbol-value 'file))) ;pacify byte-compiler
241 (if (or (and (stringp file) (tramp-tramp-file-p file))
242 (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name))))
243 (setq ad-return-value
244 (apply 'tramp-vc-do-command buffer okstatus command
245 (or file (buffer-file-name)) last flags))
246 ad-do-it)))))
247 ;;-)
248
249
250 ;; XEmacs uses this to do some of its work. Like vc-do-command, we
251 ;; need to enhance it to make VC work via TRAMP-mode.
252 ;;
253 ;; Like the previous function, this is a cut-and-paste job from the VC
254 ;; file. It's based on the vc-do-command code.
255 ;; CCC: this isn't used in Emacs 21, so do as before.
256 (defun tramp-vc-simple-command (okstatus command file &rest args)
257 ;; Simple version of vc-do-command, for use in vc-hooks only.
258 ;; Don't switch to the *vc-info* buffer before running the
259 ;; command, because that would change its default directory
260 (save-match-data
261 (let* ((v (tramp-dissect-file-name (expand-file-name file)))
262 (multi-method (tramp-file-name-multi-method v))
263 (method (tramp-file-name-method v))
264 (user (tramp-file-name-user v))
265 (host (tramp-file-name-host v))
266 (localname (tramp-file-name-localname v)))
267 (save-excursion (set-buffer (get-buffer-create "*vc-info*"))
268 (erase-buffer))
269 (let ((exec-path (append vc-path exec-path)) exec-status
270 ;; Add vc-path to PATH for the execution of this command.
271 (process-environment
272 (cons (concat "PATH=" (getenv "PATH")
273 path-separator
274 (mapconcat 'identity vc-path path-separator))
275 process-environment)))
276 ;; Call the actual process. See tramp-vc-do-command for discussion of
277 ;; why this does both (save-window-excursion) and (save-excursion).
278 ;;
279 ;; As a note, I don't think that the process-environment stuff above
280 ;; has any effect on the remote system. This is a hard one though as
281 ;; there is no real reason to expect local and remote paths to be
282 ;; identical...
283 ;;
284 ;; Daniel Pittman <daniel@danann.net>
285 (save-excursion
286 (save-window-excursion
287 ;; Actually execute remote command
288 (shell-command
289 (mapconcat 'tramp-shell-quote-argument
290 (append (list command) args (list localname)) " ")
291 (get-buffer-create"*vc-info*"))
292 ;(tramp-wait-for-output)
293 ;; Get status from command
294 (tramp-send-command multi-method method user host "echo $?")
295 (tramp-wait-for-output)
296 (setq exec-status (read (current-buffer)))
297 (message "Command %s returned status %d." command exec-status)))
298
299 ;; Maybe okstatus can be `async' here. But then, maybe the
300 ;; async thing is new in Emacs 21, but this function is only
301 ;; used in Emacs 20.
302 (cond ((> exec-status okstatus)
303 (switch-to-buffer (get-file-buffer file))
304 (shrink-window-if-larger-than-buffer
305 (display-buffer "*vc-info*"))
306 (error "Couldn't find version control information")))
307 exec-status))))
308
309 ;; This function does not exist any more in Emacs-21's VC
310 (defadvice vc-simple-command
311 (around tramp-advice-vc-simple-command
312 (okstatus command file &rest args)
313 activate)
314 "Invoke tramp-vc-simple-command for tramp files."
315 (let ((file (symbol-value 'file))) ;pacify byte-compiler
316 (if (or (and (stringp file) (tramp-tramp-file-p file))
317 (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name))))
318 (setq ad-return-value
319 (apply 'tramp-vc-simple-command okstatus command
320 (or file (buffer-file-name)) args))
321 ad-do-it)))
322
323
324 ;; `vc-workfile-unchanged-p'
325 ;; This function does not deal well with remote files, so we do the
326 ;; same as for `vc-do-command'.
327
328 ;; `vc-workfile-unchanged-p' checks the modification time, we cannot
329 ;; do that for remote files, so here's a version which relies on diff.
330 ;; CCC: this one probably works for Emacs 21, too.
331 (defun tramp-vc-workfile-unchanged-p
332 (filename &optional want-differences-if-changed)
333 (if (fboundp 'vc-backend-diff)
334 ;; Old VC. Call `vc-backend-diff'.
335 (let ((status (funcall (symbol-function 'vc-backend-diff)
336 filename nil nil
337 (not want-differences-if-changed))))
338 (zerop status))
339 ;; New VC. Call `vc-default-workfile-unchanged-p'.
340 (funcall (symbol-function 'vc-default-workfile-unchanged-p)
341 (vc-backend filename) filename)))
342
343 (defadvice vc-workfile-unchanged-p
344 (around tramp-advice-vc-workfile-unchanged-p
345 (filename &optional want-differences-if-changed)
346 activate)
347 "Invoke tramp-vc-workfile-unchanged-p for tramp files."
348 (if (and (stringp filename)
349 (tramp-tramp-file-p filename)
350 (not
351 (let ((v (tramp-dissect-file-name filename)))
352 ;; The following check is probably to test whether
353 ;; file-attributes returns correct last modification
354 ;; times. This check needs to be changed.
355 (tramp-get-remote-perl (tramp-file-name-multi-method v)
356 (tramp-file-name-method v)
357 (tramp-file-name-user v)
358 (tramp-file-name-host v)))))
359 (setq ad-return-value
360 (tramp-vc-workfile-unchanged-p filename want-differences-if-changed))
361 ad-do-it))
362
363
364 ;; Redefine a function from vc.el -- allow tramp files.
365 ;; `save-match-data' seems not to be required -- it isn't in
366 ;; the original version, either.
367 ;; CCC: this might need some work -- how does the Emacs 21 version
368 ;; work, anyway? Does it work over ange-ftp? Hm.
369 (if (not (fboundp 'vc-backend-checkout))
370 () ;; our replacement won't work and is unnecessary anyway
371 (defun vc-checkout (filename &optional writable rev)
372 "Retrieve a copy of the latest version of the given file."
373 ;; If ftp is on this system and the name matches the ange-ftp format
374 ;; for a remote file, the user is trying something that won't work.
375 (funcall (symbol-function 'vc-backend-checkout) filename writable rev)
376 (vc-resynch-buffer filename t t))
377 )
378
379
380 ;; Do we need to advise the vc-user-login-name function anyway?
381 ;; This will return the correct login name for the owner of a
382 ;; file. It does not deal with the default remote user name...
383 ;;
384 ;; That is, when vc calls (vc-user-login-name), we return the
385 ;; local login name, something that may be different to the remote
386 ;; default.
387 ;;
388 ;; The remote VC operations will occur as the user that we logged
389 ;; in with however - not always the same as the local user.
390 ;;
391 ;; In the end, I did advise the function. This is because, well,
392 ;; the thing didn't work right otherwise ;)
393 ;;
394 ;; Daniel Pittman <daniel@danann.net>
395
396 (defun tramp-handle-vc-user-login-name (&optional uid)
397 "Return the default user name on the remote machine.
398 Whenever VC calls this function, `file' is bound to the file name
399 in question. If no uid is provided or the uid is equal to the uid
400 owning the file, then we return the user name given in the file name.
401
402 This should only be called when `file' is bound to the
403 filename we are thinking about..."
404 ;; Pacify byte-compiler; this symbol is bound in the calling
405 ;; function. CCC: Maybe it would be better to move the
406 ;; boundness-checking into this function?
407 (let* ((file (symbol-value 'file))
408 (remote-uid
409 ;; With Emacs 21.4, `file-attributes' has got an optional parameter
410 ;; ID-FORMAT. Handle this case backwards compatible.
411 (if (and (functionp 'subr-arity)
412 (= 2 (cdr (funcall (symbol-function 'subr-arity)
413 (symbol-function 'file-attributes)))))
414 (nth 2 (file-attributes file 'integer))
415 (nth 2 (file-attributes file)))))
416 (if (and uid (/= uid remote-uid))
417 (error "tramp-handle-vc-user-login-name cannot map a uid to a name")
418 (let* ((v (tramp-dissect-file-name (expand-file-name file)))
419 (u (tramp-file-name-user v)))
420 (cond ((stringp u) u)
421 ((vectorp u) (elt u (1- (length u))))
422 ((null u) (user-login-name))
423 (t (error "tramp-handle-vc-user-login-name cannot cope!")))))))
424
425
426 (defadvice vc-user-login-name
427 (around tramp-vc-user-login-name activate)
428 "Support for files on remote machines accessed by TRAMP."
429 ;; We rely on the fact that `file' is bound when this is called.
430 ;; This appears to be the case everywhere in vc.el and vc-hooks.el
431 ;; as of Emacs 20.5.
432 ;;
433 ;; CCC TODO there should be a real solution! Talk to Andre Spiegel
434 ;; about this.
435 (let ((file (when (boundp 'file)
436 (symbol-value 'file)))) ;pacify byte-compiler
437 (or (and (stringp file)
438 (tramp-tramp-file-p file) ; tramp file
439 (setq ad-return-value
440 (save-match-data
441 (tramp-handle-vc-user-login-name uid)))) ; get the owner name
442 ad-do-it))) ; else call the original
443
444
445 ;; Determine the name of the user owning a file.
446 (defun tramp-file-owner (filename)
447 "Return who owns FILE (user name, as a string)."
448 (let ((v (tramp-dissect-file-name
449 (expand-file-name filename))))
450 (if (not (file-exists-p filename))
451 nil ; file cannot be opened
452 ;; file exists, find out stuff
453 (save-excursion
454 (tramp-send-command
455 (tramp-file-name-multi-method v) (tramp-file-name-method v)
456 (tramp-file-name-user v) (tramp-file-name-host v)
457 (format "%s -Lld %s"
458 (tramp-get-ls-command (tramp-file-name-multi-method v)
459 (tramp-file-name-method v)
460 (tramp-file-name-user v)
461 (tramp-file-name-host v))
462 (tramp-shell-quote-argument (tramp-file-name-localname v))))
463 (tramp-wait-for-output)
464 ;; parse `ls -l' output ...
465 ;; ... file mode flags
466 (read (current-buffer))
467 ;; ... number links
468 (read (current-buffer))
469 ;; ... uid (as a string)
470 (symbol-name (read (current-buffer)))))))
471
472 ;; Wire ourselves into the VC infrastructure...
473 ;; This function does not exist any more in Emacs-21's VC
474 ;; CCC: it appears that no substitute is needed for Emacs 21.
475 (defadvice vc-file-owner
476 (around tramp-vc-file-owner activate)
477 "Support for files on remote machines accessed by TRAMP."
478 (let ((filename (ad-get-arg 0)))
479 (or (and (tramp-file-name-p filename) ; tramp file
480 (setq ad-return-value
481 (save-match-data
482 (tramp-file-owner filename)))) ; get the owner name
483 ad-do-it))) ; else call the original
484
485
486 ;; We need to make the version control software backend version
487 ;; information local to the current buffer. This is because each TRAMP
488 ;; buffer can (theoretically) have a different VC version and I am
489 ;; *way* too lazy to try and push the correct value into each new
490 ;; buffer.
491 ;;
492 ;; Remote VC costs will just have to be paid, at least for the moment.
493 ;; Well, at least, they will right until I feel guilty about doing a
494 ;; botch job here and fix it. :/
495 ;;
496 ;; Daniel Pittman <daniel@danann.net>
497 ;; CCC: this is probably still needed for Emacs 21.
498 (defun tramp-vc-setup-for-remote ()
499 "Make the backend release variables buffer local.
500 This makes remote VC work correctly at the cost of some processing time."
501 (when (and (buffer-file-name)
502 (tramp-tramp-file-p (buffer-file-name)))
503 (make-local-variable 'vc-rcs-release)
504 (setq vc-rcs-release nil)))
505 (add-hook 'find-file-hooks 'tramp-vc-setup-for-remote t)
506
507 ;; No need to load this again if anyone asks.
508 (provide 'tramp-vc)
509
510 ;;; arch-tag: 27cc42ce-da19-468d-ad5c-a2690558db60
511 ;;; tramp-vc.el ends here