Skip to content

Obey GIT_DIR if set for vcsh compatability #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 27, 2019
Merged

Conversation

alerque
Copy link
Contributor

@alerque alerque commented Mar 27, 2019

I've had my eye on powerlevel9k for a long time but never been convinced to take the dive until the git status caching thing in this fork hit the news wires and gave me a compelling reason to mash my configs around. One of the first things I noticed when switching from my long time theme (a fork of Agnoster) to powerlevel10k was that it did not handle VCSH repositories properly. VCSH relies on keeping the git directory in a disconnected state (out of the current directory path). Git handles this quite gracefully though the use of the GIT_DIR variable, but gitstatus was ignoring this.

I'm not sure if this is the best fix, perhaps it should be fixed in gitstatus instead, but this fix at least got me up and running. If there is a better place to fix it let me know and I'd be happy to make the contribution.

@romkatv
Copy link
Owner

romkatv commented Mar 27, 2019

You'll also need to change this line:

typeset -gH _P9K_NEXT_VCS_DIR=$PWD

To this:

typeset -gH _P9K_NEXT_VCS_DIR=${GIT_DIR:-$PWD}

@romkatv
Copy link
Owner

romkatv commented Mar 27, 2019

There are a few more lines that need to be changed. Here's a diff that worked for me.

diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme
index 48c393b..d750626 100755
--- a/powerlevel9k.zsh-theme
+++ b/powerlevel9k.zsh-theme
@@ -1648,7 +1648,7 @@ function _p9k_vcs_style() {
 function _p9k_vcs_render() {
   if [[ -v _P9K_NEXT_VCS_DIR ]]; then
     local -a msg
-    local dir=$PWD
+    local dir=${GIT_DIR:-$PWD}
     while true; do
       msg=("${(@0)${_P9K_LAST_GIT_PROMPT[$dir]}}")
       [[ $#msg != 0 || $dir == / ]] && break
@@ -1832,9 +1832,9 @@ function _p9k_vcs_gitstatus() {
   [[ $POWERLEVEL9K_DISABLE_GITSTATUS == true ]] && return 1
   if [[ $_P9K_REFRESH_REASON == precmd ]]; then
     if [[ -v _P9K_NEXT_VCS_DIR ]]; then
-      typeset -gH _P9K_NEXT_VCS_DIR=$PWD
+      typeset -gH _P9K_NEXT_VCS_DIR=${GIT_DIR:-$PWD}
     else
-      local dir=$PWD
+      local dir=${GIT_DIR:-$PWD}
       local -F timeout=$POWERLEVEL9K_VCS_MAX_SYNC_LATENCY_SECONDS
       while true; do
         case "$_P9K_GIT_SLOW[$dir]" in
@@ -1844,7 +1844,7 @@ function _p9k_vcs_gitstatus() {
         esac
       done
       typeset -gFH _P9K_GITSTATUS_START_TIME=$EPOCHREALTIME
-      gitstatus_query -t $timeout -c _p9k_vcs_resume POWERLEVEL9K || return 1
+      gitstatus_query -d ${GIT_DIR:-$PWD} -t $timeout -c _p9k_vcs_resume POWERLEVEL9K || return 1
       [[ $VCS_STATUS_RESULT == tout ]] && typeset -gH _P9K_NEXT_VCS_DIR=""
     fi
   fi

I have a few questions about GIT_DIR.

  • Can it be relative?
  • Can it start with ~/ which is supposed to be expanded to $HOME/?

If my reading of VCSH code is correct, the answers are yes and no respectively. If this is right, we'll need :a to ensure we are always passing absolute paths to gitstatus_query.

@alerque
Copy link
Contributor Author

alerque commented Mar 27, 2019

This isn't vcsh specific, so VCSH's specific implementation (and the fact that it is somewhat normalized) isn't the issue here, as far as I understand from the documentation and testing it right now, it's entirely possible for GIT_DIR to to be relative or start with ~/, so both of those cases do need to work.

I fixed the other places you mentioned of PWD and added the only way I could see to add the :a flag as well. Nested expansions like this aren't the prettiest thing in the world, if you know a better way I'm game. If that's the best way these commits could optionally be squashed.

@romkatv romkatv merged commit d14ae7e into romkatv:master Mar 27, 2019
@romkatv
Copy link
Owner

romkatv commented Mar 27, 2019

Thanks for the contribution!

@romkatv
Copy link
Owner

romkatv commented Mar 27, 2019

[...] it's entirely possible for GIT_DIR to [...] start with ~/ [...]

It would be unusual if such expansion was performed, but stranger things happen in life, so who knows.

I think VCSH won't expand ~. This check will fail: https://github.com/RichiH/vcsh/blob/eadb8df6aa71a76e5be36492edcadb118bd862ac/vcsh#L643.

Does it work in your environment if you set GIT_DIR or some other variable from which GIT_DIR is derived to something that starts with ~/? Make sure to quote it when setting to prevent expansion by the shell. Like this: GIT_DIR='~/blah'.

@alerque
Copy link
Contributor Author

alerque commented Mar 27, 2019

Oh botheration. When I tested this I was exporting the variable I was not quoting it, so it was getting expanded at assignment. Oops.

For your information you don't need vcsh to test this, just do something like this:

$ cd /tmp
$ export GIT_DIR=~/.zprezto/.git
$ git log -1

If it's working right any git commands you run at that point should act on the repository set instead of any found in PWD.

As such it looks like your original assessment of "yes/no" was correct. Yes it can be relative, no it does not expand '~' on it's own.

@romkatv
Copy link
Owner

romkatv commented Mar 27, 2019

For your information you don't need vcsh to test this, just do something like this...

Oh, so it's a variable recognized by git itself? TIL.

FWIW, I have several bare git repos overlaying my home directory (various dotfiles, settings, etc.). I access them via aliases that pass --git-dir and --work-dir. For example:

alias dotfiles-public='git --git-dir=$HOME/.dotfiles-public/.git --work-tree=$HOME'

So if I want to add a random file to my dotfiles-public git repo, I type dotfiles-public add -f FILE, etc.

@alerque
Copy link
Contributor Author

alerque commented Mar 27, 2019

Yes, this is a git feature exploited by vcsh to do almost exactly what you are describing with your alias system, just a bit more comprehensively. Having the environment variable set means you don't have to add it as an argument to ever command you run. You could easily manually do the same thing in a shell to work with your own overlays, VCSH just handles the context switching a bit thoroughly and makes interaction with these overlays a bit easier.

@alerque
Copy link
Contributor Author

alerque commented Mar 27, 2019

Incidentally this is why I wondered if it might not be useful to implement this in gitstatus instead of in the shell prompt theme. Given that any git commands that run in the shell are going to act on the disconnected repository, it would make sense for gitstatus to do the same thing rather than itself defaulting to PWD. In this way it would mimic what git status already does.

@romkatv
Copy link
Owner

romkatv commented Mar 27, 2019

Maybe. Another option is to make -d mandatory. I agree that the current default is error-prone. Anyone who doesn't pass -d is setting themselves up for the exact bug you've fixed in Powerlevel10k.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants