Skip to content

Commit

Permalink
fix(git): tighten up scripts to avoid inconsistencies
Browse files Browse the repository at this point in the history
* 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`
  • Loading branch information
myii committed Oct 17, 2019
1 parent 8f8f9f0 commit 778c7bb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
17 changes: 9 additions & 8 deletions ssf/files/default/git/git_10_prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ssf/files/default/git/git_20_commit_push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ssf/files/default/git/git_30_create_PR.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 '
Expand Down

0 comments on commit 778c7bb

Please sign in to comment.