5
votes

Is it possible to write a Windows .bat script that executes "git clone" in the middle of the script, so that the script continues? It is always exiting for me.

I am using a 64-bit Git version 2.8.3 from "The Git Development Community" on Windows 7 Enterprise SP1. git.exe is in my path (c:\program files\git\cmd\git.exe) - it is not indirectly executing via a git.cmd or similar file.

I am able to run a sequence of commands like

git init
git add --all
git commit -m "commit message"

and with these commands, the batch script executes each command continues normally. With

git clone {url}

the script exits immediately, usually with a doubled prompt, suggesting some sort of abnormal exit (even though %ERRORLEVEL% is 0):

git clone {url} new
Cloning into 'new'...
remote: Counting objects: 26, done
remote: Compressing objects: 100% (23/23) done.
remote: Total 26 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (26/26) done.
Checking connectivity... done.
c:\jdev\ppss\finalprep\ant2>c:\jdev\ppss\finalprep>

Common Windows tricks like using "call git clone ..." and "cmd /c git clone ..." do not seem to work. Even "start /wait cmd /c git clone ..." doesn't do it.

Any ideas?

2
I just tried in a simple bat file: echo 1 && git clone {url} {afolder} && echo 2: it does display 2 (Windows 7, git 2.10) - VonC
Check if the issue persists with Git 2.10 (github.com/git-for-windows/git/releases) - VonC
Weird. A simple 2 line script in c:\tmp\xxx (test.bat: git clone ... @echo continuing... ) does not reproduce the problem. - John Elion
I agree. Just for testing, does Git 2.10 improves the situation? If not, there must be something else in your original script. - VonC
Hadn't seen your comment! Just downloaded 2.10, it does not fix the problem. Trying to dive into the script 'environment' - i.e., I'm inside setlocal, for /F ... call, and a 2nd setlocal in the script subroutine. But my tmp folder script isn't reproducing even with all that... Thanks for your suggestions. - John Elion

2 Answers

3
votes

The line following the "git clone" was an IF statement:

git clone {url} subfolder
if "%var%" == "y" (
  @echo some commands went here
) else (
  @echo different commands went here
}

The above IF statement is not a legal DOS if statement (bad ELSE close), but you don't get any error messages from DOS - your script just exits. It looks like the previous command is causing the abort, which in my case happened to be git clone.

This has nothing to do with git whatsoever, strictly cmd.exe nonsense.

1
votes

In the specific case that was failing, the subdirectory into which "git clone" was writing already existed due to a prior "mkdir". Without the mkdir - without the subdir already existing - the batch script continues after git clone normally, and there was no need for any special tricks like cmd /c