1

I cannot seem to get variables in my batch script to subtract properly. I have the following:

set /P head=<..\BUILD_NUMBER.dat

echo %head% > ..\HEAD.dat

set desired=%5

echo %desired% > ..\test.dat

set /a "version=%desired%-%head%"

echo %version% > ..\version.dat

git checkout @{%version%}
echo %desired% > ..\BUILD_NUMBER.dat 
echo %desired% > ..\build\BUILD_NUMBER.dat 

The contents of ..\HEAD.dat is correct, however, nothing else is.

  • ..\test.dat is never created (which I don't care too much about, since it was just a test).
  • ..\BUILD_NUMBER.dat and ..\build\BUILD_NUMBER.dat have the values they had before the echoes
  • ..\version.dat contains what %desired% contains.
  • The checked out version of the git repo is the current HEAD, not the desired revision

Basically, what I'm trying to do is checkout a specific revision number from git, using rev-list --count to get the HEAD value, and subtract that from the desired version to get the negative offset for the git checkout command.

jthill
  • 55,082
  • 5
  • 77
  • 137
Chris M
  • 65
  • 1
  • 7
  • `%5` refers to the fifth argument to the batch file. Is that intended? – Stephan Aug 11 '17 at 11:51
  • Yes, the 5th argument is the revision number that I want to build, for example: 3826 – Chris M Aug 11 '17 at 11:59
  • I would like to see a session log of that code (with `echo on`) to see exactly, what happens. Can you edit that into the question, please? (just run it and copy/paste the whole output) – Stephan Aug 11 '17 at 12:02
  • 1
    I believe you've over-cropped your code fragment. Your complaint has all of the characteristics of this code being within a code-block (parenthesised series of lines) where you'd need to use `delayed expansion` (many SO articles - use `search`) to access the altered values. – Magoo Aug 11 '17 at 12:08
  • @Stephan, I'm not quite sure how to do that, since its not run from the console. It's actually a .bat file called from another .bat file, called from a php script, which is called from an ajax call on another php page, so echo on doesn't display anything. – Chris M Aug 11 '17 at 12:46
  • @Magoo, I've tried using delayed expansion, but when I did that, all I got for output was '!version!' as opposed to the contents I did use SETLOCAL ENABLEDELAYEDEXPANSION, but it didn't make a difference. That portion of the code is not within a block, but it is inside a batch file which is called from another batch file, but I don't think that counts as a block. – Chris M Aug 11 '17 at 12:50
  • @ChrisM Have you tried putting quotes around the path and file names? Some of the implied paths (``..\``) could be containing carets (`^`). – 303 Aug 12 '17 at 00:09
  • Yes, that made no difference, unfortunately. – Chris M Aug 14 '17 at 11:52

1 Answers1

0

When it comes to git commands, you could write your script in bash, and run it from any CMD Windows, with bash -c ./yourScript.sh

Make sure to write your shell script in unix mode (end of line in lf, not crlf).

You will find the find of indirection you try in a bat script are much easier in a bash script:

#!/bin/bash

head=$(cat ../BUILD_NUMBER.dat)
...
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250