Module: CommitSynchronizer

Defined in:
app/concerns/commit_synchronizer.rb

Instance Method Summary collapse

Instance Method Details

#between(commit0, commit1) ⇒ Object



32
33
34
35
36
# File 'app/concerns/commit_synchronizer.rb', line 32

def between(commit0, commit1)
  synchronize repo.commits_between(commit0, commit1)
rescue Houston::Adapters::VersionControl::CommitNotFound
  []
end

#find_or_create_by_sha(sha) ⇒ Object



23
24
25
26
27
28
29
# File 'app/concerns/commit_synchronizer.rb', line 23

def find_or_create_by_sha(sha)
  return nil if sha.nil?
  sha = sha.sha if sha.respond_to?(:sha)
  find_by_sha(sha) || create_missing_commit!(repo.native_commit(sha))
rescue Houston::Adapters::VersionControl::CommitNotFound
  nil
end

#sync!Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/concerns/commit_synchronizer.rb', line 4

def sync!
  repo.refresh!
  Houston.benchmark "Synchronize Commits" do
    existing_commits = project.commits.pluck(:sha)
    expected_commits = repo.all_commits

    create_missing_commits!   expected_commits - existing_commits

    reachable_commits = project.commits.reachable.pluck(:sha)
    flag_unreachable_commits! reachable_commits - expected_commits

    unreachable_commits = project.commits.unreachable.pluck(:sha)
    flag_reachable_commits! unreachable_commits & expected_commits

    project.update_column :head_sha, project.repo.branch("master")
  end
end

#synchronize(native_commits = []) ⇒ Object



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
# File 'app/concerns/commit_synchronizer.rb', line 39

def synchronize(native_commits=[])
  if block_given?
    native_commits = Houston.benchmark("[commits.synchonize] reading commits") do
      yield repo
    end
  end

  native_commits = native_commits.reject(&:nil?)
  return [] if native_commits.empty?

  Houston.benchmark("[commits.synchronize] synchronizing #{native_commits.length} commits") do
    shas = native_commits.map(&:sha)
    commits = where(sha: shas)

    native_commits.map do |native_commit|
      commit = commits.detect { |commit| commit.sha == native_commit.sha } ||
               create_missing_commit!(native_commit)

      # There's no reason why this shouldn't be set,
      # but in order to reduce a bunch of useless hits
      # to the cache and a bunch of log output...
      commit.project = project
      commit
    end
  end
end