Class: Rosette::Core::HeadSnapshotFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/rosette/core/snapshots/head_snapshot_factory.rb

Overview

Takes a snapshot of all the repository's heads. A “head” snapshot is a key/value map (hash) of git refs (i.e. branch names) to commit ids, where the commit ids are the first non-merge commits for each branch.

Examples:

snapshot = HeadSnapshotFactory.new
  .set_repo_config(config)
  .take_snapshot

Direct Known Subclasses

CachedHeadSnapshotFactory

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#repo_configRepoConfig (readonly)

Returns the repo config that contains the repo to take the snapshot for.

Returns:

  • (RepoConfig)

    the repo config that contains the repo to take the snapshot for.


21
22
23
24
25
26
27
28
29
30
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
# File 'lib/rosette/core/snapshots/head_snapshot_factory.rb', line 21

class HeadSnapshotFactory
  attr_reader :repo_config

  # Sets the repo config that contains the repo to take the snapshot for.
  #
  # @param [RepoConfig] repo_config
  # @return [self]
  def set_repo_config(repo_config)
    @repo_config = repo_config
    self
  end

  # Takes the snapshot.
  #
  # @return [Hash<String, String>] The head snapshot hash of refs to
  #   commit ids.
  def take_snapshot
    rev_walk = RevWalk.new(repo_config.repo.jgit_repo)
    repo_config.repo.all_head_refs.each_with_object({}) do |ref, snapshot|
      snapshot[ref] = process_ref(rev_walk, ref)
    end
  end

  protected

  def process_ref(rev_walk, ref)
    rev_walk.reset
    rev_walk.markStart(repo_config.repo.get_rev_commit(ref, rev_walk))
    rev_walk.setRevFilter(RevFilter::NO_MERGES)

    if rev_commit = rev_walk.next
      rev_commit.getId.name
    end
  end
end

Instance Method Details

#set_repo_config(repo_config) ⇒ self

Sets the repo config that contains the repo to take the snapshot for.

Parameters:

Returns:

  • (self)

28
29
30
31
# File 'lib/rosette/core/snapshots/head_snapshot_factory.rb', line 28

def set_repo_config(repo_config)
  @repo_config = repo_config
  self
end

#take_snapshotHash<String, String>

Takes the snapshot.

Returns:

  • (Hash<String, String>)

    The head snapshot hash of refs to commit ids.


37
38
39
40
41
42
# File 'lib/rosette/core/snapshots/head_snapshot_factory.rb', line 37

def take_snapshot
  rev_walk = RevWalk.new(repo_config.repo.jgit_repo)
  repo_config.repo.all_head_refs.each_with_object({}) do |ref, snapshot|
    snapshot[ref] = process_ref(rev_walk, ref)
  end
end