Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[metadata]
name = custom_hooks
version = 1.2.1
description = Some random custom hooks.
version = 1.3.0
description = Just some hooks that help save some time ...and nerves.
long_description = file: README.md
long_description_content_type = text/markdown
url = https://github.com/OriginalUtkin/custom-hooks
Expand Down
46 changes: 32 additions & 14 deletions update_version.sh
Original file line number Diff line number Diff line change
@@ -1,31 +1,49 @@
#!/usr/bin/env bash

if [[ ("$2" == "poetry") || (("$2" == "") && ( -f pyproject.toml)) ]]; then
function is_output_valid() {
line_numbers=$(eval "echo $1 | wc -l")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably no need to use eval here

is_version_correct=$(eval "echo $1 | grep -E '[0-9.]?'")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that 0-9.]? is enough.
As I said, if git something will fail, the version can contain some error message which is easily matched by this 0-9.]? regexp. I suggest ^([0-9]+\.)+[0-9]+$ which is strict and covers all our existing versions.
In case it will not cover some versions, we can update it later according the needs.


if [[ ($line_numbers -eq 1) && (! -z $is_version_correct)]]; then
return 0
else
return 1
fi

}

if [[ ("$1" == "poetry") ]]; then
version_file="pyproject.toml"
command="poetry version -s"
elif [[ ("$2" == "version_file") || (("$2" == "") && ( -f VERSION)) ]]; then
command="sed 's/[\ \"]*//g' | grep -E '(^\[tool\.poetry\])|(^version)' | sed -E 's/\[tool.poetry\]//g' | sed -E 's/version[[:blank:]]?=[[:blank:]]?//g'"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please consider using functions instead of string command:

function cmd1() { echo "cmd1" }
function cmd2() { echo "cmd2" }

alias chosen_command='cmd1'
chosen_command  # calls cmd1

``

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also I think it is possible to pipe output of previous command as the input of the function, please check https://stackoverflow.com/a/11454477/1768976


elif [[ ("$1" == "version_file") ]]; then
version_file="VERSION"
command="cat VERSION"
command="grep -E '[0-9.]+'"

else
echo "Version type '$2' not supported"
echo "Version type '$1' not supported"
exit 1
fi

tfile=$(mktemp $version_file.XXXXXXXXX)
mv $version_file $tfile

version_in_branch=$(eval "cat $version_file | $command")

git fetch -q origin master
git checkout master -q -- $version_file
version_in_master=$($command)
mv $tfile $version_file
version_in_branch=$($command)

version_in_master=$(eval "git show origin/master:$version_file | $command")

if (! is_output_valid $version_in_branch) || (! is_output_valid $version_in_master); then
echo "Version update is not correct"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it may be beneficial to provide what is the version - in case of error it can be something else than what is in master/current branch.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree, can be done by moving echo commands ("Version in branch", "Version in master") before that if

exit 1
fi

echo "Version in branch:" $version_in_branch
echo "Version in master:" $version_in_master

changed_files=$(git diff origin/master --summary --name-only | grep -E $1)
changed_files=$(git diff origin/master --summary --name-only | grep -E $2)

if [[ ("$version_in_branch" == "$version_in_master") && (! -z $changed_files)]]
then
echo "VERSION file is not updated"
echo "Version in $version_file file is not updated"
exit 1
fi
fi