From 72aa16e19b24eb73c812c13d4cd77afd7977fc25 Mon Sep 17 00:00:00 2001 From: Glenn Morris Date: Sat, 15 Sep 2012 13:00:45 -0700 Subject: [PATCH] Add option to ask bzr itself for the emacs bzr revision * lisp/version.el (emacs-bzr-version-bzr): New function. (emacs-bzr-get-version): Add optional EXTERNAL argument. --- lisp/ChangeLog | 3 ++ lisp/version.el | 94 +++++++++++++++++++++++++++++++------------------ 2 files changed, 63 insertions(+), 34 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index c9aa7e9935..b436d9e364 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,5 +1,8 @@ 2012-09-15 Glenn Morris + * version.el (emacs-bzr-version-bzr): New function. + (emacs-bzr-get-version): Add optional EXTERNAL argument. + * vc/vc-bzr.el (vc-bzr-working-revision): For lightweight local checkouts, check the parent dirstate matches the branch. Add "--tree" to "bzr revno" arguments. Don't try to shorten the diff --git a/lisp/version.el b/lisp/version.el index 47476cb268..1fb3828e15 100644 --- a/lisp/version.el +++ b/lisp/version.el @@ -104,48 +104,74 @@ Returns nil if unable to find this information." (looking-at "[0-9]+\0\\([^\0\n]+\\)\0") (match-string 1)))))) -(defun emacs-bzr-get-version (&optional dir) +(defun emacs-bzr-version-bzr (dir) + "Ask bzr itself for the version information for directory DIR." + ;; Comments on `bzr version-info': + ;; i) Unknown files also cause clean != 1. + ;; ii) It can be slow, contacting the upstream repo to get the + ;; branch nick if one is not set locally, even with a custom + ;; template that is not asking for the nick (as used here). You'd + ;; think the latter part would be trivial to fix: + ;; https://bugs.launchpad.net/bzr/+bug/882541/comments/3 + ;; https://bugs.launchpad.net/bzr/+bug/629150 + ;; You can set the nick locally with `bzr nick ...', which speeds + ;; things up enormously. `bzr revno' does not have this issue, but + ;; has no way to print the revision_id AFAICS. + (message "Waiting for bzr...") + (with-temp-buffer + (if (zerop + (call-process "bzr" nil '(t nil) nil "version-info" + "--custom" + "--template={revno} {revision_id} (clean = {clean})" + "dir")) + (buffer-string)))) + +(defun emacs-bzr-get-version (&optional dir external) "Try to return as a string the bzr revision of the Emacs sources. The format is: [revno] revision_id, where revno may be absent. Value is nil if the sources do not seem to be under bzr, or if we could not determine the revision. Note that this reports on the current state of the sources, which may not correspond to the running Emacs. -Optional argument DIR is a directory to use instead of `source-directory'." +Optional argument DIR is a directory to use instead of `source-directory'. +Optional argument EXTERNAL non-nil means to maybe ask `bzr' itself, +if the sources appear to be under bzr. If `force', always ask bzr. +Otherwise only ask bzr if we cannot find any information ourselves." (or dir (setq dir source-directory)) (when (file-directory-p (expand-file-name ".bzr/branch" dir)) - (let (file loc rev) - (cond ((file-readable-p - (setq file (expand-file-name ".bzr/branch/last-revision" dir))) - (with-temp-buffer - (insert-file-contents file) - (goto-char (point-max)) - (if (looking-back "\n") - (delete-char -1)) - (buffer-string))) - ;; OK, no last-revision. Is it a lightweight checkout? - ((file-readable-p - (setq file (expand-file-name ".bzr/branch/location" dir))) - (setq rev (emacs-bzr-version-dirstate dir)) - ;; If the parent branch is local, try looking there for the rev. - ;; Note: there is no guarantee that the parent branch's rev - ;; corresponds to this branch. This branch could have - ;; been made with a specific -r revno argument, or the - ;; parent could have been updated since this branch was created. - ;; To try and detect this, we check the dirstate revids - ;; to see if they match. - (if (and (setq loc (with-temp-buffer - (insert-file-contents file) - (if (looking-at "file://\\(.*\\)") - (match-string 1)))) - (equal rev (emacs-bzr-version-dirstate loc))) - (emacs-bzr-get-version loc) - ;; If parent does not match, the best we can do without - ;; calling external commands is to use the dirstate rev. - rev)) - ;; At this point, could fall back to: - ;; bzr version-info --custom --template='{revno} {revision_id}\n' - )))) + (if (eq external 'force) + (emacs-bzr-version-bzr dir) + (let (file loc rev) + (cond ((file-readable-p + (setq file (expand-file-name ".bzr/branch/last-revision" dir))) + (with-temp-buffer + (insert-file-contents file) + (goto-char (point-max)) + (if (looking-back "\n") + (delete-char -1)) + (buffer-string))) + ;; OK, no last-revision. Is it a lightweight checkout? + ((file-readable-p + (setq file (expand-file-name ".bzr/branch/location" dir))) + (setq rev (emacs-bzr-version-dirstate dir)) + ;; If the parent branch is local, try looking there for the rev. + ;; Note: there is no guarantee that the parent branch's rev + ;; corresponds to this branch. This branch could have + ;; been made with a specific -r revno argument, or the + ;; parent could have been updated since this branch was created. + ;; To try and detect this, we check the dirstate revids + ;; to see if they match. + (if (and (setq loc (with-temp-buffer + (insert-file-contents file) + (if (looking-at "file://\\(.*\\)") + (match-string 1)))) + (equal rev (emacs-bzr-version-dirstate loc))) + (emacs-bzr-get-version loc) + ;; If parent does not match, the best we can do without + ;; calling external commands is to use the dirstate rev. + rev)) + (external + (emacs-bzr-version-bzr dir))))))) ;; We put version info into the executable in the form that `ident' uses. (purecopy (concat "\n$Id: " (subst-char-in-string ?\n ?\s (emacs-version)) -- 2.20.1