Skip to content

Instantly share code, notes, and snippets.

@ndevenish
Last active April 4, 2019 12:21
Show Gist options
  • Save ndevenish/fd97ff7150af2a316a06f9f030da91e9 to your computer and use it in GitHub Desktop.
Save ndevenish/fd97ff7150af2a316a06f9f030da91e9 to your computer and use it in GitHub Desktop.
Script to resync dxtbx/cctbx_project branches, before split
#!/bin/bash
# The commit on the current cctbx_project/dxtbx equivalent to dxtbx master
BASE_COMMIT=cbf101d68265777cee933a203ccb683ab54ddf13
set -e
set -x
# Make sure repo exists and we're inside it
if [[ "$(basename $(pwd))" != "cctbx_project" ]]; then
if [[ ! -d cctbx_project ]]; then
git clone [email protected]:cctbx/cctbx_project.git
fi
cd cctbx_project
fi
# Make sure remotes exist
git remote add dxtbx [email protected]:dials/dxtbx.git || true
git remote add dxtbx_cctbx [email protected]:cctbx/dxtbx.git || true
git fetch --all
# Validate that the current log message is the same as the base commit
dxtbx_msg=$(git log dxtbx/master -n 1 --format='%B')
cctbx_msg=$(git log ${BASE_COMMIT} -n 1 --format='%B')
if [[ "$dxtbx_msg" != "$cctbx_msg" ]]; then
echo "Error: Messages do not match between dxtbx/master and $BASE_COMMIT"
exit 1
fi
# remote 'origin' is cctbx/cctbx_project.git
# remote 'dxtbx' is cctbx/dxtbx.git
git co master
git reset --hard origin/master
# Filter to the subdirectory in question
git filter-branch -f --subdirectory-filter dxtbx ${BASE_COMMIT}^..HEAD
# Count the number of new commits we don't have in dxtbx
n_commits=$(($(git rev-list --count ${BASE_COMMIT}^..HEAD)-1))
# Transplant this number of commits. Interactive to manually verify makes sense
git rebase -i HEAD~${n_commits} --onto dxtbx/master
# Reblack to remove intermediate commits
# Rewrite committer -> author as rebase loses this information
git filter-branch \
--tree-filter 'black .' \
--env-filter 'export GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL; export GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME; export GIT_COMMITTER_DATE=$GIT_AUTHOR_DATE' \
--prune-empty -f HEAD~${n_commits}..HEAD
set +x
echo
echo "New base commit: $(git log -n 1 --format='%H' origin/master -- dxtbx)"
echo "Need to push to dxtbx/dxtbx_cctbx remotes"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment