discourse/lib/git_utils.rb
Ted Johansson e3ce8d6858
FIX: New features git hash version tweaks (#34671)
There are two issues addressed by this PR:

We log an error to console when a git hash is passed to Discourse#has_needed_version?. This isn't correct. We should just return false.
We only check that the commit hash is in the repo, we don't check that it's actually an ancestor in the current tree. So you'd see the feature even if you're on a branch that doesn't have it.
2025-09-02 10:40:24 +08:00

39 lines
967 B
Ruby

# frozen_string_literal: true
class GitUtils
def self.git_version
self.try_git("git rev-parse HEAD", "unknown")
end
def self.git_branch
self.try_git("git branch --show-current", nil) ||
self.try_git("git config user.discourse-version", "unknown")
end
def self.full_version
self.try_git('git describe --dirty --match "v[0-9]*" 2> /dev/null', "unknown")
end
def self.has_commit?(hash)
return false if !hash.match?(/\A[a-f0-9]{40}\Z/)
self.try_git("git merge-base --is-ancestor #{hash} HEAD 2> /dev/null; echo $?", "1") == "0"
end
def self.last_commit_date
git_cmd = 'git log -1 --format="%ct"'
seconds = self.try_git(git_cmd, nil)
seconds.nil? ? nil : DateTime.strptime(seconds, "%s")
end
def self.try_git(git_cmd, default_value)
value =
begin
`#{git_cmd}`.strip
rescue StandardError
default_value
end
(!value.empty? ? value : nil) || default_value
end
end