Skip to content

Instantly share code, notes, and snippets.

@zarac
Created September 2, 2017 19:00
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 zarac/32fb3cfad4463655b1306a844f5d093d to your computer and use it in GitHub Desktop.
Save zarac/32fb3cfad4463655b1306a844f5d093d to your computer and use it in GitHub Desktop.
#!/bin/env bash
#
# a tool to help with source control
# only does git, but could/should handle more (hg, svn, ..)
#
# TODO normalize output (of find)
#
# // zarac
COLDEF="\033[0m" # default
BLACK="\033[0;30m"
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[0;33m"
BLUE="\033[0;34m"
MAGENTA="\033[0;35m"
CYAN="\033[0;36m"
WHITE="\033[0;37m"
GRAY="\033[0;90m"
CURSIVE="\033[0;3m"
UNDERLINE="\033[0;4m"
REVERSE="\033[0;7m"
COLOR_GOOD=$GREEN
COLOR_AHEAD=$YELLOW
COLOR_DIRTY=$RED
COLOR_DIRTY_AHEAD=$MAGENTA
COLOR_NO_REMOTE=$CYAN
COLOR_NO_GIT=$WHITE
function aur_find() {
curl "https://aur.archlinux.org/rpc.php?type=search&arg=$query" 2>/dev/null | jshon -e results -a -e URL -u -p -e Description -u
}
function github_find() {
curl "https://api.github.com/search/repositories?q=$query" 2>/dev/null | jshon -e items -a -e html_url -u -p -e description -u
#curl "https://api.github.com/search/repositories?q=$query" 2>/dev/null | jshon -e items -a -e html_url -u -p -e description -u -p -e forks -u -p -e watchers -u
}
function npm_find() {
curl "https://www.npmjs.com/search?q=$query" 2>/dev/null | xmllint --html --xpath '//h3/a/@href' - 2>/dev/null | sed -r 's/ ?href="package\/([^"]*)"/\1\n/g' | grep '/package' | sed -r 's| ?href="([^"]*)"|\1\n|g'
}
function sourceforge_get() {
mkdir $SRC_ROOT/$1.sourceforge &&
cd $SRC_ROOT/$1.sourceforge &&
# TODO is it always a .zip?
curl -L -o latest "https://sourceforge.net/projects/$1/files/latest/download" &&
file latest
}
function sourceforge_find() {
curl -L "https://sourceforge.net/directory/?q=$query" 2>/dev/null | xmllint --html --xpath '//li[@itemtype="http://schema.org/MobileSoftwareApplication"]/a/@href' - 2>/dev/null | sed -r 's| ?href="/projects/([^/]*)/[^ ]*|https://sourceforge.net/projects/\1\n|g'
}
if !(set | grep -q '^SRC_ROOT=');then # why this complication? ; )
SRC_ROOT=$HOME/src
fi
if [ -z $SRC_FIND ] ;then
SRC_FIND="aur github npm sourceforge"
fi
SRC_LOG=$SRC_ROOT/.src.log
cmd=$1 ; shift
start_time=`date +%s`
if [[ $cmd == 'commit' || $cmd == 'c' ]]; then
if [ ! $1 ] ; then
paths=`ls $SRC_ROOT`
else
paths=$@
fi
for f in $paths; do
echo $f
cd $SRC_ROOT/$f
git commit --interactive
done
elif [[ $cmd == 'diff' || $cmd == 'd' ]]; then
for f in `ls $SRC_ROOT`; do
echo -e "$CYAN $f$COLDEF"
cd $SRC_ROOT/$f
PAGER="" git diff
done
elif [[ $cmd == 'find' || $cmd == 'f' ]]; then
query=$1 ; shift
for source in $SRC_FIND; do
echo -e "\n $source\n"
${source}_find $query
done
elif [[ $cmd == 'get' || $cmd == 'g' ]]; then
upstream=$1 ; shift
path=$1 ; shift
destination=$1 ; shift
echo $(${upstream}_get $path)
if [[ $upstream == 'aur' || $upstream == 'a' ]]; then
name=$path
url=https://aur.archlinux.org/${path}.git
elif [[ $upstream == 'github' || $upstream == 'g' ]]; then
name=`echo $path | sed -r 's/([^/]+)\/(.*)/\2.\1/'`
url=https://github.com/${path}.git
else
echo -e "${RED}say wut? what upstream is that?${COLDEF}"
exit 1
fi
if [[ -z $destination ]]; then
destination=$SRC_ROOT/${name}.$upstream
fi
echo $start_time get $upstream from $url to $destination >> $SRC_LOG
git clone "$url" $destination
elif [[ $cmd == 'log' || $cmd == 'l' ]]; then
cat $SRC_LOG
elif [[ $cmd == 'push' || $cmd == 'p' ]]; then
if [ ! $1 ] ; then
paths=`ls $SRC_ROOT`
else
paths=$@
fi
for f in $paths; do
cd $SRC_ROOT/$f
status=$(git status --porcelain 2>&1)
statusCode=$?
if [[ $statusCode == 0 ]]; then
ahead=$(git branch -avv | grep ahead)
remotes=$(git remote | tr '\n' ' ')
if [[ $ahead && ! -z $remotes ]]; then
echo -e "$CYAN $f$COLDEF"
git branch -avv
git push
fi
fi
done
elif [[ $cmd == 'status' || $cmd == 's' ]]; then
for f in `ls $SRC_ROOT`; do
cd $SRC_ROOT/$f
# TODO keep fd2
status=$(git status --porcelain 2>&1)
statusCode=$?
if [[ $statusCode == 0 ]]; then
remotes=$(git remote | tr '\n' ' ')
ahead=$(git branch -avv | grep ahead)
COLOR=$COLOR_GOOD
if [[ $status && $ahead ]]; then
COLOR=$COLOR_DIRTY_AHEAD
elif [[ $status ]]; then
COLOR=$COLOR_DIRTY
elif [[ $ahead ]]; then
COLOR=$COLOR_AHEAD
elif [[ -z $remotes ]]; then
COLOR=$COLOR_NO_REMOTE
fi
echo -en "$GRAY- $COLOR$f$COLDEF"
if [[ $remotes ]]; then
echo -en "$GRAY $remotes$COLDEF"
fi
if [[ $ahead ]]; then
echo -en "$COLOR_AHEAD $ahead$COLDEF"
fi
if [[ $status ]]; then
echo -en "\n$status"
fi
echo ""
else
echo -e "$COLOR_NO_GIT $f$COLDEF"
fi
done
elif [[ $cmd == 'status-short' || $cmd == 'ss' ]]; then
for f in `ls $SRC_ROOT`; do
cd $SRC_ROOT/$f
# TODO keep fd2
status=$(git status --porcelain 2>&1)
statusCode=$?
COLOR=$COLOR_GOOD
if [[ $statusCode == 0 ]]; then
remotes=$(git remote | tr '\n' ' ')
ahead=$(git branch -avv | grep ahead)
COLOR=$COLOR_GOOD
if [[ $status && $ahead ]]; then
COLOR=$COLOR_DIRTY_AHEAD
elif [[ $status ]]; then
COLOR=$COLOR_DIRTY
elif [[ $ahead ]]; then
COLOR=$COLOR_AHEAD
elif [[ -z $remotes ]]; then
COLOR=$COLOR_NO_REMOTE
fi
else
COLOR=$COLOR_NO_GIT
fi
echo -en " $COLOR$f$COLDEF"
done
echo ""
elif [[ $cmd == 'status-short-list' || $cmd == 'sl' ]]; then
for f in `ls $SRC_ROOT`; do
cd $SRC_ROOT/$f
# TODO keep fd2
status=$(git status --porcelain 2>&1)
statusCode=$?
if [[ $statusCode == 0 ]]; then
remotes=$(git remote | tr '\n' ' ')
ahead=$(git branch -avv | grep ahead)
COLOR=$COLOR_GOOD
if [[ $status && $ahead ]]; then
COLOR=$COLOR_DIRTY_AHEAD
elif [[ $status ]]; then
COLOR=$COLOR_DIRTY
elif [[ $ahead ]]; then
COLOR=$COLOR_AHEAD
elif [[ -z $remotes ]]; then
COLOR=$COLOR_NO_REMOTE
fi
else
COLOR=$COLOR_NO_GIT
fi
echo -e " $COLOR$f$COLDEF"
done
else
echo -e " synopsis : (underlined means alias, e.g. \`src ss\` means src ${UNDERLINE}s${COLDEF}tatus-${UNDERLINE}s${COLDEF}hort)
src ${UNDERLINE}c${COLDEF}ommit
src ${UNDERLINE}d${COLDEF}iff
src ${UNDERLINE}f${COLDEF}ind git
src ${UNDERLINE}g${COLDEF}et github git/git [some/dir]
src ${UNDERLINE}l${COLDEF}og
src ${UNDERLINE}p${COLDEF}ush
src ${UNDERLINE}s${COLDEF}tatus
src ${UNDERLINE}s${COLDEF}tatus-${UNDERLINE}s${COLDEF}hort
src ${UNDERLINE}s${COLDEF}tatus-short-${UNDERLINE}l${COLDEF}ist
src ${UNDERLINE}u${COLDEF}pstreams - NOT IMPLEMENTED!
colors meaning :
${COLOR_NO_GIT}no git${COLDEF} - no git repository!
${COLOR_DIRTY_AHEAD}dirty and ahead${COLDEF} - stuff uncommitted and commits not pushed
${COLOR_DIRTY}dirty${COLDEF} - stuff uncommitted
${COLOR_AHEAD}ahead${COLDEF} - everything committed but not pushed
${COLOR_NO_REMOTE}no remote${COLDEF} - all committed but no remote
${COLOR_GOOD}good${COLDEF} - everything committed and pushed
configuration :
\$SRC_ROOT=$SRC_ROOT
\$SRC_FIND=$SRC_FIND
\$SRC_LOG=$SRC_LOG
want to know more? read the source :
$EDITOR \`which src\`"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment