Tweak previous emacs-bzr-get-version change
[bpt/emacs.git] / lisp / version.el
CommitLineData
758c81e8 1;;; version.el --- record version number of Emacs
6edcb099 2
acaf905b 3;; Copyright (C) 1985, 1992, 1994-1995, 1999-2012
1746e0c3 4;; Free Software Foundation, Inc.
6edcb099
DL
5
6;; Maintainer: FSF
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.
758c81e8
GM
88(defvar emacs-bzr-version nil
89 "String giving the bzr revision from which this Emacs was built.
f40a9709 90The format is: [revno] revision_id, where revno may be absent.
539aa513 91Value is nil if Emacs was not built from a bzr checkout, or if we could
a1ed8b05
GM
92not determine the revision.")
93
f40a9709
GM
94(defun emacs-bzr-version-dirstate (dir)
95 "Try to return as a string the bzr revision ID of directory DIR.
96This uses the dirstate file's parent revision entry.
97Returns nil if unable to find this information."
98 (let ((file (expand-file-name ".bzr/checkout/dirstate" dir)))
99 (when (file-readable-p file)
100 (with-temp-buffer
101 (insert-file-contents file)
102 (and (looking-at "#bazaar dirstate flat format 3")
103 (forward-line 3)
104 (looking-at "[0-9]+\0\\([^\0\n]+\\)\0")
105 (match-string 1))))))
106
758c81e8 107(defun emacs-bzr-get-version (&optional dir)
f40a9709
GM
108 "Try to return as a string the bzr revision of the Emacs sources.
109The format is: [revno] revision_id, where revno may be absent.
539aa513 110Value is nil if the sources do not seem to be under bzr, or if we could
a1ed8b05 111not determine the revision. Note that this reports on the current state
263f20cd
GM
112of the sources, which may not correspond to the running Emacs.
113
114Optional argument DIR is a directory to use instead of `source-directory'."
115 (or dir (setq dir source-directory))
fea895b1 116 (when (file-directory-p (expand-file-name ".bzr/branch" dir))
f40a9709 117 (let (file loc rev)
263f20cd 118 (cond ((file-readable-p
f40a9709 119 (setq file (expand-file-name ".bzr/branch/last-revision" dir)))
263f20cd
GM
120 (with-temp-buffer
121 (insert-file-contents file)
122 (goto-char (point-max))
123 (if (looking-back "\n")
124 (delete-char -1))
125 (buffer-string)))
126 ;; OK, no last-revision. Is it a lightweight checkout?
127 ((file-readable-p
f40a9709
GM
128 (setq file (expand-file-name ".bzr/branch/location" dir)))
129 (setq rev (emacs-bzr-version-dirstate dir))
130 ;; If the parent branch is local, try looking there for the rev.
131 ;; Note: there is no guarantee that the parent branch's rev
132 ;; corresponds to this branch. This branch could have
133 ;; been made with a specific -r revno argument, or the
134 ;; parent could have been updated since this branch was created.
135 ;; To try and detect this, we check the dirstate revids
136 ;; to see if they match.
137 (if (and (setq loc (with-temp-buffer
263f20cd
GM
138 (insert-file-contents file)
139 (if (looking-at "file://\\(.*\\)")
140 (match-string 1))))
f40a9709
GM
141 (equal rev (emacs-bzr-version-dirstate loc)))
142 (emacs-bzr-get-version loc)
143 ;; If parent does not match, the best we can do without
144 ;; calling external commands is to use the dirstate rev.
145 rev))
146 ;; At this point, could fall back to:
147 ;; bzr version-info --custom --template='{revno} {revision_id}\n'
263f20cd 148 ))))
a1ed8b05 149
e6abda02 150;; We put version info into the executable in the form that `ident' uses.
6cf2a23e
EZ
151(purecopy (concat "\n$Id: " (subst-char-in-string ?\n ?\s (emacs-version))
152 " $\n"))
6edcb099 153
6edcb099 154;;; version.el ends here