Skip to content

Instantly share code, notes, and snippets.

@srdjanmarjanovic
Created January 19, 2016 12:32
Show Gist options
  • Save srdjanmarjanovic/99a26db1b6ababe49379 to your computer and use it in GitHub Desktop.
Save srdjanmarjanovic/99a26db1b6ababe49379 to your computer and use it in GitHub Desktop.
Useful script for naming branches for Git flow
#!/bin/bash
type="$1"
title="$2"
max_length="${3:-150}"
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%%-}"
slug="${slug:0:$max_length}"
echo "$type/#$slug"
@srdjanmarjanovic
Copy link
Author

How to use:

  • place this script in your home folder e.g. /home/srdjan/slugify.bash or /Users/srdjan
  • add alias in .bash_aliases or .bash_profile file alias slugify='sh ~/slugify.bash' or whatever alias pleases you
  • restart terminal or source ~/.bash_aliases or source ~/.bash_profile so alias can be used
  • run slugify "TYPE" "NAME" where TYPE is something like bug, feature, improvement and NAME is something like task name from project management tool

here is an example from Active Collab:

slugify "improvement" "#723: Kada se podesi Task ID in Lists na Visible, format prikaza nije ispravan"

wil output

improvement/#723-kada-se-podesi-task-id-in-lists-na-visible-format-prikaza-nije-ispravan

Want to create a git branch out of a slug in one line? No problem

slugify "improvement" "#723: Kada se podesi Task ID in Lists na Visible, format prikaza nije ispravan" | xargs git checkout -b

This command will make the appropriate name for the branch, create the branch, and checkout to it

Note: this script will replace characters ŠĐČĆŽ šđčćž with s dj c c z and it will truncate NAME parameter to 150 characters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment