Module: AIGit::Git

Defined in:
lib/ai_git/git.rb

Class Method Summary collapse

Class Method Details

.commit_with_message(message) ⇒ Object

Commit using a temp file so the message can contain anything (quotes, backticks, dollar signs) without shell-escaping concerns.



43
44
45
46
47
48
49
50
# File 'lib/ai_git/git.rb', line 43

def commit_with_message(message)
  Tempfile.create("ai_git_commit_msg") do |file|
    file.write(message)
    file.flush

    run_command("git", "commit", "-F", file.path)
  end
end

.current_branchObject



18
19
20
# File 'lib/ai_git/git.rb', line 18

def current_branch
  `git rev-parse --abbrev-ref HEAD`.chomp
end

.diffObject



14
15
16
# File 'lib/ai_git/git.rb', line 14

def diff
  `git diff --cached`
end

.push_current_branchObject



52
53
54
# File 'lib/ai_git/git.rb', line 52

def push_current_branch
  run_command("git", "push", "-u", "origin", "HEAD")
end

.run_command(cmd, *args) ⇒ Object

Run a command with arguments as an array — no shell, so values are not interpolated or word-split. Accepts either:

run_command("git", "status")                  (single string of args)
run_command("git", "commit", "-m", message)   (variadic args, preferred)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ai_git/git.rb', line 26

def run_command(cmd, *args)
  argv =
    if args.length == 1 && args.first.is_a?(String)
      args.first.split
    else
      args.map(&:to_s)
    end

  _stdout, stderr, status = Open3.capture3(cmd, *argv)

  return if status.success?

  raise "Command failed: #{cmd} #{argv.join(' ')} (exit #{status.exitstatus})#{stderr.empty? ? '' : "\n#{stderr}"}"
end

.staged_filesObject



10
11
12
# File 'lib/ai_git/git.rb', line 10

def staged_files
  `git diff --cached --name-only`
end