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 type 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)



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/braid/mirror.rb', line 105

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
40
41
42
43
# 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

  if options["rails_plugin"]
    path = "vendor/plugins/#{path}"
  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



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

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

#base_revisionObject



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

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

#cached?Boolean

Returns:



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

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

#cached_urlObject



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

def cached_url
  git_cache.path(url)
end

#diffObject



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

def diff
  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



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

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

#locked?Boolean

Returns:



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

def locked?
  !!lock
end

#merged?(commit) ⇒ Boolean

Returns:



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

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



95
96
97
98
99
100
101
# File 'lib/braid/mirror.rb', line 95

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

#squashed?Boolean

Returns:



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

def squashed?
  !!squashed
end