From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: =?UTF-8?Q?K=C3=A9vin?= Le Gouguec Newsgroups: gmane.emacs.bugs Subject: bug#68183: 28.3; vc-dir fails when I have a certain branch checked out Date: Mon, 12 Feb 2024 09:08:50 +0100 Message-ID: <878r3q2jfx.fsf@gmail.com> References: <8734vici68.fsf@tromey.com> <83y1da17zw.fsf@gnu.org> <87y1dab03x.fsf@tromey.com> <87h6jun400.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="5180"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: Dmitry Gutov , Eli Zaretskii , 68183@debbugs.gnu.org, Juri Linkov To: Tom Tromey Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Feb 12 09:10:09 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rZROB-00019W-8x for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 12 Feb 2024 09:10:07 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rZRNr-0005Ak-LP; Mon, 12 Feb 2024 03:09:47 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rZRNq-0005AF-Bl for bug-gnu-emacs@gnu.org; Mon, 12 Feb 2024 03:09:46 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rZRNq-0008HP-3a for bug-gnu-emacs@gnu.org; Mon, 12 Feb 2024 03:09:46 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rZRO6-0000PC-Bf for bug-gnu-emacs@gnu.org; Mon, 12 Feb 2024 03:10:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: =?UTF-8?Q?K=C3=A9vin?= Le Gouguec Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 12 Feb 2024 08:10:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 68183 X-GNU-PR-Package: emacs Original-Received: via spool by 68183-submit@debbugs.gnu.org id=B68183.17077253561426 (code B ref 68183); Mon, 12 Feb 2024 08:10:02 +0000 Original-Received: (at 68183) by debbugs.gnu.org; 12 Feb 2024 08:09:16 +0000 Original-Received: from localhost ([127.0.0.1]:37454 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rZRNM-0000Mu-6B for submit@debbugs.gnu.org; Mon, 12 Feb 2024 03:09:16 -0500 Original-Received: from mail-wm1-x329.google.com ([2a00:1450:4864:20::329]:59760) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rZRNK-0000MR-IO for 68183@debbugs.gnu.org; Mon, 12 Feb 2024 03:09:15 -0500 Original-Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-4114e0a2936so255365e9.3 for <68183@debbugs.gnu.org>; Mon, 12 Feb 2024 00:08:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707725332; x=1708330132; darn=debbugs.gnu.org; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=NiflmERF7196VPbGsPpcod27xG9qA9tkt4gJMbVtzfo=; b=ObKCbX/vNFUU3qRUxxUfwSknQoi7EoHZQdIHfj5V3SEcG724YOWckdTEgtwvlpWn5b yIDvrVPmH0hN0hi4ITn1PQKxeJE+c66fv4GOim/MlNkr8Vbn6GIZZg1uNu4kg32XibVi lj643dIbp1R3wwtKwrMNA3wMgEpJ90xFJr6bN/mrefAPemdo8sTe7YkhVUdwwrzSO9+b v2nYP/9jhbzoa+5ynDvs3q5ZaEY42heKZDPoJ7dY/4zDgDFzUuTZDiVIZbvZjl8rHtgU ZoiWCgdfH7rilGnyWFVYyS4FjVhHwAcqJSd0Ljz+SyUcvq6BjL7lJZoTmsTPMTO8mouA ln3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707725332; x=1708330132; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=NiflmERF7196VPbGsPpcod27xG9qA9tkt4gJMbVtzfo=; b=BB7K8msDhzO+XKEKTUl7jnH7xzrsTB0q8qQ4tO5pnF5MjeHV8PL8JApp5SMV5caCgu gJoXSqr0rnYZ62W1uaCq0vhpz4RD4uoxHcTpRQ5Em2wKmax0O5PlEelpzof8JDLoU2rA bIw/Q07JMcr83FU8SXml5N65Mhj9ccUjQMPmk4Bvc3EDxW90T8NmaLk8n5qpppDmrZSQ nr0GM/oj47hNe1xH4Mvlujei97mq/g+XGKmJm//5u8FEHCHFtsaSSKXjSdakVJ41u7wS rhpQxLAL3RiMP62Gh5PKM/ww5CU6K4nVsk88Katc353BXv/+bHhH4pTHTbZbsExIe0Wy KEng== X-Forwarded-Encrypted: i=1; AJvYcCWKVj4fV5Zi0W25hZ0jwDgt3qs7LDBj9LvLDETjYJxKxzASVQ/HTy52p+AGlRtMdVFoC79lbUtwV13vjBgU1vD9RUmm04s= X-Gm-Message-State: AOJu0YyOmYbjkYVUcYeHwsZSR5WWhNPiGYNk58TItQ1W868aaY7N9XIl 6vLQ5eyzEUr+EPaLx55T6uTgEAJW4+8TTB8BxXMVIStEr/EH+GUo X-Google-Smtp-Source: AGHT+IGZdWERXjQb7MptpzfWe6C65X1jv6+2LWyP0wZidlgpCu5kW3R6RTs9PndJSDijOn0NSbJyrA== X-Received: by 2002:a05:600c:3b21:b0:40f:e3e0:181c with SMTP id m33-20020a05600c3b2100b0040fe3e0181cmr6038727wms.17.1707725331712; Mon, 12 Feb 2024 00:08:51 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWU4Rsc9rHnWKKuhfTYgDwvAIYmG/G5eoKIGwaJvjiEyzYwuHZ2cUe704bvh201FU1x+0mPnJq0zHodhb1lwKyxc7wHQfmKqsON9iI7sOp2Kiy/bxshhOjWwsbhJLQls5t7fr0Wab8WOQ== Original-Received: from amdahl30 ([2a01:e0a:253:fe0:2ef0:5dff:fed2:7b49]) by smtp.gmail.com with ESMTPSA id s8-20020a7bc388000000b00410bca333b7sm3438828wmj.27.2024.02.12.00.08.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 00:08:51 -0800 (PST) In-Reply-To: <87h6jun400.fsf@gmail.com> ("=?UTF-8?Q?K=C3=A9vin?= Le Gouguec"'s message of "Wed, 03 Jan 2024 10:46:39 +0100") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:279886 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable K=C3=A9vin Le Gouguec writes: > Tom Tromey writes: > >> Eli> Thanks, but I think we'd appreciate a reproducible recipe for this: >> Eli> how can one create a Git repository which can be used to reproduce >> Eli> this issue? >> >> This worked for me: >> >> $ cd ~/Emacs/trunk >> # This is my Emacs git repository >> $ git checkout --track -b vc-dir-bug master >> branch 'vc-dir-bug' set up to track 'master'. >> Switched to a new branch 'vc-dir-bug' >> >> >> Now invoke vc-dir on that directory. > [=E2=80=A6] > git-config(1) says that branch..remote is "." when is > tracking a local branch, whereas branch..merge points to the local > branch 'git pull' will resync with. Wonder what TRT would be for the > purposes of vc-dir? Here's a patch. tl;dr (1) When branch..remote is ".", display=E2=80=A6 > Remote : none (tracking local branch) =E2=80=A6 instead of raising an error. (2) When branch..merge is set to BRANCH, display=E2=80=A6 > Tracking : BRANCH (3) Add tests, because why not. CC'ing Dmitry and Juri, whom my brain associates with vc-git maintenance (rightly or wrongly; apologies for the noise if the latter). I might be slow to respond for the next couple of weeks, so please feel free to adapt or dismiss the patch as convenient. FWIW, off the top of my head, * the test should probably have a (skip-unless (have-git-or-something)), * maybe "none (tracking local branch)" is not informative and we should ditch it, * maybe we should fall back to "origin", like vc-git-repository-url does, * rushed the ChangeLog entry; vc-git-test--run should also be declared as a "new helper" (and maybe I should spell out that I used it to not have to depend on vc-git-- internal functions), * maybe the new header deserves a NEWS entry. I can tackle any of the above (and any other feedback), though again, if someone has a vision and I don't answer in a timely manner, don't wait on me. --=-=-= Content-Type: text/x-patch; charset=utf-8 Content-Disposition: attachment; filename=0001-Fix-vc-dir-when-remote-Git-branch-is-local.patch Content-Transfer-Encoding: quoted-printable >From ba9b1f6f340a551acab0014c69f05f38191bd345 Mon Sep 17 00:00:00 2001 From: =3D?UTF-8?q?K=3DC3=3DA9vin=3D20Le=3D20Gouguec?=3D Date: Mon, 12 Feb 2024 08:29:19 +0100 Subject: [PATCH] Fix vc-dir when "remote" Git branch is local MIME-Version: 1.0 Content-Type: text/plain; charset=3DUTF-8 Content-Transfer-Encoding: 8bit While in there, add that "tracking" branch to the vc-dir buffer. For bug#68183. * lisp/vc/vc-git.el (vc-git-dir-extra-headers): Reduce boilerplate with new function 'vc-git--out-ok'; skip calling vc-git-repository-url when REMOTE is "."; display tracking branch; prefer "none ()" to "not ()" si= nce that reads more grammatically correct. (vc-git--out-ok): Add documentation. (vc-git--out-str): New function to easily get the output from a Git command. * test/lisp/vc/vc-git-tests.el (vc-git-test--with-repo): New helper. (vc-git-test--run, vc-git-test-dir-track-local-branch): Check that vc-dir does not crash. --- lisp/vc/vc-git.el | 44 ++++++++++++++++++++++++------------ test/lisp/vc/vc-git-tests.el | 39 ++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 14 deletions(-) diff --git a/lisp/vc/vc-git.el b/lisp/vc/vc-git.el index 456417e566e..bf3032e5537 100644 --- a/lisp/vc/vc-git.el +++ b/lisp/vc/vc-git.el @@ -817,27 +817,29 @@ vc-git--cmds-in-progress cmds)) =20 (defun vc-git-dir-extra-headers (dir) - (let ((str (with-output-to-string - (with-current-buffer standard-output - (vc-git--out-ok "symbolic-ref" "HEAD")))) + (let ((str (vc-git--out-str "symbolic-ref" "HEAD")) (stash-list (vc-git-stash-list)) (default-directory dir) (in-progress (vc-git--cmds-in-progress)) =20 - branch remote remote-url stash-button stash-string) + branch remote-url stash-button stash-string upstream-branch) (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str) (progn (setq branch (match-string 2 str)) - (setq remote - (with-output-to-string - (with-current-buffer standard-output - (vc-git--out-ok "config" - (concat "branch." branch ".remote"))))) - (when (string-match "\\([^\n]+\\)" remote) - (setq remote (match-string 1 remote))) - (when (> (length remote) 0) - (setq remote-url (vc-git-repository-url dir remote)))) - (setq branch "not (detached HEAD)")) + (let ((remote (vc-git--out-str + "config" (concat "branch." branch ".remote"))) + (merge (vc-git--out-str + "config" (concat "branch." branch ".merge")))) + (when (string-match "\\([^\n]+\\)" remote) + (setq remote (match-string 1 remote))) + (pcase remote + ("." + (setq remote-url "none (tracking local branch)")) + ((pred (not string-empty-p)) + (setq remote-url (vc-git-repository-url dir remote)))) + (when (string-match "^\\(refs/heads/\\)?\\(.+\\)$" merge) + (setq upstream-branch (match-string 2 merge))))) + (setq branch "none (detached HEAD)")) (when stash-list (let* ((len (length stash-list)) (limit @@ -896,6 +898,11 @@ vc-git-dir-extra-headers (propertize "Remote : " 'face 'vc-dir-header) (propertize remote-url 'face 'vc-dir-header-value))) + (when upstream-branch + (concat + "\n" + (propertize "Tracking : " 'face 'vc-dir-header) + (propertize upstream-branch 'face 'vc-dir-header-value))) ;; For now just a heading, key bindings can be added later for variou= s bisect actions (when (memq 'bisect in-progress) (propertize "\nBisect : in progress" 'face 'vc-dir-status-warn= ing)) @@ -2219,8 +2226,17 @@ vc-git--call (apply #'process-file vc-git-program nil buffer nil "--no-pager" comma= nd args))) =20 (defun vc-git--out-ok (command &rest args) + "Run `git COMMAND ARGS...' and insert standard output in current buffer. +Return whether the process exited with status zero." (zerop (apply #'vc-git--call '(t nil) command args))) =20 +(defun vc-git--out-str (command &rest args) + "Run `git COMMAND ARGS...' and return standard output. +The exit status is ignored." + (with-output-to-string + (with-current-buffer standard-output + (apply #'vc-git--out-ok command args)))) + (defun vc-git--run-command-string (file &rest args) "Run a git command on FILE and return its output as string. FILE can be nil." diff --git a/test/lisp/vc/vc-git-tests.el b/test/lisp/vc/vc-git-tests.el index c52cd9c5875..1f8abe7b9b0 100644 --- a/test/lisp/vc/vc-git-tests.el +++ b/test/lisp/vc/vc-git-tests.el @@ -24,6 +24,8 @@ =20 ;;; Code: =20 +(require 'ert-x) +(require 'vc) (require 'vc-git) =20 (ert-deftest vc-git-test-program-version-general () @@ -81,4 +83,41 @@ vc-git-test-annotate-time (should-not (vc-git-annotate-time)) (should-not (vc-git-annotate-time)))) =20 +(defmacro vc-git-test--with-repo (name &rest body) + "Initialize a repository in a temporary directory and evaluate BODY. + +The current directory will be set to the top of that repository; NAME +will be bound to that directory's file name. Once BODY exits, the +directory will be deleted." + (declare (indent 1)) + `(ert-with-temp-directory ,name + (let ((default-directory ,name)) + (vc-create-repo 'Git) + ,@body))) + +(defun vc-git-test--run (&rest args) + "Run git ARGS=E2=80=A6, check for non-zero status, and return output." + (with-temp-buffer + (apply 'vc-git-command t 0 nil args) + (buffer-string))) + +(ert-deftest vc-git-test-dir-track-local-branch () + "Test that `vc-dir' works when tracking local branches. See bug#68183." + (vc-git-test--with-repo repo + ;; Create an initial commit to get a branch started. + (write-region "hello" nil "README") + (vc-git-test--run "add" "README") + (vc-git-test--run "commit" "-mFirst") + ;; Get current branch name lazily, to remain agnostic of + ;; init.defaultbranch. + (let ((upstream-branch + (string-trim (vc-git-test--run "branch" "--show-current")))) + (vc-git-test--run "checkout" "--track" "-b" "hack" upstream-branch) + (vc-dir default-directory) + (pcase-dolist (`(,header ,value) + `(("Branch" "hack") + ("Tracking" ,upstream-branch))) + (goto-char (point-min)) + (re-search-forward (format "^%s *: %s$" header value)))))) + ;;; vc-git-tests.el ends here --=20 2.43.0 --=-=-=--