From 778c7bb1915801442da83dc510d091c9877e8d5d Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Mon, 14 Oct 2019 20:08:54 +0100 Subject: [PATCH] fix(git): tighten up scripts to avoid inconsistencies * E.g. Minimise issues with new branches not being created from the latest commit in the `upstream` branch, even after pulling * It's also a likelihood that issues are triggered by branch names coming through in the YAML; I've needed quoting in the past when numbers are involved -- probably best to tighten up strings with `yamllint` --- ssf/files/default/git/git_10_prepare.sh | 17 +++++++++-------- ssf/files/default/git/git_20_commit_push.sh | 2 +- ssf/files/default/git/git_30_create_PR.sh | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/ssf/files/default/git/git_10_prepare.sh b/ssf/files/default/git/git_10_prepare.sh index 859583d1..86a0fb0d 100755 --- a/ssf/files/default/git/git_10_prepare.sh +++ b/ssf/files/default/git/git_10_prepare.sh @@ -14,33 +14,34 @@ COMMENT='Command `'${STATE}'` run' # Check if PR branch already exists BRANCH=$(git branch -l "${BRANCH_PR}") COMMIT= -if [ "${BRANCH}" ]; then +if [ ! -z "${BRANCH}" ]; then git checkout ${BRANCH_PR} # This may end up as blank as well, so that's why a separate `if` is required below COMMIT=$(git log -n1 | grep "${COMMIT_GREP}") fi # Perform actions depending on if a commit was found or not -if [ "${COMMIT}" ]; then +if [ ! -z "${COMMIT}" ]; then CHANGED=False else git checkout ${BRANCH_UPSTREAM} git pull + git status # If the branch existed but not the commit, assume the branch is stale (i.e. previous PR merged) # Remove it, ready to be recreated at the latest upstream commit - if [ "${BRANCH}" ]; then + if [ ! -z "${BRANCH}" ]; then git branch -d ${BRANCH_PR} fi # TODO: Improve this part, should be able to remove with the duplication with the right solution # Don't want to resort to using `git branch -D` above, since that could be premature in certain situations # Branch may _still_ exist since it might not be merged upstream BRANCH=$(git branch -l "${BRANCH_PR}") - if [ "${BRANCH}" ]; then - git checkout ${BRANCH_PR} - git merge ${BRANCH_UPSTREAM} - else - git checkout -b ${BRANCH_PR} + NEW_BRANCH='' + if [ -z "${BRANCH}" ]; then + NEW_BRANCH='-b' fi + git checkout ${NEW_BRANCH} ${BRANCH_PR} + git merge ${BRANCH_UPSTREAM} fi # Write the state line diff --git a/ssf/files/default/git/git_20_commit_push.sh b/ssf/files/default/git/git_20_commit_push.sh index a6e5bef7..9659103e 100755 --- a/ssf/files/default/git/git_20_commit_push.sh +++ b/ssf/files/default/git/git_20_commit_push.sh @@ -22,7 +22,7 @@ COMMENT='Command `'${STATE}'` run' # Prepare git options depending on if a commit was found or not COMMIT=$(git log -n1 | grep "${COMMIT_GREP}") -if [ "${COMMIT}" ]; then +if [ ! -z "${COMMIT}" ]; then AMEND='--amend' FORCE='-f' else diff --git a/ssf/files/default/git/git_30_create_PR.sh b/ssf/files/default/git/git_30_create_PR.sh index 113cc7b5..419ccd5d 100755 --- a/ssf/files/default/git/git_30_create_PR.sh +++ b/ssf/files/default/git/git_30_create_PR.sh @@ -23,7 +23,7 @@ COMMENT='Command `'${STATE}'` run' # Only create the PR if it doesn't already exist # If it already exists, the `git push` done earlier will have updated the PR already PR_EXISTS=$(curl -i https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/pulls | grep "${GH_USER}:${BRANCH_PR}") -if [ "${PR_EXISTS}" ]; then +if [ ! -z "${PR_EXISTS}" ]; then CHANGED=False else curl -H "Authorization: bearer ${GH_TOKEN}" -d '