Function: vc-responsible-backend
vc-responsible-backend is an autoloaded and byte-compiled function
defined in vc.el.gz.
Signature
(vc-responsible-backend FILE &optional NO-ERROR)
Documentation
Return the name of a backend system that is responsible for FILE.
If FILE is already registered, return the
backend of FILE. If FILE is not registered, then the
first backend in vc-handled-backends that declares itself
responsible for FILE is returned.
Note that if FILE is a symbolic link, it will not be resolved -- the responsible backend system for the symbolic link itself will be reported.
If NO-ERROR is nil, signal an error that no VC backend is responsible for the given file.
Other relevant functions are documented in the file group.
Probably introduced at or before Emacs version 28.1.
Shortdoc
;; file
(vc-responsible-backend "/src/foo/bar.c")
e.g. => Git
Source Code
;; Defined in /usr/src/emacs/lisp/vc/vc.el.gz
;;;###autoload
(defun vc-responsible-backend (file &optional no-error)
"Return the name of a backend system that is responsible for FILE.
If FILE is already registered, return the
backend of FILE. If FILE is not registered, then the
first backend in `vc-handled-backends' that declares itself
responsible for FILE is returned.
Note that if FILE is a symbolic link, it will not be resolved --
the responsible backend system for the symbolic link itself will
be reported.
If NO-ERROR is nil, signal an error that no VC backend is
responsible for the given file."
(or (and (not (file-directory-p file)) (vc-backend file))
;; FIXME it would be more efficient to walk up the directory tree,
;; stopping the first time a backend is responsible.
;;
;; First try: find a responsible backend. If this is for registration,
;; it must be a backend under which FILE is not yet registered.
(let* ((file (expand-file-name file))
(dirs (delq nil
(mapcar
(lambda (backend)
(when-let* ((dir (vc-call-backend
backend 'responsible-p file)))
;; We run DIR through `expand-file-name'
;; so that abbreviated directories, such
;; as "~/", wouldn't look "less specific"
;; due to their artificially shorter length.
(cons backend (expand-file-name dir))))
vc-handled-backends))))
;; Just a single response (or none); use it.
(if (< (length dirs) 2)
(caar dirs)
;; Several roots; we seem to have one vc inside another's
;; directory. Choose the most specific.
(caar (sort dirs (lambda (d1 d2)
(< (length (cdr d2)) (length (cdr d1))))))))
(unless no-error
(error "No VC backend is responsible for %s" file))))