Created
February 4, 2022 20:51
-
-
Save ahmedsayedabdelsalam/1f7ac4ff57c9a7cc2d7d96aa01cc96f9 to your computer and use it in GitHub Desktop.
command to sync your repos between devices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
##################### | |
##### VARIABLES ##### | |
##################### | |
SITES_PATH=$SITES | |
SYNC_REPO='[email protected]:ahmedsayedabdelsalam/project-sync.git' | |
ORIGIN_NAME='sync' | |
VERSION='0.1.0' | |
##################### | |
##### FUNCTIONS ##### | |
##################### | |
help() | |
{ | |
cat <<HELP | |
Sync your projects between devices easily. | |
First add your sync repo link & origin name inside the script in variables section. | |
Syntax: $0 arg [-h|v] | |
arguments: | |
p push Push Projects. | |
l pull Pull Projects. | |
options: | |
-h --help Print this Help. | |
-v --version Print software version and exit. | |
HELP | |
} | |
add_remote_origin_if_not_exists() { | |
if [[ -z $(git remote show | grep $ORIGIN_NAME) ]]; then | |
git remote add $ORIGIN_NAME $SYNC_REPO | |
fi | |
} | |
confirm() { | |
# Read (-n 1) only one character (-r accept backslash as a character not an escepe) | |
# $REPLY is default variable for (read) if you don't supply a name like this: read -p "my prompt" -n 1 -r my_var | |
read -p "Are you sure? [Y,n]: " -n 1 -r | |
# If no answer (just enter) go for yes or add empty line | |
[[ -z $REPLY ]] && REPLY='y' || echo | |
# Ask again if the answer is not y or n case inseneitive | |
[[ ! $REPLY =~ ^[YyNn]$ ]] && confirm | |
# Return true if yes, false if no | |
[[ $REPLY =~ ^[Yy]$ ]] | |
} | |
push() { | |
# Loop on each .git path exists on My Sites dir | |
for d in `find $SITES_PATH -name ".git"`; do | |
# Go to main repo directory | |
cd $d/.. | |
current_path=$(pwd) | |
# Git status | |
status=$(git -c color.ui=always status -s) | |
# Skip clean directories | |
[[ -z $status ]] && continue | |
echo "$status" | |
# Skip if the user dont' want to sync thie repo | |
confirm || continue | |
add_remote_origin_if_not_exists | |
# Temp commit | |
git add . | |
git commit -m "SYNC" | |
# Push to sync repo | |
project_name=${current_path#"$SITES_PATH/"} # return the relative path after sites path ex: (php/test) | |
branch_name=$(git branch --show-current) | |
git push sync "$branch_name:$project_name@$branch_name" | |
# Undo Temp commit | |
git reset --soft HEAD^ | |
echo "$current_path" | |
done | |
} | |
################## | |
##### ROUTES ##### | |
################## | |
# Show the help and exit if no arguments exists | |
[[ -z "$1" ]] && help && exit | |
# Add you route here | |
while [[ -n "$1" ]]; do | |
case "$1" in | |
p|push) # push projects | |
push | |
exit;; | |
l|pull) # pull projects | |
echo "Implement Me" | |
exit;; | |
-h|--help) # display Help | |
help | |
exit;; | |
-v|--version) # display Version | |
echo "Version: $VERSION" | |
exit;; | |
--) # The double dash makes them parameters | |
shift | |
break;; | |
-*|--*) # incorrect option | |
echo "Option $1 not recognized" | |
exit;; | |
esac | |
shift | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment