Skip to content

Instantly share code, notes, and snippets.

@astrofrog
Last active January 20, 2018 16:57
Show Gist options
  • Save astrofrog/6b3e770182ca25a2d7e81da7f1ac6c1f to your computer and use it in GitHub Desktop.
Save astrofrog/6b3e770182ca25a2d7e81da7f1ac6c1f to your computer and use it in GitHub Desktop.
Split out astropy theme
#!/usr/bin/env python
import sys
# Fetch commit message
message = sys.stdin.read()
# Check if it was a merge commit, and if so reword to include the original repo name
if message.startswith('Merge pull request #'):
message = message.replace('pull request #', 'pull request astropy/astropy-helpers#')
print(message)
#!/usr/bin/env python
# This script is meant to be used in conjunction with git filter-branch --parent-filter
# and is used to get rid of branches that don't contain any commits. It looks
# for commits where all the parents are on the main branch, and re-writes
# the parents to be just the most recent of the original parents.
#
# To run:
#
# git filter-branch -f --prune-empty --parent-filter <path_to>/parent_filter.py <branch-name>
#
# This may have to be run iteratively until the git history does not change.
#
# Documentation for --parent-filter:
#
# --parent-filter <command>
# This is the filter for rewriting the commit’s parent list. It will receive
# the # parent string on stdin and shall output the new parent string on
# stdout. The # parent string is in the format described in
# git-commit-tree[1]: empty for the # initial commit, "-p parent" for a normal
# commit and "-p parent1 -p parent2 -p # parent3 …​" for a merge commit.
import sys
import subprocess
# Get the input parent string
parent_string = sys.stdin.read()
# Get rid of the -p and keep only the commits
parents = [x for x in parent_string.split() if x != '-p']
# Find the commits on the main branch, excluding commits in merged branches
git_cmd = 'git log --first-parent --format=oneline'
result = subprocess.check_output(git_cmd, shell=True).decode('utf-8')
# The output looks like:
#
# 6dec82f2c55d4f550e81c072e825c8563c5f0de7 Merge pull request #171 from embray/optimized-docstrings
# d4160c750299917e643cb4e277833097a50995bf Merge pull request #161 from eteq/add-docs-alias
# 7046302b00545cdf4151357b9032a4ecb8570c3b Merge pull request #164 from mdboom/fix-css
# afd597d7a2815b03ac330496678c899b5042cf3e Merge pull request #160 from mdboom/fix-invalid-html
# 4b5999e04d53fa211528281d4cda5ea305d7b0ad Merge pull request #147 from embray/issue-147
#
# Keep only the commits:
main_commits = [row.split()[0].strip() for row in result.splitlines()]
# If there is only one parent, we don't need to do anything. If there is more
# than one parent, then we check if all parents are in the main branch, and if
# so we modify the parent string to include just the most recent parent.
if len(parents) > 1:
if all([p in main_commits for p in parents]):
parent_string = '-p ' + next(c for c in main_commits if c in parents)
# We need to now print the parent string back to stdout
print(parent_string)
#!/bin/bash
# Make sure we are up to date and on a branch
git fetch upstream
git checkout upstream/master
git checkout -b split-theme
# # Move the theme directory in place and remove any other irrelevant files
git filter-branch -f --prune-empty --tree-filter $PWD/../filter.sh split-theme
# # Go back to the split-theme branch (for some reason we end up with a detached HEAD)
git checkout split-theme
# Get rid of branches that no longer have any commits - this needs to be run
# multiple times before it converges - make sure the last line is
# WARNING: Ref 'refs/heads/split-theme' is unchanged, if not you will need to
# run this command again
n_commits_prev="0"
while true; do
n_commits=`git log --format=oneline | wc -l`
echo "$n_commits commits remaining on branch"
if [[ $n_commits == $n_commits_prev ]]; then
echo "Number of commits has converged, stopping"
break;
fi
git filter-branch -f --prune-empty --parent-filter $PWD/../parent_filter.py split-theme
n_commits_prev=$n_commits
done
# Finally re-write commit messages to specify original repo in the merge commits
git filter-branch -f --msg-filter $PWD/../msg_filter.py split-theme
#!/bin/bash
mkdir -p sphinx_astropy_theme
git mv astropy_helpers/sphinx/themes/bootstrap-astropy sphinx_astropy_theme/bootstrap-astropy || true
git rm -rf --ignore-unmatch astropy_helpers *.py CONTRIBUTING.md \
tox.ini setup.cfg appveyor.yml .travis.yml \
README.rst licenses CHANGES.rst MANIFEST.in \
.app* .install* continuous-integration \
.coveragerc .gitmodules .gitignore
git clean -fxd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment