Skip to content

Commit

Permalink
Merge pull request #298 from IlnurHA/rfvc
Browse files Browse the repository at this point in the history
Relative File Volatility by Commits (RFVC)
  • Loading branch information
yegor256 authored May 3, 2024
2 parents 41bc933 + 03a5645 commit b621604
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
58 changes: 58 additions & 0 deletions metrics/rfvc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/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=$1
output=$(realpath "$2")

cd "$(dirname "${java}")"
base=$(basename "${java}")

# To check that file was added in commit any time
if git status > /dev/null 2>&1 && test -n "$(git log --oneline -- "${base}")"; then
my_rvc=$(git log --pretty=format:"%h" "${java}" | wc -l)
((my_rvc+=1))
files=$(git ls-tree -r "$(git branch --show-current)" --name-only)
all_rvcs=0

# source: https://stackoverflow.com/questions/44440506/split-string-with-literal-n-in-a-for-loop
while [[ $files ]]; do # iterate as long as we have input
if [[ $files = *$'\n'* ]]; then # if there's a '\n' sequence later...
first=${files%%$'\n'*} # put everything before it into 'first'
rest=${files#*$'\n'} # and put everything after it in 'rest'
else # if there's no '\n' later...
first=${files} # then put the whole rest of the string in 'first'
rest='' # and there is no 'rest'
fi
rvc=$(git log --pretty=format:"%h" "$first" | wc -l)
((rvc+=1))
((all_rvcs+=rvc))
files=$rest
done
rfvc=$(python3 -c "print(${my_rvc} / ${all_rvcs})")
else
rfvc=0
fi

echo "RFVC ${rfvc} Relative File Volatility by Commits for file" > "${output}"
111 changes: 111 additions & 0 deletions tests/metrics/test-rfvc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/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

{
metric_script_path="${LOCAL}/metrics/rfvc.sh"
cd "${temp}"

rm -rf ./*
rm -rf .git

git init --quiet .
git config user.email '[email protected]'
git config user.name 'Foo'
git config commit.gpgsign false

java_dir="./foo/dir/"
java1="FooTest.java"
java2="FooTest2.java"
java3="FooTest3.java"

mkdir -p "${java_dir}"
cd ${java_dir}

touch "${java1}"
touch "stdout"

printf "class Foo {}" > "${java1}"
git add "${java1}"
git commit --quiet -m "first commit"
${metric_script_path} "${java1}" "stdout"
grep "RFVC 1" "stdout"

printf "class Foo {}" > "${java2}"
git add "${java2}"
git commit --quiet -m "+second commit"

${metric_script_path} "${java1}" "stdout"
grep "RFVC 0.5" "stdout"

${metric_script_path} "${java2}" "stdout"
grep "RFVC 0.5" "stdout"


printf "class Foo {}" > "${java3}"
git add "${java3}"
git commit --quiet -m "-third commit"

${metric_script_path} "${java1}" "stdout"
grep "RFVC 0.33" "stdout"

${metric_script_path} "${java2}" "stdout"
grep "RFVC 0.33" "stdout"

${metric_script_path} "${java3}" "stdout"
grep "RFVC 0.33" "stdout"


printf "class Foo2 {}" > "${java1}"
git add "${java1}"
git commit --quiet -m "forth commit"

${metric_script_path} "${java1}" "stdout"
grep "RFVC 0.5" "stdout"

${metric_script_path} "${java2}" "stdout"
grep "RFVC 0.25" "stdout"

${metric_script_path} "${java3}" "stdout"
grep "RFVC 0.25" "stdout"


printf "class Foo3 {}" > "${java1}"
git add "${java1}"
git commit --quiet -m "fifth commit"

${metric_script_path} "${java1}" "stdout"
grep "RFVC 0.6" "stdout"


${metric_script_path} "${java2}" "stdout"
grep "RFVC 0.2" "stdout"

${metric_script_path} "${java3}" "stdout"
grep "RFVC 0.2" "stdout"
} > "${stdout}" 2>&1
echo "👍🏻 Correctly calculated Relative File Volatility by Commits"

0 comments on commit b621604

Please sign in to comment.