Skip to content

Instantly share code, notes, and snippets.

@jbbn
Last active July 15, 2024 20:44
Show Gist options
  • Save jbbn/b4ddec6775668b7fe0553c9a4f06d328 to your computer and use it in GitHub Desktop.
Save jbbn/b4ddec6775668b7fe0553c9a4f06d328 to your computer and use it in GitHub Desktop.
Slugify the current git commit, appending the year and the current week number

Slugify commit script

Setup

curl -L https://gist.github.com/jbbn/b4ddec6775668b7fe0553c9a4f06d328/raw/install.sh | bash

Usage

# last commit message: "feat(api): add /version endpoint"

slugify # feat-api-add-version-endpoint--24w01

# create a branch with the slug
git branch -b $(slugify)

# alternative: manual input instead of using git information (slugify only)
slugify "My manual input" # my-manual-input--24w01
#!/usr/bin/env bash
curl -LJ https://gist.github.com/jbbn/b4ddec6775668b7fe0553c9a4f06d328/raw/slugify -o ~/.local/bin/slugify && chmod +x ~/.local/bin/slugify
#!/usr/bin/env bash
max_length="${3:-150}"
# Function to create slug from title
generate_slug() {
local title=$1
local slug="$({
tr '[A-Z]' '[a-z]' | tr -cs '[[:alnum:]]' '-'
} <<< "$title")"
slug="$({
tr '[ČčĆć]' 'c'
} <<< "$slug")"
slug="$({
sed 's/[Đđ]/dj/g'
} <<< "$slug")"
slug="$({
tr '[Žž]' 'z'
} <<< "$slug")"
slug="$({
tr '[Šš]' 's'
} <<< "$slug")"
slug="${slug##-}"
slug="${slug%%-}"
echo "${slug:0:$max_length}"
}
# Check if $1 is a number
if [[ "$1" =~ ^[0-9]+$ ]]; then
travel_back="${1:-1}"
logs=$(git log --pretty=format:%s -"${travel_back}")
title=${logs##*$'\n'}
else
title="$1"
fi
slug=$(generate_slug "$title")
suffix=$(date +"%yw%U")
echo "${slug}--${suffix}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment