avoid recursive `require' when loading semantic
[bpt/emacs.git] / lisp / version.el
CommitLineData
758c81e8 1;;; version.el --- record version number of Emacs
6edcb099 2
ba318903 3;; Copyright (C) 1985, 1992, 1994-1995, 1999-2014 Free Software
ab422c4d 4;; Foundation, Inc.
6edcb099 5
34dc21db 6;; Maintainer: emacs-devel@gnu.org
6edcb099 7;; Keywords: internal
bd78fa1d 8;; Package: emacs
6edcb099
DL
9
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
6edcb099 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
6edcb099
DL
16
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.
21
22;; You should have received a copy of the GNU General Public License
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
6edcb099 24
55535639
PJ
25;;; Commentary:
26
6edcb099
DL
27;;; Code:
28
758c81e8
GM
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.
6edcb099
DL
33This variable first existed in version 19.23.")
34
758c81e8
GM
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.
6edcb099
DL
39This variable first existed in version 19.23.")
40
758c81e8
GM
41(defconst emacs-build-time (current-time)
42 "Time at which Emacs was dumped out.")
6edcb099 43
758c81e8
GM
44(defconst emacs-build-system (system-name)
45 "Name of the system on which Emacs was built.")
6edcb099 46
694ea8e3
JB
47(defvar motif-version-string)
48(defvar gtk-version-string)
49(defvar ns-version-string)
50
758c81e8
GM
51(defun emacs-version (&optional here)
52 "Return string describing the version of Emacs that is running.
6edcb099
DL
53If optional argument HERE is non-nil, insert string at point.
54Don't use this function in programs to choose actions according
55to the system configuration; look at `system-configuration' instead."
56 (interactive "P")
f1180544 57 (let ((version-string
32226619 58 (format (if (not (called-interactively-p 'interactive))
7262075d
GM
59 "GNU Emacs %s (%s%s%s)\n of %s on %s"
60 "GNU Emacs %s (%s%s%s) of %s on %s")
6edcb099
DL
61 emacs-version
62 system-configuration
f1180544 63 (cond ((featurep 'motif)
fdbccd8b 64 (concat ", " (substring motif-version-string 4)))
09d685fb
LK
65 ((featurep 'gtk)
66 (concat ", GTK+ Version " gtk-version-string))
64bd6fd1 67 ((featurep 'x-toolkit) ", X toolkit")
601fb9b8 68 ((featurep 'ns)
cb83c00b 69 (format ", NS %s" ns-version-string))
6edcb099 70 (t ""))
3fac5d64
GM
71 (if (and (boundp 'x-toolkit-scroll-bars)
72 (memq x-toolkit-scroll-bars '(xaw xaw3d)))
73 (format ", %s scroll bars"
74 (capitalize (symbol-name x-toolkit-scroll-bars)))
75 "")
82b90229 76 (format-time-string "%Y-%m-%d" emacs-build-time)
6edcb099 77 emacs-build-system)))
f1180544 78 (if here
6edcb099 79 (insert version-string)
32226619 80 (if (called-interactively-p 'interactive)
6edcb099
DL
81 (message "%s" version-string)
82 version-string))))
83
c979fbdf 84;; We hope that this alias is easier for people to find.
6edcb099
DL
85(defalias 'version 'emacs-version)
86
a1ed8b05 87;; Set during dumping, this is a defvar so that it can be setq'd.
9475054f 88(defvar emacs-repository-version nil
ceb885a1
ER
89 "String giving the repository revision from which this Emacs was built.
90Value is nil if Emacs was not built from a repository checkout,
91or if we could not determine the revision.")
92
93(define-obsolete-variable-alias 'emacs-bzr-version
94 'emacs-repository-version "24.4")
a1ed8b05 95
f40a9709
GM
96(defun emacs-bzr-version-dirstate (dir)
97 "Try to return as a string the bzr revision ID of directory DIR.
98This uses the dirstate file's parent revision entry.
99Returns nil if unable to find this information."
100 (let ((file (expand-file-name ".bzr/checkout/dirstate" dir)))
101 (when (file-readable-p file)
102 (with-temp-buffer
103 (insert-file-contents file)
104 (and (looking-at "#bazaar dirstate flat format 3")
105 (forward-line 3)
106 (looking-at "[0-9]+\0\\([^\0\n]+\\)\0")
107 (match-string 1))))))
108
edace89f 109(defun emacs-bzr-version-bzr (dir)
72aa16e1
GM
110 "Ask bzr itself for the version information for directory DIR."
111 ;; Comments on `bzr version-info':
112 ;; i) Unknown files also cause clean != 1.
113 ;; ii) It can be slow, contacting the upstream repo to get the
114 ;; branch nick if one is not set locally, even with a custom
115 ;; template that is not asking for the nick (as used here). You'd
116 ;; think the latter part would be trivial to fix:
117 ;; https://bugs.launchpad.net/bzr/+bug/882541/comments/3
118 ;; https://bugs.launchpad.net/bzr/+bug/629150
119 ;; You can set the nick locally with `bzr nick ...', which speeds
120 ;; things up enormously. `bzr revno' does not have this issue, but
121 ;; has no way to print the revision_id AFAICS.
122 (message "Waiting for bzr...")
123 (with-temp-buffer
124 (if (zerop
125 (call-process "bzr" nil '(t nil) nil "version-info"
126 "--custom"
127 "--template={revno} {revision_id} (clean = {clean})"
edace89f 128 dir))
72aa16e1
GM
129 (buffer-string))))
130
7e03bb32
ER
131(define-obsolete-function-alias 'emacs-bzr-get-version
132 'emacs-repository-get-version "24.4")
133
9475054f 134(defun emacs-repository-get-version (&optional dir external)
ceb885a1
ER
135 "Try to return as a string the repository revision of the Emacs sources.
136The format of the returned string is dependent on the VCS in use.
137Value is nil if the sources do not seem to be under version
138control, or if we could not determine the revision. Note that
139this reports on the current state of the sources, which may not
140correspond to the running Emacs.
141
142Optional argument DIR is a directory to use instead of
143`source-directory'. Optional argument EXTERNAL non-nil means to
144maybe ask the VCS itself, if the sources appear to be under
145version control. If `force', always ask. the VCS. Otherwise
146only ask the VCS if we cannot find any information ourselves."
263f20cd 147 (or dir (setq dir source-directory))
2a5bce4f
GM
148 (cond ((file-directory-p (expand-file-name ".bzr/branch" dir))
149 (if (eq external 'force)
150 (emacs-bzr-version-bzr dir)
151 (let (file loc rev)
152 (cond ((file-readable-p
153 (setq file (expand-file-name
154 ".bzr/branch/last-revision" dir)))
155 (with-temp-buffer
156 (insert-file-contents file)
157 (goto-char (point-max))
158 (if (looking-back "\n")
159 (delete-char -1))
160 (buffer-string)))
161 ;; OK, no last-revision. Is it a lightweight checkout?
162 ((file-readable-p
163 (setq file (expand-file-name ".bzr/branch/location" dir)))
164 (setq rev (emacs-bzr-version-dirstate dir))
165 ;; If parent branch is local, try looking there for the rev.
166 ;; Note: there is no guarantee that the parent branch's rev
167 ;; corresponds to this branch. This branch could have
168 ;; been made with a specific -r revno argument, or the
169 ;; parent could have been updated since this branch was
170 ;; created.
171 ;; To try and detect this, we check the dirstate revids
172 ;; to see if they match.
173 (if (and (setq loc (with-temp-buffer
174 (insert-file-contents file)
175 (if (looking-at "file://\\(.*\\)")
176 (match-string 1))))
177 (equal rev (emacs-bzr-version-dirstate loc)))
178 (emacs-repository-get-version loc)
179 ;; If parent does not match, the best we can do without
180 ;; calling external commands is to use the dirstate rev.
181 rev))
182 (external
183 (emacs-bzr-version-bzr dir))))))
184 ((file-directory-p (expand-file-name ".git" dir))
185 (message "Waiting for git...")
186 (with-temp-buffer
187 (let ((default-directory (file-name-as-directory dir)))
983664c0 188 (and (eq 0
a5f2a870
GM
189 (condition-case nil
190 (call-process "git" nil '(t nil) nil "log"
191 "-1" "--pretty=format:%N")
192 (error nil)))
2a5bce4f
GM
193 (not (zerop (buffer-size)))
194 (replace-regexp-in-string "\n" "" (buffer-string))))))))
a1ed8b05 195
6edcb099 196;;; version.el ends here