larkery / zsh-histdb

A slightly better history for zsh
MIT License
1.27k stars 75 forks source link

`--in` option doesn't distinguish between subdir and partial path match #54

Open henrebotha opened 5 years ago

henrebotha commented 5 years ago

If I call histdb --in while in ~/dev/vim, it yields results from ~/dev/vim as well as ~/dev/vimrc. I assume the logic that is meant to handle the "current dir or below" logic is using some kind of simple substring matching, therefore seeing ~/dev/vimrc as including the substring ~/dev/vim and returning a match.

In fact, it seems that's exactly what's happening: https://github.com/larkery/zsh-histdb/blob/0a7a52a59fbc3244392ef5d19ef7c4d6f0b8838b/sqlite-history.zsh#L220

larkery commented 5 years ago

You are quite right - I'll have a look at the pattern handling in sqlite and see whether this can be improved.

JordanP-Dev commented 2 years ago

I think the following will work, right? It will match the exact directory or the directory name followed by a slash, then any other characters.

diff --git a/sqlite-history.zsh b/sqlite-history.zsh
index 32c3990..87b3ff4 100644
--- a/sqlite-history.zsh
+++ b/sqlite-history.zsh
@@ -305,7 +305,7 @@ histdb () {
         local dir=""
         for dir ($indirs); do
             dir="${${${dir#--in}#=}:-$PWD}"
-            dirwhere="${dirwhere}${dirwhere:+ or }places.dir like '$(sql_escape $dir)%'"
+            dirwhere="${dirwhere}${dirwhere:+ or }places.dir = '$(sql_escape $dir)%' or places.dir like '$(sql_escape $dir/)%'"
         done
         for dir ($atdirs); do
             dir="${${${dir#--at}#=}:-$PWD}"
larkery commented 2 years ago

This looks correct, thanks - I will apply this change.

JordanP-Dev commented 2 years ago

Oops, there is a mistake there. I accidentally left the percent sign on the first comparison. It should be:

diff --git a/sqlite-history.zsh b/sqlite-history.zsh
index 32c3990..87b3ff4 100644
--- a/sqlite-history.zsh
+++ b/sqlite-history.zsh
@@ -305,7 +305,7 @@ histdb () {
         local dir=""
         for dir ($indirs); do
             dir="${${${dir#--in}#=}:-$PWD}"
-            dirwhere="${dirwhere}${dirwhere:+ or }places.dir like '$(sql_escape $dir)%'"
+            dirwhere="${dirwhere}${dirwhere:+ or }places.dir = '$(sql_escape $dir)' or places.dir like '$(sql_escape $dir/)%'"
         done
         for dir ($atdirs); do
             dir="${${${dir#--at}#=}:-$PWD}"