8784: Fix test for latest firefox.
[arvados.git] / services / api / lib / app_version.rb
1 # If you change this file, you'll probably also want to make the same
2 # changes in apps/workbench/lib/app_version.rb.
3
4 class AppVersion
5   def self.git(*args, &block)
6     IO.popen(["git", "--git-dir", ".git"] + args, "r",
7              chdir: Rails.root.join('../..'),
8              err: "/dev/null",
9              &block)
10   end
11
12   def self.forget
13     @hash = nil
14   end
15
16   # Return abbrev commit hash for current code version: "abc1234", or
17   # "abc1234-modified" if there are uncommitted changes. If present,
18   # return contents of {root}/git-commit.version instead.
19   def self.hash
20     if (cached = Rails.configuration.source_version || @hash)
21       return cached
22     end
23
24     # Read the version from our package's git-commit.version file, if available.
25     begin
26       @hash = IO.read(Rails.root.join("git-commit.version")).strip
27     rescue Errno::ENOENT
28     end
29
30     if @hash.nil? or @hash.empty?
31       begin
32         local_modified = false
33         git("status", "--porcelain") do |git_pipe|
34           git_pipe.each_line do |_|
35             local_modified = true
36             # Continue reading the pipe so git doesn't get SIGPIPE.
37           end
38         end
39         if $?.success?
40           git("log", "-n1", "--format=%H") do |git_pipe|
41             git_pipe.each_line do |line|
42               @hash = line.chomp[0...8] + (local_modified ? '-modified' : '')
43             end
44           end
45         end
46       rescue SystemCallError
47       end
48     end
49
50     @hash || "unknown"
51   end
52 end