Isolating support code for the file-granularity back ends: the easy part.
[emacs.git] / lisp / vc / vc-filewise.el
blob9bdde5e5e31ee5f51c620c572ffd82babe49bcc5
1 ;;; vc-filewise.el --- common functions for file-oriented back ends.
3 ;; Copyright (C) 1992-1996, 1998-2014 Free Software Foundation, Inc.
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
7 ;; Package: vc
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 3 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Common functions for file-oriented back ends - SCCS, RCS, SRC, CVS
27 ;;
28 ;; The main purpose of this file is so none od this code jas to like
29 ;; in the always-resident vc-hooks. A secondary purpose is to remove
30 ;; code specific to this class of back ends from vc.el.
32 ;;; Code:
34 (eval-when-compile (require 'vc))
36 (defun vc-master-name (file)
37 "Return the master name of FILE.
38 If the file is not registered, or the master name is not known, return nil."
39 (or (vc-file-getprop file 'vc-name)
40 ;; force computation of the property by calling
41 ;; vc-BACKEND-registered explicitly
42 (let ((backend (vc-backend file)))
43 (if (and backend
44 (vc-call-backend backend 'registered file))
45 (vc-file-getprop file 'vc-name)))))
47 (defun vc-rename-master (oldmaster newfile templates)
48 "Rename OLDMASTER to be the master file for NEWFILE based on TEMPLATES."
49 (let* ((dir (file-name-directory (expand-file-name oldmaster)))
50 (newdir (or (file-name-directory newfile) ""))
51 (newbase (file-name-nondirectory newfile))
52 (masters
53 ;; List of potential master files for `newfile'
54 (mapcar
55 (lambda (s) (vc-possible-master s newdir newbase))
56 templates)))
57 (when (or (file-symlink-p oldmaster)
58 (file-symlink-p (file-name-directory oldmaster)))
59 (error "This is unsafe in the presence of symbolic links"))
60 (rename-file
61 oldmaster
62 (catch 'found
63 ;; If possible, keep the master file in the same directory.
64 (dolist (f masters)
65 (when (and f (string= (file-name-directory (expand-file-name f)) dir))
66 (throw 'found f)))
67 ;; If not, just use the first possible place.
68 (dolist (f masters)
69 (and f (or (not (setq dir (file-name-directory f)))
70 (file-directory-p dir))
71 (throw 'found f)))
72 (error "New file lacks a version control directory")))))
74 (defun vc-filewise-registered (backend file)
75 "Check if FILE is registered in BACKEND using vc-BACKEND-master-templates."
76 (let ((sym (vc-make-backend-sym backend 'master-templates)))
77 (unless (get backend 'vc-templates-grabbed)
78 (put backend 'vc-templates-grabbed t))
79 (let ((result (vc-check-master-templates file (symbol-value sym))))
80 (if (stringp result)
81 (vc-file-setprop file 'vc-name result)
82 nil)))) ; Not registered
84 (defun vc-possible-master (s dirname basename)
85 (cond
86 ((stringp s) (format s dirname basename))
87 ((functionp s)
88 ;; The template is a function to invoke. If the
89 ;; function returns non-nil, that means it has found a
90 ;; master. For backward compatibility, we also handle
91 ;; the case that the function throws a 'found atom
92 ;; and a pair (cons MASTER-FILE BACKEND).
93 (let ((result (catch 'found (funcall s dirname basename))))
94 (if (consp result) (car result) result)))))
96 (defun vc-check-master-templates (file templates)
97 "Return non-nil if there is a master corresponding to FILE.
99 TEMPLATES is a list of strings or functions. If an element is a
100 string, it must be a control string as required by `format', with two
101 string placeholders, such as \"%sRCS/%s,v\". The directory part of
102 FILE is substituted for the first placeholder, the basename of FILE
103 for the second. If a file with the resulting name exists, it is taken
104 as the master of FILE, and returned.
106 If an element of TEMPLATES is a function, it is called with the
107 directory part and the basename of FILE as arguments. It should
108 return non-nil if it finds a master; that value is then returned by
109 this function."
110 (let ((dirname (or (file-name-directory file) ""))
111 (basename (file-name-nondirectory file)))
112 (catch 'found
113 (mapcar
114 (lambda (s)
115 (let ((trial (vc-possible-master s dirname basename)))
116 (when (and trial (file-exists-p trial)
117 ;; Make sure the file we found with name
118 ;; TRIAL is not the source file itself.
119 ;; That can happen with RCS-style names if
120 ;; the file name is truncated (e.g. to 14
121 ;; chars). See if either directory or
122 ;; attributes differ.
123 (or (not (string= dirname
124 (file-name-directory trial)))
125 (not (equal (file-attributes file)
126 (file-attributes trial)))))
127 (throw 'found trial))))
128 templates))))
130 (provide 'vc-filewise)