0

I have this bash script that I want to duplicate a branch and make a PR to a remote repo using that duplicate branch then delete the duplicate branch:

gPullRequest () {
  branch=$(git rev-parse --abbrev-ref HEAD)
  if [ $# -eq 0 ]
    then
      git checkout -b development-$branch
  elif [ $# -eq 1 ]
    then
      git checkout -b release-$branch
  fi
  gp
  prBranch=$(git rev-parse --abbrev-ref HEAD)
  if [ $# -eq 0 ]
    then
      hub pull-request -h org:$prBranch -b org:development
  elif [ $# -eq 1 ]
    then
      hub pull-request -h org:$prBranch -b org:$1
  fi
  git checkout $branch
  git branch -D $prBranch
}

The problem is the variable branch gets re evaluated to what prBranch is pointing to by the time

git checkout $branch

When this code runs, the branch variable is the new branches name instead of the first value from the first line of code.

Any thoughts on how to preserve the value of branch for later execution of the bash script?

edit

gp () {
  branch=$(git rev-parse --abbrev-ref HEAD)
  git push origin $branch
}

this was previously not present in the original prompt, but is the reason for the bug.

Andrew Kim
  • 3,145
  • 4
  • 22
  • 42
  • Save its contents in another variable? – Cyrus Aug 16 '18 at 20:09
  • 2
    Nothing the that code (except maybe the mysterious `gp` command) will change the value of `branch` after it is set in the first line. – chepner Aug 16 '18 at 20:20
  • It's not clear why you need to duplicate the branch in the first place. – chepner Aug 16 '18 at 20:24
  • BTW, if you don't want to overwrite the *global* variables `branch` and `prBranch`, you should put the line `local branch prBranch` at the top of the body of your function. Probably failure to do this in the `gp` function is your bug, but obviously, the question doesn't include sufficient details to allow a certain answer -- please try to follow [mcve] guidelines in the future. – Charles Duffy Aug 16 '18 at 20:43
  • And fix your quoting -- running your code through http://shellcheck.net/ will point out all the places it's broken. – Charles Duffy Aug 16 '18 at 20:43
  • I would also suggest `-b "org:${1:-development}"`, making your second `if` statement unnecessary by providing `development` as a default value to use when `$1` is unset or empty. – Charles Duffy Aug 16 '18 at 20:46
  • yes, all of you are correct, sorry i didn't define gp function, but gp is definitely mutating it, ty! – Andrew Kim Aug 17 '18 at 13:11

1 Answers1

1

This quite certainly means your bp function starts like:

bp() {
  branch=$1
  othervar=$(whatever)
  # ...do stuff here...
}

You can't do that safely, because -- like Javascript -- variables are global by default in bash. Thus, setting branch inside a function you call changes the value in the parent as well.

Always declare your locals, like so:

bp() {
  local branch othervar
  branch=$1
  othervar=$(whatever)
  # ...do stuff here...
}

(Why two separate lines, rather than local branch=$1? Because when you want to move on to local foo=$(bar), the local command eats the exit status of bar, making its success or failure impossible to determine later; maintaining a habit of keeping your local declarations to a separate line avoids the issue).

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441