]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-vc.el
(ifconfig): Renamed from ipconfig.
[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, 2005, 2006,
4 ;; 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Daniel Pittman <daniel@danann.net>
7 ;; Keywords: comm, processes
8
9 ;; This file is part of GNU Emacs.
10
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 3, or (at your option)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
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.
31
32 ;;; Code:
33
34 (require 'vc)
35 ;; Old VC defines vc-rcs-release in vc.el, new VC requires extra module.
36 (unless (boundp 'vc-rcs-release)
37 (require 'vc-rcs))
38 (require 'tramp)
39
40 ;; Avoid byte-compiler warnings if the byte-compiler supports this.
41 ;; Currently, XEmacs supports this.
42 (eval-when-compile
43 (when (fboundp 'byte-compiler-options)
44 (let (unused-vars) ; Pacify Emacs byte-compiler
45 (defalias 'warnings 'identity) ; Pacify Emacs byte-compiler
46 (byte-compiler-options (warnings (- unused-vars))))))
47
48 ;; -- vc --
49
50 ;; This used to blow away the file-name-handler-alist and reinstall
51 ;; TRAMP into it. This was intended to let VC work remotely. It didn't,
52 ;; at least not in my XEmacs 21.2 install.
53 ;;
54 ;; In any case, tramp-run-real-handler now deals correctly with disabling
55 ;; the things that should be, making this a no-op.
56 ;;
57 ;; I have removed it from the tramp-file-name-handler-alist because the
58 ;; shortened version does nothing. This is for reference only now.
59 ;;
60 ;; Daniel Pittman <daniel@danann.net>
61 ;;
62 ;; (defun tramp-handle-vc-registered (file)
63 ;; "Like `vc-registered' for tramp files."
64 ;; (tramp-run-real-handler 'vc-registered (list file)))
65
66 ;; `vc-do-command'
67 ;; This function does not deal well with remote files, so we define
68 ;; our own version and make a backup of the original function and
69 ;; call our version for tramp files and the original version for
70 ;; normal files.
71
72 ;; The following function is pretty much copied from vc.el, but
73 ;; the part that actually executes a command is changed.
74 ;; CCC: this probably works for Emacs 21, too.
75 (defun tramp-vc-do-command (buffer okstatus command file last &rest flags)
76 "Like `vc-do-command' but invoked for tramp files.
77 See `vc-do-command' for more information."
78 (save-match-data
79 (and file (setq file (expand-file-name file)))
80 (if (not buffer) (setq buffer "*vc*"))
81 (if vc-command-messages
82 (message "Running `%s' on `%s'..." command file))
83 (let ((obuf (current-buffer)) (camefrom (current-buffer))
84 (squeezed nil)
85 (olddir default-directory)
86 vc-file status)
87 (let* ((v (tramp-dissect-file-name (expand-file-name file)))
88 (multi-method (tramp-file-name-multi-method v))
89 (method (tramp-file-name-method v))
90 (user (tramp-file-name-user v))
91 (host (tramp-file-name-host v))
92 (localname (tramp-file-name-localname v)))
93 (set-buffer (get-buffer-create buffer))
94 (set (make-local-variable 'vc-parent-buffer) camefrom)
95 (set (make-local-variable 'vc-parent-buffer-name)
96 (concat " from " (buffer-name camefrom)))
97 (setq default-directory olddir)
98
99 (erase-buffer)
100
101 (mapc
102 (lambda (s) (and s (setq squeezed (append squeezed (list s)))))
103 flags)
104 (if (and (eq last 'MASTER) file
105 (setq vc-file (vc-name file)))
106 (setq squeezed
107 (append squeezed
108 (list (tramp-file-name-localname
109 (tramp-dissect-file-name vc-file))))))
110 (if (and file (eq last 'WORKFILE))
111 (progn
112 (let* ((pwd (expand-file-name default-directory))
113 (preflen (length pwd)))
114 (if (string= (substring file 0 preflen) pwd)
115 (setq file (substring file preflen))))
116 (setq squeezed (append squeezed (list file)))))
117 ;; Unless we (save-window-excursion) the layout of windows in
118 ;; the current frame changes. This is painful, at best.
119 ;;
120 ;; As a point of note, (save-excursion) is still here only because
121 ;; it preserves (point) in the current buffer. (save-window-excursion)
122 ;; does not, at least under XEmacs 21.2.
123 ;;
124 ;; I trust that the FSF support this as well. I can't find useful
125 ;; documentation to check :(
126 ;;
127 ;; Daniel Pittman <daniel@danann.net>
128 (save-excursion
129 (save-window-excursion
130 ;; Actually execute remote command
131 ;; `shell-command' cannot be used; it isn't magic in XEmacs.
132 (tramp-handle-shell-command
133 (mapconcat 'tramp-shell-quote-argument
134 (cons command squeezed) " ") t)
135 ;;(tramp-wait-for-output)
136 ;; Get status from command
137 (tramp-send-command multi-method method user host "echo $?")
138 (tramp-wait-for-output)
139 ;; Make sure to get status from last line of output.
140 (goto-char (point-max)) (forward-line -1)
141 (setq status (read (current-buffer)))
142 (message "Command %s returned status %d." command status)))
143 (goto-char (point-max))
144 (set-buffer-modified-p nil)
145 (forward-line -1)
146 (if (or (not (integerp status))
147 (and (integerp okstatus) (< okstatus status)))
148 (progn
149 (pop-to-buffer buffer)
150 (goto-char (point-min))
151 (shrink-window-if-larger-than-buffer)
152 (error "Running `%s'...FAILED (%s)" command
153 (if (integerp status)
154 (format "status %d" status)
155 status))
156 )
157 (if vc-command-messages
158 (message "Running %s...OK" command))
159 )
160 (set-buffer obuf)
161 status))
162 ))
163
164 ;; Following code snarfed from Emacs 21 vc.el and slightly tweaked.
165 (defun tramp-vc-do-command-new (buffer okstatus command file &rest flags)
166 "Like `vc-do-command' but for TRAMP files.
167 This function is for the new VC which comes with Emacs 21.
168 Since TRAMP doesn't do async commands yet, this function doesn't, either."
169 (and file (setq file (expand-file-name file)))
170 (if vc-command-messages
171 (message "Running %s on %s..." command file))
172 (save-current-buffer
173 (unless (eq buffer t)
174 ; Pacify byte-compiler
175 (funcall (symbol-function 'vc-setup-buffer) buffer))
176 (let ((squeezed nil)
177 (inhibit-read-only t)
178 (status 0))
179 (let* ((v (when file (tramp-dissect-file-name file)))
180 (multi-method (when file (tramp-file-name-multi-method v)))
181 (method (when file (tramp-file-name-method v)))
182 (user (when file (tramp-file-name-user v)))
183 (host (when file (tramp-file-name-host v)))
184 (localname (when file (tramp-file-name-localname v))))
185 (setq squeezed (delq nil (copy-sequence flags)))
186 (when file
187 (setq squeezed (append squeezed (list (file-relative-name
188 file default-directory)))))
189 (let ((w32-quote-process-args t))
190 (when (eq okstatus 'async)
191 (message "Tramp doesn't do async commands, running synchronously."))
192 ;; `shell-command' cannot be used; it isn't magic in XEmacs.
193 (setq status (tramp-handle-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 ;;-)
221 (unless (fboundp 'process-file)
222 (if (fboundp 'vc-call-backend)
223 (defadvice vc-do-command
224 (around tramp-advice-vc-do-command
225 (buffer okstatus command file &rest flags)
226 activate)
227 "Invoke tramp-vc-do-command for tramp files."
228 (let ((file (symbol-value 'file))) ;pacify byte-compiler
229 (if (or (and (stringp file) (tramp-tramp-file-p file))
230 (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name))))
231 (setq ad-return-value
232 (apply 'tramp-vc-do-command-new buffer okstatus command
233 file ;(or file (buffer-file-name))
234 flags))
235 ad-do-it)))
236 (defadvice vc-do-command
237 (around tramp-advice-vc-do-command
238 (buffer okstatus command file last &rest flags)
239 activate)
240 "Invoke tramp-vc-do-command for tramp files."
241 (let ((file (symbol-value 'file))) ;pacify byte-compiler
242 (if (or (and (stringp file) (tramp-tramp-file-p file))
243 (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name))))
244 (setq ad-return-value
245 (apply 'tramp-vc-do-command buffer okstatus command
246 (or file (buffer-file-name)) last flags))
247 ad-do-it))))
248
249 (add-hook 'tramp-unload-hook
250 '(lambda () (ad-unadvise 'vc-do-command))))
251
252
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.
255 ;;
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).
281 ;;
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...
286 ;;
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)))
302
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))))
312
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)))
326
327 (add-hook 'tramp-unload-hook
328 '(lambda () (ad-unadvise 'vc-simple-command)))
329
330
331 ;; `vc-workfile-unchanged-p'
332 ;; This function does not deal well with remote files, so we do the
333 ;; same as for `vc-do-command'.
334
335 ;; `vc-workfile-unchanged-p' checks the modification time, we cannot
336 ;; do that for remote files, so here's a version which relies on diff.
337 ;; CCC: this one probably works for Emacs 21, too.
338 (defun tramp-vc-workfile-unchanged-p
339 (filename &optional want-differences-if-changed)
340 (if (fboundp 'vc-backend-diff)
341 ;; Old VC. Call `vc-backend-diff'.
342 (let ((status (funcall (symbol-function 'vc-backend-diff)
343 filename nil nil
344 (not want-differences-if-changed))))
345 (zerop status))
346 ;; New VC. Call `vc-default-workfile-unchanged-p'.
347 (funcall (symbol-function 'vc-default-workfile-unchanged-p)
348 (vc-backend filename) filename)))
349
350 (defadvice vc-workfile-unchanged-p
351 (around tramp-advice-vc-workfile-unchanged-p
352 (filename &optional want-differences-if-changed)
353 activate)
354 "Invoke tramp-vc-workfile-unchanged-p for tramp files."
355 (if (and (stringp filename)
356 (tramp-tramp-file-p filename)
357 (not
358 (let ((v (tramp-dissect-file-name filename)))
359 ;; The following check is probably to test whether
360 ;; file-attributes returns correct last modification
361 ;; times. This check needs to be changed.
362 (tramp-get-remote-perl (tramp-file-name-multi-method v)
363 (tramp-file-name-method v)
364 (tramp-file-name-user v)
365 (tramp-file-name-host v)))))
366 (setq ad-return-value
367 (tramp-vc-workfile-unchanged-p filename want-differences-if-changed))
368 ad-do-it))
369
370 (add-hook 'tramp-unload-hook
371 '(lambda () (ad-unadvise 'vc-workfile-unchanged-p)))
372
373
374 ;; Redefine a function from vc.el -- allow tramp files.
375 ;; `save-match-data' seems not to be required -- it isn't in
376 ;; the original version, either.
377 ;; CCC: this might need some work -- how does the Emacs 21 version
378 ;; work, anyway? Does it work over ange-ftp? Hm.
379 (if (not (fboundp 'vc-backend-checkout))
380 () ;; our replacement won't work and is unnecessary anyway
381 (defun vc-checkout (filename &optional writable rev)
382 "Retrieve a copy of the latest version of the given file."
383 ;; If ftp is on this system and the name matches the ange-ftp format
384 ;; for a remote file, the user is trying something that won't work.
385 (funcall (symbol-function 'vc-backend-checkout) filename writable rev)
386 (vc-resynch-buffer filename t t))
387 )
388
389
390 ;; Do we need to advise the vc-user-login-name function anyway?
391 ;; This will return the correct login name for the owner of a
392 ;; file. It does not deal with the default remote user name...
393 ;;
394 ;; That is, when vc calls (vc-user-login-name), we return the
395 ;; local login name, something that may be different to the remote
396 ;; default.
397 ;;
398 ;; The remote VC operations will occur as the user that we logged
399 ;; in with however - not always the same as the local user.
400 ;;
401 ;; In the end, I did advise the function. This is because, well,
402 ;; the thing didn't work right otherwise ;)
403 ;;
404 ;; Daniel Pittman <daniel@danann.net>
405
406 (defun tramp-handle-vc-user-login-name (&optional uid)
407 "Return the default user name on the remote machine.
408 Whenever VC calls this function, `file' is bound to the file name
409 in question. If no uid is provided or the uid is equal to the uid
410 owning the file, then we return the user name given in the file name.
411
412 This should only be called when `file' is bound to the
413 filename we are thinking about..."
414 ;; Pacify byte-compiler; this symbol is bound in the calling
415 ;; function. CCC: Maybe it would be better to move the
416 ;; boundness-checking into this function?
417 (let* ((file (symbol-value 'file))
418 (remote-uid
419 ;; With Emacs 22, `file-attributes' has got an optional parameter
420 ;; ID-FORMAT. Handle this case backwards compatible.
421 (if (and (functionp 'subr-arity)
422 (= 2 (cdr (funcall (symbol-function 'subr-arity)
423 (symbol-function 'file-attributes)))))
424 (nth 2 (file-attributes file 'integer))
425 (nth 2 (file-attributes file)))))
426 (if (and uid (/= uid remote-uid))
427 (error "tramp-handle-vc-user-login-name cannot map a uid to a name")
428 (let* ((v (tramp-dissect-file-name (expand-file-name file)))
429 (u (tramp-file-name-user v)))
430 (cond ((stringp u) u)
431 ((vectorp u) (elt u (1- (length u))))
432 ((null u) (user-login-name))
433 (t (error "tramp-handle-vc-user-login-name cannot cope!")))))))
434
435
436 ;; The following defadvice is no longer necessary after changes in VC
437 ;; on 2006-01-25, Andre.
438
439 ;; That means either GNU Emacs >= 22 or the "new vc" package from XEmacs
440 ;; packages collection; as of 2007-09-06, test for availability of
441 ;; `vc-find-version' works for both of those cases.
442 (unless (fboundp 'vc-find-version)
443 (defadvice vc-user-login-name
444 (around tramp-vc-user-login-name activate)
445 "Support for files on remote machines accessed by TRAMP."
446 ;; We rely on the fact that `file' is bound when this is called.
447 ;; This appears to be the case everywhere in vc.el and vc-hooks.el
448 ;; as of Emacs 20.5.
449 ;;
450 ;; With Emacs 22, the definition of `vc-user-login-name' has been
451 ;; changed. It doesn't need to be adviced any longer.
452 (let ((file (when (boundp 'file)
453 (symbol-value 'file)))) ;pacify byte-compiler
454 (or (and (stringp file)
455 (tramp-tramp-file-p file) ; tramp file
456 (setq ad-return-value
457 (save-match-data
458 (tramp-handle-vc-user-login-name (ad-get-arg 0))))) ; get the owner name
459 ad-do-it))) ; else call the original
460
461 (add-hook 'tramp-unload-hook
462 '(lambda () (ad-unadvise 'vc-user-login-name))))
463
464
465 ;; Determine the name of the user owning a file.
466 (defun tramp-file-owner (filename)
467 "Return who owns FILE (user name, as a string)."
468 (let ((v (tramp-dissect-file-name
469 (expand-file-name filename))))
470 (if (not (file-exists-p filename))
471 nil ; file cannot be opened
472 ;; file exists, find out stuff
473 (save-excursion
474 (tramp-send-command
475 (tramp-file-name-multi-method v) (tramp-file-name-method v)
476 (tramp-file-name-user v) (tramp-file-name-host v)
477 (format "%s -Lld %s"
478 (tramp-get-ls-command (tramp-file-name-multi-method v)
479 (tramp-file-name-method v)
480 (tramp-file-name-user v)
481 (tramp-file-name-host v))
482 (tramp-shell-quote-argument (tramp-file-name-localname v))))
483 (tramp-wait-for-output)
484 ;; parse `ls -l' output ...
485 ;; ... file mode flags
486 (read (current-buffer))
487 ;; ... number links
488 (read (current-buffer))
489 ;; ... uid (as a string)
490 (symbol-name (read (current-buffer)))))))
491
492 ;; Wire ourselves into the VC infrastructure...
493 ;; This function does not exist any more in Emacs-21's VC
494 ;; CCC: it appears that no substitute is needed for Emacs 21.
495 (defadvice vc-file-owner
496 (around tramp-vc-file-owner activate)
497 "Support for files on remote machines accessed by TRAMP."
498 (let ((filename (ad-get-arg 0)))
499 (or (and (tramp-file-name-p filename) ; tramp file
500 (setq ad-return-value
501 (save-match-data
502 (tramp-file-owner filename)))) ; get the owner name
503 ad-do-it))) ; else call the original
504
505 (add-hook 'tramp-unload-hook
506 '(lambda () (ad-unadvise 'vc-file-owner)))
507
508
509 ;; We need to make the version control software backend version
510 ;; information local to the current buffer. This is because each TRAMP
511 ;; buffer can (theoretically) have a different VC version and I am
512 ;; *way* too lazy to try and push the correct value into each new
513 ;; buffer.
514 ;;
515 ;; Remote VC costs will just have to be paid, at least for the moment.
516 ;; Well, at least, they will right until I feel guilty about doing a
517 ;; botch job here and fix it. :/
518 ;;
519 ;; Daniel Pittman <daniel@danann.net>
520 ;; CCC: this is probably still needed for Emacs 21.
521 (defun tramp-vc-setup-for-remote ()
522 "Make the backend release variables buffer local.
523 This makes remote VC work correctly at the cost of some processing time."
524 (when (and (buffer-file-name)
525 (tramp-tramp-file-p (buffer-file-name)))
526 (make-local-variable 'vc-rcs-release)
527 (setq vc-rcs-release nil)))
528
529 (add-hook 'find-file-hooks 'tramp-vc-setup-for-remote t)
530 (add-hook 'tramp-unload-hook
531 '(lambda ()
532 (remove-hook 'find-file-hooks 'tramp-vc-setup-for-remote)))
533
534 ;; No need to load this again if anyone asks.
535 (provide 'tramp-vc)
536
537 ;;; arch-tag: 27cc42ce-da19-468d-ad5c-a2690558db60
538 ;;; tramp-vc.el ends here