Skip to content
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

Fix git-root Not Working Correctly for Repositories in symlinks & Add Customisation Options #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

MatteoCampinoti94
Copy link

@MatteoCampinoti94 MatteoCampinoti94 commented Oct 27, 2021

Fix git-root With symlinks

Problem

As the title says, prompt-pwd would not remove the git root from the current path if the repository was located under a symlink.

The problem is caused by the fact that command git rev-parse --show-toplevel returns the physical path of the git root, whereas $PWD contains the symbolic one.

Example

➜ /Users/matteoc/iCloud/Code/prompt-pwd master ✓

The repository is located inside a symlinked folder (iCloud), and instead of showing just prompt-pwd it shows the entire physical path.

Solution

This was easily fixed by using pwd -P instead of $PWD.

Result

➜ prompt-pwd master ✓

The prompt is now printed correctly regardless of whether the repository was reached with a physical or symbolic path.

Consequences

Calling pwd -P is slower than using $PWD directly. I ran the following benchmarks to test it on my system:

( for i in {1..1000}; do; echo "$(pwd -P)" > /dev/null; done; )
0.21s user 0.43s system 93% cpu 0.689 total
( for i in {1..1000}; do; echo "$PWD" > /dev/null; done; )
0.01s user 0.02s system 98% cpu 0.031 total

The actual execution time for the singles cases are:

  • pwd -P 0.2ms
  • $PWD 0.01ms

I am using a relatively powerful machine, but even if the script was running on a machine an order of magnitude slower, it would still take a millisecond at most for either pwd or $PWD.

@MatteoCampinoti94 MatteoCampinoti94 changed the title Fix git-root not working correctly for repositories in symlinks. Fix git-root Not Working Correctly for Repositories in symlinks, Add Customization Options Oct 27, 2021
@MatteoCampinoti94 MatteoCampinoti94 changed the title Fix git-root Not Working Correctly for Repositories in symlinks, Add Customization Options Fix git-root Not Working Correctly for Repositories in symlinks & Add Customisation Options Oct 27, 2021
@MatteoCampinoti94
Copy link
Author

MatteoCampinoti94 commented Oct 27, 2021

Add Customisation Options

Path Format

This first modifications adds the :zim:prompt-pwd:path-format style key to use as path format instead of the default ${(%):-%~}.

The path-format style is only used outside of git repositories of if the git-root options is disabled.

Example

# zstyle ':zim:prompt-pwd:path-format' format '%~ :-)'~/path/to/folder/ :-)

Path Length

This second modifications adds the :zim:prompt-pwd:path-length style key to use as the maximum number of elements to visualise in the path (excluding the root/~ and the separator). The minimum accepted value is 1.

The path-format option takes precedence over path-length as they cannot really be made to work together, and path-format is much more specific.

Like path-format, path-length is only used outside of git repositories of if the git-root options is disabled.

Example

Given the full path ~/iCloud/Documents/2021/Work, here are some examples:

# zstyle ':zim:prompt-pwd:path-length' path-length 3~/.../Documents/2021/Work`
# zstyle ':zim:prompt-pwd:path-length' path-length 1~/.../Work`
# zstyle ':zim:prompt-pwd:path-length' path-length 0~/iCloud/Documents/2021/Work`
# zstyle ':zim:prompt-pwd:path-length' path-length 5~/iCloud/Documents/2021/Work`

Problems

I am not exactly sure why, but the fish-style dir-length option does not work when together with path-length. I am not sure exactly how the substitution on prompt-pwd#L23 works, but I believe the problem is in that line since the splitting works.

@MatteoCampinoti94
Copy link
Author

I've updated the path length style option to use path-length instead of format to follow the same naming as the fifth dir length option.

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

Successfully merging this pull request may close these issues.

1 participant