Skip to content

Instantly share code, notes, and snippets.

@basilevs
Last active August 16, 2019 10:46
Show Gist options
  • Save basilevs/ca86d3e99b361a3e5045a989778e2549 to your computer and use it in GitHub Desktop.
Save basilevs/ca86d3e99b361a3e5045a989778e2549 to your computer and use it in GitHub Desktop.
Updates an existing Gerrit change
#!/bin/bash
# updateGerritChange.sh
#
# Updates an existing Gerrit change for master branch with a state of current Git branch based on that change
#
# Allows to keep a history of changes for a Gerrit change in a local Git branch.
#
# Usage:
# git checkout master
# git pull origin master
# git checkout -b change_number
# git pull ssh://git.eclipse.org:29418/repo_path refs/changes/change_branch
# make changes
# git commit
# make changes
# git commit
# ~/bin/updateGerritChange
# make changes
# git commit
# ~/bin/updateGerritChange
set -ex
if [[ `git status --porcelain | wc -l` != 0 ]] ; then
>&2 git status
>&2 echo Working copy is dirty
return 1
fi
TARGET_BRANCH=master
BRANCH=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
if [[ -z $BRANCH ]]; then
>&2 echo No branch given
exit 2
fi
CHANGE_COMMIT=$(git log "origin/$TARGET_BRANCH...HEAD" --no-decorate --grep="Change-Id: I" --max-count=1 --format="%H")
if [[ -z $CHANGE_COMMIT ]] || [[ "HEAD" == $CHANGE_COMMIT ]]; then
>&2 echo Can\'t find the change to update
exit 3
fi
MESSAGE=`git log --format=%B -n 1 $CHANGE_COMMIT`
if [[ -z $MESSAGE ]] ; then
>&2 echo Commit $CHANGE_COMMIT has empty message
exit 4
fi
git pull origin "$TARGET_BRANCH" --ff-only || exit 5
git branch -D tmp || true
git checkout -b tmp
git reset --soft "origin/$TARGET_BRANCH"
git log -n 1
git status
git commit --reuse-message $CHANGE_COMMIT
git push origin "HEAD:refs/for/$TARGET_BRANCH"
git checkout $BRANCH
git branch -D tmp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment