3

寻求解决方案以实现以下目标:

  • 如果当前未在本地创建上创建分支
  • 如果它已经存在提示用户并移动到下一条语句

到目前为止,我已经让它工作了,但并不完全在那里。我的问题确实是后者,但我想花点时间重新思考整个事情,并获得一些关于如何更合理地写这个的反馈。

当分支存在时,该变量existing_branch提供SHArefs/heads/branchName,否则 git 会占据并提供预期的fatal:

check_for_branch() {
 args=("$@")
 echo `$branch${args[0]}`
 existing_branch=$?
}

create_branch() {
  current="git rev-parse --abbrev-ref HEAD"
  branch="git show-ref --verify refs/heads/"

  args=("$@")
  branch_present=$(check_for_branch ${args[0]})
  echo $branch_present
  read -p "Do you really want to create branch $1 " ans
  case $ans in
    y | Y | yes | YES | Yes)
        if [  ! -z branch_present ]; then
          echo  "Branch already exists"
        else
          `git branch ${args[0]}`
          echo  "Created ${args[0]} branch"
        fi
    ;;
     n | N | no | NO | No)
      echo "exiting"
    ;;
    *)
    echo "Enter something I can work with y or n."
    ;;
    esac
}
4

1 回答 1

6

您可以避免提示分支是否已存在,并稍微缩短脚本,如下所示:

create_branch() {
  branch="${1:?Provide a branch name}"

  if git show-ref --verify --quiet "refs/heads/$branch"; then
    echo >&2 "Branch '$branch' already exists."
  else
    read -p "Do you really want to create branch $1 " ans
    ...
  fi
}
于 2013-01-19T11:59:28.120 回答