1

this grabs the output from a remote branch list with git::

for /f "delims=\" %r in ('git branch -r') do (
::then in here I want to get rid of the origin/HEAD -> line of the output
::and do a few git ops on the other lines, which are the names of branches
)

anyhow, I'm finding this frustrating as apparently batch doesn't have regex

here's the code I'm using to do this in bash

for remote in `git branch -r | grep -v '\->'`;
do echo $remote; #...git stuff
done;

the grep removes the "origin/HEAD -> origin/master" line of the output of git branch -r

So I'm hoping to ask how to implement the 'contains' verb

for /f "delims=\" %r in ('git branch -r') do (
if not %r contains HEAD (
::...git stuff
)
)

on a loop variable

this stackoverflow question appears to answer a similar question, although in my attempts to implement as such, I became confused by % symbols and no permutation of them yielded function

EDIT FOR FUTURE READERS: there is some regex with findstr /r piped onto git branch -r

Community
  • 1
  • 1

2 Answers2

2
for /f "delims=\" %%r in ('git branch -r^|findstr "HEAD"') do (
 echo ...git stuff %%r
)

should give you a start.

Note: %%r, not %r within a batch file - %r would work directly from the prompt.

Your delims=\ filter will produce that portion up to the first \ of any line from git branch -r which contains HEAD - sorry, I don't talk bash-ish; you'd need to say precisely what the HEAD string you want to locate is.

Use "delims=" fo the entire line - omitting the delims option will set delimiters to the default set (space, comma, semicolon, etc.)

  • Don't use ::-comments within a block (parenthesised statement-sequence) as it's actually a broken label and cmd doesn't appeciate labels within a block. Use REM comments here instead.

The resultant strings output from the findstr (which acts on a brain-dead verion of regex) will be processed through to the echo (or whatever statement you may substitute here) - if there are none, the for will appear to be skipped.

Quite what your target string would be for findstr I can't tell. From the prompt, findstr /? may reveal. You may also be able to use find (find /?) - but if you are using cygwin the *nix version of find overrides windows-native.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • This was very useful - I didn't know how to pipe in batch, or about findstr anyhow, I found the /r regex option on findstr and was able to make a regex which suited my needs – downtowncanada Nov 16 '14 at 21:25
1

I don't know what the git branch output looks like, but with a test case of

test 1
HEAD test \-> 2
test 3

test 4

the following prints all the text lines except the one containing \->

@setlocal enableextensions enabledelayedexpansion
@echo off

for /f "tokens=*" %%r in (d:\test2.txt) do (
    set str1=%%r
   if "!str1:\->=!"=="!str1!" (
       echo %%r
   )
)

The if test is fundamentally doing this test: string1.replace("HEAD", "") == string1.

Your loop variable needs to be %r if used directly in the command prompt, but %%r if in a batch file.

The string replacement is a part of environment variables, not loop variables, so it needs to be put into a holding string (str1) to work with. If you have the command extensions enabled ( enableextensions ).

And because environment variable setting operations happen when the script is read, you need to override that with enabledelayedexpansion and using !str1! instead of %str1%, otherwise the value of str1 won't change from one loop to the next.

(PS. Use PowerShell instead. Get-Content D:\test2.txt | Select-String "\->" -NotMatch ).

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87