Class: Chicanery::Git::Repo

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/chicanery/git.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path, params = {}) ⇒ Repo

Returns a new instance of Repo.



10
11
12
13
14
# File 'lib/chicanery/git.rb', line 10

def initialize name, path, params={}
  @name, @path = name, path
  @branches = params[:branches] || []
  @remotes = params[:remotes] || {}
end

Instance Attribute Details

#branchesObject (readonly)

Returns the value of attribute branches.



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

def branches
  @branches
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#remotesObject (readonly)

Returns the value of attribute remotes.



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

def remotes
  @remotes
end

Instance Method Details

#git(command) ⇒ Object



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

def git command
  `git #{command}`.chomp
end

#head(branch) ⇒ Object



44
45
46
# File 'lib/chicanery/git.rb', line 44

def head branch
  sha "log -n 1 #{branch} --pretty=oneline"
end

#in_repoObject



16
17
18
# File 'lib/chicanery/git.rb', line 16

def in_repo
  Dir.chdir(path) { yield }
end

#prepareObject



20
21
22
23
24
# File 'lib/chicanery/git.rb', line 20

def prepare
  return if File.exists? path
  mkdir_p path
  in_repo { git 'init' }
end

#remote_head(url, branch) ⇒ Object



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

def remote_head url, branch
  sha "ls-remote #{url} #{branch}"
end

#sha(command) ⇒ Object



48
49
50
# File 'lib/chicanery/git.rb', line 48

def sha command
  git(command).split.first
end

#stateObject



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

def state
  prepare
  response = {}
  in_repo do
    remotes.each do |name, remote|
      (remote[:branches] || ['master']).each do |branch|
        response["#{name}/#{branch}"] = remote_head remote[:url], branch
      end
    end
    branches.each { |branch| response[branch] = head branch }
  end
  response
end