From 96f819a8f80d71880dc2eb5403f334c0e129fc2b Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Thu, 12 Dec 2024 21:02:09 -0500 Subject: [PATCH] Hooks updated. Added a way to ignore posts to fediverse by creating .git/ignorepost. --- pre-commit-version-increment | 10 ---- pre-push-fediverse | 111 ++++++++++++++++++++++++++++++----- 2 files changed, 96 insertions(+), 25 deletions(-) diff --git a/pre-commit-version-increment b/pre-commit-version-increment index 0c36de5..dc64146 100755 --- a/pre-commit-version-increment +++ b/pre-commit-version-increment @@ -38,16 +38,6 @@ if [ ! -f "$versionFile" ]; then exit 1 fi -# Check for uncommitted changes (excluding version file) -if [ -n "$(git diff --name-only | grep -v "$versionFile")" ]; then - echo -e "${YELLOW}Warning: You have unstaged changes in other files.${NC}" - read -p "Do you want to continue with the commit? (y/N) " -n 1 -r - echo - if [[ ! $REPLY =~ ^[Yy]$ ]]; then - exit 1 - fi -fi - # Check if the branch name contains "temp" or "temporary" if [[ "$branchName" =~ temp(orary)? ]]; then echo -e "${YELLOW}Warning: You appear to be on a temporary branch ($branchName).${NC}" diff --git a/pre-push-fediverse b/pre-push-fediverse index 052609f..ed4c5ec 100755 --- a/pre-push-fediverse +++ b/pre-push-fediverse @@ -1,23 +1,104 @@ -#!/bin/bash +#!/usr/bin/env bash # Requires toot: https://github.com/ihabunek/toot -# remote is $1 e.g. origin -# url is $2 -url="${2#*@}" +# This script posts git commit information to the Fediverse using the toot CLI +# To temperarily turn off posting, in project root: +# touch .git/ignorepost + +# Check if .git/ignorepost exists - if so, exit silently +if [[ -f ".git/ignorepost" ]]; then + exit 0 +fi + +# Do not allow errors +set -e + +remote="$1" +url="$2" + +# Clean up the repository URL to create a web-friendly link +url="${url#*@}" url="${url%.git}" url="${url#*://}" -shopt -s extglob +shopt -s extglob # Enable extended pattern matching url="${url/:+([0-9])/}" url="https://${url}" -branch="$(git branch | grep "\* ")" -branch="${branch:2}" -commit="$(git log -1 --pretty=%B)" -project="${2##*/}" -project="${project%.git}" -pusher="$(git log | grep -m1 "Author: ")" -pusher="${pusher#Author: }" -pusher="${pusher% <*@*.*>}" -toot post -v public -p "$project" -t "text/markdown" "$pusher pushed changes to the $branch branch of the [$project project]($url) -$commit" &> /dev/null +# Extract project name from repository URL +project="${url##*/}" +project="${project%.git}" + +# Get pusher's name from git config more safely +pusher="$(git config user.name)" +if [[ -z "$pusher" ]]; then + echo "Error: Could not determine user name from git config" + exit 1 +fi + +# Initialize message +message="" +tags="" +branches="" + +# Read push information from stdin +while read localRef localSha remoteRef remoteSha; do + # Skip if no refs are being pushed + [[ -z "$localRef" ]] && continue + + # Check if this is a tag push + if [[ "$localRef" == refs/tags/* ]]; then + tagName="${localRef#refs/tags/}" + if [[ -z "$tags" ]]; then + tags="$tagName" + else + tags="$tags, $tagName" + fi + else + # Get current branch name from ref + branch="${localRef#refs/heads/}" + + # Verify we can access the commit + if ! git cat-file -e "$localSha"; then + echo "Error: Cannot access commit $localSha" + exit 1 + fi + + # Get latest commit message + commit="$(git log -1 --pretty=%B $localSha)" + + if [[ -z "$branches" ]]; then + branches="$branch" + else + branches="$branches, $branch" + fi + fi +done + +# Construct the final message +if [[ "${branches##*,}" != "${branches}" ]]; then + branches="${branches%, *} and ${branches##*, }" +fi +if [[ "${tags##*,}" != "${tags}" ]]; then + tags="${tags%, *} and ${tags##*, }" +fi +if [[ -n "$tags" ]]; then + message="$pusher tagged [$project project]($url) with: $tags" +elif [[ -n "$branches" ]]; then + if [[ "${branches% and }" != "${branches}" ]]; then + message="$pusher pushed changes to the $branches branches of the [$project project]($url)" + else + message="$pusher pushed changes to the $branches branch of the [$project project]($url)" + fi + if [[ "$branches" != *","* ]]; then + # Only add commit message if pushing to a single branch + message="$message +$commit" + fi +fi + +# Only post if we have something to say +if [[ -n "$message" ]]; then + toot post -v public -p "$project" -t "text/markdown" "$message" &> /dev/null +fi + exit 0