-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-dir
28 lines (28 loc) · 1.63 KB
/
git-dir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash
# git-dir
# A useful bash script for running git commands on directories
if [[ $# -eq 0 ]] ; then # If no arguments are passed offer some guidance
printf "Usage: git-dir [git commands and options] [path]\n\n"
else
INIT_DIR="$PWD" # Store the current directory so we can return to it
GIT_ARGS=${@:1:$(($# - 1))} # Get all of the args except for the last one as git arguments
TARGET_PATH=${@:$#} # Get the last argument as the target path
cd "$TARGET_PATH" # change to the target path directory
if [ -d .git ]; then # If the current directory is a git repo then run the git command here
git $GIT_ARGS
else # If the current directory is not a repo, search for subdirs for repos
REPO_COUNT=$(ls -R -d */.git 2>/dev/null | wc -l)
if [ $REPO_COUNT -gt 0 ]; then # If we found repos
printf "Found $REPO_COUNT repositories:\n\n" # List the repos
ls -R -d */.git 2>/dev/null | sed 's/\/.git//' && echo ""
read -r -p "Run git $GIT_ARGS on these repos? [Y/n] " CONFIRM #prompt for confirmation
if [[ $CONFIRM =~ ^[Yy]$ || $CONFIRM == "" ]]; then # if Y, y, or [Enter] run git command on each repo
printf "\n"
ls -R -d */.git 2>/dev/null | sed 's/\/.git//' | xargs -P10 -I{} git -C {} $GIT_ARGS
fi
else
echo "No repositories found."
fi
fi
cd "$INIT_DIR" # return to the directory that we started in
fi