Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to compare a git branch with a base branch, find the changed/added/removed CSS classes, and search the codebase for uses
#!/bin/bash
# Changed CSS Class Search
# ------------------------
# Script to compare a git branch with a base branch, find the changed/added/removed CSS classes, and search the codebase for uses
#
# Required params:
# --base - The base branch
# --compare - The branch to compare
# --extension - The file extension to search in
# --repo - The absolute path to the repository
#
# Example: ./changedCssClassSearch.sh --base=develop --compare=origin/feature --extension=html --repo=/home/user/git/repo/
#
# Dependent on having Element Finder installed: https://github.com/keeganstreet/element-finder
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
function searchCss {
# get the log | grep for changed classes | strip whitespace from the start of the lines | strip the opening bracket, plus and minus | strip @media etc | strip anything starting with digit | replace commas with newlines | strip bracketed fancyness e.g: [disabled="disabled"] | remove :before etc | sort list and make
out="$(git log -p --no-merges --left-only ${COMPARE}...${BASE} *.css | grep -o "^.*{" | sed 's/[[:space:]]//g' | sed 's|[{+-]||g' | sed '/^@/d' | sed '/^[0-9]/d' | sed 's/,/\n/g' | sed 's/\[[^][]*\]//g'| sed 's/:.*//' | sort -u)"
}
for i in "$@"; do
case $i in
-b=*|--base=*)
BASE="${i#*=}"
shift # past argument=value
;;
-c=*|--compare=*)
COMPARE="${i#*=}"
shift # past argument=value
;;
-e=*|--extension=*)
EXTENSION="${i#*=}"
shift # past argument=value
;;
-r=*|--repo=*)
REPO="${i#*=}"
shift # past argument=value
;;
*)
# unknown option
;;
esac
done
cd $REPO
printf "\n--------------------\n"
printf "${RED}Running search for ${NC} - ${GREEN}Comparing ${COMPARE} with ${BASE}${NC}"
printf "\n--------------------\n\n"
searchCss
echo "$out" | while IFS= read -r line; do
elout="$(elfinder --selector $line --extension ${EXTENSION})"
if [[ ${elout} != *"Found 0 matches in 0 files"* ]]
then
printf "${elout}"
printf "\n\n--------------------\n\n"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.