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 remote branch squashed revision lock)

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)



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

def method_missing(name, *args)
  if ATTRIBUTES.find { |attribute| name.to_s =~ /^(#{attribute})(=)?$/ }
    unless $2
      attributes[$1]
    else
      attributes[$1] = args[0]
    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



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

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

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

  unless path = options['path'] || extract_path_from_url(url)
    raise PathRequired
  end

  remote   = "#{branch}/braid/#{path}"
  squashed = !options['full']

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

Instance Method Details

#==(comparison) ⇒ Object



41
42
43
# File 'lib/braid/mirror.rb', line 41

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

#base_revisionObject



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

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

#cached?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/braid/mirror.rb', line 76

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

#cached_urlObject



88
89
90
# File 'lib/braid/mirror.rb', line 88

def cached_url
  git_cache.path(url)
end

#diffObject



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

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

#fetchObject



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

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

#locked?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/braid/mirror.rb', line 45

def locked?
  !!lock
end

#merged?(commit) ⇒ Boolean

Returns:

  • (Boolean)


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

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)
  if squashed?
    !!base_revision && git.merge_base(commit, base_revision) == commit
  else
    git.merge_base(commit, 'HEAD') == commit
  end
end

#remoteObject



92
93
94
95
96
97
98
# File 'lib/braid/mirror.rb', line 92

def remote
  if (attributes['remote'] && attributes['remote'] =~ /^braid\//)
    attributes['remote'] = "#{branch}/#{attributes['remote']}"
  else
    attributes['remote']
  end
end

#squashed?Boolean

Returns:

  • (Boolean)


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

def squashed?
  !!squashed
end