Class: Braid::Mirror

Inherits:
Object
  • Object
show all
Includes:
Operations::VersionControl
Defined in:
lib/braid/mirror.rb

Defined Under Namespace

Classes: NoTagAndBranch, PathRequired, UnknownType

Constant Summary collapse

ATTRIBUTES =
%w(url branch revision tag path)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Operations::VersionControl

#git, #git_cache

Constructor Details

#initialize(path, attributes = {}) ⇒ Mirror

Returns a new instance of Mirror.



25
26
27
28
# File 'lib/braid/mirror.rb', line 25

def initialize(path, attributes = {})
  @path       = path.sub(/\/$/, '')
  @attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/braid/mirror.rb', line 118

def method_missing(name, *args)
  if ATTRIBUTES.find { |attribute| name.to_s =~ /^(#{attribute})(=)?$/ }
    if $2
      attributes[$1] = args[0]
    else
      attributes[$1]
    end
  else
    raise NameError, "unknown attribute `#{name}'"
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



23
24
25
# File 'lib/braid/mirror.rb', line 23

def attributes
  @attributes
end

#pathObject (readonly)

Returns the value of attribute path.



23
24
25
# File 'lib/braid/mirror.rb', line 23

def path
  @path
end

Class Method Details

.new_from_options(url, options = {}) ⇒ Object

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/braid/mirror.rb', line 30

def self.new_from_options(url, options = {})
  url    = url.sub(/\/$/, '')

  raise NoTagAndBranch if options['tag'] && options['branch']

  tag = options['tag']
  branch = options['branch'] || (tag.nil? ? 'master' : nil)

  path = (options['path'] || extract_path_from_url(url)).sub(/\/$/, '')
  raise PathRequired unless path

  remote_path = options['remote_path']

  attributes = {'url' => url, 'branch' => branch, 'path' => remote_path, 'tag' => tag}
  self.new(path, attributes)
end

Instance Method Details

#==(comparison) ⇒ Object



47
48
49
# File 'lib/braid/mirror.rb', line 47

def ==(comparison)
  path == comparison.path && attributes == comparison.attributes
end

#base_revisionObject



82
83
84
85
86
87
88
# File 'lib/braid/mirror.rb', line 82

def base_revision
  if revision
    git.rev_parse(revision)
  else
    inferred_revision
  end
end

#cached?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/braid/mirror.rb', line 78

def cached?
  git.remote_url(remote) == cached_url
end

#cached_urlObject



108
109
110
# File 'lib/braid/mirror.rb', line 108

def cached_url
  git_cache.path(url)
end

#diffObject



66
67
68
69
70
71
# File 'lib/braid/mirror.rb', line 66

def diff
  fetch
  remote_hash = git.rev_parse(versioned_path(base_revision))
  local_hash  = git.tree_hash(path)
  remote_hash != local_hash ? git.diff_tree(remote_hash, local_hash) : ''
end

#fetchObject



73
74
75
76
# File 'lib/braid/mirror.rb', line 73

def fetch
  git_cache.fetch(url) if cached?
  git.fetch(remote)
end

#local_refObject



90
91
92
93
94
# File 'lib/braid/mirror.rb', line 90

def local_ref
  return "#{self.remote}/#{self.branch}" unless self.branch.nil?
  return "tags/#{self.tag}" unless self.tag.nil?
  self.revision
end

#locked?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/braid/mirror.rb', line 51

def locked?
  branch.nil? && tag.nil?
end

#merged?(commit) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
# File 'lib/braid/mirror.rb', line 55

def merged?(commit)
  # tip from spearce in #git:
  # `test z$(git merge-base A B) = z$(git rev-parse --verify A)`
  commit = git.rev_parse(commit)
  !!base_revision && git.merge_base(commit, base_revision) == commit
end

#remoteObject



112
113
114
# File 'lib/braid/mirror.rb', line 112

def remote
  "#{branch || tag || 'revision'}/braid/#{path}"
end

#remote_pathObject



100
101
102
# File 'lib/braid/mirror.rb', line 100

def remote_path
  self.attributes['path']
end

#remote_path=(remote_path) ⇒ Object



104
105
106
# File 'lib/braid/mirror.rb', line 104

def remote_path=(remote_path)
  self.attributes['path'] = remote_path
end

#remote_refObject



96
97
98
# File 'lib/braid/mirror.rb', line 96

def remote_ref
  self.branch.nil? ? "+refs/tags/#{self.tag}" : "+refs/heads/#{self.branch}"
end

#versioned_path(revision) ⇒ Object



62
63
64
# File 'lib/braid/mirror.rb', line 62

def versioned_path(revision)
  "#{revision}:#{self.remote_path}"
end