Module: Strata::CLI::Utils::Git

Defined in:
lib/strata/cli/utils/git.rb

Class Method Summary collapse

Class Method Details

.changed_file_paths_since(commit_hash) ⇒ Object



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/strata/cli/utils/git.rb', line 72

def changed_file_paths_since(commit_hash)
  return [] unless git_repo?
  return [] unless commit_hash

  unless validate_commit_hash(commit_hash)
    raise Strata::CommandError, "Invalid commit hash format: #{commit_hash.inspect}"
  end

  # Check if commit exists in repo
  _, _, status = Open3.capture3("git", "rev-parse", "--verify", "#{commit_hash}^{commit}")
  return [] unless status.success?

  # Get file paths that changed (for added and modified files only, exclude deleted)
  # --diff-filter=ACMR: Added, Copied, Modified, Renamed (excludes Deleted)
  diff_output = run_git_command("diff", "--name-status", "--diff-filter=ACMR", commit_hash, "HEAD")
  return [] unless diff_output

  paths = []
  diff_output.lines.each do |line|
    line = line.strip
    next if line.empty?

    parts = line.split(/\s+/)
    next if parts.length < 2

    status = parts[0]
    # For renames (R), parts[1] is old path, parts[2] is new path
    # For others, parts[1] is the path
    if status.start_with?("R")
      # Rename: use the new path
      paths << parts[2] if parts.length >= 3 && parts[2]
    elsif parts[1]
      # Added, Copied, Modified: use the path
      paths << parts[1]
    end
  end

  # Filter to only yml files and return unique paths
  paths.select { |path| path&.end_with?(".yml", ".yaml") }.uniq
end

.changed_files_since(commit_hash) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/strata/cli/utils/git.rb', line 52

def changed_files_since(commit_hash)
  return [] unless git_repo?
  return [] unless commit_hash

  unless validate_commit_hash(commit_hash)
    raise Strata::CommandError, "Invalid commit hash format: #{commit_hash.inspect}"
  end

  # Check if commit exists in repo
  _, _, status = Open3.capture3("git", "rev-parse", "--verify", "#{commit_hash}^{commit}")
  return [] unless status.success?

  # Get all changed files since that commit (including added, modified, deleted, renamed)
  diff_output = run_git_command("diff", "--name-status", commit_hash, "HEAD")
  return [] unless diff_output

  modifications = parse_diff_output(diff_output)
  filter_yml_files(modifications)
end

.check_commit_status(branch_name) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/strata/cli/utils/git.rb', line 205

def check_commit_status(branch_name)
  return {status: :ok, message: "Not a git repository"} unless git_repo?

  remote_branch = "origin/#{branch_name}"

  # Check if remote branch exists
  remote_sha, _, remote_status = Open3.capture3("git", "rev-parse", remote_branch)
  unless remote_status.success?
    return {status: :ok, message: "Remote branch not found, proceeding with local commit"}
  end

  remote_sha = remote_sha.strip
  local_sha = run_git_command("rev-parse", "HEAD")

  # If SHAs are the same, branches are in sync
  return {status: :same, message: "Local branch is in sync with remote"} if local_sha == remote_sha

  # Check if local is ahead, behind, or diverged
  # rev-list --left-right shows: > = commits in local not in remote (ahead), < = commits in remote not in local (behind)
  diff_output, = Open3.capture3("git", "rev-list", "--left-right", "#{remote_sha}...#{local_sha}")

  ahead_count = diff_output.lines.count { |line| line.start_with?(">") }
  behind_count = diff_output.lines.count { |line| line.start_with?("<") }

  if ahead_count.positive? && behind_count.zero?
    {status: :ahead, message: "Local branch is #{ahead_count} commit(s) ahead of remote"}
  elsif ahead_count.zero? && behind_count.positive?
    {status: :behind, message: "Local branch is #{behind_count} commit(s) behind remote"}
  else
    {status: :diverged,
     message: "Local branch has diverged from remote (#{ahead_count} ahead, #{behind_count} behind)"}
  end
end

.check_requirement_and_exit_if_unavailable(args) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/strata/cli/utils/git.rb', line 124

def check_requirement_and_exit_if_unavailable(args)
  # Allow help and version commands to work without Git
  command = args.first
  return if args.empty? || %w[version help].include?(command)

  return if git_available?

  Thor::Shell::Color.new.say_error "ERROR: Git is required but not found. Please install Git to use strata-cli.",
    :red
  exit 1
end

.commit_file(file_path, commit_message, project_path = Dir.pwd) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/strata/cli/utils/git.rb', line 239

