Class: MultiRepo::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/multirepo/git/repo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Repo

Returns a new instance of Repo.



11
12
13
14
# File 'lib/multirepo/git/repo.rb', line 11

def initialize(path)
  @path = path
  @basename = Pathname.new(path).basename.to_s
end

Instance Attribute Details

#basenameObject

Returns the value of attribute basename.



9
10
11
# File 'lib/multirepo/git/repo.rb', line 9

def basename
  @basename
end

#pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/multirepo/git/repo.rb', line 8

def path
  @path
end

Instance Method Details

#branch(name) ⇒ Object



104
105
106
# File 'lib/multirepo/git/repo.rb', line 104

def branch(name)
  Branch.new(self, name)
end

#changesObject



44
45
46
47
48
# File 'lib/multirepo/git/repo.rb', line 44

def changes
  output = GitRunner.run(@path, "status --porcelain", Verbosity::OUTPUT_NEVER)
  lines = output.split("\n").each(&:strip).delete_if{ |f| f == "" }
  lines.map { |l| Change.new(l) }
end

#checkout(ref_name, force = false) ⇒ Object



73
74
75
76
77
# File 'lib/multirepo/git/repo.rb', line 73

def checkout(ref_name, force = false)
  forceFlag = force ? " -f" : ""
  GitRunner.run(@path, "checkout#{forceFlag} #{ref_name}", Verbosity::OUTPUT_ON_ERROR)
  GitRunner.last_command_succeeded
end

#clean?Boolean

Returns:



32
33
34
# File 'lib/multirepo/git/repo.rb', line 32

def clean?
  changes.count == 0
end

#clone(url, options = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/multirepo/git/repo.rb', line 57

def clone(url, options = nil)
  options = {} unless options
  
  branch = options[:branch]

  command = "clone #{url} #{@path}"
  command << " -q" if options[:quiet] || false
  command << " -b #{branch}" if branch
  command << " --recurse-submodules"
  command << " --shallow-submodules" if options[:shallow] || false
  command << " --depth 1" if options[:shallow] || false
  
  GitRunner.run_as_system(".", command)
  GitRunner.last_command_succeeded
end

#commit(id) ⇒ Object



112
113
114
# File 'lib/multirepo/git/repo.rb', line 112

def commit(id)
  Commit.new(self, id)
end

#current_branchObject



91
92
93
94
95
96
# File 'lib/multirepo/git/repo.rb', line 91

def current_branch
  return nil unless exists? && head_born?
  name = GitRunner.run(@path, "rev-parse --abbrev-ref HEAD", Verbosity::OUTPUT_NEVER).strip
  return nil if name == "HEAD" # Code assumes that current_branch will be nil when we're in floating HEAD
  Branch.new(self, name)
end

#current_commitObject



86
87
88
89
# File 'lib/multirepo/git/repo.rb', line 86

def current_commit
  return nil unless exists? && head_born?
  Commit.new(self, head.commit_id)
end

#current_revisionObject



28
29
30
# File 'lib/multirepo/git/repo.rb', line 28

def current_revision
  (current_branch || current_commit).name
end

#exists?Boolean

Inspection

Returns:



18
19
20
21
# File 'lib/multirepo/git/repo.rb', line 18

def exists?
  return false unless Dir.exist?("#{@path}/.git")
  return GitRunner.run(@path, "rev-parse --is-inside-work-tree", Verbosity::OUTPUT_NEVER).strip == "true"
end

#fetchObject

Operations



52
53
54
55
# File 'lib/multirepo/git/repo.rb', line 52

def fetch
  GitRunner.run_as_system(@path, "fetch --prune")
  GitRunner.last_command_succeeded
end

#headObject

Current



81
82
83
84
# File 'lib/multirepo/git/repo.rb', line 81

def head
  return nil unless exists? && head_born?
  Ref.new(self, "HEAD")
end

#head_born?Boolean

Returns:



23
24
25
26
# File 'lib/multirepo/git/repo.rb', line 23

def head_born?
  result = GitRunner.run(@path, "rev-parse HEAD --", Verbosity::OUTPUT_NEVER).strip
  return !result.start_with?("fatal: bad revision")
end

#local_branchesObject



36
37
38
# File 'lib/multirepo/git/repo.rb', line 36

def local_branches
  branches_by_removing_prefix(%r{^refs/heads/})
end

#ref(name) ⇒ Object

Factory methods



100
101
102
# File 'lib/multirepo/git/repo.rb', line 100

def ref(name)
  Ref.new(self, name)
end

#remote(name) ⇒ Object



108
109
110
# File 'lib/multirepo/git/repo.rb', line 108

def remote(name)
  Remote.new(self, name)
end

#remote_branchesObject



40
41
42
# File 'lib/multirepo/git/repo.rb', line 40

def remote_branches
  branches_by_removing_prefix(%r{^refs/remotes/})
end