agkozak/zsh-z

Feature: Prefer directories which contain a .git directory, if query matches repo directory name

zachriggle opened this issue · 1 comments

I use zsh-z primarily to jump between various directories that are tracked as git repositories.

Occasionally, there will be a higher-'frecency' match that is a subpath of some repository, when I really want to change to a repository that matches the query.

It would be nice if zsh-z had an optional flag to prioritize directories which contain a .git directory. This could be done at scoring time, or at invocation-time (i.e. of all matches for query QUERY, take the first one that has a .git directory, if the directory name itself matches QUERY, otherwise the most 'frecent' directory). This would only take effect if the query were a match for the name of the repository itself, so that e.g. z somedir doesn't drop me at the root of the repository, but would behave as it does not normally.

TL;DR An environment variable that prioritizes git repository roots if the search query matches the directory name containing a git repository (but queries for subpaths that do not match the repo name are unchanged).

Examples

I want to change to the git repository that has 'iob' in the name. I recently spent some time in a completely unrelated directory that has 'iob' somewhere run its subpath, and it is now ranked higher. This is undesirable for my use case.

Current behavior

~ ❯❯❯ z iob
~/s/C/m/m/IOBxxx/P/somedir ❯❯❯

Desired behavior

~ ❯❯❯ z iob
~/s/C/iobXYZ ❯❯❯ ls -d .git
.git

Your idea is a clever one. I’ll play around with it a bit.

In the meantime, I think you might be able to accomplish what you want by using the setting

ZSHZ_UNCOMMON=1

It’s an alternative way of deciding which directory to switch to. If you run

z bar

and the best match is /foo/bar/bat, you’ll end up in /foo/bar because you searched for bar. If you’d run z foo and the best match was /foo/bar/bat, you’d end up in /foo.

That’s the setting I use myself. See if it helps.