Class: Ace::Git::Secrets::Commands::RewriteCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/git/secrets/commands/rewrite_command.rb

Overview

CLI command for rewriting Git history to remove tokens

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ RewriteCommand

Returns a new instance of RewriteCommand.



16
17
18
# File 'lib/ace/git/secrets/commands/rewrite_command.rb', line 16

def initialize(options)
  @options = options
end

Class Method Details

.execute(options) ⇒ Integer

Execute rewrite-history command

Parameters:

  • options (Hash)

    Command options

Returns:

  • (Integer)

    Exit code (0=success, 1=failure, 2=error)



12
13
14
# File 'lib/ace/git/secrets/commands/rewrite_command.rb', line 12

def self.execute(options)
  new(options).execute
end

Instance Method Details

#executeObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
# File 'lib/ace/git/secrets/commands/rewrite_command.rb', line 20

def execute
  # Ensure gitleaks is available
  Atoms::GitleaksRunner.ensure_available!

  cleaner = Organisms::HistoryCleaner.new(
    repository_path: ".",
    gitleaks_config: Ace::Git::Secrets.gitleaks_config_path
  )

  # Load tokens from scan file if provided
  tokens = load_tokens_from_file if @options[:scan_file]
  return 1 if @options[:scan_file] && tokens.nil?

  # First pass - get confirmation requirements
  result = cleaner.clean(
    tokens: tokens,
    dry_run: @options[:dry_run],
    force: @options[:force],
    create_backup: @options.fetch(:backup, true)
  )

  # Handle dry run
  if result[:dry_run]
    puts "DRY RUN - No changes made"
    puts
    puts result[:message]

    if result[:tokens]
      puts
      puts "Tokens that would be removed:"
      result[:tokens].each do |t|
        puts "  - #{t[:type]}: #{t[:masked_value]} (#{t[:file]}:#{t[:commit]})"
      end
    end
    return 0
  end

  # Handle confirmation required
  if result[:requires_confirmation]
    puts result[:message]
    print "\nConfirmation: "

    input = $stdin.gets&.strip

    unless cleaner.valid_confirmation?(input)
      puts "\nConfirmation failed. No changes made."
      return 1
    end

    # Re-run with force after confirmation
    result = cleaner.clean(
      tokens: tokens,
      force: true,
      create_backup: @options.fetch(:backup, true)
    )
  end

  # Handle result
  if result[:success]
    puts result[:message]
    puts result[:next_steps] if result[:next_steps]
    0
  else
    puts "Error: #{result[:message]}"
    1
  end
rescue => e
  puts "Error: #{e.message}"
  puts e.backtrace.first(5).join("\n") if ENV["DEBUG"]
  2
end