Class: Ace::Git::Secrets::Molecules::GitRewriter

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/git/secrets/molecules/git_rewriter.rb

Overview

Wrapper for git-filter-repo to remove tokens from Git history Handles tool availability, filter building, and execution

Constant Summary collapse

FILTER_REPO_INSTALL_INSTRUCTIONS =
<<~MSG
  git-filter-repo is required for history rewriting.
  Install with: brew install git-filter-repo
  See: https://github.com/newren/git-filter-repo
MSG

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository_path: ".") ⇒ GitRewriter

Returns a new instance of GitRewriter.

Parameters:

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

    Path to git repository

Raises:

  • (ArgumentError)

    If repository_path is not a valid git repository



23
24
25
26
# File 'lib/ace/git/secrets/molecules/git_rewriter.rb', line 23

def initialize(repository_path: ".")
  @repository_path = File.expand_path(repository_path)
  validate_repository_path!
end

Instance Attribute Details

#repository_pathObject (readonly)

Returns the value of attribute repository_path.



19
20
21
# File 'lib/ace/git/secrets/molecules/git_rewriter.rb', line 19

def repository_path
  @repository_path
end

Instance Method Details

#available?Boolean

Check if git-filter-repo is available

Returns:

  • (Boolean)


30
31
32
# File 'lib/ace/git/secrets/molecules/git_rewriter.rb', line 30

def available?
  system("which git-filter-repo > /dev/null 2>&1")
end

#clean_working_directory?Boolean

Check if working directory is clean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
# File 'lib/ace/git/secrets/molecules/git_rewriter.rb', line 36

def clean_working_directory?
  output, status = Open3.capture2(
    "git", "-C", repository_path, "status", "--porcelain",
    err: File::NULL
  )
  status.success? && output.strip.empty?
end

#create_backup(backup_path) ⇒ Boolean

Create a backup of the repository

Parameters:

  • backup_path (String)

    Path for backup

Returns:

  • (Boolean)

    Success status



91
92
93
94
95
96
97
98
99
# File 'lib/ace/git/secrets/molecules/git_rewriter.rb', line 91

def create_backup(backup_path)
  _, status = Open3.capture2(
    "git", "clone", "--mirror", repository_path, backup_path,
    err: File::NULL
  )
  status.success?
rescue
  false
end

#rewrite(tokens, dry_run: false) ⇒ Hash

Rewrite history to remove specific tokens

Parameters:

  • tokens (Array<DetectedToken>)

    Tokens to remove

  • dry_run (Boolean) (defaults to: false)

    Whether to preview changes only

Returns:

  • (Hash)

    Result with :success, :message, :changes keys



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
# File 'lib/ace/git/secrets/molecules/git_rewriter.rb', line 48

def rewrite(tokens, dry_run: false)
  unless available?
    return {
      success: false,
      message: FILTER_REPO_INSTALL_INSTRUCTIONS,
      changes: []
    }
  end

  unless clean_working_directory?
    return {
      success: false,
      message: "Working directory has uncommitted changes. Commit or stash before rewriting history.",
      changes: []
    }
  end

  if tokens.empty?
    return {
      success: true,
      message: "No tokens to remove",
      changes: []
    }
  end

  # Build replacement expressions
  replacements = build_replacements(tokens)

  if dry_run
    {
      success: true,
      dry_run: true,
      message: "Dry run: Would remove #{tokens.size} token(s) from history",
      changes: replacements.map { |r| {original: r[:pattern], replacement: r[:replacement]} }
    }
  else
    execute_filter_repo(replacements)
  end
end