gitbatch-filescripting

Why does my git command output blank to a text file when I try to use variables as inputs instead of hardcoded values?


I am in the process of writing a windows batch file to simplify the generation of some documentation, I am relatively new to this. Part of this involves interrogating my git repository to find the two most recent release tags. Once I have these tags the intention is then to use the git log command to and write the output to a text file in a different directory.

I am able to write the output of a git log to a text file if I write the git log command with the tag names as constants, however when I use the variables that have the tag names stored I can only get the output visible in the cmd window, and the text files produced are blank.

Running with known tags as below will output both in the command window and to a text file

git log --format="%%s" OLD_RELEASE..NEW_RELEASE > "C:\Documentation\Commits.txt"

The output in the command window and the output text file shows as below and aligns with the comments in git (poor comments I know but I have tried to keep this information deliberately vague)

V3.1.2.0 19 Sept
V3.1.1.2 6 August
V3.1.1.1 16 July 
V3.1.1.1 15 July 
V3.1.2.0 14 July 
535 indicator added to setup screen
fault 4 severity level reduced from non-threatening

However if I have first searched the tags to determine the two tag names and set them as the variable "OldTag" and "NewTag" and then use the same command however formatted as below, I see the correct identical command in the command window and the list of commit messages but the output text file is blank.

git log --format="%%s" %OldTag%..%NewTag% > "C:\Documentation\Commits.txt" 

I have found that running a hybrid command where the second variable is used as a hardcoded value, appears to work with the output being written to the text file. So it appears as though the second variable usage is preventing the output command from running

git log --format="%%s" %OldTag%..NEW_RELEASE > "C:\Documentation\Commits.txt"

Solution

  • I have solved this if you place the git command into brackets and then output to a text file this will work. However as per Comments below there is no reason this should work unless the tag ends in a digit (in my case it does).

    (
    git log --format="%%s" %TagOld%..%TagNew%
    )> "C:\Documentation\Commits.txt"
    

    Another solution that does work is top place the redirection at the beginning of the line eg

    > "C:\Documentation\Commits.txt" git log --format="%%s" %TagOld%..%TagNew%
    

    Finally comments above indicate that there is an output command that can be used with git log. IF you wan to achieve the same result the following format also works

    git log --output="C:\Documentation\Commits.txt" --format="%%s" %TagOld%..%TagNew%