Class: Braid::Mirror

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

Defined Under Namespace

Classes: PathRequired, UnknownType

Constant Summary collapse

ATTRIBUTES =
%w(url branch revision lock 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.



20
21
22
23
# File 'lib/braid/mirror.rb', line 20

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)



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/braid/mirror.rb', line 100

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.



18
19
20
# File 'lib/braid/mirror.rb', line 18

def attributes
  @attributes
end

#pathObject (readonly)

Returns the value of attribute path.



18
19
20
# File 'lib/braid/mirror.rb', line 18

def path
  @path
end

Class Method Details

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

Raises:



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/braid/mirror.rb', line 25

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

  branch = options['branch'] || 'master'

  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}
  self.new(path, attributes)
end

Instance Method Details

#==(comparison) ⇒ Object



39
40
41
# File 'lib/braid/mirror.rb', line 39

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

#base_revisionObject



74
75
76
77
78
79
80
# File 'lib/braid/mirror.rb', line 74

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

#cached?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/braid/mirror.rb', line 70

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

#cached_urlObject



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

def cached_url
  git_cache.path(url)
end

#diffObject



58
59
60
61
62
63
# File 'lib/braid/mirror.rb', line 58

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



65
66
67
68
# File 'lib/braid/mirror.rb', line 65

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

#locked?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/braid/mirror.rb', line 43

def locked?
  !!lock
end

#merged?(commit) ⇒ Boolean

Returns:

  • (Boolean)


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

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



94
95
96
# File 'lib/braid/mirror.rb', line 94

def remote
  "#{branch}/braid/#{path}"
end

#remote_pathObject



82
83
84
# File 'lib/braid/mirror.rb', line 82

def remote_path
  self.attributes['path']
end

#remote_path=(remote_path) ⇒ Object



86
87
88
# File 'lib/braid/mirror.rb', line 86

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

#versioned_path(revision) ⇒ Object



54
55
56
# File 'lib/braid/mirror.rb', line 54

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