Class: Braid::Mirror

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

Defined Under Namespace

Classes: CannotGuessType, PathRequired, UnknownType

Constant Summary collapse

TYPES =
%w(git svn)
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, #git_svn, #svn

Constructor Details

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

Returns a new instance of Mirror.



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

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})(=)?$/ }
    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.



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

def attributes
  @attributes
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/braid/mirror.rb', line 31

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

  branch = options["branch"] || "master"

  if type = options["type"] || extract_type_from_url(url)
    raise UnknownType, type unless TYPES.include?(type)
  else
    raise CannotGuessType, url
  end

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

  if options["rails_plugin"]
    path = "vendor/plugins/#{path}"
  end

  remote = "braid/#{path}".gsub("_", '-') # stupid git svn changes all _ to ., weird
  squashed = !options["full"]
  branch = nil if type == "svn"

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

Instance Method Details

#==(comparison) ⇒ Object



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

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

#base_revisionObject



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

def base_revision
  if revision
    unless type == "svn"
      git.rev_parse(revision)
    else
      git_svn.commit_hash(remote, revision)
    end
  else
    inferred_revision
  end
end

#cached?Boolean

Returns:

  • (Boolean)


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

def cached?
  git.remote_url(remote) == git_cache.path(url)
end

#diffObject



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

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, path) : ""
end

#fetchObject



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

def fetch
  unless type == "svn"
    git_cache.fetch(url) if cached?
    git.fetch(remote)
  else
    git_svn.fetch(remote)
  end
end

#locked?Boolean

Returns:

  • (Boolean)


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

def locked?
  !!lock
end

#merged?(commit) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
84
# File 'lib/braid/mirror.rb', line 75

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

#squashed?Boolean

Returns:

  • (Boolean)


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

def squashed?
  !!squashed
end

#typeObject



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

def type
  # override Object#type
  attributes["type"]
end