1 ;;; version.el --- record version number of Emacs
3 ;; Copyright (C) 1985, 1992, 1994-1995, 1999-2016 Free Software
6 ;; Maintainer: emacs-devel@gnu.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 (defconst emacs-major-version
30 (progn (string-match "^[0-9]+" emacs-version)
31 (string-to-number (match-string 0 emacs-version)))
32 "Major version number of this version of Emacs.
33 This variable first existed in version 19.23.")
35 (defconst emacs-minor-version
36 (progn (string-match "^[0-9]+\\.\\([0-9]+\\)" emacs-version)
37 (string-to-number (match-string 1 emacs-version)))
38 "Minor version number of this version of Emacs.
39 This variable first existed in version 19.23.")
41 ;; FIXME: The next variable should also be a constant if
42 ;; `deterministic-dump' is t.
43 (defconst emacs-build-time (current-time)
44 "Time at which Emacs was dumped out.")
46 (defconst emacs-build-system
47 (if deterministic-dump "elided" (system-name))
48 "Name of the system on which Emacs was built.")
50 (defvar motif-version-string)
51 (defvar gtk-version-string)
52 (defvar ns-version-string)
53 (defvar cairo-version-string)
55 (defun emacs-version (&optional here)
56 "Return string describing the version of Emacs that is running.
57 If optional argument HERE is non-nil, insert string at point.
58 Don't use this function in programs to choose actions according
59 to the system configuration; look at `system-configuration' instead."
62 (format (if (not (called-interactively-p 'interactive))
63 "GNU Emacs %s (%s%s%s%s)\n of %s"
64 "GNU Emacs %s (%s%s%s%s) of %s")
67 (cond ((featurep 'motif)
68 (concat ", " (substring motif-version-string 4)))
70 (concat ", GTK+ Version " gtk-version-string))
71 ((featurep 'x-toolkit) ", X toolkit")
73 (format ", NS %s" ns-version-string))
76 (format ", cairo version %s" cairo-version-string)
78 (if (and (boundp 'x-toolkit-scroll-bars)
79 (memq x-toolkit-scroll-bars '(xaw xaw3d)))
80 (format ", %s scroll bars"
81 (capitalize (symbol-name x-toolkit-scroll-bars)))
83 (format-time-string "%Y-%m-%d" emacs-build-time))))
85 (insert version-string)
86 (if (called-interactively-p 'interactive)
87 (message "%s" version-string)
90 ;; We hope that this alias is easier for people to find.
91 (defalias 'version 'emacs-version)
93 ;; Set during dumping, this is a defvar so that it can be setq'd.
94 (defvar emacs-repository-version nil
95 "String giving the repository revision from which this Emacs was built.
96 Value is nil if Emacs was not built from a repository checkout,
97 or if we could not determine the revision.")
99 (define-obsolete-variable-alias 'emacs-bzr-version
100 'emacs-repository-version "24.4")
102 (define-obsolete-function-alias 'emacs-bzr-get-version
103 'emacs-repository-get-version "24.4")
105 (defun emacs-repository-version-git (dir)
106 "Ask git itself for the version information for directory DIR."
107 (message "Waiting for git...")
109 (let ((default-directory (file-name-as-directory dir)))
111 (with-demoted-errors "Error running git rev-parse: %S"
112 (call-process "git" nil '(t nil) nil "rev-parse" "HEAD")))
113 (progn (goto-char (point-min))
114 (looking-at "[0-9a-fA-F]\\{40\\}"))
117 (defun emacs-repository--version-git-1 (file dir)
118 "Internal subroutine of `emacs-repository-get-version'."
119 (when (file-readable-p file)
121 (insert-file-contents file)
122 (cond ((looking-at "[0-9a-fA-F]\\{40\\}")
124 ((looking-at "ref: \\(.*\\)")
125 (emacs-repository--version-git-1
126 (expand-file-name (match-string 1) dir)
129 (defun emacs-repository-get-version (&optional dir external)
130 "Try to return as a string the repository revision of the Emacs sources.
131 The format of the returned string is dependent on the VCS in use.
132 Value is nil if the sources do not seem to be under version
133 control, or if we could not determine the revision. Note that
134 this reports on the current state of the sources, which may not
135 correspond to the running Emacs.
137 Optional argument DIR is a directory to use instead of `source-directory'.
138 Optional argument EXTERNAL non-nil means to just ask the VCS itself,
139 if the sources appear to be under version control. Otherwise only ask
140 the VCS if we cannot find any information ourselves."
141 (or dir (setq dir source-directory))
142 (let* ((base-dir (expand-file-name ".git" dir))
143 (in-main-worktree (file-directory-p base-dir))
144 (in-linked-worktree nil)
146 ;; If the sources are in a linked worktree, .git is a file that points to
147 ;; the location of the main worktree and the repo's administrative files.
148 (when (and (not in-main-worktree)
149 (file-regular-p base-dir)
150 (file-readable-p base-dir))
152 (insert-file-contents base-dir)
153 (when (looking-at "gitdir: \\(.*\.git\\)\\(.*\\)$")
154 (setq base-dir (match-string 1)
155 sub-dir (concat base-dir (match-string 2))
156 in-linked-worktree t))))
157 ;; We've found a worktree, either main or linked.
158 (when (or in-main-worktree in-linked-worktree)
160 (emacs-repository-version-git dir)
161 (or (if in-linked-worktree
162 (emacs-repository--version-git-1
163 (expand-file-name "HEAD" sub-dir) base-dir)
164 (let ((files '("HEAD" "refs/heads/master"))
166 (while (and (not rev)
167 (setq file (car files)))
168 (setq file (expand-file-name file base-dir)
170 rev (emacs-repository--version-git-1 file base-dir)))
172 ;; AFAICS this doesn't work during dumping (bug#20799).
173 (emacs-repository-version-git dir))))))
175 ;; We put version info into the executable in the form that `ident' uses.
176 (purecopy (concat "\n$Id: " (subst-char-in-string ?\n ?\s (emacs-version))
179 ;;; version.el ends here