Class: CommitCraft::GitClient

Inherits:
Object
  • Object
show all
Defined in:
lib/commitcraft/git_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(repo_path: Dir.pwd) ⇒ GitClient

Returns a new instance of GitClient.



6
7
8
9
# File 'lib/commitcraft/git_client.rb', line 6

def initialize(repo_path: Dir.pwd)
  @repo_path = repo_path
  validate_git_repo!
end

Instance Method Details

#all_diffObject



22
23
24
# File 'lib/commitcraft/git_client.rb', line 22

def all_diff
  execute_command("git diff HEAD")
end

#commit(message, amend: false) ⇒ Object



38
39
40
41
42
# File 'lib/commitcraft/git_client.rb', line 38

def commit(message, amend: false)
  escaped_message = message.gsub("'", "'\\''")
  cmd = amend ? "git commit --amend -m '#{escaped_message}'" : "git commit -m '#{escaped_message}'"
  execute_command(cmd)
end

#current_branchObject



30
31
32
# File 'lib/commitcraft/git_client.rb', line 30

def current_branch
  execute_command("git branch --show-current").strip
end

#file_statsObject



48
49
50
51
# File 'lib/commitcraft/git_client.rb', line 48

def file_stats
  stats = execute_command("git diff --cached --numstat")
  parse_file_stats(stats)
end

#recent_commits(count = 5) ⇒ Object



34
35
36
# File 'lib/commitcraft/git_client.rb', line 34

def recent_commits(count = 5)
  execute_command("git log -#{count} --oneline").split("\n")
end

#staged_diffObject

Raises:



11
12
13
14
15
16
# File 'lib/commitcraft/git_client.rb', line 11

def staged_diff
  diff = execute_command("git diff --cached")
  raise NoChangesError, "No staged changes found. Use 'git add' to stage your changes." if diff.empty?

  diff
end

#staged_filesObject



44
45
46
# File 'lib/commitcraft/git_client.rb', line 44

def staged_files
  execute_command("git diff --cached --name-only").split("\n")
end

#statusObject



26
27
28
# File 'lib/commitcraft/git_client.rb', line 26

def status
  execute_command("git status --short")
end

#unstaged_diffObject



18
19
20
# File 'lib/commitcraft/git_client.rb', line 18

def unstaged_diff
  execute_command("git diff")
end