Skip to content

Commit 25b3ee8

Browse files
fix(files): Fallback to seconds check if file modification status
In case `nsec` is 0 in the fs stat, fallback to `sec` value.
1 parent af0713d commit 25b3ee8

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lua/orgmode/files/file.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ local Footnote = require('orgmode.objects.footnote')
1111
local Memoize = require('orgmode.utils.memoize')
1212

1313
---@class OrgFileMetadata
14-
---@field mtime number
14+
---@field mtime number File modified time in nanoseconds
15+
---@field mtime_sec number File modified time in seconds
1516
---@field changedtick number
1617

1718
---@class OrgFileOpts
@@ -48,6 +49,7 @@ function OrgFile:new(opts)
4849
index = 0,
4950
metadata = {
5051
mtime = stat and stat.mtime.nsec or 0,
52+
mtime_sec = stat and stat.mtime.sec or 0,
5153
changedtick = opts.bufnr and vim.api.nvim_buf_get_changedtick(opts.bufnr) or 0,
5254
},
5355
}
@@ -150,7 +152,11 @@ function OrgFile:is_modified()
150152
if not stat then
151153
return false
152154
end
153-
return stat.mtime.nsec ~= self.metadata.mtime
155+
if stat.mtime.nsec > 0 then
156+
return stat.mtime.nsec ~= self.metadata.mtime
157+
end
158+
159+
return stat.mtime.sec ~= self.metadata.mtime_sec
154160
end
155161

156162
---Parse the file and update the root node
@@ -901,6 +907,7 @@ function OrgFile:_update_lines(lines, bufnr)
901907
local stat = vim.uv.fs_stat(self.filename)
902908
if stat then
903909
self.metadata.mtime = stat.mtime.nsec
910+
self.metadata.mtime_sec = stat.mtime.sec
904911
end
905912
return self
906913
end

tests/plenary/files/file_spec.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe('OrgFile', function()
2020
assert.are.same('* Headline 1', file.content)
2121
local stat = vim.uv.fs_stat(filename) or {}
2222
assert.are.same(stat.mtime.nsec, file.metadata.mtime)
23+
assert.are.same(stat.mtime.sec, file.metadata.mtime_sec)
2324
assert.are.same(0, file.metadata.changedtick)
2425
end)
2526

@@ -39,6 +40,7 @@ describe('OrgFile', function()
3940
assert.are.same('* Headline 2', file.content)
4041
local stat = vim.uv.fs_stat(filename) or {}
4142
assert.are.same(stat.mtime.nsec, file.metadata.mtime)
43+
assert.are.same(stat.mtime.sec, file.metadata.mtime_sec)
4244
assert.are.same(0, file.metadata.changedtick)
4345
vim.cmd('write!')
4446
file:reload_sync()

0 commit comments

Comments
 (0)