module

Memo::Files

File indexing support for memo CLI

Provides directory walking with ignore file support, binary file detection, and file metadata tracking for incremental updates.

Instance methods

binary?(path : String) : Bool

Check if file is binary using libmagic

Source
binary_by_content?(path : String, sample_size : Int32 = 8192) : Bool

Fallback binary detection by checking for null bytes

Source
collect(root : String, ignore_file : String = ".gitignore") : Array(String)

Collect all text files in directory

Source
content_hash(path : String) : Bytes

Compute SHA256 hash of file content

Source
count(db : DBHandle) : Int64

Count indexed files

Source
delete(db : DBHandle, source_id : Int64) : Bool

Delete file record

Source
file_info(path : String, root : String) : FileInfo

DEPRECATED Paths are absolute now; use #file_info(path)

Source
file_info(path : String) : FileInfo

Get file info (path, hash, mtime, size). The path is absolute with symlinks resolved, so a file has one record however it's reached.

Source
find(db : DBHandle, path : String, root : String, rekey : Bool = true) : FileRecord | Nil

The record for the file at path (absolute, as file_info gives it). Before memo keyed files by absolute path, it keyed them by their path relative to the directory indexed (root, absolute with symlinks resolved): a record under that key is found too, and with rekey moved to path.

Source
get_by_hash(db : DBHandle, hash : Bytes) : FileRecord | Nil

Get file record by content hash

Source
get_by_path(db : DBHandle, path : String) : FileRecord | Nil

Get file record by path

Source
get_by_source(db : DBHandle, source_id : Int64) : FileRecord | Nil

Get file record by source_id

Source
list(db : DBHandle, limit : Int32 = 100, offset : Int32 = 0) : Array(FileRecord)

List all indexed files

Source
needs_update?(record : FileRecord, current_mtime : Int64) : Bool

Check if file needs re-indexing (mtime changed)

Source
store(db : DBHandle, source_id : Int64, info : FileInfo)

Store file record in database

Source
under(db : DBHandle, root : String) : Array(Tuple(Int64, String))

The files recorded under directory root (absolute), as {source_id, path}

Source
walk(root : String, ignore_file : String = ".gitignore", &block : String -> )

Walk directory and yield text files, respecting ignore patterns

Uses ignoreme for .gitignore-style filtering. Uses magic.cr to detect and skip binary files. Symlinks inside the tree are skipped, to files and directories alike: they can lead out of it (a checkout could link to your home directory) or loop. The root itself may be one.

Options:

  • root: Directory to walk
  • ignore_file: Ignore file name (default ".gitignore")
Source

Nested types