tramp-vc.el 21.1 KB
Newer Older
Kai Großjohann's avatar
Kai Großjohann committed
1 2
;;; tramp-vc.el --- Version control integration for TRAMP.el

3
;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
Michael Albinus's avatar
Michael Albinus committed
4
;;   2005, 2006 Free Software Foundation, Inc.
Kai Großjohann's avatar
Kai Großjohann committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

;; Author: Daniel Pittman <daniel@danann.net>
;; Keywords: comm, processes

;; This file is part of GNU Emacs.

;; GNU Emacs 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, or (at your option)
;; any later version.

;; GNU Emacs 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 GNU Emacs; see the file COPYING.  If not, write to the
Lute Kamstra's avatar
Lute Kamstra committed
23 24
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
Kai Großjohann's avatar
Kai Großjohann committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

;;; Commentary:

;; See the main module, 'tramp.el' for discussion of the purpose of TRAMP.
;; This module provides integration between remote files accessed by TRAMP and
;; the Emacs version control system.

;;; Code:

(eval-when-compile
  (require 'cl))
(require 'vc)
;; Old VC defines vc-rcs-release in vc.el, new VC requires extra module.
(unless (boundp 'vc-rcs-release)
  (require 'vc-rcs))
(require 'tramp)

42 43 44 45 46 47 48 49
;; Avoid byte-compiler warnings if the byte-compiler supports this.
;; Currently, XEmacs supports this.
(eval-when-compile
  (when (fboundp 'byte-compiler-options)
    (let (unused-vars) ; Pacify Emacs byte-compiler
      (defalias 'warnings 'identity) ; Pacify Emacs byte-compiler
      (byte-compiler-options (warnings (- unused-vars))))))

Kai Großjohann's avatar
Kai Großjohann committed
50 51 52 53 54
;; -- vc --

;; This used to blow away the file-name-handler-alist and reinstall
;; TRAMP into it. This was intended to let VC work remotely. It didn't,
;; at least not in my XEmacs 21.2 install.
55
;;
Kai Großjohann's avatar
Kai Großjohann committed
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
;; In any case, tramp-run-real-handler now deals correctly with disabling
;; the things that should be, making this a no-op.
;;
;; I have removed it from the tramp-file-name-handler-alist because the
;; shortened version does nothing. This is for reference only now.
;;
;; Daniel Pittman <daniel@danann.net>
;;
;; (defun tramp-handle-vc-registered (file)
;;   "Like `vc-registered' for tramp files."
;;   (tramp-run-real-handler 'vc-registered (list file)))

;; `vc-do-command'
;; This function does not deal well with remote files, so we define
;; our own version and make a backup of the original function and
;; call our version for tramp files and the original version for
;; normal files.

;; The following function is pretty much copied from vc.el, but
;; the part that actually executes a command is changed.
;; CCC: this probably works for Emacs 21, too.
(defun tramp-vc-do-command (buffer okstatus command file last &rest flags)
  "Like `vc-do-command' but invoked for tramp files.
See `vc-do-command' for more information."
  (save-match-data
Kai Großjohann's avatar
Kai Großjohann committed
81
    (and file (setq file (expand-file-name file)))
Kai Großjohann's avatar
Kai Großjohann committed
82 83 84 85 86 87 88
    (if (not buffer) (setq buffer "*vc*"))
    (if vc-command-messages
	(message "Running `%s' on `%s'..." command file))
    (let ((obuf (current-buffer)) (camefrom (current-buffer))
	  (squeezed nil)
	  (olddir default-directory)
	  vc-file status)
Kai Großjohann's avatar
Kai Großjohann committed
89
      (let* ((v (tramp-dissect-file-name (expand-file-name file)))
Kai Großjohann's avatar
Kai Großjohann committed
90 91 92 93
	     (multi-method (tramp-file-name-multi-method v))
	     (method (tramp-file-name-method v))
	     (user (tramp-file-name-user v))
	     (host (tramp-file-name-host v))
Kai Großjohann's avatar
Kai Großjohann committed
94
	     (localname (tramp-file-name-localname v)))
Kai Großjohann's avatar
Kai Großjohann committed
95 96 97 98 99
	(set-buffer (get-buffer-create buffer))
	(set (make-local-variable 'vc-parent-buffer) camefrom)
	(set (make-local-variable 'vc-parent-buffer-name)
	     (concat " from " (buffer-name camefrom)))
	(setq default-directory olddir)
100

Kai Großjohann's avatar
Kai Großjohann committed
101 102 103 104 105 106 107 108 109 110
	(erase-buffer)

	(mapcar
	 (function
	  (lambda (s) (and s (setq squeezed (append squeezed (list s))))))
	 flags)
	(if (and (eq last 'MASTER) file
		 (setq vc-file (vc-name file)))
	    (setq squeezed
		  (append squeezed
Kai Großjohann's avatar
Kai Großjohann committed
111
			  (list (tramp-file-name-localname
Kai Großjohann's avatar
Kai Großjohann committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
				 (tramp-dissect-file-name vc-file))))))
	(if (and file (eq last 'WORKFILE))
	    (progn
	      (let* ((pwd (expand-file-name default-directory))
		     (preflen (length pwd)))
		(if (string= (substring file 0 preflen) pwd)
		    (setq file (substring file preflen))))
	      (setq squeezed (append squeezed (list file)))))
	;; Unless we (save-window-excursion) the layout of windows in
	;; the current frame changes. This is painful, at best.
	;;
	;; As a point of note, (save-excursion) is still here only because
	;; it preserves (point) in the current buffer. (save-window-excursion)
	;; does not, at least under XEmacs 21.2.
	;;
	;; I trust that the FSF support this as well. I can't find useful
	;; documentation to check :(
	;;
	;; Daniel Pittman <daniel@danann.net>
	(save-excursion
	  (save-window-excursion
	    ;; Actually execute remote command
Michael Albinus's avatar
Michael Albinus committed
134 135
	    ;; `shell-command' cannot be used; it isn't magic in XEmacs.
	    (tramp-handle-shell-command
Kai Großjohann's avatar
Kai Großjohann committed
136 137 138 139 140 141 142 143 144 145 146 147 148
	     (mapconcat 'tramp-shell-quote-argument
			(cons command squeezed) " ") t)
	    ;;(tramp-wait-for-output)
	    ;; Get status from command
	    (tramp-send-command multi-method method user host "echo $?")
	    (tramp-wait-for-output)
	    ;; Make sure to get status from last line of output.
	    (goto-char (point-max)) (forward-line -1)
	    (setq status (read (current-buffer)))
	    (message "Command %s returned status %d." command status)))
	(goto-char (point-max))
	(set-buffer-modified-p nil)
	(forward-line -1)
149 150
	(if (or (not (integerp status))
		(and (integerp okstatus) (< okstatus status)))
Kai Großjohann's avatar
Kai Großjohann committed
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
	    (progn
	      (pop-to-buffer buffer)
	      (goto-char (point-min))
	      (shrink-window-if-larger-than-buffer)
	      (error "Running `%s'...FAILED (%s)" command
		     (if (integerp status)
			 (format "status %d" status)
		       status))
	      )
	  (if vc-command-messages
	      (message "Running %s...OK" command))
	  )
	(set-buffer obuf)
	status))
    ))

;; Following code snarfed from Emacs 21 vc.el and slightly tweaked.
(defun tramp-vc-do-command-new (buffer okstatus command file &rest flags)
  "Like `vc-do-command' but for TRAMP files.
This function is for the new VC which comes with Emacs 21.
Since TRAMP doesn't do async commands yet, this function doesn't, either."
  (and file (setq file (expand-file-name file)))
  (if vc-command-messages
      (message "Running %s on %s..." command file))
  (save-current-buffer
176 177 178
    (unless (eq buffer t)
      ; Pacify byte-compiler
      (funcall (symbol-function 'vc-setup-buffer) buffer))
Kai Großjohann's avatar
Kai Großjohann committed
179 180 181 182 183 184 185 186
    (let ((squeezed nil)
	  (inhibit-read-only t)
	  (status 0))
      (let* ((v (when file (tramp-dissect-file-name file)))
             (multi-method (when file (tramp-file-name-multi-method v)))
             (method (when file (tramp-file-name-method v)))
             (user (when file (tramp-file-name-user v)))
             (host (when file (tramp-file-name-host v)))
Kai Großjohann's avatar
Kai Großjohann committed
187
             (localname (when file (tramp-file-name-localname v))))
Kai Großjohann's avatar
Kai Großjohann committed
188 189
      (setq squeezed (delq nil (copy-sequence flags)))
      (when file
190 191
	(setq squeezed (append squeezed (list (file-relative-name
					       file default-directory)))))
Kai Großjohann's avatar
Kai Großjohann committed
192 193 194
      (let ((w32-quote-process-args t))
        (when (eq okstatus 'async)
          (message "Tramp doesn't do async commands, running synchronously."))
Michael Albinus's avatar
Michael Albinus committed
195 196
	;; `shell-command' cannot be used; it isn't magic in XEmacs.
        (setq status (tramp-handle-shell-command
Kai Großjohann's avatar
Kai Großjohann committed
197 198
                      (mapconcat 'tramp-shell-quote-argument
                                 (cons command squeezed) " ") t))
199 200
        (when (or (not (integerp status))
		  (and (integerp okstatus) (< okstatus status)))
Kai Großjohann's avatar
Kai Großjohann committed
201 202 203 204 205 206 207
          (pop-to-buffer (current-buffer))
          (goto-char (point-min))
          (shrink-window-if-larger-than-buffer)
          (error "Running %s...FAILED (%s)" command
                 (if (integerp status) (format "status %d" status) status))))
      (if vc-command-messages
          (message "Running %s...OK" command))
208 209 210 211
      ; Pacify byte-compiler
      (funcall (symbol-function 'vc-exec-after)
	       `(run-hook-with-args
		 'vc-post-command-functions ',command ',localname ',flags))
Kai Großjohann's avatar
Kai Großjohann committed
212 213 214 215 216 217 218 219 220 221 222
      status))))


;; The context for a VC command is the current buffer.
;; That makes a test on the buffers file more reliable than a test on the
;; arguments.
;; This is needed to handle remote VC correctly - else we test against the
;; local VC system and get things wrong...
;; Daniel Pittman <daniel@danann.net>
;;-(if (fboundp 'vc-call-backend)
;;-    () ;; This is the new VC for which we don't have an appropriate advice yet
Michael Albinus's avatar
Michael Albinus committed
223
;;-)
224
(unless (fboundp 'process-file)
Michael Albinus's avatar
Michael Albinus committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238
  (if (fboundp 'vc-call-backend)
      (defadvice vc-do-command
	(around tramp-advice-vc-do-command
		(buffer okstatus command file &rest flags)
		activate)
	"Invoke tramp-vc-do-command for tramp files."
	(let ((file (symbol-value 'file)))    ;pacify byte-compiler
	  (if (or (and (stringp file)     (tramp-tramp-file-p file))
		  (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name))))
	      (setq ad-return-value
		    (apply 'tramp-vc-do-command-new buffer okstatus command
			   file ;(or file (buffer-file-name))
			   flags))
	    ad-do-it)))
Kai Großjohann's avatar
Kai Großjohann committed
239 240
    (defadvice vc-do-command
      (around tramp-advice-vc-do-command
Michael Albinus's avatar
Michael Albinus committed
241 242
	      (buffer okstatus command file last &rest flags)
	      activate)
Kai Großjohann's avatar
Kai Großjohann committed
243
      "Invoke tramp-vc-do-command for tramp files."
Michael Albinus's avatar
Michael Albinus committed
244 245 246 247 248 249 250 251 252 253
      (let ((file (symbol-value 'file)))  ;pacify byte-compiler
	(if (or (and (stringp file)     (tramp-tramp-file-p file))
		(and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name))))
	    (setq ad-return-value
		  (apply 'tramp-vc-do-command buffer okstatus command
			 (or file (buffer-file-name)) last flags))
	  ad-do-it))))

  (add-hook 'tramp-unload-hook
	    '(lambda () (ad-unadvise 'vc-do-command))))
Kai Großjohann's avatar
Kai Großjohann committed
254 255 256 257 258 259 260 261 262 263 264 265 266


;; XEmacs uses this to do some of its work. Like vc-do-command, we
;; need to enhance it to make VC work via TRAMP-mode.
;;
;; Like the previous function, this is a cut-and-paste job from the VC
;; file. It's based on the vc-do-command code.
;; CCC: this isn't used in Emacs 21, so do as before.
(defun tramp-vc-simple-command (okstatus command file &rest args)
  ;; Simple version of vc-do-command, for use in vc-hooks only.
  ;; Don't switch to the *vc-info* buffer before running the
  ;; command, because that would change its default directory
  (save-match-data
Kai Großjohann's avatar
Kai Großjohann committed
267
    (let* ((v (tramp-dissect-file-name (expand-file-name file)))
Kai Großjohann's avatar
Kai Großjohann committed
268 269 270 271
	   (multi-method (tramp-file-name-multi-method v))
	   (method (tramp-file-name-method v))
	   (user (tramp-file-name-user v))
	   (host (tramp-file-name-host v))
Kai Großjohann's avatar
Kai Großjohann committed
272
	   (localname (tramp-file-name-localname v)))
Kai Großjohann's avatar
Kai Großjohann committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
      (save-excursion (set-buffer (get-buffer-create "*vc-info*"))
		      (erase-buffer))
      (let ((exec-path (append vc-path exec-path)) exec-status
	    ;; Add vc-path to PATH for the execution of this command.
	    (process-environment
	     (cons (concat "PATH=" (getenv "PATH")
			   path-separator
			   (mapconcat 'identity vc-path path-separator))
		   process-environment)))
	;; Call the actual process. See tramp-vc-do-command for discussion of
	;; why this does both (save-window-excursion) and (save-excursion).
	;;
	;; As a note, I don't think that the process-environment stuff above
	;; has any effect on the remote system. This is a hard one though as
	;; there is no real reason to expect local and remote paths to be
	;; identical...
	;;
	;; Daniel Pittman <daniel@danann.net>
	(save-excursion
	  (save-window-excursion
	    ;; Actually execute remote command
Michael Albinus's avatar
Michael Albinus committed
294 295
	    ;; `shell-command' cannot be used; it isn't magic in XEmacs.
	    (tramp-handle-shell-command
Kai Großjohann's avatar
Kai Großjohann committed
296
	     (mapconcat 'tramp-shell-quote-argument
Kai Großjohann's avatar
Kai Großjohann committed
297
			(append (list command) args (list localname)) " ")
Kai Großjohann's avatar
Kai Großjohann committed
298 299 300 301 302 303 304
	     (get-buffer-create"*vc-info*"))
					;(tramp-wait-for-output)
	    ;; Get status from command
	    (tramp-send-command multi-method method user host "echo $?")
	    (tramp-wait-for-output)
	    (setq exec-status (read (current-buffer)))
	    (message "Command %s returned status %d." command exec-status)))
305

306 307 308
	;; Maybe okstatus can be `async' here.  But then, maybe the
	;; async thing is new in Emacs 21, but this function is only
	;; used in Emacs 20.
Kai Großjohann's avatar
Kai Großjohann committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
	(cond ((> exec-status okstatus)
	       (switch-to-buffer (get-file-buffer file))
	       (shrink-window-if-larger-than-buffer
		(display-buffer "*vc-info*"))
	       (error "Couldn't find version control information")))
	exec-status))))

;; This function does not exist any more in Emacs-21's VC
(defadvice vc-simple-command
  (around tramp-advice-vc-simple-command
	  (okstatus command file &rest args)
	  activate)
  "Invoke tramp-vc-simple-command for tramp files."
  (let ((file (symbol-value 'file)))    ;pacify byte-compiler
    (if (or (and (stringp file)     (tramp-tramp-file-p file))
            (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name))))
        (setq ad-return-value
326
              (apply 'tramp-vc-simple-command okstatus command
Kai Großjohann's avatar
Kai Großjohann committed
327 328 329
                     (or file (buffer-file-name)) args))
      ad-do-it)))

Michael Albinus's avatar
Michael Albinus committed
330 331 332
(add-hook 'tramp-unload-hook
	  '(lambda () (ad-unadvise 'vc-simple-command)))

Kai Großjohann's avatar
Kai Großjohann committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

;; `vc-workfile-unchanged-p'
;; This function does not deal well with remote files, so we do the
;; same as for `vc-do-command'.

;; `vc-workfile-unchanged-p' checks the modification time, we cannot
;; do that for remote files, so here's a version which relies on diff.
;; CCC: this one probably works for Emacs 21, too.
(defun tramp-vc-workfile-unchanged-p
  (filename &optional want-differences-if-changed)
  (if (fboundp 'vc-backend-diff)
      ;; Old VC.  Call `vc-backend-diff'.
      (let ((status (funcall (symbol-function 'vc-backend-diff)
                             filename nil nil
                             (not want-differences-if-changed))))
        (zerop status))
    ;; New VC.  Call `vc-default-workfile-unchanged-p'.
350 351
      (funcall (symbol-function 'vc-default-workfile-unchanged-p)
	       (vc-backend filename) filename)))
Kai Großjohann's avatar
Kai Großjohann committed
352 353 354 355 356 357 358 359 360 361

(defadvice vc-workfile-unchanged-p
  (around tramp-advice-vc-workfile-unchanged-p
          (filename &optional want-differences-if-changed)
          activate)
  "Invoke tramp-vc-workfile-unchanged-p for tramp files."
  (if (and (stringp filename)
	   (tramp-tramp-file-p filename)
	   (not
	    (let ((v	(tramp-dissect-file-name filename)))
Kai Großjohann's avatar
Kai Großjohann committed
362 363 364
	      ;; The following check is probably to test whether
	      ;; file-attributes returns correct last modification
	      ;; times.  This check needs to be changed.
Kai Großjohann's avatar
Kai Großjohann committed
365 366 367 368 369 370 371 372
	      (tramp-get-remote-perl (tramp-file-name-multi-method v)
				   (tramp-file-name-method v)
				   (tramp-file-name-user v)
				   (tramp-file-name-host v)))))
      (setq ad-return-value
            (tramp-vc-workfile-unchanged-p filename want-differences-if-changed))
    ad-do-it))

Michael Albinus's avatar
Michael Albinus committed
373 374 375
(add-hook 'tramp-unload-hook
	  '(lambda () (ad-unadvise 'vc-workfile-unchanged-p)))

Kai Großjohann's avatar
Kai Großjohann committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393

;; Redefine a function from vc.el -- allow tramp files.
;; `save-match-data' seems not to be required -- it isn't in
;; the original version, either.
;; CCC: this might need some work -- how does the Emacs 21 version
;; work, anyway?  Does it work over ange-ftp?  Hm.
(if (not (fboundp 'vc-backend-checkout))
    () ;; our replacement won't work and is unnecessary anyway
(defun vc-checkout (filename &optional writable rev)
  "Retrieve a copy of the latest version of the given file."
  ;; If ftp is on this system and the name matches the ange-ftp format
  ;; for a remote file, the user is trying something that won't work.
  (funcall (symbol-function 'vc-backend-checkout) filename writable rev)
  (vc-resynch-buffer filename t t))
)


;; Do we need to advise the vc-user-login-name function anyway?
394
;; This will return the correct login name for the owner of a
Kai Großjohann's avatar
Kai Großjohann committed
395 396
;; file. It does not deal with the default remote user name...
;;
397
;; That is, when vc calls (vc-user-login-name), we return the
Kai Großjohann's avatar
Kai Großjohann committed
398
;; local login name, something that may be different to the remote
399
;; default.
Kai Großjohann's avatar
Kai Großjohann committed
400 401 402 403
;;
;; The remote VC operations will occur as the user that we logged
;; in with however - not always the same as the local user.
;;
404
;; In the end, I did advise the function. This is because, well,
Kai Großjohann's avatar
Kai Großjohann committed
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
;; the thing didn't work right otherwise ;)
;;
;; Daniel Pittman <daniel@danann.net>

(defun tramp-handle-vc-user-login-name (&optional uid)
  "Return the default user name on the remote machine.
Whenever VC calls this function, `file' is bound to the file name
in question.  If no uid is provided or the uid is equal to the uid
owning the file, then we return the user name given in the file name.

This should only be called when `file' is bound to the
filename we are thinking about..."
  ;; Pacify byte-compiler; this symbol is bound in the calling
  ;; function.  CCC: Maybe it would be better to move the
  ;; boundness-checking into this function?
420 421
  (let* ((file (symbol-value 'file))
	 (remote-uid
Michael Albinus's avatar
Michael Albinus committed
422
	  ;; With Emacs 22, `file-attributes' has got an optional parameter
423 424 425 426 427 428
	  ;; ID-FORMAT. Handle this case backwards compatible.
	  (if (and (functionp 'subr-arity)
		   (= 2 (cdr (funcall (symbol-function 'subr-arity)
				      (symbol-function 'file-attributes)))))
	      (nth 2 (file-attributes file 'integer))
	    (nth 2 (file-attributes file)))))
429
    (if (and uid (/= uid remote-uid))
Kai Großjohann's avatar
Kai Großjohann committed
430
	(error "tramp-handle-vc-user-login-name cannot map a uid to a name")
Kai Großjohann's avatar
Kai Großjohann committed
431
      (let* ((v (tramp-dissect-file-name (expand-file-name file)))
Kai Großjohann's avatar
Kai Großjohann committed
432 433 434 435 436 437 438
	     (u (tramp-file-name-user v)))
	(cond ((stringp u) u)
	      ((vectorp u) (elt u (1- (length u))))
	      ((null    u) (user-login-name))
	      (t	   (error "tramp-handle-vc-user-login-name cannot cope!")))))))


439 440 441
;; The following defadvice is no longer necessary after changes in VC
;; on 2006-01-25, Andre.

442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
(unless (fboundp 'process-file)
  (defadvice vc-user-login-name
    (around tramp-vc-user-login-name activate)
    "Support for files on remote machines accessed by TRAMP."
    ;; We rely on the fact that `file' is bound when this is called.
    ;; This appears to be the case everywhere in vc.el and vc-hooks.el
    ;; as of Emacs 20.5.
    ;;
    ;; With Emacs 22, the definition of `vc-user-login-name' has been
    ;; changed.  It doesn't need to be adviced any longer.
    (let ((file (when (boundp 'file)
		  (symbol-value 'file))))    ;pacify byte-compiler
      (or (and (stringp file)
	       (tramp-tramp-file-p file)	; tramp file
	       (setq ad-return-value
		     (save-match-data
		       (tramp-handle-vc-user-login-name uid)))) ; get the owner name
	  ad-do-it)))                     ; else call the original
Kai Großjohann's avatar
Kai Großjohann committed
460

461 462
  (add-hook 'tramp-unload-hook
	    '(lambda () (ad-unadvise 'vc-user-login-name))))
Michael Albinus's avatar
Michael Albinus committed
463

464

Kai Großjohann's avatar
Kai Großjohann committed
465 466 467
;; Determine the name of the user owning a file.
(defun tramp-file-owner (filename)
  "Return who owns FILE (user name, as a string)."
468
  (let ((v (tramp-dissect-file-name
Kai Großjohann's avatar
Kai Großjohann committed
469 470
	    (expand-file-name filename))))
    (if (not (file-exists-p filename))
Kai Großjohann's avatar
Kai Großjohann committed
471 472 473 474 475 476 477 478 479 480 481
        nil                             ; file cannot be opened
      ;; file exists, find out stuff
      (save-excursion
        (tramp-send-command
         (tramp-file-name-multi-method v) (tramp-file-name-method v)
         (tramp-file-name-user v) (tramp-file-name-host v)
         (format "%s -Lld %s"
                 (tramp-get-ls-command (tramp-file-name-multi-method v)
                                     (tramp-file-name-method v)
                                     (tramp-file-name-user v)
                                     (tramp-file-name-host v))
Kai Großjohann's avatar
Kai Großjohann committed
482
                 (tramp-shell-quote-argument (tramp-file-name-localname v))))
Kai Großjohann's avatar
Kai Großjohann committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
        (tramp-wait-for-output)
        ;; parse `ls -l' output ...
        ;; ... file mode flags
        (read (current-buffer))
        ;; ... number links
        (read (current-buffer))
        ;; ... uid (as a string)
        (symbol-name (read (current-buffer)))))))

;; Wire ourselves into the VC infrastructure...
;; This function does not exist any more in Emacs-21's VC
;; CCC: it appears that no substitute is needed for Emacs 21.
(defadvice vc-file-owner
  (around tramp-vc-file-owner activate)
  "Support for files on remote machines accessed by TRAMP."
  (let ((filename (ad-get-arg 0)))
    (or (and (tramp-file-name-p filename) ; tramp file
             (setq ad-return-value
		   (save-match-data
		     (tramp-file-owner filename)))) ; get the owner name
        ad-do-it)))                     ; else call the original

Michael Albinus's avatar
Michael Albinus committed
505 506 507
(add-hook 'tramp-unload-hook
	  '(lambda () (ad-unadvise 'vc-file-owner)))

Kai Großjohann's avatar
Kai Großjohann committed
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527

;; We need to make the version control software backend version
;; information local to the current buffer. This is because each TRAMP
;; buffer can (theoretically) have a different VC version and I am
;; *way* too lazy to try and push the correct value into each new
;; buffer.
;;
;; Remote VC costs will just have to be paid, at least for the moment.
;; Well, at least, they will right until I feel guilty about doing a
;; botch job here and fix it. :/
;;
;; Daniel Pittman <daniel@danann.net>
;; CCC: this is probably still needed for Emacs 21.
(defun tramp-vc-setup-for-remote ()
  "Make the backend release variables buffer local.
This makes remote VC work correctly at the cost of some processing time."
  (when (and (buffer-file-name)
             (tramp-tramp-file-p (buffer-file-name)))
    (make-local-variable 'vc-rcs-release)
    (setq vc-rcs-release nil)))
Michael Albinus's avatar
Michael Albinus committed
528

Kai Großjohann's avatar
Kai Großjohann committed
529
(add-hook 'find-file-hooks 'tramp-vc-setup-for-remote t)
Michael Albinus's avatar
Michael Albinus committed
530 531 532
(add-hook 'tramp-unload-hook
	  '(lambda ()
	     (remove-hook 'find-file-hooks 'tramp-vc-setup-for-remote)))
Kai Großjohann's avatar
Kai Großjohann committed
533 534 535 536

;; No need to load this again if anyone asks.
(provide 'tramp-vc)

Miles Bader's avatar
Miles Bader committed
537
;;; arch-tag: 27cc42ce-da19-468d-ad5c-a2690558db60
Kai Großjohann's avatar
Kai Großjohann committed
538
;;; tramp-vc.el ends here