Skip to content

Commit

Permalink
fix: add browser nanoseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
jayree committed Jun 20, 2023
1 parent 99859d4 commit 7e933a5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
11 changes: 11 additions & 0 deletions __tests__/test-GitIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,15 @@ describe('GitIndex', () => {
expect(index.entriesMap.get('c').stages.length).toBe(1)
})
})

it('ensure nanoseconds are always present', async () => {
const { fs, dir } = await makeFixture('test-GitIndex')
const buffer = await fs.read(path.join(dir, 'simple-index'))
const index = await GitIndex.from(buffer)

const ctimeSeconds = index.entriesMap.get('world.txt').ctimeSeconds
const ctimeNanoseconds = index.entriesMap.get('world.txt').ctimeNanoseconds
expect(ctimeSeconds).not.toBeNull()
expect(ctimeNanoseconds).not.toBeNull()
})
})
29 changes: 13 additions & 16 deletions __tests__/test-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,19 @@ describe('status', () => {
* Without the fix, the second write is not detected and the status is 'added'
* instead of '*added'
*/
;(process.browser ? xit : it)(
'status of a file changed twice within a second',
async () => {
const { fs, dir } = await makeFixture('test-empty')
const file = 'a.txt'
it('status of a file changed twice within a second', async () => {
const { fs, dir } = await makeFixture('test-empty')
const file = 'a.txt'

const start = Date.now()
await fs.write(path.join(dir, file), 'Hi')
await add({ fs, dir, filepath: file })
const a = await status({ fs, dir, filepath: file })
expect(a).toEqual('added')
await fs.write(path.join(dir, file), 'Ho')
const start = Date.now()
await fs.write(path.join(dir, file), 'Hi')
await add({ fs, dir, filepath: file })
const a = await status({ fs, dir, filepath: file })
expect(a).toEqual('added')
await fs.write(path.join(dir, file), 'Ho')

const b = await status({ fs, dir, filepath: file })
expect(b).toEqual('*added')
expect(Date.now() - start).toBeLessThan(1000)
}
)
const b = await status({ fs, dir, filepath: file })
expect(b).toEqual('*added')
expect(Date.now() - start).toBeLessThan(1000)
})
})
1 change: 1 addition & 0 deletions src/models/FileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ export class FileSystem {
async lstat(filename) {
try {
const stats = await this._lstat(filename)
// For non-browser (local) scenarios regular git 'add' command might be used to write the index. Therefore we need to have the exact nanoseconds (see. normalizeStats.js SecondsNanoseconds ).
if (!process.browser) {
const statsNs = await this._lstat(filename, { bigint: true })
stats.mtimeNs = statsNs.mtimeNs
Expand Down
6 changes: 5 additions & 1 deletion src/utils/normalizeStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ function SecondsNanoseconds(givenSeconds, givenNanoseconds, nanoseconds, date) {
return [givenSeconds, givenNanoseconds]
}
let seconds
// For browser scenarios isomorphic-git 'add' will be used to write the index. Reading and writing are handled using normalizeStats ( see FileSystem.js lstat() ).
if (nanoseconds === undefined) {
const milliseconds = date.valueOf()
seconds = Math.trunc(milliseconds / 1000)
} else {
nanoseconds = (milliseconds - seconds * 1000) * 1000000 // nanoseconds with millisecond precision
}
// For non-browser (local) scenarios
else {
seconds = Number(nanoseconds / BigInt(1e9))
nanoseconds = Number(nanoseconds % BigInt(1e9))
}
Expand Down

0 comments on commit 7e933a5

Please sign in to comment.