vc-filewise.el 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ;;; vc-filewise.el --- common functions for file-oriented back ends.
  2. ;; Copyright (C) 1992-1996, 1998-2015 Free Software Foundation, Inc.
  3. ;; Author: FSF (see vc.el for full credits)
  4. ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
  5. ;; Package: vc
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Common functions for file-oriented back ends - SCCS, RCS, SRC, CVS
  19. ;;
  20. ;; The main purpose of this file is so none of this code has to live
  21. ;; in the always-resident vc-hooks. A secondary purpose is to remove
  22. ;; code specific to this class of back ends from vc.el.
  23. ;;; Code:
  24. (eval-when-compile (require 'vc))
  25. (defun vc-master-name (file)
  26. "Return the master name of FILE.
  27. If the file is not registered, or the master name is not known, return nil."
  28. (or (vc-file-getprop file 'vc-name)
  29. ;; force computation of the property by calling
  30. ;; vc-BACKEND-registered explicitly
  31. (let ((backend (vc-backend file)))
  32. (if (and backend
  33. (vc-filewise-registered backend file))
  34. (vc-file-getprop file 'vc-name)))))
  35. (defun vc-rename-master (oldmaster newfile templates)
  36. "Rename OLDMASTER to be the master file for NEWFILE based on TEMPLATES."
  37. (let* ((dir (file-name-directory (expand-file-name oldmaster)))
  38. (newdir (or (file-name-directory newfile) ""))
  39. (newbase (file-name-nondirectory newfile))
  40. (masters
  41. ;; List of potential master files for `newfile'
  42. (mapcar
  43. (lambda (s) (vc-possible-master s newdir newbase))
  44. templates)))
  45. (when (or (file-symlink-p oldmaster)
  46. (file-symlink-p (file-name-directory oldmaster)))
  47. (error "This is unsafe in the presence of symbolic links"))
  48. (rename-file
  49. oldmaster
  50. (catch 'found
  51. ;; If possible, keep the master file in the same directory.
  52. (dolist (f masters)
  53. (when (and f (string= (file-name-directory (expand-file-name f)) dir))
  54. (throw 'found f)))
  55. ;; If not, just use the first possible place.
  56. (dolist (f masters)
  57. (and f (or (not (setq dir (file-name-directory f)))
  58. (file-directory-p dir))
  59. (throw 'found f)))
  60. (error "New file lacks a version control directory")))))
  61. (defun vc-filewise-registered (backend file)
  62. "Check if FILE is registered in BACKEND using vc-BACKEND-master-templates."
  63. (let ((sym (vc-make-backend-sym backend 'master-templates)))
  64. (unless (get backend 'vc-templates-grabbed)
  65. (put backend 'vc-templates-grabbed t))
  66. (let ((result (vc-check-master-templates file (symbol-value sym))))
  67. (if (stringp result)
  68. (vc-file-setprop file 'vc-name result)
  69. nil)))) ; Not registered
  70. (provide 'vc-filewise)