version.el 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. ;;; version.el --- record version number of Emacs
  2. ;; Copyright (C) 1985, 1992, 1994-1995, 1999-2015 Free Software
  3. ;; Foundation, Inc.
  4. ;; Maintainer: emacs-devel@gnu.org
  5. ;; Keywords: internal
  6. ;; Package: emacs
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;; Code:
  20. (defconst emacs-major-version
  21. (progn (string-match "^[0-9]+" emacs-version)
  22. (string-to-number (match-string 0 emacs-version)))
  23. "Major version number of this version of Emacs.
  24. This variable first existed in version 19.23.")
  25. (defconst emacs-minor-version
  26. (progn (string-match "^[0-9]+\\.\\([0-9]+\\)" emacs-version)
  27. (string-to-number (match-string 1 emacs-version)))
  28. "Minor version number of this version of Emacs.
  29. This variable first existed in version 19.23.")
  30. (defconst emacs-build-time (current-time)
  31. "Time at which Emacs was dumped out.")
  32. ;; I think this should be obsoleted/removed. It's just one more meaningless
  33. ;; difference between different builds. It's usually not even an fqdn.
  34. (defconst emacs-build-system (system-name)
  35. "Name of the system on which Emacs was built.")
  36. (defvar motif-version-string)
  37. (defvar gtk-version-string)
  38. (defvar ns-version-string)
  39. (defvar cairo-version-string)
  40. (defun emacs-version (&optional here)
  41. "Return string describing the version of Emacs that is running.
  42. If optional argument HERE is non-nil, insert string at point.
  43. Don't use this function in programs to choose actions according
  44. to the system configuration; look at `system-configuration' instead."
  45. (interactive "P")
  46. (let ((version-string
  47. (format (if (not (called-interactively-p 'interactive))
  48. "GNU Emacs %s (%s%s%s%s)\n of %s"
  49. "GNU Emacs %s (%s%s%s%s) of %s")
  50. emacs-version
  51. system-configuration
  52. (cond ((featurep 'motif)
  53. (concat ", " (substring motif-version-string 4)))
  54. ((featurep 'gtk)
  55. (concat ", GTK+ Version " gtk-version-string))
  56. ((featurep 'x-toolkit) ", X toolkit")
  57. ((featurep 'ns)
  58. (format ", NS %s" ns-version-string))
  59. (t ""))
  60. (if (featurep 'cairo)
  61. (format ", cairo version %s" cairo-version-string)
  62. "")
  63. (if (and (boundp 'x-toolkit-scroll-bars)
  64. (memq x-toolkit-scroll-bars '(xaw xaw3d)))
  65. (format ", %s scroll bars"
  66. (capitalize (symbol-name x-toolkit-scroll-bars)))
  67. "")
  68. (format-time-string "%Y-%m-%d" emacs-build-time))))
  69. (if here
  70. (insert version-string)
  71. (if (called-interactively-p 'interactive)
  72. (message "%s" version-string)
  73. version-string))))
  74. ;; We hope that this alias is easier for people to find.
  75. (defalias 'version 'emacs-version)
  76. ;; Set during dumping, this is a defvar so that it can be setq'd.
  77. (defvar emacs-repository-version nil
  78. "String giving the repository revision from which this Emacs was built.
  79. Value is nil if Emacs was not built from a repository checkout,
  80. or if we could not determine the revision.")
  81. (define-obsolete-variable-alias 'emacs-bzr-version
  82. 'emacs-repository-version "24.4")
  83. (define-obsolete-function-alias 'emacs-bzr-get-version
  84. 'emacs-repository-get-version "24.4")
  85. (defun emacs-repository-version-git (dir)
  86. "Ask git itself for the version information for directory DIR."
  87. (message "Waiting for git...")
  88. (with-temp-buffer
  89. (let ((default-directory (file-name-as-directory dir)))
  90. (and (eq 0
  91. (with-demoted-errors "Error running git rev-parse: %S"
  92. (call-process "git" nil '(t nil) nil "rev-parse" "HEAD")))
  93. (progn (goto-char (point-min))
  94. (looking-at "[0-9a-fA-F]\\{40\\}"))
  95. (match-string 0)))))
  96. (defun emacs-repository--version-git-1 (file)
  97. "Internal subroutine of `emacs-repository-get-version'."
  98. (when (file-readable-p file)
  99. (erase-buffer)
  100. (insert-file-contents file)
  101. (cond ((looking-at "[0-9a-fA-F]\\{40\\}")
  102. (match-string 0))
  103. ((looking-at "ref: \\(.*\\)")
  104. (emacs-repository--version-git-1
  105. (expand-file-name (match-string 1)
  106. (file-name-directory file)))))))
  107. (defun emacs-repository-get-version (&optional dir external)
  108. "Try to return as a string the repository revision of the Emacs sources.
  109. The format of the returned string is dependent on the VCS in use.
  110. Value is nil if the sources do not seem to be under version
  111. control, or if we could not determine the revision. Note that
  112. this reports on the current state of the sources, which may not
  113. correspond to the running Emacs.
  114. Optional argument DIR is a directory to use instead of `source-directory'.
  115. Optional argument EXTERNAL non-nil means to just ask the VCS itself,
  116. if the sources appear to be under version control. Otherwise only ask
  117. the VCS if we cannot find any information ourselves."
  118. (or dir (setq dir source-directory))
  119. (when (file-directory-p (expand-file-name ".git" dir))
  120. (if external
  121. (emacs-repository-version-git dir)
  122. (or (let ((files '("HEAD" "refs/heads/master"))
  123. file rev)
  124. (with-temp-buffer
  125. (while (and (not rev)
  126. (setq file (car files)))
  127. (setq file (expand-file-name (format ".git/%s" file) dir)
  128. files (cdr files)
  129. rev (emacs-repository--version-git-1 file))))
  130. rev)
  131. ;; AFAICS this doesn't work during dumping (bug#20799).
  132. (emacs-repository-version-git dir)))))
  133. ;; We put version info into the executable in the form that `ident' uses.
  134. (purecopy (concat "\n$Id: " (subst-char-in-string ?\n ?\s (emacs-version))
  135. " $\n"))
  136. ;;; version.el ends here