Skip to content

Instantly share code, notes, and snippets.

@khromalabs
Last active May 31, 2020 15:54
Show Gist options
  • Save khromalabs/30c56bfbdab50ffef275236b87798221 to your computer and use it in GitHub Desktop.
Save khromalabs/30c56bfbdab50ffef275236b87798221 to your computer and use it in GitHub Desktop.
Script en bash para automatizar mi trabajo diario en Git (texto en español)
#!/bin/bash
ROOT=/var/www/
SHOW_LAST_COMMITS=2
TOP_LEVEL=""
READ_PRE=">"
ECHO_PRE=" "
function process_project {
CHANGES_NEW_FILES=$(git status --porcelain 2>/dev/null | egrep ^\\?\\?| cat)
if [ "$CHANGES_NEW_FILES" != "" ]; then
echo -e "$ECHO_PRE Nuevo(s) fichero(s):\n$CHANGES_NEW_FILES"
read -p "$READ_PRE ¿Añadir nuevos ficheros? (s/N) " NEW_FILES
if [ "$NEW_FILES" = "s" -o "$NEW_FILES" = "S" ]; then
git add .
fi
fi
CHANGES_OTHERS=$(git status --porcelain 2>/dev/null | egrep -v ^\\?\\? | cat)
if [ "$CHANGES_OTHERS" != "" ]; then
if [ "$SHOW_LAST_COMMITS" -gt 0 ]; then
echo "$ECHO_PRE Ultimo(s) commits:"
git log -n $SHOW_LAST_COMMITS --abbrev-commit --date=relative --graph --pretty=format:"%Cred%h%Creset — %s %Cgreen(%cr)%Creset"
fi
echo -e "$ECHO_PRE Cambios:\n${CHANGES_OTHERS}"
if [ "$CHANGES_OTHERS" != "" ]; then
read -p "$READ_PRE ¿Revisar diferencias? (s/N) " REVIEW
if [ "$REVIEW" = "s" -o "$REVIEW" = "S" ]; then
git diff HEAD~0 --minimal
fi
read -p "$READ_PRE Mensaje de commit [WIP]: " COMMIT
COMMIT=${COMMIT:-WIP}
git commit -a -m "$COMMIT"
fi
else
echo "$ECHO_PRE No hay cambios"
fi
UNPUSHED=$(git log --branches --not --remotes --oneline | wc -l)
REMOTE=$(git config --get remote.origin.url)
PUSHED=""
if [ "$UNPUSHED" != "0" -a "$REMOTE" != "" ]; then
read -p "$READ_PRE Hay $UNPUSHED commits locales. ¿Hacer push a servidor? (s/N) " PUSH
if [ "$PUSH" = "s" -o "$PUSH" = "S" ]; then
CHECK_UPSTREAM_CMD="git for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD) origin/mainline"
UPSTREAM_BRANCH=$($CHECK_UPSTREAM_CMD)
if [ "$UPSTREAM_BRANCH" = "''" ]; then
read -p "$READ_PRE La rama no tiene copia remota. ¿Subir a 'origin'? (s/N) " UPSTREAM_ORIGIN
if [ "$UPSTREAM_ORIGIN" = "s" -o "$UPSTREAM_ORIGIN" = "S" ]; then
git push -u origin HEAD
fi
else
git push
fi
PUSHED="1"
fi
else
echo "$ECHO_PRE No hay commits locales"
fi
echo ""
}
echo ""
if [ "$#" -lt 1 ]; then
TOP_LEVEL=$(git rev-parse --show-toplevel 2>/dev/null)
if [ -z "$TOP_LEVEL" ]; then
echo -e "\ngcp: Git-Commit-Push\n"
echo -e "Se esperaba estar en proyecto o parámetros: <proyecto1> [ .. <proyectoN> ]"
echo -e "Directorio raíz definido para proyectos: $ROOT\n"
exit
fi
fi
set -eu
pushd . 1>/dev/null
if [ "$TOP_LEVEL" = "" ]; then
cd $ROOT
PROJECTS=("$@")
LEN=${#PROJECTS[@]}
for (( i = 0; i<${LEN}; i++ ));
do
if [[ -d "$ROOT${PROJECTS[$i]}" ]]; then
echo "Procesando $ROOT${PROJECTS[$i]}"
cd $ROOT${PROJECTS[$i]}
process_project
else
echo -e "El directorio $ROOT${PROJECTS[$i]} no existe!\n"
fi
done
else
cd $TOP_LEVEL
echo "Procesando $TOP_LEVEL"
process_project
fi
echo -e "Hecho\n"
popd 1>/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment