summaryrefslogtreecommitdiff
path: root/lib/booru/version_reader.rb
blob: 649fe2dedd706db6b7147d4c21fc263962e11be4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# frozen_string_literal: true

require 'rugged'
require 'rugged/repository'

module Booru
  # A helper class for reading the current repository
  # version.
  class VersionReader
    # Get a new reference to the application repository.
    #
    # This is not memoized, so as to avoid holding a long-lived
    # reference to a repository object in the application.
    #
    # @return [Rugged::Repository]
    def self.repo
      Rugged::Repository.new(Rails.root.to_s)
    end

    # The current branch name as a string (e.g. "master").
    #
    # @return [String]
    def self.branch_name
      repo.branches.detect(&:head?).name
    rescue ArgumentError
      'unknown'
    end

    # The object id of the most recent commit as a string
    # (e.g. "3717f613f48df0222311f974cf8a06c8a6c97bae").
    #
    # @return [String]
    def self.head_commit
      repo.head.target.oid
    rescue ArgumentError
      'unknown'
    end
  end
end