From 285356f4b46d85832d28d135ed82132a1dd07843 Mon Sep 17 00:00:00 2001 From: Alexander Afonin <63101279+lueFlake@users.noreply.github.com> Date: Wed, 9 Oct 2024 13:56:25 +0300 Subject: [PATCH 01/13] Create aocih.sh --- metrics/aocih.sh | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 metrics/aocih.sh diff --git a/metrics/aocih.sh b/metrics/aocih.sh new file mode 100644 index 00000000..f7963b92 --- /dev/null +++ b/metrics/aocih.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash +# The MIT License (MIT) +# +# Copyright (c) 2021-2024 Yegor Bugayenko +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +set -e +set -o pipefail + +java_file=$1 +output=$2 + +creation_timestamp=$(git log --diff-filter=A --format=%at -- "$java_file" | tail -1) +if [[ -z "$creation_timestamp" ]]; then + age_in_hours=0 +else + current_timestamp=$(date +%s) + age_in_seconds=$((current_timestamp - creation_timestamp)) + age_in_hours=$((age_in_seconds / 3600)) +fi + +echo "AoCiH $age_in_hours Age of the class in hours" > "$output" \ No newline at end of file From 9ec96fce3be93617e72a1d41f36aa354d3a19d52 Mon Sep 17 00:00:00 2001 From: Alexander Afonin <63101279+lueFlake@users.noreply.github.com> Date: Wed, 9 Oct 2024 14:44:59 +0300 Subject: [PATCH 02/13] added tests for aocih used authors.sh as an example --- metrics/aocih.sh | 2 +- tests/metrics/test-aocih.sh | 50 +++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests/metrics/test-aocih.sh diff --git a/metrics/aocih.sh b/metrics/aocih.sh index f7963b92..6a07f2de 100644 --- a/metrics/aocih.sh +++ b/metrics/aocih.sh @@ -36,4 +36,4 @@ else age_in_hours=$((age_in_seconds / 3600)) fi -echo "AoCiH $age_in_hours Age of the class in hours" > "$output" \ No newline at end of file +echo "AoCiH $age_in_hours Age of the class in hours" > "$output" \ No newline at end of file diff --git a/tests/metrics/test-aocih.sh b/tests/metrics/test-aocih.sh new file mode 100644 index 00000000..cead3282 --- /dev/null +++ b/tests/metrics/test-aocih.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +# The MIT License (MIT) +# +# Copyright (c) 2021-2024 Yegor Bugayenko +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +set -e +set -o pipefail + +temp=$1 +stdout=$2 + +{ + tmp=$(mktemp -d /tmp/XXXX) + "${LOCAL}/metrics/age_of_class.sh" "${tmp}/Test.java" "${temp}/stdout" + grep -q "AoCiH 0 " "${temp}/stdout" +} > "${stdout}" 2>&1 +echo "👍🏻 Didn't fail in non-git directory" + +{ + mkdir -p "${temp}/foo" + cd "${temp}/foo" + git init --quiet . + git config user.email 'foo@example.com' + git config user.name 'Foo' + java="Test.java" + echo "class Foo {}" > "${java}" + git add "${java}" + git config commit.gpgsign false + git commit --quiet -am "Initial commit" + "${LOCAL}/metrics/age_of_class.sh" "${java}" stdout + grep -qP "AoCiH [^0]" stdout +} > "${stdout}" 2>&1 +echo "👍🏻 Correctly calculated AoCiH in the repository" \ No newline at end of file From b488e8b048fc05e6e5cc60e5b10e4ba644f3d67d Mon Sep 17 00:00:00 2001 From: Alexander Afonin <63101279+lueFlake@users.noreply.github.com> Date: Fri, 11 Oct 2024 03:10:39 +0300 Subject: [PATCH 03/13] Update aocih.sh --- metrics/aocih.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/metrics/aocih.sh b/metrics/aocih.sh index 6a07f2de..c45403e1 100644 --- a/metrics/aocih.sh +++ b/metrics/aocih.sh @@ -27,13 +27,13 @@ set -o pipefail java_file=$1 output=$2 -creation_timestamp=$(git log --diff-filter=A --format=%at -- "$java_file" | tail -1) -if [[ -z "$creation_timestamp" ]]; then +repo_first_commit=$(git log --reverse --format=%at | head -1) +file_first_commit=$(git log --diff-filter=A --format=%at -- "$java_file" | tail -1) +if [[ -z "$file_first_commit" ]]; then age_in_hours=0 else - current_timestamp=$(date +%s) - age_in_seconds=$((current_timestamp - creation_timestamp)) + age_in_seconds=$((file_first_commit - repo_first_commit)) age_in_hours=$((age_in_seconds / 3600)) fi -echo "AoCiH $age_in_hours Age of the class in hours" > "$output" \ No newline at end of file +echo "AoCiH $age_in_hours Age of Class in Hours" > "$output" \ No newline at end of file From c08929369e378389df276de29116edba92d69209 Mon Sep 17 00:00:00 2001 From: Alexander Afonin <63101279+lueFlake@users.noreply.github.com> Date: Fri, 11 Oct 2024 10:55:05 +0300 Subject: [PATCH 04/13] Update aocih.sh --- metrics/aocih.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metrics/aocih.sh b/metrics/aocih.sh index c45403e1..58be7b34 100644 --- a/metrics/aocih.sh +++ b/metrics/aocih.sh @@ -25,7 +25,7 @@ set -e set -o pipefail java_file=$1 -output=$2 +output=$(realpath "$2") repo_first_commit=$(git log --reverse --format=%at | head -1) file_first_commit=$(git log --diff-filter=A --format=%at -- "$java_file" | tail -1) @@ -36,4 +36,4 @@ else age_in_hours=$((age_in_seconds / 3600)) fi -echo "AoCiH $age_in_hours Age of Class in Hours" > "$output" \ No newline at end of file +echo "AoCiH $age_in_hours Age of Class in Hours" > "$output" From 5a9d3373c9670b235c101fcd0e84c515f8d96f35 Mon Sep 17 00:00:00 2001 From: Alexander Afonin <63101279+lueFlake@users.noreply.github.com> Date: Sat, 12 Oct 2024 13:23:06 +0300 Subject: [PATCH 05/13] fixed aocih.sh --- metrics/aocih.sh | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/metrics/aocih.sh b/metrics/aocih.sh index 58be7b34..d1057015 100644 --- a/metrics/aocih.sh +++ b/metrics/aocih.sh @@ -27,13 +27,25 @@ set -o pipefail java_file=$1 output=$(realpath "$2") -repo_first_commit=$(git log --reverse --format=%at | head -1) -file_first_commit=$(git log --diff-filter=A --format=%at -- "$java_file" | tail -1) -if [[ -z "$file_first_commit" ]]; then - age_in_hours=0 +age_in_hours=0 +handle_error() { + echo "AoCiH 0 Age of Class in Hours" > "$output" +} +trap handle_error ERR + +cd "$(realpath $(dirname "${java_file}"))" +echo "$(realpath $(dirname "${java_file}"))" +if git status > /dev/null 2>&1; then + repo_first_commit=$(git log --reverse --format=%at | head -1) + file_first_commit=$(git log --diff-filter=A --format=%at -- "$java_file" | tail -1) + if [[ -z "$file_first_commit" ]]; then + age_in_hours=0 + else + age_in_seconds=$((file_first_commit - repo_first_commit)) + age_in_hours=$((age_in_seconds / 3600)) + fi else - age_in_seconds=$((file_first_commit - repo_first_commit)) - age_in_hours=$((age_in_seconds / 3600)) + age_in_hours=0 fi echo "AoCiH $age_in_hours Age of Class in Hours" > "$output" From d4b614b83f66e315e7e6bb85b9dd72ed2f423125 Mon Sep 17 00:00:00 2001 From: Alexander Afonin <63101279+lueFlake@users.noreply.github.com> Date: Sat, 12 Oct 2024 13:39:11 +0300 Subject: [PATCH 06/13] Update aocih.sh --- metrics/aocih.sh | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/metrics/aocih.sh b/metrics/aocih.sh index d1057015..ff79f7cd 100644 --- a/metrics/aocih.sh +++ b/metrics/aocih.sh @@ -24,17 +24,13 @@ set -e set -o pipefail -java_file=$1 +java_file=$(realpath $1) output=$(realpath "$2") age_in_hours=0 -handle_error() { - echo "AoCiH 0 Age of Class in Hours" > "$output" -} -trap handle_error ERR -cd "$(realpath $(dirname "${java_file}"))" -echo "$(realpath $(dirname "${java_file}"))" +cd "$(dirname "${java_file}")" + if git status > /dev/null 2>&1; then repo_first_commit=$(git log --reverse --format=%at | head -1) file_first_commit=$(git log --diff-filter=A --format=%at -- "$java_file" | tail -1) @@ -44,8 +40,6 @@ if git status > /dev/null 2>&1; then age_in_seconds=$((file_first_commit - repo_first_commit)) age_in_hours=$((age_in_seconds / 3600)) fi -else - age_in_hours=0 fi echo "AoCiH $age_in_hours Age of Class in Hours" > "$output" From c29bd5accc07d387b230e5f318693d0a2010a69d Mon Sep 17 00:00:00 2001 From: Alexander Afonin <63101279+lueFlake@users.noreply.github.com> Date: Sat, 12 Oct 2024 13:40:42 +0300 Subject: [PATCH 07/13] Update aocih.sh --- metrics/aocih.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metrics/aocih.sh b/metrics/aocih.sh index ff79f7cd..0014a935 100644 --- a/metrics/aocih.sh +++ b/metrics/aocih.sh @@ -24,7 +24,7 @@ set -e set -o pipefail -java_file=$(realpath $1) +java_file=$(realpath "$1") output=$(realpath "$2") age_in_hours=0 From 3be7bb7dcee357a6181e0c20cfab341747e9fc50 Mon Sep 17 00:00:00 2001 From: Alexander Afonin <63101279+lueFlake@users.noreply.github.com> Date: Sat, 12 Oct 2024 14:23:39 +0300 Subject: [PATCH 08/13] refactor of condition --- metrics/aocih.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/metrics/aocih.sh b/metrics/aocih.sh index 0014a935..431536ea 100644 --- a/metrics/aocih.sh +++ b/metrics/aocih.sh @@ -34,9 +34,7 @@ cd "$(dirname "${java_file}")" if git status > /dev/null 2>&1; then repo_first_commit=$(git log --reverse --format=%at | head -1) file_first_commit=$(git log --diff-filter=A --format=%at -- "$java_file" | tail -1) - if [[ -z "$file_first_commit" ]]; then - age_in_hours=0 - else + if [[ -n "$file_first_commit" ]]; then age_in_seconds=$((file_first_commit - repo_first_commit)) age_in_hours=$((age_in_seconds / 3600)) fi From 067001e70dec9beb1923ba4a06fba5f2e26e6bdc Mon Sep 17 00:00:00 2001 From: Alexander Afonin <63101279+lueFlake@users.noreply.github.com> Date: Mon, 14 Oct 2024 14:20:13 +0300 Subject: [PATCH 09/13] Update aocih.sh --- metrics/aocih.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/metrics/aocih.sh b/metrics/aocih.sh index 431536ea..6a4d1a5a 100644 --- a/metrics/aocih.sh +++ b/metrics/aocih.sh @@ -28,7 +28,6 @@ java_file=$(realpath "$1") output=$(realpath "$2") age_in_hours=0 - cd "$(dirname "${java_file}")" if git status > /dev/null 2>&1; then From e22a5e66d94a1aaa0c2dd72a6f4b659700f45a25 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 14 Oct 2024 23:58:58 +0300 Subject: [PATCH 10/13] chmod +x executed --- metrics/aocih.sh | 0 tests/metrics/test-aocih.sh | 0 2 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 metrics/aocih.sh mode change 100644 => 100755 tests/metrics/test-aocih.sh diff --git a/metrics/aocih.sh b/metrics/aocih.sh old mode 100644 new mode 100755 diff --git a/tests/metrics/test-aocih.sh b/tests/metrics/test-aocih.sh old mode 100644 new mode 100755 From 01f8be528b75aa2a4b5d547f779eb7b2657a3175 Mon Sep 17 00:00:00 2001 From: Alexander Afonin <63101279+lueFlake@users.noreply.github.com> Date: Tue, 15 Oct 2024 00:20:57 +0300 Subject: [PATCH 11/13] Update test-aocih.sh --- tests/metrics/test-aocih.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/metrics/test-aocih.sh b/tests/metrics/test-aocih.sh index cead3282..ac4c47ee 100755 --- a/tests/metrics/test-aocih.sh +++ b/tests/metrics/test-aocih.sh @@ -28,7 +28,7 @@ stdout=$2 { tmp=$(mktemp -d /tmp/XXXX) - "${LOCAL}/metrics/age_of_class.sh" "${tmp}/Test.java" "${temp}/stdout" + "${LOCAL}/metrics/aocih.sh" "${tmp}/Test.java" "${temp}/stdout" grep -q "AoCiH 0 " "${temp}/stdout" } > "${stdout}" 2>&1 echo "👍🏻 Didn't fail in non-git directory" @@ -44,7 +44,7 @@ echo "👍🏻 Didn't fail in non-git directory" git add "${java}" git config commit.gpgsign false git commit --quiet -am "Initial commit" - "${LOCAL}/metrics/age_of_class.sh" "${java}" stdout + "${LOCAL}/metrics/aocih.sh" "${java}" stdout grep -qP "AoCiH [^0]" stdout } > "${stdout}" 2>&1 echo "👍🏻 Correctly calculated AoCiH in the repository" \ No newline at end of file From 6633d7af04554096a7cde78087a30798a5e7965b Mon Sep 17 00:00:00 2001 From: Alexander Afonin <63101279+lueFlake@users.noreply.github.com> Date: Tue, 15 Oct 2024 00:52:08 +0300 Subject: [PATCH 12/13] Update test-aocih.sh --- tests/metrics/test-aocih.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/metrics/test-aocih.sh b/tests/metrics/test-aocih.sh index ac4c47ee..51392f84 100755 --- a/tests/metrics/test-aocih.sh +++ b/tests/metrics/test-aocih.sh @@ -39,11 +39,15 @@ echo "👍🏻 Didn't fail in non-git directory" git init --quiet . git config user.email 'foo@example.com' git config user.name 'Foo' + git config commit.gpgsign false + touch empty.file + git add "empty.file" + git commit --quiet -am "Initial commit" java="Test.java" echo "class Foo {}" > "${java}" git add "${java}" - git config commit.gpgsign false - git commit --quiet -am "Initial commit" + git commit --quiet -am "Second commit" + git commit --amend --no-edit --date="$(date -d "+1 hour" --rfc-2822)" "${LOCAL}/metrics/aocih.sh" "${java}" stdout grep -qP "AoCiH [^0]" stdout } > "${stdout}" 2>&1 From 7aded1b92aff6422a233202e6e6bb4cec059bd64 Mon Sep 17 00:00:00 2001 From: Alexander Afonin <63101279+lueFlake@users.noreply.github.com> Date: Tue, 15 Oct 2024 00:52:52 +0300 Subject: [PATCH 13/13] make test even more certain --- tests/metrics/test-aocih.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/metrics/test-aocih.sh b/tests/metrics/test-aocih.sh index 51392f84..1e3a5b7a 100755 --- a/tests/metrics/test-aocih.sh +++ b/tests/metrics/test-aocih.sh @@ -49,6 +49,6 @@ echo "👍🏻 Didn't fail in non-git directory" git commit --quiet -am "Second commit" git commit --amend --no-edit --date="$(date -d "+1 hour" --rfc-2822)" "${LOCAL}/metrics/aocih.sh" "${java}" stdout - grep -qP "AoCiH [^0]" stdout + grep -qP "AoCiH 1" stdout } > "${stdout}" 2>&1 echo "👍🏻 Correctly calculated AoCiH in the repository" \ No newline at end of file