gitignore

Architecture Overview

Detailed technical architecture of the gitignore tool.

๐Ÿ—๏ธ System Architecture

Gitignore is a compiled C application with embedded templates and smart merging capabilities.

Core Components

gitignore/
โ”œโ”€โ”€ src/                    # Source code
โ”‚   โ”œโ”€โ”€ main.c             # Command-line interface & argument parsing
โ”‚   โ”œโ”€โ”€ init.c             # Template initialization
โ”‚   โ”œโ”€โ”€ sync.c             # GitHub template synchronization
โ”‚   โ”œโ”€โ”€ utils.c            # Utility functions
โ”‚   โ”œโ”€โ”€ features.c         # Advanced features (backup, global)
โ”‚   โ”œโ”€โ”€ global_backup.c    # Global gitignore management
โ”‚   โ”œโ”€โ”€ cache_config.c     # Caching and configuration
โ”‚   โ””โ”€โ”€ templates.c        # Embedded template data (generated)
โ”œโ”€โ”€ templates/             # Template source files
โ”œโ”€โ”€ scripts/               # Build scripts
โ”œโ”€โ”€ man/                   # Manual pages
โ””โ”€โ”€ Makefile              # Build system

๐Ÿ“Š Data Flow

1. Command Processing

User Input โ†’ main.c::parse_flags() โ†’ Command Dispatch โ†’ Feature Implementation

2. Template Merging

Template Selection โ†’ templates.c::get_builtin_template() โ†’ merge_templates() โ†’ .gitignore

3. GitHub Sync

Language Request โ†’ download_template() โ†’ Cache Check โ†’ HTTP Download โ†’ Cache Store โ†’ Merge

๐Ÿ”ง Key Components

Main Entry Point (main.c)

Responsibilities:

Key Functions:

Template System (templates.c)

Generated from: scripts/generate_templates.sh

Features:

Structure:

const char* get_builtin_template(const char *name) {
    if (strcmp(name, "python") == 0) return python_template;
    if (strcmp(name, "node") == 0) return node_template;
    // ... etc
}

Configuration System (cache_config.c)

Configuration File: ~/.config/gitignore/config.conf

Features:

Structure:

typedef struct {
    char **default_templates;
    int default_count;
    int auto_backup;
    int cache_enabled;
    int cache_duration;
    int verbose;
    int quiet;
    int use_color;
} config_t;

Caching System

Cache Location: ~/.config/gitignore/cache/

Features:

Cache Structure:

~/.config/gitignore/cache/
โ”œโ”€โ”€ python.gitignore
โ”œโ”€โ”€ node.gitignore
โ””โ”€โ”€ ... (other synced templates)

Backup System

Backup Location: ~/.config/gitignore/backups/

Features:

Backup Naming: backup_YYYY-MM-DD_HH-MM-SS

๐Ÿ”„ Core Algorithms

Smart Merging Algorithm

Purpose: Intelligently combine multiple templates while removing duplicates.

Process:

  1. Load all requested templates
  2. Split into individual patterns
  3. Normalize patterns (trim whitespace, remove comments)
  4. Deduplication using hash-based comparison
  5. Sort patterns for consistency
  6. Write to .gitignore

Key Features:

Pattern Detection Algorithm

Purpose: Distinguish between commands and file patterns.

Rules:

  1. Contains / โ†’ Path pattern
  2. Contains * โ†’ Wildcard pattern
  3. Contains . โ†’ Hidden file pattern
  4. File exists in filesystem โ†’ Path pattern
  5. Matches known command names โ†’ Requires --add flag
  6. Default โ†’ Pattern

Auto-Detection Algorithm

Purpose: Automatically determine project type from files.

Detection Map:

// File โ†’ Template mapping
"package.json" โ†’ "node"
"requirements.txt" โ†’ "python"
"setup.py" โ†’ "python"
"Cargo.toml" โ†’ "rust"
"go.mod" โ†’ "go"
"pom.xml" โ†’ "java"
"build.gradle" โ†’ "java"

๐Ÿ“ Directory Structure

Source Layout

src/
โ”œโ”€โ”€ main.c          # CLI entry point (263 lines)
โ”œโ”€โ”€ init.c          # Template initialization
โ”œโ”€โ”€ sync.c          # GitHub synchronization
โ”œโ”€โ”€ utils.c         # String/file utilities
โ”œโ”€โ”€ features.c      # Interactive mode, auto-detect
โ”œโ”€โ”€ global_backup.c # Global gitignore features
โ”œโ”€โ”€ cache_config.c  # Configuration and caching
โ””โ”€โ”€ templates.c     # Generated template data

Configuration Directories

~/.config/gitignore/
โ”œโ”€โ”€ config.conf           # User configuration
โ”œโ”€โ”€ templates/           # Custom user templates
โ”œโ”€โ”€ cache/               # Downloaded templates
โ””โ”€โ”€ backups/             # .gitignore backups

๐Ÿ”— Dependencies

Required Libraries

Build Dependencies

๐Ÿš€ Performance Characteristics

Memory Usage

Speed Optimizations

Scalability

๐Ÿ›ก๏ธ Error Handling

Error Categories

Error Propagation

Function Call โ†’ Error Check โ†’ Error Code Return โ†’ User-Friendly Message

Recovery Mechanisms

๐Ÿ”ง Build System

Makefile Targets

all: templates $(TARGET)          # Build everything
templates: templates.c            # Generate embedded templates
clean:                            # Remove build artifacts
install:                          # Install system-wide
test:                             # Run test suite
regen-templates:                  # Force template regeneration

Template Generation Process

templates/*.gitignore โ†’ scripts/generate_templates.sh โ†’ src/templates.c

Generated Code Structure:

// Auto-generated file - DO NOT EDIT
const char *python_template = "# Python\n*.pyc\n__pycache__/\n...";
const char *node_template = "# Node.js\nnode_modules/\n...";
// ... etc

const char* get_builtin_template(const char *name) {
    // Lookup logic
}

๐Ÿงช Testing Strategy

Unit Tests

Integration Tests

Manual Testing

๐Ÿ”ฎ Future Architecture

Planned Improvements

Scalability Considerations