Skip to content

Commit 63bae0f

Browse files
authored
fix (path:normalize): handle filenames with the same prefix as the home directory (#355)
Scenario: home: /home/foo file: /home/foobar/baz.txt normalize returned ~/bar/baz.txt instead of /home/foobar/baz.txt
1 parent fb1a16e commit 63bae0f

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lua/plenary/path.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,11 @@ function Path:normalize(cwd)
349349
-- Substitute home directory w/ "~"
350350
-- string.gsub is not useful here because usernames with dashes at the end
351351
-- will be seen as a regexp pattern rather than a raw string
352-
local start, finish = string.find(self.filename, path.home, 1, true)
352+
local home = path.home
353+
if string.sub(path.home, -1) ~= path.sep then
354+
home = home .. path.sep
355+
end
356+
local start, finish = string.find(self.filename, home, 1, true)
353357
if start == 1 then
354358
self.filename = "~" .. path.sep .. string.sub(self.filename, (finish + 1), -1)
355359
end

tests/plenary/path_spec.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ describe("Path", function()
235235
p._cwd = "/tmp/lua"
236236
assert.are.same("~/test_file", p:normalize())
237237
end)
238+
239+
it("handles filenames with the same prefix as the home directory", function()
240+
local p = Path:new "/home/test.old/test_file"
241+
p.path.home = "/home/test"
242+
assert.are.same("/home/test.old/test_file", p:normalize())
243+
end)
238244
end)
239245

240246
describe(":shorten", function()

0 commit comments

Comments
 (0)