Hooks updated. Added a way to ignore posts to fediverse by creating .git/ignorepost.

This commit is contained in:
Storm Dragon 2024-12-12 21:02:09 -05:00
parent 5ac7a8f5f6
commit 96f819a8f8
2 changed files with 96 additions and 25 deletions

View File

@ -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}"

View File

@ -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