From 4a824dfdbe00b173159dea35b3fe820b81f5439e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Fri, 5 Jan 2024 15:47:37 +0100 Subject: [PATCH 1/2] build: avoid pushing wip branches --- .githooks/pre-push.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 .githooks/pre-push.py diff --git a/.githooks/pre-push.py b/.githooks/pre-push.py new file mode 100644 index 0000000000..23d23fd0af --- /dev/null +++ b/.githooks/pre-push.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +# This script avoids to push branches starting with a "#". This is the way +# how I store ticket related feature branches that are work in progress. + +# Once a feature branch is finished, it will be rebased to mains HEAD, +# its commits squashed, merged into main and the branch deleted afterwards. + +# Call this script from your ".git/hooks/pre-push" file like this (supporting +# Linux, Windows and MacOS) + +# #!/bin/sh +# python3 .githooks/pre-push.py + +import subprocess +import re + +# This hook is called with the following parameters: +# $1 -- Name of the remote to which the push is being done +# $2 -- URL to which the push is being done +# If pushing without using a named remote, those arguments will be equal. + +# Information about the commits being pushed is supplied as lines to +# the standard input in the form: +# +# This hook prevents the push of commits that belong to branches starting with +# an "#" (whiwork in progress). + +def main(): + local_branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], universal_newlines=True).strip() + wip_prefix = "^#\d+(?:\b.*)$" + message = 'The branch {local_branch} can not be pushed as it starts with a '#' which marks it as work in progress' + + if re.match(wip_prefix, local_branch): + print(message) + exit(1) + + exit(0) + +if __name__ == "__main__": + main() From 3f4392fe157a0fd43fa40eba64d1c77fa77f54ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Fri, 5 Jan 2024 16:15:09 +0100 Subject: [PATCH 2/2] build: pre-push refinement --- .githooks/pre-push.py | 10 +++++----- .gitignore | 1 + 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.githooks/pre-push.py b/.githooks/pre-push.py index 23d23fd0af..78574199d3 100644 --- a/.githooks/pre-push.py +++ b/.githooks/pre-push.py @@ -10,6 +10,7 @@ # Linux, Windows and MacOS) # #!/bin/sh +# echo 'execute .githooks/pre-push.py' >> .githooks/hooks.log # python3 .githooks/pre-push.py import subprocess @@ -28,13 +29,12 @@ import re def main(): local_branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], universal_newlines=True).strip() - wip_prefix = "^#\d+(?:\b.*)$" - message = 'The branch {local_branch} can not be pushed as it starts with a '#' which marks it as work in progress' - + wip_prefix = '^#\\d+(?:\\b.*)$' if re.match(wip_prefix, local_branch): - print(message) + print(f'The branch {local_branch} can not be pushed as it starts with a "#" which marks it as work in progress', file=open(".githooks/hooks.log", "a")) + print(f'The branch {local_branch} can not be pushed as it starts with a "#" which marks it as work in progress') exit(1) - + print(f'Pushing branch {local_branch}', file=open(".githooks/hooks.log", "a")) exit(0) if __name__ == "__main__": diff --git a/.gitignore b/.gitignore index 4344fe182c..06430e18ee 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .DS_Store +.githooks/hooks.log .hugo_build.lock exampleSite/public* exampleSite/hugo*.exe