Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[build] When generating image version, handle case where current commit has no reachable tags #2506

Merged
merged 1 commit into from
Jan 31, 2019
Merged
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
2 changes: 1 addition & 1 deletion functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ sonic_get_version() {
BUILD_NUMBER=${BUILD_NUMBER:-0}
## Check if we are on tagged commit
## Note: escape the version string by sed: / -> _
if [ "$describe" == "$latest_tag" ]; then
if [ -n "$latest_tag" ] && [ "$describe" == "$latest_tag" ]; then
Copy link
Collaborator

Choose a reason for hiding this comment

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

usually, to avoid empty string, we can do x"$describe" == x"$latest_tag"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The convention you describe doesn't fix this issue. Prepending an "x" to the string only will prevent a bash error if either of the strings are empty (and unquoted). For example: if [ x$VAR1 == x$VAR2 ]. In this case, if VAR1 is empty and VAR2 contains "somestring", the statement would expand to if [ x == somestring ], whereas without the x's, it would expand to if [ == somestring ], which is invalid syntax and cause the script to crash. With quotes (if [ x"$VAR1" == x"$VAR2" ], this would evaluate to if [ x == xsomestring ], thus making a valid statement which would correctly return false. There is no need to prepend characters if the variables in the conditional are quoted.

In the case presented here, however, the strings are already quoted, so we don't have to worry about a crash. In fact, the issue here stems from both strings being empty. However, adding x's here doesn't solve the issue, because both latest_tag and describe are empty strings, so this comparison returns true, and will continue to return true if we prepend x's, so the result will be the same: since latest_tag is empty, sonic_get_version() would still return an empty string.

echo "${latest_tag}${dirty}" | sed 's/\//_/g'
else
echo "${branch_name}.${BUILD_NUMBER}${dirty:--$(git rev-parse --short HEAD)}" | sed 's/\//_/g'
Expand Down