MSBuild: Compare ItemGroups or access by index
<p>For a C++ project, I want to autogenerate a <code>defs.h</code> file with project definitions, such as the date, git commit, ... to automate the versioning process of my application.</p>

<p>Therefore I am trying to create a MSBuild Target that will extract the latest git tag, git commit, and the current date and save it to a temporary <code>gitinfo.txt</code> file.
Another build target will depend on that file and generate a .h file.
In order to avoid unnecessary recompiles of my project, the .h file and for that reason the <code>gitinfo.txt</code> file shall only be rewritten, if any of the information has changes.</p>

<p>So my idea is the following:</p>

<ol>
<li>Calculate git and date info</li>
<li>If available, read in the existing <code>gitinfo.txt</code></li>
<li>Compare the calculated values to those in the txt file</li>
<li>If anything has changed, rewrite the <code>gitinfo.txt</code></li>
</ol>

<p>I've mastered steps 1. and 2., however I am not sure how to process the values after reading them.</p>

<pre><code>&lt;!-- The purpose of this target is to update gitinfo.txt if git information (commit...) has changed --&gt;
&lt;Target
  Name="GetHeaderInfos"
  BeforeTargets="ClCompile"
  Outputs="$(IntDir)\gitinfo.txt"
&gt;

  &lt;!-- Get information about the state of this repo--&gt;    
  &lt;GitDescribe&gt;
    &lt;Output TaskParameter="Tag" PropertyName="NewGitTag" /&gt;
    &lt;Output TaskParameter="CommitHash" PropertyName="NewGitCommitHash" /&gt;
    &lt;Output TaskParameter="CommitCount" PropertyName="NewGitCommitCount" /&gt;
  &lt;/GitDescribe&gt;

  &lt;!-- Get the current date --&gt;
  &lt;Time Format="dd.MM.yyyy"&gt;
    &lt;Output TaskParameter="FormattedTime" PropertyName="NewBuildDate" /&gt;
  &lt;/Time&gt;

  &lt;ReadLinesFromFile File="$(IntDir)\gitinfo.txt" Condition="Exists('$(IntDir)\gitinfo.txt')"&gt;
    &lt;Output TaskParameter="Lines" ItemName="Version" /&gt;
  &lt;/ReadLinesFromFile&gt; 

  &lt;!-- Comparison here! HOW TO DO IT PROPERLY --&gt;
  &lt;PropertyGroup&gt;
     &lt;TagChanged&gt; &lt;!-- `$(NewGitTag)` == `$(Version)[0]` --&gt; &lt;/TagChanged&gt;
     &lt;!-- Other comparisons --&gt;
  &lt;/PropertyGroup&gt;
&lt;/Target&gt;
</code></pre>

<p>And this could be the content of <code>gitinfo.txt</code></p>

<pre><code>v4.1.4
04fe34ab
1
31.07.2016
</code></pre>

<p>I am not quite sure how to compare the values now. I need to compare <code>$(NewGitTag)</code> to the first value in the <code>$(Version)</code> version variable, and so on.</p>

<p>I haven't found an example, that actually accesses the variables after reading them from a file. The <a href="https://msdn.microsoft.com/de-de/library/ms164299.aspx" rel="nofollow">official documentation</a> provides no help, nor have I found anything on stackoverflow or the likes.</p>

<p>I only know that the <code>$(Version)</code> variable holds a list, and I can batch process it. How can I compare its content to the defined variables <code>$(NewGitTag)</code>, <code>$(NewGitCommitHash)</code>, <code>$(NewGitCommitCount)</code> and <code>$(NewBuildDate)</code>?</p>