Class: Ace::GitCommit::Atoms::GitignoreChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/git_commit/atoms/gitignore_checker.rb

Overview

GitignoreChecker detects files that match gitignore patterns Uses git check-ignore to determine if paths are ignored

Instance Method Summary collapse

Instance Method Details

#categorize_paths(paths, git_executor) ⇒ Hash

Categorize paths into: valid (not gitignored), force_add (gitignored but tracked), skipped (gitignored and untracked)

Parameters:

  • paths (Array<String>)

    Paths to check

  • git_executor (GitExecutor)

    Git executor instance

Returns:

  • (Hash)

    => […], :force_add => […], :skipped => […]

    • :valid - paths that are NOT gitignored

    • :force_add - paths that ARE gitignored but are tracked in git (use git add -f)

    • :skipped - paths that ARE gitignored and NOT tracked (skip these)



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
# File 'lib/ace/git_commit/atoms/gitignore_checker.rb', line 37

def categorize_paths(paths, git_executor)
  return {valid: [], force_add: [], skipped: []} if paths.nil? || paths.empty?

  valid = []
  force_add = []
  skipped = []

  paths.each do |path|
    result = check_ignore(path, git_executor)
    if result[:ignored]
      # Path matches gitignore - check if it's tracked
      if tracked?(path, git_executor)
        # Tracked file in gitignored location - force add it
        force_add << {path: path, pattern: result[:pattern]}
      else
        # Untracked and gitignored - skip it
        skipped << {path: path, pattern: result[:pattern]}
      end
    else
      valid << path
    end
  end

  {valid: valid, force_add: force_add, skipped: skipped}
end

#filter_ignored(paths, git_executor) ⇒ Hash

Legacy method for backward compatibility

Parameters:

  • paths (Array<String>)

    Paths to check

  • git_executor (GitExecutor)

    Git executor instance

Returns:

  • (Hash)

    => […], :ignored => […]



67
68
69
70
71
72
73
# File 'lib/ace/git_commit/atoms/gitignore_checker.rb', line 67

def filter_ignored(paths, git_executor)
  result = categorize_paths(paths, git_executor)
  {
    valid: result[:valid] + result[:force_add].map { |f| f[:path] },
    ignored: result[:skipped]
  }
end

#ignored?(path, git_executor) ⇒ Boolean

Check if a single file/path is gitignored

Parameters:

  • path (String)

    File or directory path to check

  • git_executor (GitExecutor)

    Git executor instance

Returns:

  • (Boolean)

    True if path is gitignored



13
14
15
16
# File 'lib/ace/git_commit/atoms/gitignore_checker.rb', line 13

def ignored?(path, git_executor)
  result = check_ignore(path, git_executor)
  result[:ignored]
end

#tracked?(path, git_executor) ⇒ Boolean

Check if a file is tracked in git (exists in the index)

Parameters:

  • path (String)

    File path to check

  • git_executor (GitExecutor)

    Git executor instance

Returns:

  • (Boolean)

    True if file is tracked



22
23
24
25
26
27
28
# File 'lib/ace/git_commit/atoms/gitignore_checker.rb', line 22

def tracked?(path, git_executor)
  # git ls-files returns the path if it's tracked, empty if not
  result = git_executor.execute("ls-files", "--error-unmatch", path)
  !result.nil? && !result.strip.empty?
rescue GitError
  false
end