From 31a64724d61d8ad0ee11e23f4a6891e14b567143 Mon Sep 17 00:00:00 2001 From: Ben Grande Date: Mon, 13 Nov 2023 14:26:13 +0000 Subject: [PATCH] Fix git description validation --- files/git/.config/git/shell/.git-shell-lib | 20 +++++++++++++++----- files/git/.config/git/shell/description | 2 +- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/files/git/.config/git/shell/.git-shell-lib b/files/git/.config/git/shell/.git-shell-lib index 84939fc..063b865 100755 --- a/files/git/.config/git/shell/.git-shell-lib +++ b/files/git/.config/git/shell/.git-shell-lib @@ -15,12 +15,21 @@ base="src" ## Regex: can contain "A-Za-z0-9_.-" but must not start or end with "_.-". # shellcheck disable=SC2034 regex="^[A-Za-z0-9]+([A-Za-z0-9_.-]+[A-Za-z0-9]+)?$" +description_regex="^[A-Za-z0-9 _.-]+$" fail_invalid_name(){ echo "Error: invalid value for key: $1" >&2 exit 1 } +test_description(){ + key="$1" + value="$2" + if ! (echo "$value" | grep -E -q "${description_regex}"); then + fail_invalid_name "$key" + fi +} + test_name(){ key="$1" value="$2" @@ -37,13 +46,14 @@ clean_repo_name(){ } is_bare(){ - repo="$1" - if ! test -d "$repo"; then - echo "Repository doesn't exist: $repo" >&2 + _repo="$1" + if ! test -d "$_repo"; then + echo "Repository doesn't exist: $_repo" >&2 return 1 fi - if ! test "$(git -C "$repo" rev-parse --is-bare-repository)" = "true"; then - echo "Repository is not bare: $repo" >&2 + if ! test "$(git -C "$_repo" rev-parse --is-bare-repository)" = "true" + then + echo "Repository is not bare: $_repo" >&2 return 1 fi } diff --git a/files/git/.config/git/shell/description b/files/git/.config/git/shell/description index 5b047d7..c9f0489 100755 --- a/files/git/.config/git/shell/description +++ b/files/git/.config/git/shell/description @@ -36,6 +36,6 @@ if test -z "${3-}"; then fi description="$(echo "$3" | cut -c 1-80)" -test_name description "$description" +test_description description "$description" echo "$description" | tee "$description_file"