8784: Fix test for latest firefox.
[arvados.git] / apps / workbench / lib / app_version.rb
1 # If you change this file, you'll probably also want to make the same
2 # changes in services/api/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             STDERR.puts _
36             local_modified = true
37             # Continue reading the pipe so git doesn't get SIGPIPE.
38           end
39         end
40         if $?.success?
41           git("log", "-n1", "--format=%H") do |git_pipe|
42             git_pipe.each_line do |line|
43               @hash = line.chomp[0...8] + (local_modified ? '-modified' : '')
44             end
45           end
46         end
47       rescue SystemCallError
48       end
49     end
50
51     @hash || "unknown"
52   end
53 end