In the case of the for
loop you will not be able to get the next element in the list but you can use the while
loop and the shift
loop to do it:
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 tags list is generated directly by Git and the command tac
is used to invert the order of it) and the entire list is sent to the function as arguments $ 1, $ 2, $ 3 etc. But you can do something straight like "show_commits v1.2.3 v1.2.2 v1.0.0".
However, while receiving all of them, the function will only use one argument, see that only one argument - ${1}
- is used to assign the value to the first_tag and next_tag .
Here is the shift
that moves the arguments to the left, thus $ 1 becomes $ 0, $ 2 becomes $ 1 etc and $ 0 is always discarded in the process ... this is done until the variable next_tag receives an empty value, which means that there are no more values and the loop is interrupted.