You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

181 lines
4.5 KiB

gcd_help() {
/bin/echo "Usage: to auto cd into a git project."
/bin/echo "By Default with no arguments -> list all git projects."
/bin/echo "\$ gcd PROJECT -> will cd into project folder and give git status."
/bin/echo "Commands: \$ gcd PROJECT [auto, pull, push, nostatus, addall]"
/bin/echo "NEW: \$ gcd-push -> to push an git repo dir, and \$ gcd-pop -> to pop last dir"
/bin/echo "[nopop] -> will not gpop before it does its gpush"
/bin/echo "\$ gcd-pop -> will go back to last folder used by gcd-push"
/bin/echo "To edit Projects List: \$ gcd editprojects"
}
gcd_branch() {
echo "You are on Git Branch: "
/usr/bin/git branch --list
}
GIT_DIR_STACK=()
git_dirs() {
if [ ${#GIT_DIR_STACK[@]} -eq 0 ]; then
echo "Empty Stack" 1>&2
return 1
fi
printf "Git Push Dirs: "
for s in "${GIT_DIR_STACK[@]}"; do
if [ -z "$1" ] || [ ! "${GIT_DIR_STACK[-1]}" == "$s" ]; then
printf "$s "
fi
done
echo -e "\n"
}
git_pushd() {
if [ ${#GIT_DIR_STACK[@]} -eq 0 ]; then
GIT_DIR_STACK+=( $(pwd) )
fi
if [ ! -z "$1" ]; then
local gitdirname=$1
local nopush=false
for s in "${GIT_DIR_STACK[@]}"; do
if [ "$s" == "$gitdirname" ]; then
nopush=true
break;
fi
done
if [ "$nopush" == false ]; then
GIT_DIR_STACK+=( "$gitdirname" )
else
echo "Already in Stack..."
fi
if [ -z "$2" ]; then
git_dirs here
fi
cd $gitdirname
fi
}
git_popd() {
if [[ ${GIT_DIR_STACK[@]} ]]; then
if [ ${#GIT_DIR_STACK[@]} -eq 2 ]; then
cd ${GIT_DIR_STACK[-2]}
echo "Stack now empty."
GIT_DIR_STACK=()
elif [ ${#GIT_DIR_STACK[@]} -gt 2 ]; then
cd ${GIT_DIR_STACK[-2]}
if [ -z "$1" ]; then
git_dirs here
fi
unset GIT_DIR_STACK[-1]
else
echo "Empty Stack" 1>&2
return 1
fi
else
echo "Empty Stack" 1>&2
return 1
fi
}
alias gcd-push='git_pushd'
alias gcd-pop='git_popd'
alias gcd-dirs='git_dirs'
gcd_x() {
if [ -z "$2" ]; then
git_popd 2> /dev/null > /dev/null
git_pushd "$1"
elif [ "$2" == "nopop" ]; then
git_pushd "$1"
else
cd "$1"
fi
}
gcd() {
#set -x
local git_projects=~/.gitprojects
if [ ! -r $git_projects ]; then
/opt/profiles/scripts/locate_gits.sh
if [ $? -eq 0 ]; then
mv /tmp/gitprojects $git_projects
else
echo "Opps!!"
exit 1
fi
fi
if [ -z "$1" ]; then
/bin/echo
elif [ $1 == "--help" ] || [ $1 == "-help" ] || [ $1 == "help" ] || [ $1 == "?" ]; then
gcd_help
return 0
elif [ $1 == "editprojects" ]; then
$EDITOR "$git_projects"
return 0
elif [ $1 == "me" ]; then
$EDITOR ${_ENV_PATH}git_cd.env
return 0
fi
IFSOLD=$IFS
IFS=',';
found="false"
while read -r label mycommand desc;
do
[[ $label =~ ^#.* ]] && continue
if [ -z "$1" ]; then
/bin/echo "gcd ${label} , PATH= ${mycommand}, DESC: ${desc}"
else
[[ "$label" == "$1" ]] && { found="true"; break; }
fi
done < "$git_projects"
IFS=$IFSOLD
if [ "$found" == "true" ] && [ ! -z $mycommand ]; then
if [ -z $2 ]; then
gcd_x "$mycommand"
/usr/bin/git status
elif [ $2 == "nopop" ]; then
gcd_x "$mycommand" nopop
/usr/bin/git status
elif [ $2 == "auto" ]; then
/opt/profiles/scripts/git/dogit "$mycommand"
gcd_x "$mycommand" $3
elif [ $2 == "pull" ]; then
/opt/profiles/scripts/git/gpull "$mycommand"
gcd_x "$mycommand" $3
elif [ $2 == "push" ]; then
/opt/profiles/scripts/git/gpull-and-push "$mycommand"
gcd_x "$mycommand" $3
elif [ $2 == "nostatus" ]; then
gcd_x "$mycommand" $3
elif [ $2 == "addall" ]; then
gcd_x "$mycommand" $3
/usr/bin/git add .
/usr/bin/git status
else
gcd_help
fi
fi
#set +x
}
gpcd() {
local gcmdlist=()
IFSOLD=$IFS
IFS=',';
while read -r label mycommand desc; do
[[ $label =~ ^#.* ]] && continue
if [ "$label" == "$1" ]; then
gcd $@
return 0
fi
gcmdlist+=("$label" "$desc")
done < ~/.gitprojects
IFS=$IFSOLD
command=$(dialog --ok-label "CD" --cancel-label "EXIT" --output-fd 1 \
--colors \
--menu "Select git project:" 0 0 0 "${gcmdlist[@]}")
clear
gcd $command $@
}