Class: InvocaGems::GitRepo

Inherits:
Object
  • Object
show all
Includes:
ShellCommand
Defined in:
lib/invoca_gems/git_repo.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ShellCommand

included, #shell_command

Constructor Details

#initialize(presenter, repo_root = nil) ⇒ GitRepo

Returns a new instance of GitRepo.



10
11
12
13
# File 'lib/invoca_gems/git_repo.rb', line 10

def initialize(presenter, repo_root = nil)
  @presenter = presenter
  @repo_root = Pathname.new(repo_root ||  Dir.pwd)
end

Instance Attribute Details

#presenterObject

Returns the value of attribute presenter.



8
9
10
# File 'lib/invoca_gems/git_repo.rb', line 8

def presenter
  @presenter
end

Class Method Details

.clone(root_directory, uri, directory_name, presenter:) ⇒ Object



55
56
57
# File 'lib/invoca_gems/git_repo.rb', line 55

def self.clone(root_directory, uri, directory_name, presenter:)
  git_cmd(root_directory, "clone #{uri} #{directory_name}", presenter: presenter)
end

.git_cmd(path, str, raise_errors: true, presenter:) ⇒ Object



72
73
74
# File 'lib/invoca_gems/git_repo.rb', line 72

def self.git_cmd(path, str, raise_errors: true, presenter: )
  shell_command(command: "/usr/bin/git #{str}", path: path, raise_errors: raise_errors, presenter: presenter)
end

Instance Method Details

#branchObject



15
16
17
# File 'lib/invoca_gems/git_repo.rb', line 15

def branch
  git_cmd('rev-parse --abbrev-ref HEAD')
end

#branch_exists?(branch_name) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
# File 'lib/invoca_gems/git_repo.rb', line 41

def branch_exists?(branch_name)
  branch_name_escaped = Regexp.escape(branch_name)
  if result = git_cmd("branch -a")
    result.split("\n").any? do |branch_line|
      branch_line =~ / #{branch_name_escaped}\z/ || branch_line =~ / remotes\/origin\/#{branch_name_escaped}\z/
    end
  end
end

#checkout_branch(branch_name) ⇒ Object



50
51
52
# File 'lib/invoca_gems/git_repo.rb', line 50

def checkout_branch(branch_name)
  git_cmd("checkout #{branch_name}")
end

#commit(message) ⇒ Object



31
32
33
34
# File 'lib/invoca_gems/git_repo.rb', line 31

def commit(message)
  git_cmd("add -A")
  git_cmd("commit -m #{Shellwords.escape(message)}")
end

#create_branch_at(branch_name, sha) ⇒ Object



63
64
65
# File 'lib/invoca_gems/git_repo.rb', line 63

def create_branch_at(branch_name, sha)
  git_cmd("branch #{branch_name} #{sha}")
end

#fetchObject



27
28
29
# File 'lib/invoca_gems/git_repo.rb', line 27

def fetch
  git_cmd("fetch")
end

#git_cmd(str, raise_errors: true) ⇒ Object



76
77
78
# File 'lib/invoca_gems/git_repo.rb', line 76

def git_cmd(str, raise_errors: true)
  self.class.git_cmd(@repo_root, str, raise_errors: raise_errors, presenter: presenter)
end

#is_ancestor_commit?(earlier_sha, current_sha) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/invoca_gems/git_repo.rb', line 36

def is_ancestor_commit?(earlier_sha, current_sha)
  git_cmd("merge-base --is-ancestor #{earlier_sha} #{current_sha}", raise_errors: false)
end

#pushObject



67
68
69
# File 'lib/invoca_gems/git_repo.rb', line 67

def push
  git_cmd("push -u origin #{branch}")
end

#shaObject



19
20
21
# File 'lib/invoca_gems/git_repo.rb', line 19

def sha
  git_cmd("rev-parse --verify HEAD")
end

#unsaved_changes?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/invoca_gems/git_repo.rb', line 59

def unsaved_changes?
  !git_cmd("diff --quiet", raise_errors: false) || !git_cmd("diff --quiet --cached", raise_errors: false)
end

#uriObject



23
24
25
# File 'lib/invoca_gems/git_repo.rb', line 23

def uri
  git_cmd("config --get remote.origin.url")
end