unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#52172: Fix vc-git--program-version to support Git for macOS version string
@ 2021-11-29  1:44 Justin Schell
  2021-11-29 14:47 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 8+ messages in thread
From: Justin Schell @ 2021-11-29  1:44 UTC (permalink / raw)
  To: 52172

[-- Attachment #1: Type: text/plain, Size: 1349 bytes --]

The current `vc-git.el` `vc-git--program-version` function doesn't
support the version string returned by Git on macOS. `git version` on
macOS returns e.g., "git version 2.30.1 (Apple Git-130)" and
`vc-git--program-version` currently returns "0" instead of "2.30.1".

I've attached patches for two options for a fix.

`topic.fix-vc-git--program-version.ignore-trailing.patch` updates the
regexp used to match numeric version strings that are a combination of
digits and dots (starting with a digit), ignoring anything else that
comes after. Any trailing dots are then stripped. This is more general
than the current approach of supporting a numeric version string only,
or a numeric version string followed by the specific additional
strings that some Git versions append (currently only the additional
string appended by Git for Windows is supported) and supports
reasonable version strings that I can think of that potential other
versions of Git might return. I'm unaware of why this approach wasn't
used in the previous fix used for Git for Windows support. As such,
I've also attached `topic.fix-vc-git--program-version.support-macos`,
which adds to the current approach, adding support for the specific
additional string that Git for macOS returns, if that is preferred.

Also included are unit tests for each approach.

Thanks,
Justin Schell

[-- Attachment #2: topic.fix-vc-git--program-version.ignore-trailing.patch --]
[-- Type: application/octet-stream, Size: 3680 bytes --]

diff --git a/lisp/vc/vc-git.el b/lisp/vc/vc-git.el
index 2d35061b26..3aaf995d1b 100644
--- a/lisp/vc/vc-git.el
+++ b/lisp/vc/vc-git.el
@@ -298,12 +298,15 @@ included in the completions."
              (vc-git--run-command-string nil "version")))
         (setq vc-git--program-version
               (if (and version-string
-                       ;; Git for Windows appends ".windows.N" to the
-                       ;; numerical version reported by Git.
+                       ;; Some Git versions append additional strings
+                       ;; to the numerical version string. E.g., Git
+                       ;; for Windows appends ".windows.N", while Git
+                       ;; for Mac appends " (Apple Git-N)". Capture
+                       ;; numerical version and ignore the rest.
                        (string-match
-                        "git version \\([0-9.]+\\)\\(\\.windows\\.[0-9]+\\)?$"
+                        "git version \\([0-9][0-9.]+\\)"
                         version-string))
-                  (match-string 1 version-string)
+                  (string-trim-right (match-string 1 version-string) "\\.")
                 "0")))))
 
 (defun vc-git--git-status-to-vc-state (code-list)
diff --git a/test/lisp/vc/vc-git-tests.el b/test/lisp/vc/vc-git-tests.el
new file mode 100644
index 0000000000..997ab3c4b5
--- /dev/null
+++ b/test/lisp/vc/vc-git-tests.el
@@ -0,0 +1,67 @@
+;;; vc-git-tests.el --- tests for vc/vc-git.el  -*- lexical-binding:t -*-
+
+;; Copyright (C) 2016-2021 Free Software Foundation, Inc.
+
+;; Author: Justin Schell <justinmschell@gmail.com>
+;; Maintainer: emacs-devel@gnu.org
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;; Code:
+
+(require 'vc-git)
+
+(ert-deftest vc-git-test-program-version-general ()
+  (vc-git-test--run-program-version-test
+   "git version 2.30.1.0"
+   "2.30.1.0"))
+
+(ert-deftest vc-git-test-program-version-windows ()
+  (vc-git-test--run-program-version-test
+   "git version 2.30.1.1.windows.1"
+   "2.30.1.1"))
+
+(ert-deftest vc-git-test-program-version-apple ()
+  (vc-git-test--run-program-version-test
+   "git version 2.30.1.2 (Apple Git-130)"
+   "2.30.1.2"))
+
+(ert-deftest vc-git-test-program-version-other ()
+  (vc-git-test--run-program-version-test
+   "git version 2.30.1.3.foo.bar"
+   "2.30.1.3"))
+
+(ert-deftest vc-git-test-program-version-invalid-leading-string ()
+  (vc-git-test--run-program-version-test
+   "git version foo.bar.2.30.1.4"
+   "0"))
+
+(ert-deftest vc-git-test-program-version-invalid-leading-dot ()
+  (vc-git-test--run-program-version-test
+   "git version .2.30.1.5"
+   "0"))
+
+(defun vc-git-test--run-program-version-test
+    (mock-version-string expected-output)
+  (cl-letf* (((symbol-function 'vc-git--run-command-string)
+              (lambda (_file _args) mock-version-string))
+             (vc-git--program-version nil)
+             (actual-output (vc-git--program-version)))
+    (should (equal actual-output expected-output))))
+
+;;; vc-git-tests.el ends here

[-- Attachment #3: topic.fix-vc-git--program-version.support-macos --]
[-- Type: application/octet-stream, Size: 2997 bytes --]

diff --git a/lisp/vc/vc-git.el b/lisp/vc/vc-git.el
index 2d35061b26..eab1450a41 100644
--- a/lisp/vc/vc-git.el
+++ b/lisp/vc/vc-git.el
@@ -299,9 +299,10 @@ included in the completions."
         (setq vc-git--program-version
               (if (and version-string
                        ;; Git for Windows appends ".windows.N" to the
-                       ;; numerical version reported by Git.
+                       ;; numerical version reported by Git. Git for
+                       ;; macOS appends " (Apple Git-N)".
                        (string-match
-                        "git version \\([0-9.]+\\)\\(\\.windows\\.[0-9]+\\)?$"
+                        "git version \\([0-9.]+\\)\\(\\.windows\\.[0-9]+\\)?\\( (Apple Git\\-[0-9]+)\\)?$"
                         version-string))
                   (match-string 1 version-string)
                 "0")))))
diff --git a/test/lisp/vc/vc-git-tests.el b/test/lisp/vc/vc-git-tests.el
new file mode 100644
index 0000000000..abcd9d42be
--- /dev/null
+++ b/test/lisp/vc/vc-git-tests.el
@@ -0,0 +1,57 @@
+;;; vc-git-tests.el --- tests for vc/vc-git.el  -*- lexical-binding:t -*-
+
+;; Copyright (C) 2016-2021 Free Software Foundation, Inc.
+
+;; Author: Justin Schell <justinmschell@gmail.com>
+;; Maintainer: emacs-devel@gnu.org
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;; Code:
+
+(require 'vc-git)
+
+(ert-deftest vc-git-test-program-version-general ()
+  (vc-git-test--run-program-version-test
+   "git version 2.29.0"
+   "2.29.0"))
+
+(ert-deftest vc-git-test-program-version-windows ()
+  (vc-git-test--run-program-version-test
+   "git version 2.29.1.windows.1"
+   "2.29.1"))
+
+(ert-deftest vc-git-test-program-version-apple ()
+  (vc-git-test--run-program-version-test
+   "git version 2.29.2 (Apple Git-130)"
+   "2.29.2"))
+
+(ert-deftest vc-git-test-program-version-other ()
+  (vc-git-test--run-program-version-test
+   "git version 2.29.3.foo.bar"
+   "0"))
+
+(defun vc-git-test--run-program-version-test
+    (mock-version-string expected-output)
+  (cl-letf* (((symbol-function 'vc-git--run-command-string)
+              (lambda (_file _args) mock-version-string))
+             (vc-git--program-version nil)
+             (actual-output (vc-git--program-version)))
+    (should (equal actual-output expected-output))))
+
+;;; vc-git-tests.el ends here

^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-12-06  1:52 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-29  1:44 bug#52172: Fix vc-git--program-version to support Git for macOS version string Justin Schell
2021-11-29 14:47 ` Lars Ingebrigtsen
2021-12-06  0:57   ` Dmitry Gutov
2021-12-06  1:29     ` Lars Ingebrigtsen
2021-12-06  1:44       ` Justin Schell
2021-12-06  1:52         ` Lars Ingebrigtsen
2021-12-06  0:58   ` Dmitry Gutov
2021-12-06  1:02     ` Lars Ingebrigtsen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).