This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
ZigStack is a comprehensive file management CLI tool written in Zig. It provides six powerful commands for organizing, analyzing, and managing files:
- organize: Categorize files by extension, date, size, or duplicates
- analyze: Disk usage analysis with visualization and content metadata
- dedupe: Find and manage duplicate files with interactive or automatic resolution
- archive: Archive old files based on age with optional compression
- watch: Monitor directories and auto-organize new files with custom rules
- workspace: Manage developer workspaces and clean build artifacts
The tool emphasizes safety (dry-run by default), performance (>1000 files/sec), and flexibility (extensive configuration options).
- Entry Point:
src/main.zig- Main application entry, command routing, and CLI - Core Modules:
src/core/- File info, config, organization, tracking, utilities - Commands:
src/commands/- Six command implementations (organize, analyze, dedupe, archive, watch, workspace)organize.zig- File categorization and organizationanalyze.zig- Disk usage analysis and reportingdedupe.zig- Duplicate file detection and managementarchive.zig- Age-based file archivingwatch.zig- Directory monitoring with rules engineworkspace.zig- Developer workspace managementcommand.zig- Command registry and dispatch systemwatch_rules.zig- Rules engine for watch command
- Test Helpers:
src/test_helpers.zig- Reusable test utilities and scenarios - Build Configuration:
build.zig- Multi-target build with separate test runners (test, test-unit, test-integration) - Documentation:
docs/- Comprehensive documentation including guides, command references, FAQ, and troubleshooting
File Management:
FileInfo: Extended withsize,created_time,modified_time,hashfor advanced featuresFileCategory: Enum with 9 categories (Documents, Images, Videos, Audio, Archives, Code, Data, Configuration, Other)OrganizationPlan: HashMap for categories, StringHashMap for date/size-based directory structures
Configuration:
Config: CLI flags including date/size/duplicate/recursive optionsConfigData: JSON-based custom category definitionsDateFormat: year, year_month, year_month_dayDuplicateAction: skip, rename, replace, keep_both
Safety:
MoveTracker: Records all file moves for rollback capabilityMoveRecord: Original and destination paths for each move
File Analysis:
getFileExtension(): Handles regular files, hidden files, no-extension casescategorizeFileByExtension(): Maps extensions to categories (case-insensitive)getFileStats(): Retrieves size, timestamps from filesystemcalculateFileHash(): SHA-256 hashing for duplicate detection
Organization Logic:
listFiles(): Main directory traversal and categorizationformatDatePath(): Converts timestamps to date-based paths (year/month/day)organizeBySizeAndCategory(): Separates large files from regular filesdetectDuplicates(): SHA-256 based duplicate detection with configurable actions
Operations:
createDirectories(): Creates category folders with conflict handlingmoveFiles(): Moves files with rollback support on errorsresolveFilenameConflict(): Auto-renames conflicting files (file_1.txt, file_2.txt, etc.)
# Build the project
zig build
# Run with preview mode (default, no changes)
zig build run -- /path/to/analyze
# Run with file operations
zig build run -- --move /path/to/organize
# Build directly and run
./zig-out/bin/zigstack --move /tmp/testThe build system supports multiple test targets for focused testing:
# Run all tests (unit + integration)
zig build test
# Run only unit tests (core modules and commands)
zig build test-unit
# Run only integration tests (backward compatibility, workflows)
zig build test-integration
# List all available build targets
zig build --helpTest Organization:
- Unit Tests: Focus on individual functions and modules
src/core/utils_test.zig- Core utility functionssrc/commands/command_test.zig- Command parsing and routing
- Integration Tests: Test multi-module workflows
src/commands/backward_compat_test.zig- Backward compatibility scenarios
- Test Helpers:
src/test_helpers.zigprovides reusable test utilities:TestScenario- Managed temporary test directoriescreateTestFile()- File creation helpersverifyFileMoved()- Operation verificationcountFilesInDir()- Directory analysis
Using Test Helpers:
const helpers = @import("test_helpers.zig");
test "my test" {
var scenario = try helpers.TestScenario.init(allocator);
defer scenario.deinit();
try scenario.createFile("test.txt", "content");
// Test operations...
}# Quick compile check
zig build
# Test-driven development with focused testing
zig build test-unit && zig build test-integration
# Full test suite
zig build test
# Run application
zig build run -- /tmp/test
# Test advanced features
zig build run -- --by-date --by-size --detect-dups --recursive --verbose --move /tmp/testzigstack <command> [OPTIONS] <directory>
# or (backward compatible with v0.2.0)
zigstack [OPTIONS] <directory> # Implies 'organize' command- organize - Categorize and organize files (default command)
- analyze - Disk usage analysis with visualization
- dedupe - Find and manage duplicate files
- archive - Archive files older than specified age
- watch - Monitor directory and auto-organize files
- workspace - Manage developer workspace projects
-d/--dry-run: Preview mode (default)-m/--move: Create directories and move files--by-date --date-format [year|year-month|year-month-day]: Organize by date--by-size --size-threshold MB: Separate large files--detect-dups --dup-action [skip|rename|replace|keep-both]: Handle duplicates--recursive --max-depth N: Process subdirectories-V/--verbose: Detailed logging
- analyze:
--content,--json,--top N,--min-size MB - dedupe:
--auto [keep-oldest|keep-newest|keep-largest],--hardlink,--delete,--format [json|csv] - archive:
--older-than DURATION,--dest PATH,--compress [tar.gz],--move - watch:
--rules FILE,--interval SECONDS,--daemon - workspace:
scan|cleanup,--strategy [conservative|moderate|aggressive],--project-type TYPE
Comprehensive test coverage with modular organization:
The build system provides three test execution modes:
-
zig build test- All tests (unit + integration)- Runs complete test suite
- Uses
src/main.zigas root module for all imports
-
zig build test-unit- Unit tests only- Filters:
core/utils_test,commands/command_test - Fast execution for TDD workflows
- Tests individual functions and modules
- Filters:
-
zig build test-integration- Integration tests only- Filters:
commands/backward_compat_test - Tests multi-module workflows and scenarios
- Verifies system behavior end-to-end
- Filters:
Unit Tests (test-unit):
- Extension extraction and categorization
- Date format parsing and path generation
- Hash calculation and duplicate detection
- Config parsing and validation
- Command parsing and routing
Integration Tests (test-integration):
- Directory creation workflows
- File moving with conflict resolution
- Rollback functionality on errors
- Empty directory and special character handling
- Backward compatibility with legacy CLI
Edge Case Coverage:
- Empty filenames, very long extensions
- Invalid characters, hidden files
- Boundary conditions (no extension, dots in names)
- Date edge cases (leap years, timezones)
- Large files and memory-efficient hashing
src/test_helpers.zig provides reusable test infrastructure:
Core Functions:
createTempTestDir()- Create unique temporary directoriesremoveTempTestDir()- Clean up test directoriescreateTestFile()- Create files with contentcreateTestFilesWithExtensions()- Batch file creationcreateTestDirStructure()- Create directory hierarchiescountFilesInDir()- Count files with optional extension filterfileExistsInDir()- Check file existencecreateTestFileWithSize()- Create files of specific sizesverifyFileMoved()- Verify file move operations
TestScenario Builder:
var scenario = try TestScenario.init(allocator);
defer scenario.deinit(); // Automatic cleanup
try scenario.createFile("test.txt", "content");
try scenario.createFiles(&[_][]const u8{".jpg", ".png"});
try scenario.createSubdir("Documents");- Create test file in appropriate module directory
- Add test block with
_ = @import("path/to/test.zig");insrc/main.zig - Use test helpers for setup/teardown
- Update build.zig filters if creating new test category
Critical memory patterns in this codebase:
Allocator Usage:
- GeneralPurposeAllocator for main operations
- ArenaAllocator for temporary test data
- Careful cleanup in defer blocks
String Handling:
- All FileInfo strings are duped and must be freed
- Path buffers use std.fs.MAX_PATH_BYTES (4096)
- Config data cleanup via ConfigData.deinit()
HashMap Management:
- OrganizationPlan uses both typed HashMap and StringHashMap
- Each ArrayList in maps requires separate deinit
- Proper iteration and cleanup in nested structures
Rollback Safety:
- MoveTracker dupes all paths to preserve state
- Reverse-order rollback on any move failure
- All recorded moves freed in MoveTracker.deinit()