Skip to content

Commit

Permalink
Bump version to 24.592.F.2011
Browse files Browse the repository at this point in the history
  • Loading branch information
rishikeshsreehari committed Nov 20, 2024
1 parent 4b3ee1f commit f441f5c
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 50 deletions.
8 changes: 4 additions & 4 deletions data/version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Version": "24.591.F.2011",
"PushCount": 591,
"LastCommitLong": "b3e17babd89e32efa31ab93d7e9e58446fabd550",
"LastCommitShort": "b3e17ba"
"Version": "24.592.F.2011",
"PushCount": 592,
"LastCommitLong": "TBD",
"LastCommitShort": "TBD"
}
128 changes: 82 additions & 46 deletions scripts/git_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,22 @@
from pathlib import Path

VERSION_FILE = "data/version.json"
LOCK_FILE = ".git/post-commit.lock"
LOCK_FILE = ".git/pre-commit.lock"

# Utility Functions
def check_lock():
"""Check if the lock file exists."""
return os.path.exists(LOCK_FILE)

def create_lock():
"""Create the lock file."""
Path(LOCK_FILE).touch()

def remove_lock():
"""Remove the lock file."""
if os.path.exists(LOCK_FILE):
os.remove(LOCK_FILE)

def get_current_commit_hash():
"""Get the current commit hash."""
try:
Expand Down Expand Up @@ -85,60 +98,83 @@ def on_button_click(type_choice):
root.mainloop()
return result["type"]

# Main Functions
def pre_commit():
"""Handle pre-commit tasks."""
# Check for staged changes
staged_changes = subprocess.check_output(
["git", "diff", "--cached", "--name-only"], text=True
).strip().splitlines()
if not staged_changes:
print("No changes staged for commit.")
sys.exit(1)

# Get commit type via GUI
commit_type = get_commit_type_gui()
if not commit_type:
print("No commit type selected. Aborting commit.")
sys.exit(1)

# Read and update version
version_data = read_version_file()
current_push_count = version_data.get("PushCount", 0)
new_push_count = current_push_count + 1
current_date = datetime.now().strftime("%d%m")
version = f"24.{new_push_count}.{commit_type}.{current_date}"

version_data["Version"] = version
version_data["PushCount"] = new_push_count
version_data["LastCommitLong"] = "TBD"
version_data["LastCommitShort"] = "TBD"

with open(VERSION_FILE, "w") as f:
json.dump(version_data, f, indent=4)

# Stage changes and commit
subprocess.run(["git", "add", "-u"])
subprocess.run(["git", "commit", "-m", f"Bump version to {version}"])
# Avoid re-execution during amendments
if check_lock():
return

# Create a lock to prevent recursive invocation
create_lock()

try:
# Check for staged changes
staged_changes = subprocess.check_output(
["git", "diff", "--cached", "--name-only"], text=True
).strip().splitlines()
if not staged_changes:
print("No changes staged for commit.")
sys.exit(1)

# Get commit type via GUI
commit_type = get_commit_type_gui()
if not commit_type:
print("No commit type selected. Aborting commit.")
sys.exit(1)

# Read and update version
version_data = read_version_file()
current_push_count = version_data.get("PushCount", 0)
new_push_count = current_push_count + 1
current_date = datetime.now().strftime("%d%m")
version = f"24.{new_push_count}.{commit_type}.{current_date}"

version_data["Version"] = version
version_data["PushCount"] = new_push_count
version_data["LastCommitLong"] = "TBD"
version_data["LastCommitShort"] = "TBD"

with open(VERSION_FILE, "w") as f:
json.dump(version_data, f, indent=4)

# Stage changes and commit
subprocess.run(["git", "add", "-u"])
subprocess.run(["git", "commit", "-m", f"Bump version to {version}"])
finally:
# Remove lock after execution
remove_lock()

def post_commit():
"""Handle post-commit tasks."""
# Get current commit hash
long_hash, short_hash = get_current_commit_hash()
if not long_hash or not short_hash:
print("Could not retrieve current commit hash.")
# Avoid re-execution due to amendments
if check_lock():
return

# Update version.json with commit hash
version_data = read_version_file()
version_data["LastCommitLong"] = long_hash
version_data["LastCommitShort"] = short_hash
try:
# Create a lock to prevent recursive invocation
create_lock()

# Get current commit hash
long_hash, short_hash = get_current_commit_hash()
if not long_hash or not short_hash:
print("Could not retrieve current commit hash.")
return

with open(VERSION_FILE, "w") as f:
json.dump(version_data, f, indent=4)
# Update version.json with commit hash
version_data = read_version_file()
version_data["LastCommitLong"] = long_hash
version_data["LastCommitShort"] = short_hash

# Stage and amend commit
subprocess.run(["git", "add", VERSION_FILE])
subprocess.run(["git", "commit", "--amend", "--no-edit", "--no-verify"])
with open(VERSION_FILE, "w") as f:
json.dump(version_data, f, indent=4)

# Stage and amend commit
subprocess.run(["git", "add", VERSION_FILE])
subprocess.run(["git", "commit", "--amend", "--no-edit", "--no-verify"])
finally:
# Remove lock after execution
remove_lock()

if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "post-commit":
Expand Down

0 comments on commit f441f5c

Please sign in to comment.