Class: Remotes::Refspec

Inherits:
Struct
  • Object
show all
Defined in:
lib/remotes/refspec.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#forcedObject

Returns the value of attribute forced

Returns:

  • (Object)

    the current value of forced



10
11
12
# File 'lib/remotes/refspec.rb', line 10

def forced
  @forced
end

#sourceObject

Returns the value of attribute source

Returns:

  • (Object)

    the current value of source



10
11
12
# File 'lib/remotes/refspec.rb', line 10

def source
  @source
end

#targetObject

Returns the value of attribute target

Returns:

  • (Object)

    the current value of target



10
11
12
# File 'lib/remotes/refspec.rb', line 10

def target
  @target
end

Class Method Details

.canonical(name) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/remotes/refspec.rb', line 19

def self.canonical(name)
  return nil if name.to_s == ""
  return name unless Revision.valid_ref?(name)

  first  = Pathname.new(name).descend.first
  dirs   = [Refs::REFS_DIR, Refs::HEADS_DIR, Refs::REMOTES_DIR]
  prefix = dirs.find { |dir| dir.basename == first }

  (prefix&.dirname || Refs::HEADS_DIR).join(name).to_s
end

.expand(specs, refs) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/remotes/refspec.rb', line 30

def self.expand(specs, refs)
  specs = specs.map { |spec| Refspec.parse(spec) }

  specs.reduce({}) do |mappings, spec|
    mappings.merge(spec.match_refs(refs))
  end
end

.invert(specs, ref) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/remotes/refspec.rb', line 38

def self.invert(specs, ref)
  specs = specs.map { |spec| Refspec.parse(spec) }

  map = specs.reduce({}) do |mappings, spec|
    spec.source, spec.target = spec.target, spec.source
    mappings.merge(spec.match_refs([ref]))
  end

  map.keys.first
end

.parse(spec) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/remotes/refspec.rb', line 11

def self.parse(spec)
  match  = REFSPEC_FORMAT.match(spec)
  source = Refspec.canonical(match[2])
  target = Refspec.canonical(match[4]) || source

  Refspec.new(source, target, match[1] == "+")
end

Instance Method Details

#match_refs(refs) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/remotes/refspec.rb', line 49

def match_refs(refs)
  return { target => [source, forced] } unless source.to_s.include?("*")

  pattern  = /^#{ source.sub("*", "(.*)") }$/
  mappings = {}

  refs.each do |ref|
    next unless match = pattern.match(ref)
    dst = match[1] ? target.sub("*", match[1]) : target
    mappings[dst] = [ref, forced]
  end

  mappings
end

#to_sObject



64
65
66
67
# File 'lib/remotes/refspec.rb', line 64

def to_s
  spec = forced ? "+" : ""
  spec + [source, target].join(":")
end