def commit_file(file_path, commit_message, project_path = Dir.pwd)
  return false unless git_repo?

  # Change to project directory for git operations
  Dir.chdir(project_path) do
    # Check if file exists
    return false unless File.exist?(file_path)

    # Check if file has changes (modified, added, or untracked)
    stdout, _, status = Open3.capture3("git", "status", "--porcelain", file_path)
    return false unless status.success?

    # If no changes, nothing to commit
    return false if stdout.strip.empty?

    # Stage the file (handles both modified and untracked files)
    _, _, add_status = Open3.capture3("git", "add", file_path)
    return false unless add_status.success?

    # Check if there are actually staged changes to commit
    # git diff --cached --quiet returns 0 if no changes, 1 if changes exist
    _, _, diff_status = Open3.capture3("git", "diff", "--cached", "--quiet", file_path)
    # If success? is true (exitstatus == 0), there are no changes staged
    return false if diff_status.success?

    # Commit the file
    _, _, commit_status = Open3.capture3("git", "commit", "-m", commit_message)
    commit_status.success?
  end
rescue
  false
end

.commit_infoObject



24
25
26
27
28
29
30
31
32
# File 'lib/strata/cli/utils/git.rb', line 24

def commit_info
  sha = run_git_command("rev-parse", "HEAD")
  message = run_git_command("log", "-1", "--pretty=%B")

  {
    sha: sha,
    message: message || "Manual deployment"
  }
end

.committer_infoObject



34
35
36
37
38
39
40
41
42
# File 'lib/strata/cli/utils/git.rb', line 34

def committer_info
  name = run_git_command("config", "user.name")
  email = run_git_command("config", "user.email")

  {
    name: name || "Unknown",
    email: email || "unknown@example.com"
  }
end

.current_branchObject



18
19
20
21
22
# File 'lib/strata/cli/utils/git.rb', line 18

def current_branch
  return "main" unless git_repo?

  run_git_command("rev-parse", "--abbrev-ref", "HEAD")
end

.fetch_branch(branch_name) ⇒ Object



199
200
201
202
203
# File 'lib/strata/cli/utils/git.rb', line 199

def fetch_branch(branch_name)
  return unless git_repo?

  run_git_command("fetch", "origin", branch_name)
end

.file_modificationsObject



44
45
46
47
48
49
50
# File 'lib/strata/cli/utils/git.rb', line 44

def file_modifications
  diff_output = run_git_command("diff", "--name-status", "HEAD~1")
  return [] unless diff_output

  modifications = parse_diff_output(diff_output)
  filter_yml_files(modifications)
end

.filter_yml_files(modifications) ⇒ Object



185
186
187
188
189
190
# File 'lib/strata/cli/utils/git.rb', line 185

def filter_yml_files(modifications)
  modifications.select do |mod|
    path = mod.is_a?(Array) ? (mod[1] || "") : ""
    path.end_with?(".yml", ".yaml")
  end
end

.format_file_modification(status, path) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/strata/cli/utils/git.rb', line 170

def format_file_modification(status, path)
  case status
  when /^R(\d+)$/
    similarity = Regexp.last_match(1)
    old_path, new_path = path.split(/\s+/, 2)
    ["R#{similarity}", old_path, new_path]
  when "A"
    ["A", path]
  when "M"
    ["M", path]
  when "D"
    ["D", path]
  end
end

.git_available?Boolean

Returns:

  • (Boolean)


117
118
119
120
121
122
# File 'lib/strata/cli/utils/git.rb', line 117

def git_available?
  _, _, status = Open3.capture3("git", "--version")
  status.success?
rescue
  false
end

.git_remote_url(remote_name = "origin") ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/strata/cli/utils/git.rb', line 136

def git_remote_url(remote_name = "origin")
  return nil unless git_repo?

  url = run_git_command("remote", "get-url", remote_name)
  return nil if url.nil? || url.strip.empty?

  url.strip
rescue
  nil
end

.git_repo?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/strata/cli/utils/git.rb', line 113

def git_repo?
  File.directory?(".git")
end

.parse_diff_output(output) ⇒ Object



152
153
154
155
156
# File 'lib/strata/cli/utils/git.rb', line 152

def parse_diff_output(output)
  output.lines.map do |line|
    parse_diff_status(line.strip)
  end.compact
end

.parse_diff_status(line) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/strata/cli/utils/git.rb', line 158

def parse_diff_status(line)
  return nil if line.empty?

  parts = line.split(/\s+/, 2)
  return nil if parts.length < 2

  status = parts[0]
  path = parts[1]

  format_file_modification(status, path)
end

.run_git_command(*args) ⇒ Object



147
148
149
150
# File 'lib/strata/cli/utils/git.rb', line 147

def run_git_command(*args)
  stdout, = Open3.capture3("git", *args)
  stdout&.strip
end

.uncommitted_changes?Boolean

Returns:

  • (Boolean)


192
193
194
195
196
197
# File 'lib/strata/cli/utils/git.rb', line 192

def uncommitted_changes?
  return false unless git_repo?

  stdout, = Open3.capture3("git", "status", "--porcelain")
  !stdout.strip.empty?
end

.validate_commit_hash(commit_hash) ⇒ Object



11
12
13
14
15
16
# File 'lib/strata/cli/utils/git.rb', line 11

def validate_commit_hash(commit_hash)
  return false unless commit_hash.is_a?(String)
  return false unless commit_hash.match?(/\A[a-f0-9]{7,40}\z/i)

  true
end