- (let ((name-and-type (vc-registered file)))
- (if name-and-type
- (progn
- (vc-file-setprop file 'vc-backend (cdr name-and-type))
- (vc-file-setprop file 'vc-name (car name-and-type)))))))
-
-(defun vc-backend (file)
- "Return the version-control type of a file, nil if it is not registered."
- (and file
- (or (vc-file-getprop file 'vc-backend)
- (let ((name-and-type (vc-registered file)))
- (if name-and-type
- (progn
- (vc-file-setprop file 'vc-name (car name-and-type))
- (vc-file-setprop file 'vc-backend (cdr name-and-type))))))))
-
-;;; properties indicating the locking state
-
-(defun vc-cvs-status (file)
- ;; Return the cvs status of FILE
- ;; (Status field in output of "cvs status")
- (cond ((vc-file-getprop file 'vc-cvs-status))
- (t (vc-fetch-master-properties file)
- (vc-file-getprop file 'vc-cvs-status))))
-
-(defun vc-master-locks (file)
- ;; Return the lock entries in the master of FILE.
- ;; Return 'none if there are no such entries, and a list
- ;; of the form ((VERSION USER) (VERSION USER) ...) otherwise.
- (cond ((vc-file-getprop file 'vc-master-locks))
- (t (vc-fetch-master-properties file)
- (vc-file-getprop file 'vc-master-locks))))
-
-(defun vc-master-locking-user (file)
- ;; Return the master file's idea of who is locking
- ;; the current workfile version of FILE.
- ;; Return 'none if it is not locked.
- (let ((master-locks (vc-master-locks file)) lock)
- (if (eq master-locks 'none) 'none
- ;; search for a lock on the current workfile version
- (setq lock (assoc (vc-workfile-version file) master-locks))
- (cond (lock (cdr lock))
- ('none)))))
-
-(defun vc-locking-user (file)
- ;; Return the name of the person currently holding a lock on FILE.
- ;; Return nil if there is no such person.
- ;; Under CVS, a file is considered locked if it has been modified since
- ;; it was checked out. Under CVS, this will sometimes return the uid of
- ;; the owner of the file (as a number) instead of a string.
- ;; The property is cached. It is only looked up if it is currently nil.
- ;; Note that, for a file that is not locked, the actual property value
- ;; is 'none, to distinguish it from an unknown locking state. That value
- ;; is converted to nil by this function, and returned to the caller.
- (let ((locking-user (vc-file-getprop file 'vc-locking-user)))
- (if locking-user
- ;; if we already know the property, return it
- (if (eq locking-user 'none) nil locking-user)
-
- ;; otherwise, infer the property...
- (cond
- ;; in the CVS case, check the status
- ((eq (vc-backend file) 'CVS)
- (if (and (not (eq (vc-cvs-status file) 'locally-modified))
- (not (eq (vc-cvs-status file) 'needs-merge)))
- (vc-file-setprop file 'vc-locking-user 'none)
- ;; The expression below should return the username of the owner
- ;; of the file. It doesn't. It returns the username if it is
- ;; you, or otherwise the UID of the owner of the file. The
- ;; return value from this function is only used by
- ;; vc-dired-reformat-line, and it does the proper thing if a UID
- ;; is returned.
- ;;
- ;; The *proper* way to fix this would be to implement a built-in
- ;; function in Emacs, say, (username UID), that returns the
- ;; username of a given UID.
- ;;
- ;; The result of this hack is that vc-directory will print the
- ;; name of the owner of the file for any files that are
- ;; modified.
- (let ((uid (nth 2 (file-attributes file))))
- (if (= uid (user-uid))
- (vc-file-setprop file 'vc-locking-user (user-login-name))
- (vc-file-setprop file 'vc-locking-user uid)))))
-
- ;; RCS case: attempt a header search. If this feature is
- ;; disabled, vc-consult-rcs-headers always returns nil.
- ((and (eq (vc-backend file) 'RCS)
- (eq (vc-consult-rcs-headers file) 'rev-and-lock)))
-
- ;; if the file permissions are not trusted,
- ;; use the information from the master file
- ((or (not vc-keep-workfiles)
- (eq vc-mistrust-permissions 't)
- (and vc-mistrust-permissions
- (funcall vc-mistrust-permissions
- (vc-backend-subdirectory-name file))))
- (vc-file-setprop file 'vc-locking-user (vc-master-locking-user file)))
-
- ;; Otherwise: Use the file permissions. (But if it turns out that the
- ;; file is not owned by the user, use the master file.)
- ;; This implementation assumes that any file which is under version
- ;; control and has -rw-r--r-- is locked by its owner. This is true
- ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
- ;; We have to be careful not to exclude files with execute bits on;
- ;; scripts can be under version control too. Also, we must ignore the
- ;; group-read and other-read bits, since paranoid users turn them off.
- ;; This hack wins because calls to the somewhat expensive
- ;; `vc-fetch-master-properties' function only have to be made if
- ;; (a) the file is locked by someone other than the current user,
- ;; or (b) some untoward manipulation behind vc's back has changed
- ;; the owner or the `group' or `other' write bits.
- (t
- (let ((attributes (file-attributes file)))
- (cond ((string-match ".r-..-..-." (nth 8 attributes))
- (vc-file-setprop file 'vc-locking-user 'none))
- ((and (= (nth 2 attributes) (user-uid))
- (string-match ".rw..-..-." (nth 8 attributes)))
- (vc-file-setprop file 'vc-locking-user (user-login-name)))
- (t
- (vc-file-setprop file 'vc-locking-user
- (vc-master-locking-user file))))
- )))
- ;; recursively call the function again,
- ;; to convert a possible 'none value
- (vc-locking-user file))))
-
-;;; properties to store current and recent version numbers
-
-(defun vc-latest-version (file)
- ;; Return version level of the latest version of FILE
- (cond ((vc-file-getprop file 'vc-latest-version))
- (t (vc-fetch-properties file)
- (vc-file-getprop file 'vc-latest-version))))
-
-(defun vc-your-latest-version (file)
- ;; Return version level of the latest version of FILE checked in by you
- (cond ((vc-file-getprop file 'vc-your-latest-version))
- (t (vc-fetch-properties file)
- (vc-file-getprop file 'vc-your-latest-version))))
-
-(defun vc-top-version (file)
- ;; Return version level of the highest revision on the default branch
- ;; If there is no default branch, return the highest version number
- ;; on the trunk.
- ;; This property is defined for RCS only.
- (cond ((vc-file-getprop file 'vc-top-version))
- (t (vc-fetch-master-properties file)
- (vc-file-getprop file 'vc-top-version))))
-
-(defun vc-fetch-properties (file)
- ;; Fetch vc-latest-version and vc-your-latest-version
- ;; if that wasn't already done.
- (cond
- ((eq (vc-backend file) 'RCS)
- (save-excursion
- (set-buffer (get-buffer-create "*vc-info*"))
- (vc-insert-file (vc-name file) "^desc")
- (vc-parse-buffer
- (list '("^\\([0-9]+\\.[0-9.]+\\)\ndate[ \t]+\\([0-9.]+\\);" 1 2)
- (list (concat "^\\([0-9]+\\.[0-9.]+\\)\n"
- "date[ \t]+\\([0-9.]+\\);[ \t]+"
- "author[ \t]+"
- (regexp-quote (user-login-name)) ";") 1 2))
- file
- '(vc-latest-version vc-your-latest-version))
- (if (get-buffer "*vc-info*")
- (kill-buffer (get-buffer "*vc-info*")))))
- (t (vc-fetch-master-properties file))
- ))
+ ;; force computation of the property by calling
+ ;; vc-BACKEND-registered explicitly
+ (if (and (vc-backend file)
+ (vc-call-backend (vc-backend file) 'registered file))
+ (vc-file-getprop file 'vc-name))))
+
+(defun vc-checkout-model (file)
+ "Indicate how FILE is checked out.
+
+If FILE is not registered, this function always returns nil.
+For registered files, the possible values are:
+
+ 'implicit FILE is always writeable, and checked out `implicitly'
+ when the user saves the first changes to the file.
+
+ 'locking FILE is read-only if up-to-date; user must type
+ \\[vc-next-action] before editing. Strict locking
+ is assumed.
+
+ 'announce FILE is read-only if up-to-date; user must type
+ \\[vc-next-action] before editing. But other users
+ may be editing at the same time."
+ (or (vc-file-getprop file 'vc-checkout-model)
+ (if (vc-backend file)
+ (vc-file-setprop file 'vc-checkout-model
+ (vc-call checkout-model file)))))
+
+(defun vc-user-login-name (&optional uid)
+ "Return the name under which the user is logged in, as a string.
+\(With optional argument UID, return the name of that user.)
+This function does the same as function `user-login-name', but unlike
+that, it never returns nil. If a UID cannot be resolved, that
+UID is returned as a string."
+ (or (user-login-name uid)
+ (number-to-string (or uid (user-uid)))))
+
+(defun vc-state (file)
+ "Return the version control state of FILE.
+
+If FILE is not registered, this function always returns nil.
+For registered files, the value returned is one of:
+
+ 'up-to-date The working file is unmodified with respect to the
+ latest version on the current branch, and not locked.
+
+ 'edited The working file has been edited by the user. If
+ locking is used for the file, this state means that
+ the current version is locked by the calling user.
+
+ USER The current version of the working file is locked by
+ some other USER (a string).
+
+ 'needs-patch The file has not been edited by the user, but there is
+ a more recent version on the current branch stored
+ in the master file.
+
+ 'needs-merge The file has been edited by the user, and there is also
+ a more recent version on the current branch stored in
+ the master file. This state can only occur if locking
+ is not used for the file.
+
+ 'unlocked-changes The current version of the working file is not locked,
+ but the working file has been changed with respect
+ to that version. This state can only occur for files
+ with locking; it represents an erroneous condition that
+ should be resolved by the user (vc-next-action will
+ prompt the user to do it)."
+ ;; FIXME: New (sub)states needed (?):
+ ;; - `added' (i.e. `edited' but with no base version yet,
+ ;; typically represented by vc-workfile-version = "0")
+ ;; - `conflict' (i.e. `edited' with conflict markers)
+ ;; - `removed'
+ ;; - `copied' and `moved' (might be handled by `removed' and `added')
+ (or (vc-file-getprop file 'vc-state)
+ (if (vc-backend file)
+ (vc-file-setprop file 'vc-state
+ (vc-call state-heuristic file)))))
+
+(defun vc-recompute-state (file)
+ "Recompute the version control state of FILE, and return it.
+This calls the possibly expensive function vc-BACKEND-state,
+rather than the heuristic."
+ (vc-file-setprop file 'vc-state (vc-call state file)))
+
+(defsubst vc-up-to-date-p (file)
+ "Convenience function that checks whether `vc-state' of FILE is `up-to-date'."
+ (eq (vc-state file) 'up-to-date))
+
+(defun vc-default-state-heuristic (backend file)
+ "Default implementation of vc-state-heuristic.
+It simply calls the real state computation function `vc-BACKEND-state'
+and does not employ any heuristic at all."
+ (vc-call-backend backend 'state file))
+
+(defun vc-workfile-unchanged-p (file)
+ "Return non-nil if FILE has not changed since the last checkout."
+ (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
+ (lastmod (nth 5 (file-attributes file))))
+ (if (and checkout-time
+ ;; Tramp and Ange-FTP return this when they don't know the time.
+ (not (equal lastmod '(0 0))))
+ (equal checkout-time lastmod)
+ (let ((unchanged (vc-call workfile-unchanged-p file)))
+ (vc-file-setprop file 'vc-checkout-time (if unchanged lastmod 0))
+ unchanged))))
+
+(defun vc-default-workfile-unchanged-p (backend file)
+ "Check if FILE is unchanged by diffing against the master version.
+Return non-nil if FILE is unchanged."
+ (zerop (condition-case err
+ ;; If the implementation supports it, let the output
+ ;; go to *vc*, not *vc-diff*, since this is an internal call.
+ (vc-call diff file nil nil "*vc*")
+ (wrong-number-of-arguments
+ ;; If this error came from the above call to vc-BACKEND-diff,
+ ;; try again without the optional buffer argument (for
+ ;; backward compatibility). Otherwise, resignal.
+ (if (or (not (eq (cadr err)
+ (indirect-function
+ (vc-find-backend-function (vc-backend file)
+ 'diff))))
+ (not (eq (caddr err) 4)))
+ (signal (car err) (cdr err))
+ (vc-call diff file))))))