Skip to content

Commit 72320bd

Browse files
committed
dev: move modify of version.txt from post-commit to pre-push
to avoid conflicts on frequent rebases of feature branches
1 parent 57de3e1 commit 72320bd

File tree

4 files changed

+58
-74
lines changed

4 files changed

+58
-74
lines changed

.githooks/post-commit.py

Lines changed: 0 additions & 56 deletions
This file was deleted.

.githooks/pre-push.py

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,70 @@
11
#!/usr/bin/env python3
22

3-
# This script avoids to push branches starting with a "#". This is the way
4-
# how I store ticket related feature branches that are work in progress.
5-
6-
# Once a feature branch is finished, it will be rebased to mains HEAD,
7-
# its commits squashed, merged into main and the branch deleted afterwards.
8-
3+
# Purpose:
4+
#
5+
# - avoids to push branches starting with a "#". This is the way
6+
# how I store ticket related feature branches that are work in progress
7+
#
8+
# - updates the version file "layouts/partials/version.txt" with the latest
9+
# available commit hash before pushing
10+
#
11+
# Installation:
12+
#
913
# Call this script from your ".git/hooks/pre-push" file like this (supporting
1014
# Linux, Windows and MacOS)
11-
15+
#
16+
# ```sh
1217
# #!/bin/sh
1318
# python3 .githooks/pre-push.py
19+
# ```
1420

1521
from datetime import datetime
1622
import os
1723
import re
1824
import subprocess
1925

26+
def check_wip_branch(script_name, log_file, time, repo_name):
27+
local_branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], universal_newlines=True).strip()
28+
wip_prefix = '^#\\d+(?:\\b.*)$'
29+
if re.match(wip_prefix, local_branch):
30+
print(f'{time}: {repo_name} - {script_name} - Branch "{local_branch}" was not pushed because its name starts with a "#" which marks it as work in progress', file=open(log_file, "a"))
31+
print(f'{script_name} - Branch "{local_branch}" was not pushed because its name starts with a "#" which marks it as work in progress')
32+
exit(1)
33+
34+
def update_version(script_name, log_file, time, repo_name):
35+
file_path = 'layouts/partials/version.txt'
36+
try:
37+
f = open(file_path, 'r+')
38+
except OSError as e:
39+
print(f'{time}: {repo_name} - {script_name} - Could not open {file_path}: [{e.errno}] {e.strerror}', file=open(log_file, "a"))
40+
print(f'{script_name} - Could not open {file_path}: [{e.errno}] {e.strerror}')
41+
exit(1)
42+
with f:
43+
version = f.read().strip()
44+
new_version = ''
45+
match = re.match(r'(\d+\.\d+\.\d+)(?:\+([^+]+))?', version)
46+
if match:
47+
semver = match.group(1)
48+
old_hash = match.group(2)
49+
new_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD~1']).decode('utf-8').strip()
50+
print(f'{time}: {repo_name} - {script_name} - old hash {old_hash} - new hash {new_hash}', file=open(log_file, "a"))
51+
print(f'{script_name} - old hash {old_hash} - new hash {new_hash}')
52+
if old_hash != new_hash:
53+
new_version = f'{semver}+{new_hash}'
54+
f.seek(0)
55+
f.write(new_version)
56+
f.truncate()
57+
f.close()
58+
subprocess.check_call(['git', 'add', file_path])
59+
subprocess.check_call(['git', 'commit', '--amend', '--no-edit'])
60+
print(f'{time}: {repo_name} - {script_name} - New version {new_version} was written to {file_path}', file=open(log_file, "a"))
61+
else:
62+
print(f'{time}: {repo_name} - {script_name} - No change in hash, file {file_path} not updated', file=open(log_file, "a"))
63+
else:
64+
print(f'{time}: {repo_name} - {script_name} - Invalid version format in {file_path}', file=open(log_file, "a"))
65+
print(f'{script_name} - Invalid version format in {file_path}')
66+
exit(1)
67+
2068
# This hook is called with the following parameters:
2169
# $1 -- Name of the remote to which the push is being done
2270
# $2 -- URL to which the push is being done
@@ -25,8 +73,6 @@
2573
# Information about the commits being pushed is supplied as lines to
2674
# the standard input in the form:
2775
# <local ref> <local sha1> <remote ref> <remote sha1>
28-
# This hook prevents the push of commits that belong to branches starting with
29-
# an "#" (which are work in progress).
3076

3177
def main():
3278
script_name = "PRE-PUSH"
@@ -36,13 +82,8 @@ def main():
3682
repo_root = subprocess.check_output(['git', 'rev-parse', '--show-toplevel'], universal_newlines=True).strip()
3783
repo_name = os.path.basename(repo_root)
3884

39-
local_branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], universal_newlines=True).strip()
40-
wip_prefix = '^#\\d+(?:\\b.*)$'
41-
if re.match(wip_prefix, local_branch):
42-
print(f'{time}: {repo_name} - {script_name} - Branch "{local_branch}" was not pushed because its name starts with a "#" which marks it as work in progress', file=open(log_file, "a"))
43-
print(f'{script_name} - Branch "{local_branch}" was not pushed because its name starts with a "#" which marks it as work in progress')
44-
exit(1)
45-
print(f'{time}: {repo_name} - {script_name} - Branch "{local_branch}" was pushed', file=open(log_file, "a"))
85+
check_wip_branch(script_name, log_file, time, repo_name)
86+
update_version(script_name, log_file, time, repo_name)
4687
exit(0)
4788

4889
if __name__ == "__main__":

CLAUDE.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ Example: `search: improve Orama integration for multilingual sites`
166166
### Git Hooks
167167

168168
Python-based git hooks in `.githooks/`:
169-
- `post-commit.py` - Post-commit processing
170169
- `pre-push.py` - Pre-push validation
171170

172171
## Important Files

layouts/partials/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9.0.3+6ce76522e50393128730839d3ffa3deb12863262
1+
9.0.3+57de3e1982fe4f34061610fa61c80ccf16148fd5

0 commit comments

Comments
 (0)