From 954938c29a990e8ad3064d6fa138498e94818882 Mon Sep 17 00:00:00 2001 From: Florian Ramillien Date: Fri, 19 Jan 2024 15:12:09 +0100 Subject: [PATCH 1/4] Add --squash-history option This option is similar to --no-history (avoid bloating the repository size) but keep data. We squash all commits from gh-pages branch filtered on the current commit message. This allow workflows like keeping commits and data for a released doc and always override nightlies documentations. --- README.md | 6 ++++++ ghp_import.py | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/README.md b/README.md index 8187768..03be4ea 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/ghp_import.py b/ghp_import.py index 328422b..ef31121 100755 --- a/ghp_import.py +++ b/ghp_import.py @@ -196,6 +196,15 @@ 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 + # FILTER_BRANCH_SQUELCH_WARNING=1 ??? + git.check_call('filter-branch', '--force', '--commit-filter', filter_script, 'refs/heads/%s' % branch) + + def options(): return [ (('-n', '--no-jekyll'), dict( @@ -238,6 +247,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', @@ -278,6 +293,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') From 1554611d97603986ee34d2d62e99ada37752e05a Mon Sep 17 00:00:00 2001 From: Florian Ramillien Date: Mon, 22 Jan 2024 10:39:56 +0100 Subject: [PATCH 2/4] Fix git-filter warning Add env var FILTER_BRANCH_SQUELCH_WARNING to avoid warnings and temporisation when git-filter is called. --- ghp_import.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ghp_import.py b/ghp_import.py index ef31121..bf5cf68 100755 --- a/ghp_import.py +++ b/ghp_import.py @@ -201,8 +201,8 @@ def squash_history(git, branch, message=None): 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 - # FILTER_BRANCH_SQUELCH_WARNING=1 ??? - git.check_call('filter-branch', '--force', '--commit-filter', filter_script, 'refs/heads/%s' % branch) + git.check_call('filter-branch', '--force', '--commit-filter', filter_script, 'refs/heads/%s' % branch, + env={ **os.environ , 'FILTER_BRANCH_SQUELCH_WARNING':'1' }) def options(): From 208f9d91e62a1fe38f0f22df2251516bc61d7219 Mon Sep 17 00:00:00 2001 From: Florian Ramillien Date: Thu, 25 Jan 2024 18:01:32 +0100 Subject: [PATCH 3/4] Flake8 formatting --- ghp_import.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/ghp_import.py b/ghp_import.py index bf5cf68..32b6aa0 100755 --- a/ghp_import.py +++ b/ghp_import.py @@ -197,12 +197,27 @@ def run_import(git, srcdir, **opts): def squash_history(git, branch, message=None): - filter_test = '[ "$(git rev-parse refs/heads/%s)" != "$GIT_COMMIT" ]' % branch + 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' }) + 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(): From 9f1ab1d8cd31b0bd37349c55a96a3ab97a442b41 Mon Sep 17 00:00:00 2001 From: Florian Ramillien Date: Thu, 25 Jan 2024 14:13:45 +0100 Subject: [PATCH 4/4] Update Python versions Avoid deprecated Python version 2.7, being removed from GitHub Actions images. --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 9464d2a..0dd0aa2 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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', '']