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