Module: Amp::Repositories::BranchManager

Includes:
Amp::RevlogSupport::Node
Included in:
LocalRepository
Defined in:
lib/amp/repository/branch_manager.rb

Overview

BranchManager

Michael Scott for Amp.

More seriously, this class handles reading/writing to the branch cache and figuring out what the head revisions are for each branch and such.

Constant Summary

Constants included from Amp::RevlogSupport::Node

Amp::RevlogSupport::Node::NULL_ID, Amp::RevlogSupport::Node::NULL_REV

Instance Method Summary collapse

Methods included from Amp::RevlogSupport::Node

#short

Instance Method Details

#branch_headsHash

Loads the head revisions for each branch. Each branch has at least one, but possible more than one, head.

Returns:

  • (Hash)

    a map, where the branch names are keys and the values are the heads of the branch



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/amp/repository/branch_manager.rb', line 33

def branch_heads
  # Gets the mapping of branch names to branch heads, but uses caching to avoid
  # doing IO and tedious computation over and over. As long as our tip doesn't
  # change, the cache will remain valid.
  
  # Check our current tip
  tip = self.changelog.tip
  # Do we have a cache, and if we do, is the saved cache == tip?
  if !@branch_cache.nil? && @branch_cache_tip == tip
    # if so, cache HIT
    return @branch_cache
  end
  
  # nope? cache miss
  # save the old tip
  oldtip = @branch_cache_tip
  # save the new tip
  @branch_cache_tip = tip
  
  # reset the branch cache
  @branch_cache = @branch_cache.nil? ? {} : @branch_cache.clear # returns same hash, but empty
  # if we didn't have an old cache, or it was invalid, read in the branches again
  if oldtip.nil? || self.changelog.node_map[oldtip].nil?
    partial, last, last_rev = read_branches!
  else
    # Otherwise, dig up the cached hash!
    last_rev = self.changelog.rev(oldtip)
    # Get the last branch cache
    partial = @u_branch_cache
  end
  # Save the branch cache (updating it if we have to)
  save_branch_cache(partial, last_rev)
  
  # Cache our saved hash
  @u_branch_cache = partial
  
  # Copy the partial into the branch_cache
  partial.each { |k, v| @branch_cache[k] = v }
  @branch_cache
end

#branch_tagsObject

Returns a dict where branch names map to the tipmost head of the branch, open heads come before closed



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/amp/repository/branch_manager.rb', line 76

def branch_tags
  tags = {}
  branch_heads.each do |branch_node, local_heads|
    head = nil
    local_heads.reverse_each do |h| # get the tip if its a closed
      if !(changelog.read(h)[5].include?("close"))
        head = h
        break
      end
    end
    head = local_heads.last if head.nil?
    tags[branch_node] = head # it's the tip
  end
  return tags
end

#save_branch_cache(partial, last_rev) ⇒ Object

Saves the branches with the given “partial” and the last_rev index.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/amp/repository/branch_manager.rb', line 14

def save_branch_cache(partial, last_rev)
  tiprev = self.size - 1
  # If our cache is outdated, then update it and re-write it
  if last_rev != tiprev
    # search for new heads
    update_branches!(partial, last_rev+1, tiprev+1)
    # write our data out
    write_branches!(partial, self.changelog.tip, tiprev)
  end
  # return our mappings
  partial
end

#save_branches(branches, tip, tip_revision) ⇒ Object

Saves the branches, the tip-most node, and the tip-most revision to the branch cache.



96
97
98
# File 'lib/amp/repository/branch_manager.rb', line 96

def save_branches(branches, tip, tip_revision)
  write_branches!(branches, tip, tip_revision)
end