Skip to content

Instantly share code, notes, and snippets.

@pradyumnac
Last active December 1, 2022 16:20
Show Gist options
  • Save pradyumnac/25840aa2eb847551364883a820df7ddb to your computer and use it in GitHub Desktop.
Save pradyumnac/25840aa2eb847551364883a820df7ddb to your computer and use it in GitHub Desktop.
GIT helpers written in bash for root folder containing multiple git repos (subfolders)

Multi Operations on a root git repo folder

Install

Dependencies:

  • Gum
  • Bash shell

All git repos in a folder (to be worked on). Example: ~/repos.
Make sure to copy these scripts into this folder

Copy the following and update the folder path lin line 1 before running

ROOT_FOLDER="<folder_name>"
curl https://gist.githubusercontent.com/pradyumnac/25840aa2eb847551364883a820df7ddb/raw/e9cf0e79a1f3092a5fab3dc43fdee1c015d13c40/pullrepos.sh -o $ROOT_FOLDER/pullrepos.sh
curl https://gist.githubusercontent.com/pradyumnac/25840aa2eb847551364883a820df7ddb/raw/ec965681aaeeab972a65c743f111e60373403f59/pushrepos.sh -o $ROOT_FOLDER/pushrepos.sh
curl https://gist.githubusercontent.com/pradyumnac/25840aa2eb847551364883a820df7ddb/raw/ec965681aaeeab972a65c743f111e60373403f59/repostatus.sh -o $ROOT_FOLDER/repostatus.sh
chmod +x pullrepos.sh pushrepos.sh repostatus.sh

Status

Get all repos in folder where files are not stage/committed

./repostatus.sh

Push

Push all folders selected

This pull up an interactive list of repos to choose. you can choose multiple using tab and navigate using arrows

./pushrepos.sh

Pushes entered repos to default their remote branch

./pushrepos.sh -c <repo1> [<repo2>..]

Make sure that default branch is selected using git push -u

Pull

Push all folders selected

This pull up an interactive list of repos to choose. you can choose multiple using tab and navigate using arrows

./pullrepos.sh

Pulls entered repos from default their remote branch

./pullrepos.sh -c <repo1> [<repo2>..]
#!/usr/bin/env bash
gum style \
--foreground 12 --border-foreground 12 --border double \
--align center --width 50 --margin "1 2" --padding "2 4" \
'Multi GitRepo Puller' 'v0.0.1'
if [ ! -z "$1" ]
then
if [ $1 == "-c" ]
then
if [ -z {@:2} ]
then
echo "Error: Atleast 1 repo must be present"
echo "Suppliled: $@"
exit
else
REPO_LIST=$(echo ${@:2} | tr ' ' '\n')
fi
else
echo "Error: Invalid argument"
echo "Valid switch: -c, Passed: $1"
exit
fi
else
# REPO_LIST="env fish shellscripts nvim"
# REPO_LIST="env fish shellscripts nvim newsboat exports tmux rclone"
REPO_LIST=$(echo $(ls -d */|gum choose --limit 10)|tr ' ' '\n')
if [ -z "$REPO_LIST" ]; then echo "Aborting.." && exit 0; fi
fi
echo $REPO_LIST
while read repo
do
echo "Pulling " $repo
if [ -d "$repo" ]
then
cd $repo
git pull
cd ..
else
echo "Folder $repo not present"
fi
done < <(echo $REPO_LIST|tr ' ' '\n')
#!/usr/bin/env bash
gum style \
--foreground 12 --border-foreground 12 --border double \
--align center --width 50 --margin "1 2" --padding "2 4" \
'Multi Repo Pusher' 'v0.0.1'
if [ ! -z "$1" ]
then
if [ $1 == "-c" ]
then
if [ -z {@:2} ]
then
echo "Error: Atleast 1 repo must be present"
echo "Suppliled: $@"
exit
else
REPO_LIST=$(echo ${@:2} | tr ' ' '\n')
fi
else
echo "Error: Invalid argument"
echo "Valid switch: -c, Passed: $1"
exit
fi
else
# REPO_LIST="env fish shellscripts nvim"
# REPO_LIST="env fish shellscripts nvim newsboat exports tmux rclone"
REPO_LIST=$(echo $(ls -d */|gum choose --limit 10)|tr ' ' '\n')
if [ -z "$REPO_LIST" ]; then echo "Aborting.." && exit 0; fi
fi
while read repo
do
echo "Pushing " $repo
if [ -d $repo ]
then
cd $repo
DIFFTEXT=$(git diff HEAD)
if [ ! -z "$DIFFTEXT" ]
then
echo $DIFFTEXT
gum confirm "Commit changes?" && \
git add . && \
git commit -a -m "$(gum input --placeholder 'commit message')" && \
git push
else
echo "No changes!"
fi
cd ..
else
echo "Folder $repo not present"
fi
done < <(echo $REPO_LIST|tr ' ' '\n')
#!/usr/bin/env bash
{
for x in *;
do
# echo $x;
git --work-tree="$x" --git-dir="$x/.git" status|if grep -q "working tree clean"; then echo "✅ $x";else echo "❌ $x"; fi;
done;
} 2>/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment