Next loop element

0

I'm trying to implement a commit search algorithm between two tags with the git log tag1...tag2 command. For this I used the command git tag which returns the tags of a repository.

With this result, I thought about iterating over it by running the command git

for tag in $Tags
do
    git log $tag .. $proximaTag
done

My question is: How do I get the value of the next tag? In this case, how do I get the value of the next item in the array?

Is there any better way to implement this kind of algorithm?

    
asked by anonymous 23.11.2018 / 18:47

1 answer

2

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.

    
24.11.2018 / 00:07