Skip to content

Instantly share code, notes, and snippets.

@chuhlomin
Created July 10, 2020 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuhlomin/93770d500e8d25c3fe604d80f3a9abe3 to your computer and use it in GitHub Desktop.
Save chuhlomin/93770d500e8d25c3fe604d80f3a9abe3 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# group_by_owner script reads directory (passed as a first agrument),
# finds any Git-repositories in subdirectories,
# parses first the line from `git remote -v` for each repositry,
# moves repository to ~/Projects/<owner>/<repo>
#
# ⚠️ Can work incorrectly for repositories with many "remotes"
# (or without any "remotes").
#
# Usage:
# sh group_by_owner.sh ~/IdeaProjects
# sh group_by_owner.sh ~/GolandProjects
# sh group_by_owner.sh ~/PycharmProjects
#
# License: GPL v3.0 https://opensource.org/licenses/GPL-3.0
# Author: Konstantin Chukhlomin
# Version: 1.0
# Target directory:
PROJECTS_DIR=~/Projects
SOURCE_DIR=$1
echo "==> [INFO] Starting";
SCANNED=0
FOUND=0
MOVED=0
FAILED=()
for DIRECTORY in $(ls -d $SOURCE_DIR/*/)
do
echo "==> [INFO] cd $DIRECTORY";
cd $DIRECTORY;
if [ -d ".git" ]; then
((FOUND+=1))
export REMOTE_MULTILINE=$(git remote -v);
read -r REMOTE_FIRST_LINE <<< "$REMOTE_MULTILINE";
# origin git@github.com:be5invis/Iosevka.git (fetch)
[[ "${REMOTE_FIRST_LINE}" =~ git@github.com:([a-z0-9]+)\/ ]];
OWNER=${BASH_REMATCH[1]};
# be5invis
if [ ! -z "$OWNER" ]; then
echo "==> [INFO] mv ${DIRECTORY} $PROJECTS_DIR/$OWNER";
mkdir -p $PROJECTS_DIR/$OWNER;
mv $DIRECTORY $PROJECTS_DIR/$OWNER;
if [ $? -eq 0 ]; then
((MOVED+=1))
else
FAILED+=($DIRECTORY)
fi
else
echo "==> [WARN] Failed to detect owner for $REMOTE_FIRST_LINE";
fi
else
echo "==> [INFO] skipping $DIRECTORY – not a Git repository";
fi
((SCANNED+=1))
done
echo "==> [INFO] Stopped."
echo "Directories scanned: $SCANNED"
echo "Repositories found: $FOUND"
echo "Repositories moved: $MOVED"
echo "Failed to move:"
printf '%s\n' "${FAILED[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment