Reputation: 47
For context, I am building a Jenkins job to automatically git tag our branch code. The idea is to construct a branch tag naming standard as follows:
<latest_tag_in_branch>.<branch_name>.<revision>
Let's assume a branch name of 'hotFix'. If the latest tag doesn't include the branch name in it (e.g. 2.0.55
), then we should append this with a revision of 1. E.g.
2.0.55.hotFix.1
If another tag was created on this hotFix branch without merging in any other code (and therefore the latest common tag doesn't change), then this becomes:
2.0.55.hotFix.2
If we then merge with master, which has subsequent tags created (e.g. let's say with 2.0.58 being the latest), then the next build of this branch would 'reset' to:
2.0.58.hotFix.1
The command:
git describe --abbrev=0 --tags
will get me the latest tag. It would be, for example, either 2.0.55
or 2.0.55.hotFix.1
. How can I proceed with this information in a bash shell environment to complete the rest of the tag name? E.g. either append <branch_name>.1
or increment the <revision>
by 1.
My code so far:
> git describe --abbrev=0 --tags
(returns either e.g., 2.0.55, or 2.0.55.hotFix.3)
Being relatively inexperienced in bash, I'm unsure of the best approach on what to do next.
Upvotes: 1
Views: 460
Reputation: 9007
You can pipe the output of your git
command into another process which can manipulate the text (I'll prefer awk
here).
git describe --abbrev=0 --tags | awk -F. -v OFS=. '{
if (NF == 3) $0 = $0".hotFix.1"
else if (NF >= 5) $5 += 1
} 1'
What this does is that it will redirect the output from your git
command into awk
(as input).
We set the input and output delimiter to .
by respectively doing -F.
and -v OFS=.
.
awk
will run the script we provided in braces.
Our script will check if the input has three arguments (in the case of 2.0.55
). NF
is a pre-defined variable that stores the number of fields in the input. $0 = $0".hotFix.1"
will concatenate .hotFix.1
onto the entire input (that's $0
).
If the input doesn't have three arguments, we check that it has at least 5 arguments (NF >= 5
). Then increment the fifth field by 1 ($5 += 1
).
Finally, once the script has finished running, we print the statement by attaching a 1
after the closing brace. Note that if the input only had one or two fields, it would print the input unmodified.
Note that, you could also write your awk
on a single line. I chose to write it on several lines since it's a tad more readable.
If you wish to use regex to check that the fifth field is an integer (before applying the += 1
, you could modify the else if
to
else if (NF >= 5 && $5 ~ /^[0-9]*$/) $5 += 1
This will check that the number of fields (NF
) is greater than 5 and check that the fifth field ($5
) matches (~
) a regex pattern (/.../
) with a number ([0-9]*
) from beginning (^
) to end ($
).
Upvotes: 1
Reputation: 98098
branch_name="hotFix"
example1="2.0.55.hotFix.1"
example2="2.0.55"
regex_short="([0-9]+[.][0-9]+[.][0-9]+)"
regex_long="($regex_short[.]$branch_name)[.]([0-9]*)"
for example in $example1 $example2; do
if [[ $example =~ $regex_long ]]; then
prefix=${BASH_REMATCH[2]}
version=${BASH_REMATCH[3]}
let ++version
elif [[ $example =~ $regex_short ]]; then
prefix=${BASH_REMATCH[1]}
version=1
fi
nvers="$prefix.$branch_name.$version"
echo "old_version: $example -> new_version: $nvers"
done
Upvotes: 1