Skip to content

Instantly share code, notes, and snippets.

@gullz
Created July 5, 2019 07:33
Show Gist options
  • Save gullz/51abe21274655523df448b828a593318 to your computer and use it in GitHub Desktop.
Save gullz/51abe21274655523df448b828a593318 to your computer and use it in GitHub Desktop.
Add directories into git repos recursively, many git repos.
#! /bin/bash
#!/usr/bin/expect -f
level=0
path=""
createRepo()
{
repo_name=$1
test -z $repo_name && echo "Repo name required." 1>&2 && exit 1
echo $repo_name
}
checkDir()
{
if [ $level -eq 3 ]
then
return
fi
dir=$1
if [ $dir == "." ]
then
return
fi
if [ $dir == ".." ]
then
return
fi
if [ $dir == ".git" ]
then
return
fi
cd $dir > /dev/null 2>&1
result=$?
curDir=`pwd`
if [ $result -eq 0 ]
then
path="$path"_"$dir"
local localPath=$path
level=`expr $level + 1`
ls .git > /dev/null 2>&1
result=$?
dirSize=`du -sh .`
echo $path is a directory size=$dirSize in level=$level
navigateDir
else
return
fi
if [ $result -eq 0 ]
then
echo $curDir is a git repository
else
if [ $level -eq 1 ]
then
createRepo $localPath
git init > /dev/null 2>&1
git add . > /dev/null 2>&1
git commit -m "\"adding all files inside $dir\"" > /dev/null 2>&1
else
createRepo $path
git submodule init # > /dev/null 2>&1
git add . > /dev/null 2>&1
git commit -m "\"adding all files inside $dir\"" > /dev/null 2>&1
fi
fi
cd ..
level=`expr $level - 1`
}
navigateDir()
{
files=`ls`
for dir in $files
do
checkDir $dir
done
}
navigateDir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment