-1

This may be a very basic question, although all responses I could find for it were to "install git" or "be in a git directory." As far as I can see, both of these are resolved already, yet my issue persists. Although the solution could be trivially solved by doing away with having a default behaviour with no input, I'm curious as to what obscure (or basic) behaviour is causing this.

The question [Git via Batch]

Lets say I have a git repo at "C:\RepoBase", amongst possible others, but this is the main repo I want to default to. Without discussing further functionality, the script will either CD to a location provided as input or to the default path if no input is provided, and then proceed to get the name of the current head commit.

:: Go to the repo in question
if [%1]==[] (
    cd C:\RepoBase
) else (
    cd %1
)
:: Get name of current branch
FOR /F "tokens=*" %%g IN (
    'call git rev-parse --abbrev-ref HEAD'
) do (
    SET CurrentBranch=%%g
)

This solution works. If however, I want to have the inconsequential benefit of using the name of the path that I cd'd into later without needing to use a conditional again, even although it would be possible to get the name of the directory after moving into it, I try to set a variable to use as the path. It does CD into the directory fine, but from the same location as the above partial, the below partial's git calls result in a "not recognized." Why?

:: Go to the repo in question
SET PATH=%1
if [%1]==[] (
    SET PATH=C:\RepoBase
) 
cd "%PATH%"
:: Get name of current branch
FOR /F "tokens=*" %%g IN (
    'call git rev-parse --abbrev-ref HEAD'
) do (
    SET CurrentBranch=%%g
)
Skenvy
  • 724
  • 1
  • 4
  • 15
  • I would guess that you never actually did a `cd` into the Git directory in the second case. You should check your bash script to make sure the `cd` logic is correct. – Tim Biegeleisen Aug 30 '18 at 02:01
  • the *batch* cd works fine to put me in the intended directory, with or without arguments operating fine. this was specifically the answers I'd found elsewhere that didn't apply :( – Skenvy Aug 30 '18 at 06:46

1 Answers1

3

%PATH% is a system variable that stores directories where Windows looks for executables so that you don't always have to call the full path to everything. You wrote over it, so things that you would normally not need to provide the full path to now require a full path.

Change your variable name to something else, like REPO_PATH.

SomethingDark
  • 13,229
  • 5
  • 50
  • 55
  • I'm curious why this stopped Git from working, even after the CD into the location worked, unless Git relies on the context of %PATH% and it can be implicitly set in a local context? – Skenvy Aug 30 '18 at 06:45
  • `cd %PATH%` will still work because `%PATH%` has a valid value; it's just the functionality that's been hindered. Ordinarily, Windows will first look for an executable that gets called in the local directory and then go one by one through the folders listed in `%PATH%`. – SomethingDark Aug 30 '18 at 10:44