1

Lazy programmer here, I'm making a simple shell script that takes a branch name from the user input, transforms that name into proper format and creates new branch locally, then pushes it to the remote.

So the goal is to transform a string e.g. 'Mary had a little lamb' into 'mary-had-a-little-lamb', removing all characters that aren't digits or letters along the way as well as replacing all spaces, single or multiple, with -.

I have a working solution but it looks pretty ugly to me, how can I improve it?

Also, is there a way to check if the specified branch already exists locally and only proceed if it doesn't?

#!/bin/bash
currentBranch=$(git branch --show-current)
echo "Checking out from branch $currentBranch"
echo "Enter new branch name:"
read branchName
branchName=$(echo $branchName | tr -d ':-') #remove special characters
branchName=$(echo $branchName | tr -s ' ') #replace multiple spaces with one
branchName=$(echo $branchName | tr ' ' '-') #replace spaces with -
branchName=${branchName,,}

echo "Checking out new branch $branchName..."
git checkout -b $branchName
echo "Pushing new branch $branchName to the remote..."
git push origin $branchName
1

2 Answers 2

4

You can use Bash's built-in string substitution:

#!/usr/bin/env bash

# Take this for a test
branch_name='foo bar    baz:: ::: qux----corge'

# Need extglob for the pattern to replace
shopt -s extglob

# Do the substition with extglob pattern
#san_branch_name="${branch_name//+([[:punct:][:space:]-])/-}"
# this is a shorter filter for valid git identifiers
san_branch_name="${branch_name//+([^[:alnum:]])/-}"


# For debug purposes
declare -p branch_name san_branch_name

Actual output:

declare -- branch_name="foo bar    baz:: ::: qux----corge"
declare -- san_branch_name="foo-bar-baz-qux-corge"
Sign up to request clarification or add additional context in comments.

2 Comments

This works, thanks, except that ' - ' gets turned into '---' so I need to squeeze that.
@PaulJK I improved the pattern to also reduce multiple dashes - to only one and replace all punctuation.
1

I suggest you to use sed in order to sanitize you branch name by sed as follow:

sanitized_branch_name=$(echo ${branchName} | sed -E 's/\s+/\s/g' | sed -E 's/[\s:]/\-/g')

About how to check branch it is enough:


if git branch -a | grep $sanitized_branch_name 2>& 1>/dev/null; then
  echo "${sanitized_branch_name} branch exists!"
fi

Edit (example output):

$ branchName="antonio  petri:cca"

$ echo ${branchName} | sed -E 's/\s+/\s/g' | sed -E 's/[\s:]/\-/g'

antonio-petri-cca

9 Comments

Getting the "sed: -e expression #1, char 11: unterminated `s' command" error.
Sorry, I fixed an error (I introduced a : instead of a /). Please try again.
Sorry, that's still not it. E.g. input string "MT100 domestic - wrong mapping of tag :20: " becomes "MT100-dome-tic---wrong-mapping-of-tag-:20:". Any chance you might test locally first?
Dont't forget Bash has built-in substitution support
You can pass sed multiple " -e '<expression>' ", so you don't need the repeated " | sed ".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.