Class: GitAuto::Services::GitService

Inherits:
Object
  • Object
show all
Defined in:
lib/git_auto/services/git_service.rb

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Instance Method Details

#commit(message) ⇒ Object



20
21
22
23
24
25
# File 'lib/git_auto/services/git_service.rb', line 20

def commit(message)
  validate_git_repository!
  validate_staged_changes!
  first_line = message.split("\n").first.strip
  execute_git_command("commit", "-m", first_line)
end

#get_commit_history(limit = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/git_auto/services/git_service.rb', line 27

def get_commit_history(limit = nil)
  validate_git_repository!
  format = "%H%n%s%n%an%n%aI"
  command = ["log", "--pretty=format:#{format}", "--no-merges"]
  command << "-#{limit}" if limit

  output = execute_git_command(*command)
  return [] if output.empty?

  output.split("\n\n").map do |commit|
    hash, subject, author, date = commit.split("\n")
    {
      hash: hash,
      subject: subject,
      author: author,
      date: date
    }
  end
end

#get_staged_diffObject



10
11
12
13
# File 'lib/git_auto/services/git_service.rb', line 10

def get_staged_diff
  validate_git_repository!
  execute_git_command("diff", "--cached")
end

#get_staged_filesObject



15
16
17
18
# File 'lib/git_auto/services/git_service.rb', line 15

def get_staged_files
  validate_git_repository!
  execute_git_command("diff", "--cached", "--name-only").split("\n")
end

#repository_statusObject



47
48
49
50
51
52
53
# File 'lib/git_auto/services/git_service.rb', line 47

def repository_status
  {
    has_staged_changes: has_staged_changes?,
    is_clean: is_clean?,
    has_commits: has_commits?
  }
end