* configure.ac: Fix minimum Jansson version requirement.
[emacs.git] / lisp / vc / vc-filewise.el
blob87a2e33264c0a06d366d24be6359da29d1888ff5
1 ;;; vc-filewise.el --- common functions for file-oriented back ends.
3 ;; Copyright (C) 1992-1996, 1998-2017 Free Software Foundation, Inc.
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: emacs-devel@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 <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Common functions for file-oriented back ends - SCCS, RCS, SRC, CVS
28 ;; The main purpose of this file is so none of this code has to live
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-filewise-registered backend 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 (provide 'vc-filewise)