Class: Manifestly::ManifestItem

Inherits:
Object
  • Object
show all
Defined in:
lib/manifestly/manifest_item.rb

Defined Under Namespace

Classes: InvalidManifestItem, MultipleSameNameRepositories, RepositoryNotFound

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository) ⇒ ManifestItem

Returns a new instance of ManifestItem.



8
9
10
11
# File 'lib/manifestly/manifest_item.rb', line 8

def initialize(repository)
  @repository = repository
  @commit = repository.current_commit
end

Instance Attribute Details

#commitObject

Returns the value of attribute commit.



114
115
116
# File 'lib/manifestly/manifest_item.rb', line 114

def commit
  @commit
end

#repositoryObject (readonly)

Returns the value of attribute repository.



115
116
117
# File 'lib/manifestly/manifest_item.rb', line 115

def repository
  @repository
end

Class Method Details

.from_file_string(string, repositories) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/manifestly/manifest_item.rb', line 52

def self.from_file_string(string, repositories)
  repo_name, dir, sha = parse_file_string(string)

  matching_repositories = repositories.select do |repo|
    # directory name will be the most unique way to match and should always be available
    # wheres repo_name won't always be
    repo.deepest_working_dir == dir
  end

  # TODO test these two exceptions!
  if matching_repositories.size > 1
    raise(MultipleSameNameRepositories, "Dir: [#{dir}], Repo: [#{repo_name}]")
  end

  if matching_repositories.empty?
    raise(RepositoryNotFound, "Dir: [#{dir}], Repo: [#{repo_name}]")
  end

  repository = matching_repositories.first

  item = ManifestItem.new(repository)
  item.set_commit_by_sha(sha)
  item
end

.parse_file_string(string) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/manifestly/manifest_item.rb', line 77

def self.parse_file_string(string)
  sha_re = "([a-fA-F0-9]+)"
  repo_re = '([\w-]+\/[\w-]+)'
  old_dir_re_1 = '\((.*)\)'
  old_dir_re_2 = '(.*)'
  new_dir_re = '\[(.*)\]'

  # New style: `[dir] org/repo@sha`
  if string =~ /#{new_dir_re}\W*#{repo_re}\W*@\W*#{sha_re}/
    dir = $1.strip
    repo_name = $2.strip
    sha = $3.strip
  # New style where there is no GH repo: `[dir]@sha`
  elsif string =~ /#{new_dir_re}\W*@\W*#{sha_re}/
    dir = $1.strip
    sha = $2.strip
  # Old style with dir shown: `org/repo (dir) @ sha`
  elsif string =~ /#{repo_re}\W*#{old_dir_re_1}\W*@\W*#{sha_re}/
    repo_name = $1.strip
    dir = $2.strip
    sha = $3.strip
  # Old style with implicit dir: `org/repo @ sha`
  elsif string =~ /#{repo_re}\W*@\W*#{sha_re}/
    repo_name = $1.strip
    sha = $2.strip
    dir = repo_name.split('/').last
  # Old style with no repo: `dir @ sha`
  elsif string =~ /#{old_dir_re_2}\W*@\W*#{sha_re}/
    dir = $1.strip
    sha = $2.strip
  else
    raise(InvalidManifestItem, string)
  end

  [repo_name, dir, sha]
end

Instance Method Details

#checkout_commit!(fetch_if_unfound = false) ⇒ Object



29
30
31
# File 'lib/manifestly/manifest_item.rb', line 29

def checkout_commit!(fetch_if_unfound=false)
  @repository.checkout_commit(@commit.sha, fetch_if_unfound)
end

#commit_tagsObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/manifestly/manifest_item.rb', line 33

def commit_tags
  @repository.git.tags.select do |tag|
    # Gotta do some digging to get tagged sha out of annotated tags
    tagged_sha = tag.annotated? ?
                 tag.contents_array[0].split(" ").last == commit :
                 tag.sha

    tagged_sha == commit.sha
  end.map(&:name)
end

#fetchObject



25
26
27
# File 'lib/manifestly/manifest_item.rb', line 25

def fetch
  @repository.fetch
end

#repository_nameObject



13
14
15
# File 'lib/manifestly/manifest_item.rb', line 13

def repository_name
  @repository.display_name
end

#set_commit_by_index(index) ⇒ Object



17
18
19
# File 'lib/manifestly/manifest_item.rb', line 17

def set_commit_by_index(index)
  @commit = @repository.commits[index]
end

#set_commit_by_sha(sha) ⇒ Object



21
22
23
# File 'lib/manifestly/manifest_item.rb', line 21

def set_commit_by_sha(sha)
  @commit = @repository.find_commit(sha)
end

#to_file_stringObject



44
45
46
47
48
49
50
# File 'lib/manifestly/manifest_item.rb', line 44

def to_file_string
  dir = @repository.deepest_working_dir
  repo = @repository.github_name_or_path
  tags = commit_tags

  "[#{dir}]#{repo.nil? ? '' : ' ' + repo}@#{commit.sha[0..9]}#{' # ' + tags.join(",") if tags.any?}"
end