Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --squash-history option #127

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ${{ matrix.os }}-latest
strategy:
matrix:
python-version: ['2.7', '3.9']
python-version: ['3.7', '3.12']
os: [ubuntu, windows, macos]
opts: ['--shell', '']

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Options:
out. [none]
-f, --force Force the push to the repository.
-o, --no-history Force new commit without parent history.
-a, --squash-history Force squash commits, remove history but keep data.
-r REMOTE, --remote=REMOTE
The name of the remote to push to. [origin]
-b BRANCH, --branch=BRANCH
Expand All @@ -90,6 +91,11 @@ The `-o` option will discard any previous history and ensure that only a
single commit is always pushed to the `gh-pages` branch. This is useful to
avoid bloating the repository size and is **highly recommended**.

The `-a` option will discard some previous commits, keeping data. Only commits
matching current commit message are discarded ("Update documentation" if `-m`
option is not set). This is useful to avoid bloating the repository size but
without discarding any data. Similar to `-o` but without data loss.

You can specify a different branch with `-b`. This is useful for user and
organization page, which are served from the `master` branch.

Expand Down
33 changes: 33 additions & 0 deletions ghp_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,30 @@ def run_import(git, srcdir, **opts):
sys.stdout.write(enc("Failed to process commit.\n"))


def squash_history(git, branch, message=None):
filter_test = (
'[ "$(git rev-parse refs/heads/%s)" != "$GIT_COMMIT" ] '
% branch
)
if message:
filter_test += (
'&& [ $(git show -s --format=%%B "$GIT_COMMIT" '
'| grep -c "%s") -gt 0 ]'
% message
)
filter_script = (
'if %s ; then skip_commit "$@"; else git commit-tree "$@"; fi'
% filter_test
)
git.check_call(
'filter-branch',
'--force',
'--commit-filter', filter_script,
'refs/heads/%s' % branch,
env={**os.environ, 'FILTER_BRANCH_SQUELCH_WARNING': '1'}
)


def options():
return [
(('-n', '--no-jekyll'), dict(
Expand Down Expand Up @@ -238,6 +262,12 @@ def options():
action='store_true',
help='Force new commit without parent history.',
)),
(('-a', '--squash-history'), dict(
dest='squash_history',
default=False,
action='store_true',
help='Force squash commits, remove history but keep data.',
)),
(('-r', '--remote'), dict(
dest='remote',
default='origin',
Expand Down Expand Up @@ -278,6 +308,9 @@ def ghp_import(srcdir, **kwargs):

run_import(git, srcdir, **opts)

if opts['squash_history']:
squash_history(git, opts['branch'], opts['mesg'])

if opts['push']:
if opts['force'] or opts['no_history']:
git.check_call('push', opts['remote'], opts['branch'], '--force')
Expand Down