Class: Ace::GitCommit::Molecules::FileStager

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/git_commit/molecules/file_stager.rb

Overview

FileStager handles staging files for commit

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(git_executor, gitignore_checker: nil) ⇒ FileStager

Returns a new instance of FileStager.



10
11
12
13
14
15
16
# File 'lib/ace/git_commit/molecules/file_stager.rb', line 10

def initialize(git_executor, gitignore_checker: nil)
  @git = git_executor
  @gitignore_checker = gitignore_checker || Atoms::GitignoreChecker.new
  @last_error = nil
  @last_skipped_files = []
  @had_valid_files = false
end

Instance Attribute Details

#last_errorObject (readonly)

Returns the value of attribute last_error.



8
9
10
# File 'lib/ace/git_commit/molecules/file_stager.rb', line 8

def last_error
  @last_error
end

#last_skipped_filesObject (readonly)

Returns the value of attribute last_skipped_files.



8
9
10
# File 'lib/ace/git_commit/molecules/file_stager.rb', line 8

def last_skipped_files
  @last_skipped_files
end

Instance Method Details

#all_files_skipped?Boolean

Check if the last stage_paths call had all files gitignored

Returns:

  • (Boolean)

    True if all files were skipped due to gitignore



147
148
149
# File 'lib/ace/git_commit/molecules/file_stager.rb', line 147

def all_files_skipped?
  @last_skipped_files.any? && !@had_valid_files && @last_error.nil?
end

#files_staged?(files) ⇒ Boolean

Check if specific files are staged

Parameters:

  • files (Array<String>)

    Files to check

Returns:

  • (Boolean)

    True if all files are staged



67
68
69
70
# File 'lib/ace/git_commit/molecules/file_stager.rb', line 67

def files_staged?(files)
  staged = staged_files
  files.all? { |f| staged.include?(f) }
end

#stage_allBoolean

Stage all changes in the repository

Returns:

  • (Boolean)

    True if successful



34
35
36
37
38
39
40
41
# File 'lib/ace/git_commit/molecules/file_stager.rb', line 34

def stage_all
  @last_error = nil
  @git.execute("add", "-A")
  true
rescue GitError => e
  @last_error = e.message
  false
end

#stage_files(files) ⇒ Boolean

Stage specific files

Parameters:

  • files (Array<String>)

    Files to stage

Returns:

  • (Boolean)

    True if successful



21
22
23
24
25
26
27
28
29
30
# File 'lib/ace/git_commit/molecules/file_stager.rb', line 21

def stage_files(files)
  return false if files.nil? || files.empty?

  @last_error = nil
  @git.execute("add", *files)
  true
rescue GitError => e
  @last_error = e.message
  false
end

#stage_paths(paths, quiet: false) ⇒ Boolean

Stage only files within specified paths Resets staging area first, then stages only files in paths

Parameters:

  • paths (Array<String>)

    Paths to stage (files or directories)

  • quiet (Boolean) (defaults to: false)

    Suppress output about skipped files

Returns:

  • (Boolean)

    True if successful (including when all files skipped)



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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ace/git_commit/molecules/file_stager.rb', line 77

def stage_paths(paths, quiet: false)
  return false if paths.nil? || paths.empty?

  @last_error = nil
  @last_skipped_files = []
  @had_valid_files = false

  begin
    # Categorize paths: valid (not gitignored), force_add (gitignored but tracked), skipped (gitignored and untracked)
    result = @gitignore_checker.categorize_paths(paths, @git)

    # Track skipped files (gitignored and not tracked)
    if result[:skipped].any?
      @last_skipped_files = result[:skipped]
      unless quiet
        warn "⚠ Skipping gitignored files (not tracked):"
        result[:skipped].each do |info|
          if info[:pattern]
            warn "  #{info[:path]}"
            warn "  (matches pattern: #{info[:pattern]})"
          else
            warn "  #{info[:path]}"
          end
        end
      end
    end

    # Combine valid paths and force_add paths
    # Force add paths need -f flag because they're in gitignored locations but are tracked
    normal_paths = result[:valid]
    force_add_paths = result[:force_add].map { |f| f[:path] }

    all_paths = normal_paths + force_add_paths

    # If all files are skipped (gitignored and untracked), return success
    if all_paths.empty?
      return true
    end

    @had_valid_files = true

    # Reset staging area to clear everything
    @git.execute("reset", "--quiet")

    # Stage normal files (retry with -f if path is ignored)
    normal_paths.each do |path|
      @git.execute("add", path)
    rescue GitError => e
      # If git says path is ignored but we expected it to work, try force add
      if e.message.include?("ignored by one of your .gitignore files")
        @git.execute("add", "-f", path)
      else
        raise
      end
    end

    # Stage tracked files in gitignored locations with force flag
    force_add_paths.each do |path|
      @git.execute("add", "-f", path)
    end

    true
  rescue GitError => e
    @last_error = e.message
    false
  end
end

#staged_filesArray<String>

Get list of staged files Uses –no-renames to ensure deleted files from directory renames are included

Returns:

  • (Array<String>)

    List of staged file paths



60
61
62
# File 'lib/ace/git_commit/molecules/file_stager.rb', line 60

def staged_files
  @git.execute("diff", "--cached", "--name-only", "--no-renames").strip.split("\n")
end

#unstage_files(files) ⇒ Boolean

Unstage specific files

Parameters:

  • files (Array<String>)

    Files to unstage

Returns:

  • (Boolean)

    True if successful



46
47
48
49
50
51
52
53
54
55
# File 'lib/ace/git_commit/molecules/file_stager.rb', line 46

def unstage_files(files)
  return false if files.nil? || files.empty?

  @git.execute("reset", "HEAD", *files)
  true
rescue GitError
  # If HEAD doesn't exist (new repo), use rm --cached
  @git.execute("rm", "--cached", *files)
  true
end