@@ -311,19 +311,18 @@ def _from_normalized_path(self, path: Optional[str]) -> Optional[str]:
311
311
return relpath
312
312
313
313
def _create (self , path : str ) -> str :
314
- path = self ._normalize_path (path )
315
- id = self ._uuid ()
316
- self .con .execute ("INSERT INTO Files (id, path) VALUES (?, ?)" , (id , path ))
317
- return id
318
-
319
- def index (self , path : str ) -> str :
320
314
path = self ._normalize_path (path )
321
315
row = self .con .execute ("SELECT id FROM Files WHERE path = ?" , (path ,)).fetchone ()
322
316
existing_id = row and row [0 ]
323
317
324
318
if existing_id :
325
319
return existing_id
320
+
321
+ id = self ._uuid ()
322
+ self .con .execute ("INSERT INTO Files (id, path) VALUES (?, ?)" , (id , path ))
323
+ return id
326
324
325
+ def index (self , path : str ) -> str :
327
326
# create new record
328
327
id = self ._create (path )
329
328
self .con .commit ()
@@ -613,6 +612,7 @@ def _sync_file(self, path, stat_info):
613
612
"SELECT id, path, crtime FROM Files WHERE ino = ?" , (stat_info .ino ,)
614
613
).fetchone ()
615
614
615
+
616
616
# if ino is not in database, return None
617
617
if src is None :
618
618
return None
@@ -626,6 +626,7 @@ def _sync_file(self, path, stat_info):
626
626
# otherwise update existing record with new path, moving any indexed
627
627
# children if necessary. then return its id
628
628
self ._update (id , path = path )
629
+
629
630
if stat_info .is_dir and old_path != path :
630
631
self ._move_recursive (old_path , path )
631
632
self ._update_cursor = True
@@ -672,6 +673,17 @@ def _create(self, path, stat_info):
672
673
dangerous and may throw a runtime error if the file is not guaranteed to
673
674
have a unique `ino`.
674
675
"""
676
+ # If the path exists
677
+ existing_id , ino = None , None
678
+ row = self .con .execute ("SELECT id, ino FROM Files WHERE path = ?" , (path ,)).fetchone ()
679
+ if row :
680
+ existing_id , ino = row
681
+
682
+ # If the file ID already exists and the current file matches our records
683
+ # return the file ID instead of creating a new one.
684
+ if existing_id and stat_info .ino == ino :
685
+ return existing_id
686
+
675
687
id = self ._uuid ()
676
688
self .con .execute (
677
689
"INSERT INTO Files (id, path, ino, crtime, mtime, is_dir) VALUES (?, ?, ?, ?, ?, ?)" ,
0 commit comments