In the case of the loop for
you will not be able to catch the next element from the list but it is possible to use the loop while
and the command shift
to do so:
show_commits(){
first_tag=${1}
while true; do
shift
next_tag=${1}
if [[ $next_tag == '' ]]; then
break
else
git log "${first_tag}".."${next_tag}" --pretty=%H
echo
first_tag=$next_tag
fi
done
}
show_commits $( git tag | tac )
The list of tags is generated directly by Git and the command tac
serves to reverse her order) and all of it is sent to the function as the arguments $1, $2, $3 etc. But you can do something direct like "show_commits v1.2.3 v1.2.2 v1.0.0".
However, despite receiving all of them, the function will use only one argument, see that only one argument -- ${1}
-- is used to assign the value to variables first_tag and next_tag.
Therein comes the shift
that takes care of shifting the arguments to the left, so $1 turns $0, $2 turns $1 etc and $0 is always discarded in the process... this is done until the variable next_tag receives an empty value, which means there are no more values and the loop is broken.
I don’t know much about bash, but following the logic of programming, wouldn’t you be able to get the index of that iteration? then you would do something like $tags[i] .. $tags[++i]. However, you probably would have to make an auxiliary index to not modify the current value.
– Leonardo Theodoro