Skip to content

Instantly share code, notes, and snippets.

@rushkeldon
Last active April 15, 2024 06:30
Show Gist options
  • Save rushkeldon/7bf19bfbb7d7b42f4221876ba5b4d73b to your computer and use it in GitHub Desktop.
Save rushkeldon/7bf19bfbb7d7b42f4221876ba5b4d73b to your computer and use it in GitHub Desktop.
useful bash functions for your .bash_profile or similar
# region : timers
# example usage :
# npm run clean && startTimer && npm run build && endTimer
function startTimer() {
START_TIME=$(date +%s.%N)
}
function endTimer() {
END_TIME=$(date +%s.%N)
DURATION=$(echo "$END_TIME - $START_TIME" | bc)
DURATION_MS=$(echo "$DURATION * 1000" | bc)
DURATION_SEC=$(echo "scale=3; $DURATION" | bc)
DURATION_MIN=$(echo "scale=2; $DURATION / 60" | bc)
echo "Elapsed time: ${DURATION_MS} ms (${DURATION_SEC} seconds / ${DURATION_MIN} minutes)"
}
# endregion : timers
# region : git commands #
# Usage
# getFirstHashWithIn "path/to/your/file" "searchTerm"
function getFirstHashWithIn() {
file=$1
searchTerm=$2
# Search through the commit history for the specified file, looking for the search term
# The command performs a reverse log search (oldest to newest) and stops at the first occurrence
for commit in $(git rev-list --reverse HEAD "$file"); do
if git show "$commit:$file" | grep -q "$searchTerm"; then
echo "$commit"
return 0
fi
done
echo "Search term not found in the file's history."
return 1
}
function checkoutBefore() {
local commitHash=$1
git checkout $commitHash^
}
# commitHash=$(getFirstHashWithIn "path/to/your/file" "searchTerm") && checkoutBefore $commitHash
function main() {
git checkout main
}
function checkout() {
if [ -z "$1" ]
then
echo "ERROR - no branch name was supplied."
else
git checkout $1
fi
}
# branch
# either lists the branches - or - if branch name is provided creates a new branch and checks it out
# if the provided branch exists then it is checked out anyway
function branch() {
if [ -z "$1" ]; then
git branch
else
echo "creating branch '"$1"'"
git branch "$1"
git checkout "$1"
fi
}
# delete a branch both locally and remotely
function deleteBranch() {
if [ -z "$1" ]; then
echo "ERROR - no branch name was supplied."
else
echo "deleting branch '"$1"'"
git branch -D "$1"
git push origin --delete "$1"
fi
}
function resetBranch() {
if [ -z "$1" ]; then
echo "ERROR - no branch name was supplied."
else
checkout develop
branch temp
echo "deleting local branch '"$1"'"
git branch -D "$1"
echo "checking out a fresh remote branch '"$1"'"
checkout "$1"
git branch -D temp
fi
}
# checkout latest branch
function back() {
git checkout -
}
# removes local branches that don't exist upstream
function prune() {
git remote prune origin
}
# commit
# commits all changes with the provided msg or 'incremental' if no msg provided
function commit() {
local currentBranch=$(git rev-parse --abbrev-ref HEAD);
local ticketNumber=${currentBranch#*\/}
local commitPrefix="RE #$ticketNumber"
echo $commitPrefix
if [ -z "$1" ]; then
git commit -a -m "$ticketNumber - incremental"
# git commit -a --amend
else
# using the echo allows escaped newlines '\n' in the commit msg
git commit -a -m "$ticketNumber : $(echo -e "$1")"
fi
}
# fetch
# fetches all branches of a repo
function fetch() {
git branch -r | grep -v '\->' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
}
# push - pushes to the ORIGIN current branch unless some other branch is specified
function push() {
if [ -z "$1" ]; then
local currentBranch=$(git rev-parse --abbrev-ref HEAD);
echo "pushing to ORIGIN $currentBranch"
git push origin $currentBranch
else
echo "pushing to ORIGIN $1"
git push origin "$1"
fi
}
# pull - pulls from the ORIGIN current branch unless some other branch is specified
function pull() {
if [ -z "$1" ]; then
local currentBranch=$(git rev-parse --abbrev-ref HEAD);
echo "pulling from ORIGIN $currentBranch"
git pull origin $currentBranch
else
echo "pulling from ORIGIN $1"
git pull origin "$1"
fi
}
# gets the middle commit of the current branch since the provided date
function getMiddleCommit() {
local since=$1
local logOutput=$(git log --pretty=format:"%H %ad" --date=short --since="$since")
local commitCount=$(echo "$logOutput" | wc -l)
local middleIndex=$((commitCount/2 + 1))
local middleHash=$(echo "$logOutput" | sed -n "${middleIndex}p" | awk '{print $1}')
echo "$middleHash"
}
# pruneToMain - deletes all local branches except for main
function pruneToMain() {
git branch | grep -v ^main$ | xargs git branch -D
}
function develop() {
git checkout develop
}
function main() {
git checkout main
}
# endregion : git commands #
# useful only if you are a. using nvm and b. work on multiple projects that use diff node vers
# set node version by directory
function setNodeByDir() {
currDir=${PWD}
case "$currDir" in
~/Desktop/working/project-a*)
nvm use v16.13.1
;;
~/Desktop/working/project-b*)
nvm use v14.16.1
;;
~/Desktop/working/project-c*)
nvm use node
;;
~/Desktop/working/project-d*)
nvm use v17.0.0
;;
*)
nvm use node
esac
}
# call it whenever .bash_profile is sourced
setNodeByDir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment