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