1

Here is my post-merge hook script. Trying to do some cleanup/setup on post merge based on the currentBranch. Not getting the output i expect

# !/bin/bash 

git branch --contains $2
curbranch1=$(git branch --contains $2)
echo  $curbranch1
curbranch2=$(echo "$curbranch1"|grep "*")
echo  $curbranch2


#output

Switched to branch 'develop'
* develop
NET SRC-Fiery-PF-PS VB6Dev bin develop
NET SRC-Fiery-PF-PS VB6Dev bin develop
user3924036
  • 273
  • 2
  • 8

1 Answers1

2

Try to assign a value to a variable without space around the equal sign:

currentBranch=$(git branch --contains $2)

NET SRC-Fiery-PF-PS VB6Dev bin develop <--Should match previous line?

It means the newline is not kept by default.
Try it in two steps:

currentBranch=$(git branch --contains $2)
currentBranch=$(echo "$currentBranch"|grep "*")

See also "How can I have a newline in a string in sh?"

Even in one step, it works:

#!/bin/sh

n=$(git branch --contains @|grep "*")
echo "$n"
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks. that error went away. #!/bin/bash git branch --contains $2 | grep "*" curbranch=$(git branch --contains $2 | grep "*") echo $curbranch #output Switched to branch 'develop' * develop NET SRC-Fiery-PF-PS VB6Dev bin develop Look at the 3rd line. That should exactly match what i see in the 2nd line. #develop – user3924036 Aug 26 '15 at 07:35
  • @user3924036 I agree, it should work, provided you don't add space around the equal when assigning `curbranch`. Edit your question with your new script and issue: that will be easier to read. – VonC Aug 26 '15 at 07:36
  • @user3924036 I have edited my answer to address your entirely new question. – VonC Aug 26 '15 at 08:30
  • Sorry. no change..Please see my latest script and output. – user3924036 Aug 26 '15 at 10:02
  • @user3924036 see my edited answer. Try it with `#!/bin/sh` – VonC Aug 26 '15 at 10:27