Class: Ace::Git::Secrets::Organisms::HistoryCleaner

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/git/secrets/organisms/history_cleaner.rb

Overview

Orchestrates the history cleaning workflow Combines scanning, confirmation, backup, and rewriting

Requires gitleaks to be installed: brew install gitleaks

Constant Summary collapse

CONFIRMATION_TEXT =
"REWRITE HISTORY"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository_path: ".", gitleaks_config: nil, exclusions: nil) ⇒ HistoryCleaner

Returns a new instance of HistoryCleaner.

Parameters:

  • repository_path (String) (defaults to: ".")

    Path to git repository

  • gitleaks_config (String, nil) (defaults to: nil)

    Path to gitleaks config file

  • exclusions (Array<String>, nil) (defaults to: nil)

    Glob patterns for files to exclude



21
22
23
24
25
26
27
28
29
# File 'lib/ace/git/secrets/organisms/history_cleaner.rb', line 21

def initialize(repository_path: ".", gitleaks_config: nil, exclusions: nil)
  @repository_path = File.expand_path(repository_path)
  @rewriter = Molecules::GitRewriter.new(repository_path: @repository_path)
  @scanner = Molecules::HistoryScanner.new(
    repository_path: @repository_path,
    gitleaks_config: gitleaks_config,
    exclusions: exclusions
  )
end

Instance Attribute Details

#repository_pathObject (readonly)

Returns the value of attribute repository_path.



16
17
18
# File 'lib/ace/git/secrets/organisms/history_cleaner.rb', line 16

def repository_path
  @repository_path
end

#rewriterObject (readonly)

Returns the value of attribute rewriter.



16
17
18
# File 'lib/ace/git/secrets/organisms/history_cleaner.rb', line 16

def rewriter
  @rewriter
end

#scannerObject (readonly)

Returns the value of attribute scanner.



16
17
18
# File 'lib/ace/git/secrets/organisms/history_cleaner.rb', line 16

def scanner
  @scanner
end

Instance Method Details

#clean(tokens: nil, dry_run: false, force: false, create_backup: true, backup_path: nil) ⇒ Hash

Clean tokens from history

Parameters:

  • tokens (Array<DetectedToken>, nil) (defaults to: nil)

    Tokens to remove (scans if nil)

  • dry_run (Boolean) (defaults to: false)

    Preview only

  • force (Boolean) (defaults to: false)

    Skip confirmation

  • create_backup (Boolean) (defaults to: true)

    Create backup before rewriting

  • backup_path (String, nil) (defaults to: nil)

    Custom backup path

Returns:

  • (Hash)

    Result with :success, :message, :report keys



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ace/git/secrets/organisms/history_cleaner.rb', line 38

def clean(tokens: nil, dry_run: false, force: false, create_backup: true, backup_path: nil)
  # Scan if tokens not provided (needed for both dry-run and actual run)
  if tokens.nil?
    report = scanner.scan
    tokens = report.tokens
  end

  if tokens.empty?
    return {
      success: true,
      message: "No tokens found to remove. Repository is clean.",
      tokens_removed: 0
    }
  end

  # Dry run - just show what would be removed (no rewriter needed)
  if dry_run
    return dry_run_result(tokens)
  end

  # Check prerequisites (only needed for actual rewrite)
  unless rewriter.available?
    return {
      success: false,
      message: Molecules::GitRewriter::FILTER_REPO_INSTALL_INSTRUCTIONS
    }
  end

  unless rewriter.clean_working_directory?
    return {
      success: false,
      message: "Working directory has uncommitted changes. Commit or stash first."
    }
  end

  # Require confirmation unless forced
  unless force
    return {
      success: false,
      requires_confirmation: true,
      message: confirmation_warning(tokens),
      confirmation_text: CONFIRMATION_TEXT
    }
  end

  # Create backup if requested
  if create_backup
    backup_result = create_repository_backup(backup_path)
    unless backup_result[:success]
      return {
        success: false,
        message: "Failed to create backup: #{backup_result[:message]}"
      }
    end
    puts "Backup created at: #{backup_result[:path]}"
  end

  # Execute rewrite
  result = rewriter.rewrite(tokens)

  if result[:success]
    {
      success: true,
      message: result[:message],
      tokens_removed: tokens.size,
      next_steps: post_rewrite_instructions
    }
  else
    {
      success: false,
      message: result[:message]
    }
  end
end

#valid_confirmation?(input) ⇒ Boolean

Validate confirmation text

Parameters:

  • input (String)

    User input

Returns:

  • (Boolean)


116
117
118
# File 'lib/ace/git/secrets/organisms/history_cleaner.rb', line 116

def valid_confirmation?(input)
  input.strip == CONFIRMATION_TEXT
end