{"thread":{"id":"61400","subject":"[PATCH] completion: zsh: stop leaking local cache variable","startedAt":"2024-04-30T21:53:18Z","lastAt":"2024-04-30T23:18:53Z","messageCount":2,"participants":["D. Ben Knoble via GitGitGadget","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"493822","messageId":"pull.1725.git.1714513995564.gitgitgadget@gmail.com","threadId":"61400","inReplyTo":null,"subject":"[PATCH] completion: zsh: stop leaking local cache variable","fromName":"D. Ben Knoble via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2024-04-30T21:53:15Z","receivedAt":"2024-04-30T21:53:18Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"From: \"D. Ben Knoble\" <ben.knoble+github@gmail.com>\n\nCompleting commands like \"git rebase\" in one repository will leak the\nlocal __git_repo_path into the shell's environment so that completing\ncommands after changing to a different repository will give the old\nrepository's references (or none at all).\n\nThe bug report on the mailing list [1] suggests one simple way to observe\nthis yourself:\n\nEnter the following commands from some directory:\n  mkdir a b b/c\n  for d (a b); git -C $d init && git -C $d commit --allow-empty -m init\n  cd a\n  git branch foo\n  pushd ../b/c\n  git branch bar\n\nNow type these:\n  git rebase <TAB>… # completion for bar available; C-c to abort\n  declare -p __git_repo_path # outputs /path/to/b/.git\n  popd\n  git branch # outputs foo, main\n  git rebase <TAB>… # completion candidates are bar, main!\n\nIdeally, the last typed <TAB> should be yielding foo, main.\n\nCommit beb6ee7163 (completion: extract repository discovery from\n__gitdir(), 2017-02-03) anticipated this problem by marking\n__git_repo_path as local in __git_main and __gitk_main for Bash\ncompletion but did not give the same mark to _git for Zsh completion.\nThus make __git_repo_path local for Zsh completion, too.\n\n[1]: https://lore.kernel.org/git/CALnO6CBv3+e2WL6n6Mh7ZZHCX2Ni8GpvM4a-bQYxNqjmgZdwdg@mail.gmail.com/\n\nSigned-off-by: D. Ben Knoble <ben.knoble+github@gmail.com>\n---\n    completion: zsh: stop leaking local cache variable\n    \n    Prevent leaking a local variable used to cache the repo path, which\n    breaks future completions in different repositories using the shell,\n    when using contributed Zsh completion.\n    \n    ------------------------------------------------------------------------\n    \n    I made a few attempts at starting a test script for this based on\n    https://unix.stackexchange.com/a/668827/301073, but that code doesn't\n    work and it was all becoming precariously complicated (sh starting zsh\n    to start zsh in a pty which would receive keystrokes and check specific\n    outputs: I couldn't make certain pieces work in a normal way locally,\n    let alone as part of Git's test suite). Suffice to say I have tested\n    this myself?\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1725%2Fbenknoble%2Ffix-zsh-completion-repo-cache-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1725/benknoble/fix-zsh-completion-repo-cache-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/1725\n\n contrib/completion/git-completion.zsh | 1 +\n 1 file changed, 1 insertion(+)\n\ndiff --git a/contrib/completion/git-completion.zsh b/contrib/completion/git-completion.zsh\nindex cac6f618817..f5877bd7a1e 100644\n--- a/contrib/completion/git-completion.zsh\n+++ b/contrib/completion/git-completion.zsh\n@@ -272,6 +272,7 @@ _git ()\n {\n \tlocal _ret=1\n \tlocal cur cword prev\n+\tlocal __git_repo_path\n \n \tcur=${words[CURRENT]}\n \tprev=${words[CURRENT-1]}\n\nbase-commit: 786a3e4b8d754d2b14b1208b98eeb0a554ef19a8\n-- \ngitgitgadget\n"},{"id":"493824","messageId":"xmqqzftawiau.fsf@gitster.g","threadId":"61400","inReplyTo":"pull.1725.git.1714513995564.gitgitgadget@gmail.com","subject":"Re: [PATCH] completion: zsh: stop leaking local cache variable","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2024-04-30T23:18:49Z","receivedAt":"2024-04-30T23:18:53Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"D. Ben Knoble via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> Commit beb6ee7163 (completion: extract repository discovery from\n> __gitdir(), 2017-02-03) anticipated this problem by marking\n> __git_repo_path as local in __git_main and __gitk_main for Bash\n> completion but did not give the same mark to _git for Zsh completion.\n> Thus make __git_repo_path local for Zsh completion, too.\n\nWill queue.  The explanation makes tons of sense.\n\nThanks.\n"}]}