Skip to content

Instantly share code, notes, and snippets.

@armenr
Created March 16, 2021 20:50
Show Gist options
  • Save armenr/c464805a69a70e4f6ba76f73b3451959 to your computer and use it in GitHub Desktop.
Save armenr/c464805a69a70e4f6ba76f73b3451959 to your computer and use it in GitHub Desktop.
Amplitude_SwitchEnv
#! /bin/bash
set -e
## Step Zero
## Checks to see if we've checked out to a newly-created branch & if we have, auto-commits an empty "first" commit
##
## Rationale: Due to the way Git maintains refs to hashes for HEAD (even when moving between branches of different names)
## we need to first always add a default/empty commit on new branch checkout. It's also nice because it orients you.
##
## Don't believe me?
## Try this out: 1. Create an empty folder
## 2. git init
## 3. git checkout -b my_new_branch
## 4. git branch
## 5. Surprised? There is no output! That's because git isn't aware of the fact you're in a new branch
## as the HEAD ref isn't actually pointing at ANYTHING! :)
## Caveat: We don't want this to happen in the case of "protected" branches:
## ----> staging/qa, production/prod, main/master, dev/development
## The post-merge hook runs after a successful merge command. You can use it to restore data in the working tree that
## Git can’t track, ## such as permissions data. This hook can likewise validate the presence of files external to Git
## control that you may want copied in when the working tree changes.
# Git passes these to the hook as arguments, so we use them.
from_branch_hash=$1
to_branch_hash=$2
checkout_type=$3
# These are
root_branches=("development" "dev" "staging" "qa" "production" "prod" "main" "master")
root_branches_no_main=("development" "dev" "staging" "qa" "production" "prod")
# These are useful variables for working with branches
project_root_dir=$(git rev-parse --show-toplevel)
from_branch_name=$(git reflog | awk 'NR==1{ print $6; exit }')
to_branch_name=$(git reflog | awk 'NR==1{ print $8; exit }')
from_branch_prefix=$(echo $from_branch_name | cut -d- -f1)
to_branch_prefix=$(echo $to_branch_name | cut -d- -f1)
# These are branch suffixes
# TODO: This needs to be fxed
# Currently, if there is no "-" in the string, it returns the prefix (e.g. "staging") as the suffix as well
to_branch_suffix=$(echo "$to_branch_name" | cut -d "-" -f2)
from_branch_suffix=$(echo "$from_branch_name" | cut -d "-" -f2)
# Checks to see if an array contains a string parameter
array_contains() {
local seeking=$1
shift
local in=1
for element; do
if [[ $element == "$seeking" ]]; then
in=0
break
fi
done
return $in
}
containsElement() {
local e match="$1"
shift
for e; do [[ "$e" == "$match" ]] && return 0; done
return 1
}
create_initial_commit() {
git commit --allow-empty -m "Created branch $1 from $2"
}
get_branch_history() {
local in=0
if [[ $1 == "master" ]] || [[ $1 == "main" ]]; then
echo "master always has history!"
in=1
return $in
fi
if [[ $(git log $1 --not $(git for-each-ref --format='%(refname)' refs/heads/ | grep -v "refs/heads/$1")) ]]; then
return $in
else
in=1
return $in
fi
}
if [[ "$from_branch_name" == "$to_branch_name" ]]; then exit; fi
if [[ $checkout_type == 1 ]] && [[ "$from_branch_hash" == "$to_branch_hash" ]] && [[ ! $(get_branch_history "$to_branch_name") ]]; then
if [[ "$to_branch_name" != *"-"* ]] && containsElement "$to_branch_name" "${root_branches[@]}"; then
echo "Creating new ROOT BRANCH!"
create_initial_commit "$to_branch_name" "$from_branch_name"
fi
if [[ "$to_branch_name" == *"-"* ]] && containsElement "$to_branch_prefix" "${root_branches[@]}"; then
echo "Complies with naming convention!"
create_initial_commit "$to_branch_name" "$from_branch_name"
fi
if [[ "$to_branch_name" != *"-"* ]] && ! containsElement "$to_branch_name" "${root_branches[@]}" &&
! containsElement "$to_branch_prefix" "${root_branches[@]}"; then
echo "[DENIED] Branch naming does not comply with repository convention!"
echo "[ACTION] Checking you back into previous branch..."
git checkout "$from_branch_name"
echo "[CLEANUP] Removing invalid branch..."
git branch -D "$to_branch_name"
echo "[NOTICE] Please use a valid prefix-formatted branch name, and try again..."
fi
fi
if [[ -d "$project_root_dir/amplify" && "$(ls -A "$project_root_dir/amplify")" && "$to_branch_name" == 'master' || "$to_branch_name" == 'main' ]]; then
echo "[PROTECTED] Folder named 'amplify' not permitted to exist in your $to_branch_name branch!"
rm -rf "$project_root_dir"/amplify."$from_branch_name" &&
cp -pRP "$project_root_dir"/amplify "$project_root_dir"/amplify."$from_branch_name" &&
rm -rf "$project_root_dir"/amplify
echo "If git reports untracked files or changes, you should CHECK AND COMMIT those first!"
exit
elif
[[ "$from_branch_name" == "master" || "$from_branch_name" == "main" ]] &&
[[ $(containsElement "$to_branch_name" "${root_branches_no_main[@]}") || $(containsElement "$to_branch_prefix" "${root_branches_no_main[@]}") ]]
then
echo "[NOTICE] Checking for existing amplify env of this branch..."
if [[ -e "$project_root_dir"/amplify ]]; then
echo "[NOTICE] Found existing, branch-specific amplify env!"
rm -rf "$project_root_dir"/amplify
fi
if [[ -e "$project_root_dir"/amplify."$to_branch_prefix" ]]; then
echo "[NOTICE] Found existing, branch-specific amplify env!"
cp -pRP "$project_root_dir"/amplify."$to_branch_prefix" "$project_root_dir"/amplify
fi
fi
. "$(dirname "$0")/husky.sh"
## Things to save and/or TODO:
## # Moves dangling amplify folder to its appropriate amplify.stage folder
## NOTE: DESTRUCTIVE! IT WILL OVERWRITE ANY EXISTING FILES!
# moveFilesWithOverwrite () {
# local existing_amplify_folder=$1
# local amplify_folder_destination=$2
# }
# Moves and nests dangling amplify folder to its corresponding amplify.stage folder
## NOTE: It will NEST the folder!
# moveFilesWithoutOverwriting () {
# local existing_amplify_folder=$1
# local amplify_folder_destination=$2
# }
#!/bin/sh
# husky
# Created by Husky v4.3.8 (https://github.com/typicode/husky#readme)
# At: 3/12/2021, 1:57:57 AM
# From: /Users/armenr/Desktop/stelth-labs/gruv-neos-alpha/arubani/node_modules/husky (https://github.com/typicode/husky#readme)
#!/bin/bash
set -e
# Get the current branch name
current_branch_name=$(git branch | grep "*" | sed "s/\* //")
project_root_dir=$(git rev-parse --show-toplevel)
# Get the name of the branch & branch prefix that was just merged
reflog_message=$(git reflog -1)
short_commit_hash=$(git rev-parse --short HEAD)
merged_branch_name=$(echo "$reflog_message" | cut -d" " -f 4 | sed "s/://")
merged_branch_prefix=$(echo "$merged_branch_name" | cut -d- -f1)
# if the merged branch was master - don't do anything
if [[ $merged_branch_name = "master" ]]; then
exit 0
fi
# Begin output
echo -e "[NOTIFY]\n You've just merged the branch \"$merged_branch_name\" into \"$current_branch_name\". "
if [[ -e "$project_root_dir"/amplify ]]; then
echo -e "[ALERT]\n Looks like you've merged an amplify folder from branch prefix $merged_branch_prefix!"
echo -e "[ACTION]\n Moving amplify folder from branch $merged_branch_prefix to its appropriate environment folder: amplify.$merged_branch_prefix!"
if [[ -e "$project_root_dir"/amplify ]]; then
rm -rf "$project_root_dir"/amplify."$merged_branch_prefix"
cp -pRP "$project_root_dir"/amplify "$project_root_dir"/amplify."$merged_branch_prefix"
rm -rf "$project_root_dir"/amplify
git add . && git commit -m "[Post-merge autohook]: Moved env $merged_branch_prefix amplify app (commit id: $short_commit_hash) to destination env folder."
fi
exit $?
fi
# # Ask the question
# read -p "Do you want to delete the \"$merged_branch_name\" branch? (y/N) " answer
# # Check if the answer is a single lowercase Y
# if [[ "$answer" == "y" ]]; then
# # Delete the local branch
# echo "Deleting local branch \"$merged_branch_name\""
# git branch -d $merged_branch_name
# # Delete the remote branch
# echo "Deleting remote branch"
# git push origin --delete $merged_branch_name
# exit 1
# else
# echo "Did not delete the \"$merged_branch_name\" branch"
# fi
. "$(dirname "$0")/husky.sh"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment