Files
profile/helpers/updates.sh
T
2026-10-13 16:21:24 +02:00

63 lines
2.8 KiB
Bash

#!/usr/bin/env bash
# ------------------------------------------------------------------------------
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
# Protected by the BSD3 license. Please read bellow for details.
#
# * Redistribution and use in source and binary forms,
# * with or without modification, are permitted provided
# * that the following conditions are met:
# *
# * Redistributions of source code must retain the above
# * copyright notice, this list of conditions and the
# * following disclaimer.
# *
# * Redistributions in binary form must reproduce the above
# * copyright notice, this list of conditions and the following
# * disclaimer in the documentation and/or other materials
# * provided with the distribution.
# *
# * Neither the name of the copyright holder nor the names
# * of any other contributors may be used to endorse or
# * promote products derived from this software without
# * specific prior written permission.
# *
# * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# * OF SUCH DAMAGE.
# ------------------------------------------------------------------------------
# Private helpers of profile.d/updates.sh, loaded by the module itself.
# Check a branch name. It ends up in URLs and in git command lines, so it is
# restricted to plain ref characters, without a leading "-" or "..".
# Usage: _updt_valid_branch <name>
_updt_valid_branch()
{
[[ "$1" =~ ^[A-Za-z0-9][A-Za-z0-9._/-]*$ && "$1" != *..* ]]
}
# Tell whether the published version $2 is an update for the installed one $1.
# Versions look like MAJOR.MINOR.PATCH[-REVISION[_STAGE_N]]. The base version
# (before the first "-" or "_") decides, compared in version order; with the same
# base, any difference (a new revision, a release candidate becoming final) is
# an update. Returns 0 when it is, 1 otherwise (equal, or the installed version
# is the more recent one, as on a development branch).
# Usage: _updt_is_newer <installed> <published>
_updt_is_newer()
{
local cur="$1" new="$2" cur_base new_base
[[ "$cur" == "$new" ]] && return 1
cur_base="${cur%%[-_]*}"
new_base="${new%%[-_]*}"
[[ "$cur_base" == "$new_base" ]] && return 0
[[ "$(printf '%s\n%s\n' "$cur_base" "$new_base" | sort -V | tail -n 1)" == "$new_base" ]]
}