[效率提升]自动合并feature分支到develop的sh脚本

127 阅读1分钟
#!/bin/bash

# 进入项目目录
# project_path="/xxxx/"
# cd "${project_path}"

cd ..

# 判断当前分支
current_branch=$(git branch --show-current)

# 判断目标分支是否存在
target_branch=$(git branch --list develop)

echo "current branch: ${current_branch}"
echo "target branch: ${target_branch}"

# 去掉“feature-”后剩下的内容
commit_message=${current_branch#feature-}
echo "commit message: $commit_message"

git add .
git commit -m "$commit_message"
git push origin "$current_branch"

# 判断develop分支是否存在
if [ -z "$target_branch" ]; then
  echo "develop本地分支不存在"
  git checkout -b develop origin/develop
else
  git checkout develop
  git pull origin develop:develop
fi

# 合并分支
git merge --no-edit "$current_branch"

# 判断拉取更新后是否有冲突
conflict_num=$(git status --porcelain | grep '^UU' | wc -l)
echo "Conflict number: ${conflict_num}"
if [ "$conflict_num" -gt 0 ]; then
  # 把当前文件区还原到develop最新的一次commit
  git reset --hard HEAD
  echo -e "\033[41m There are conflicts, please solve them! \033[0m"
else
  echo -e "\033[42mMerge successfully!\033[0m"

  # 输入y确认push,此时可在sourcetree等工具查看合并状况,确认后再输入y执行push
  read -p "push it immediately?(y/n):" input
  input=$(echo "$input" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
  if [[ $input == "y" ]]; then

      # push上去 最后还是自己来操作比较稳妥
      git push origin develop

      echo -e "\033[42mPush OK!\033[0m"

      # 切换回原来的分支继续搬砖
      git checkout "$current_branch"
  else
      echo -e "\033[41mCancel!\033[0m"
      echo -e "\033[41mStay in develop!\033[0m"
      exit 1
  fi
fi