Class: Fixman::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/fixman/repository.rb

Constant Summary collapse

FIELD_LABEL_WIDTH =
20
GIT_URL_REGEXP =
%r{
  (?:github\.com)\/
  (?<owner>(?:\w|-)+)
  \/
  (?<name>(?:\w|-)+)
  (?:\.git)?
  $
}xi

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo_params, fixtures_base, extra_repo_info) ⇒ Repository

Returns a new instance of Repository.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fixman/repository.rb', line 22

def initialize(repo_params, fixtures_base, extra_repo_info)
  @url = repo_params[:url]
  @name = repo_params[:name]
  @owner = repo_params[:owner]
  @sha = repo_params[:sha]
  @groups = repo_params[:groups]

  [:url, :name, :owner, :sha, :groups].each { |key| repo_params.delete key }
  @other_fields = repo_params

  @extra_repo_info = extra_repo_info
  @path = fixtures_base + canonical_name
end

Instance Attribute Details

#groupsObject

Returns the value of attribute groups.



19
20
21
# File 'lib/fixman/repository.rb', line 19

def groups
  @groups
end

#nameObject

Returns the value of attribute name.



19
20
21
# File 'lib/fixman/repository.rb', line 19

def name
  @name
end

#other_fieldsObject

Returns the value of attribute other_fields.



19
20
21
# File 'lib/fixman/repository.rb', line 19

def other_fields
  @other_fields
end

#ownerObject

Returns the value of attribute owner.



19
20
21
# File 'lib/fixman/repository.rb', line 19

def owner
  @owner
end

#pathObject (readonly)

Returns the value of attribute path.



20
21
22
# File 'lib/fixman/repository.rb', line 20

def path
  @path
end

#shaObject

Returns the value of attribute sha.



19
20
21
# File 'lib/fixman/repository.rb', line 19

def sha
  @sha
end

#urlObject

Returns the value of attribute url.



19
20
21
# File 'lib/fixman/repository.rb', line 19

def url
  @url
end

Class Method Details

.extract_owner_and_name(url) ⇒ Object



88
89
90
91
# File 'lib/fixman/repository.rb', line 88

def extract_owner_and_name(url)
  match_data = GIT_URL_REGEXP.match url
  [match_data[:owner], match_data[:name]] if match_data
end

.retrieve_head_sha(url) ⇒ Object



93
94
95
96
# File 'lib/fixman/repository.rb', line 93

def retrieve_head_sha url
  ref = Git.ls_remote url
  ref['head'][:sha]
end

Instance Method Details

#canonical_nameObject



55
56
57
# File 'lib/fixman/repository.rb', line 55

def canonical_name
  "#{@owner}/#{@name}"
end

#destroyObject



51
52
53
# File 'lib/fixman/repository.rb', line 51

def destroy
  FileUtils.rm_rf @path if File.exist? @path
end

#fetchObject



40
41
42
43
# File 'lib/fixman/repository.rb', line 40

def fetch
  git = Git.clone @url, @path
  git.reset_hard @sha
end

#fetched?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/fixman/repository.rb', line 36

def fetched?
  File.exist? @path
end

#summaryObject



59
60
61
# File 'lib/fixman/repository.rb', line 59

def summary
  "#{canonical_name}\t#{@sha}"
end

#to_sObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fixman/repository.rb', line 63

def to_s
  str = StringIO.new
  str.puts 'Repository'.ljust(FIELD_LABEL_WIDTH) + canonical_name
  str.puts 'URL'.ljust(FIELD_LABEL_WIDTH) + @url
  str.puts 'Commit SHA'.ljust(FIELD_LABEL_WIDTH) + @sha
  str.puts 'Groups'.ljust(FIELD_LABEL_WIDTH) + @groups.join(', ')
  @extra_repo_info.each do |info|
    str.puts info[:label].ljust(FIELD_LABEL_WIDTH) +
      @other_fields[info[:symbol]]
  end
  str.string
end

#to_yamlObject



76
77
78
79
80
81
82
83
84
85
# File 'lib/fixman/repository.rb', line 76

def to_yaml
  {
    name: @name,
    owner: @owner,
    sha: @sha,
    url: @url,
    access_right: @access_right,
    notes: @notes
  }.merge(@other_fields).to_yaml
end

#upgradeObject



45
46
47
48
49
# File 'lib/fixman/repository.rb', line 45

def upgrade
  git = Git.open @path
  git.pull
  git.reset_hard @sha
end