Skip to content

Commit

Permalink
Add initial implementation of elegant git
Browse files Browse the repository at this point in the history
  • Loading branch information
extsoft committed Aug 21, 2017
1 parent 5b7fc66 commit d9b2383
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 1 deletion.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
# elegant-git
elegant-git
===========
Allows easy handle git tasks.

[![Build Status](https://travis-ci.org/extsoft/elegant-git.svg?branch=master)](https://travis-ci.org/extsoft/elegant-git)
[![PDD status](http://www.0pdd.com/svg?name=extsoft/elegant-git)](http://www.0pdd.com/p?name=extsoft/elegant-git)

[![Rultor.com](http://www.rultor.com/b/extsoft/elegant-git)](http://www.rultor.com/p/extsoft/elegant-git)

Usage
=====
```bash
$ git elegant
feature
pull
push
push-after-rebase
rebase
init
clone
add
clear-local
```
162 changes: 162 additions & 0 deletions src/main/git-elegant
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#!/bin/bash -e

__red=`tput setaf 1`
__green=`tput setaf 2`
__magenta=`tput setaf 5`
__reset=`tput sgr0`

__gm() {
echo "${__green}$@${__reset}"
}

__rm() {
echo "${__red}$@${__reset}"
}

__mmn() {
echo -n "${__magenta}$@${__reset}"
}

MASTER="master"
RMASTER="origin/master"

__config=(
'user.name'
'user.email'
)

commands() {
echo "feature"
echo "pull"
echo "push"
echo "push-after-rebase"
echo "rebase"
echo "init"
echo "clone"
echo "add"
echo "clear-local"
}

_validate() {
if [ -z "$1" ]; then
__rm "'$2' is not set"
exit -1
fi
}

__branches() {
local branch_command="$1"; shift
local b=$(eval "$branch_command" | sed -e 's|[* ]||g')
echo ${b[@]}
}

__loop_ask() {
local c="$1"; shift
local m="$1"; shift
[ -z "$1" ] && return 0
for i in $@; do
__mmn "$m [$i] "
read answer
if [ -z "$answer" ]; then
eval "$c $i"
fi
done
}

__loop() {
local c="$1"; shift
[ -z "$1" ] && return 0
for i in $@; do
eval "$c $i"
done
}

__batch() {
local MM="$1"; shift
local AM="$1"; shift
local CM="$1"; shift
__mmn "$MM "
read answer
if [ -z "$answer" ]; then
__loop "$CM" $@
else
__loop_ask "$CM" "$AM" $@
fi
}

pull() {
_validate "$1" "Specify branch name"
git checkout "$1"
git fetch --tags
git pull
}

push() {
local BRANCH=$(__branches 'git branch | grep \*')
git push -u origin $BRANCH:$BRANCH
}

rebase() {
git fetch --tags
git rebase $RMASTER
}

push-after-rebase() {
rebase
push
}

feature() {
_validate "$1" "Specify feature name"
pull $MASTER
git checkout -b "$1"
}

_rewrite_config() {
for line in "${__config[@]}"
do
local BASE=$(git config --global "$line")
__mmn "$line [$BASE]: "
read value
local VAR_VALUE=${value:-$BASE}
git config --local $line "$VAR_VALUE"
done
}

init() {
git init
_rewrite_config
}

clone() {
_validate "$1" "Specify URL to clone"
git clone "$1"
cd $(basename -s .git $1)
_rewrite_config
}

add() {
local FILES=$(git ls-files -m)
__gm "There are candidates to be added to commit:"
__loop "__gm -" ${FILES[@]}
__batch "Do you want to add all of them?" "Add this?" "git add" ${FILES[@]}
git status
}

clear-local() {
pull $MASTER
local cmd="git branch -lvv | grep gone | awk {'print \$1'}"
__loop "git branch -d" $(eval "$cmd") || \
(
__gm "There are unmerged branches:" && \
__loop "__gm -" $(eval "$cmd") && \
__batch "Do you want to delete all unmerged branches?" "Delete this?" "git branch -D" $(eval "$cmd")
)
}


FUNC=$1
[ -z "$FUNC" ] && commands && exit -1
shift
eval '$FUNC $@'

22 changes: 22 additions & 0 deletions src/main/git-elegant-completion
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

_git_elegant() {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"

case "${prev}" in
pull)
local data=$(git branch | awk -F ' +' '! /\(no branch\)/ {print $2}')
COMPREPLY=( $(compgen -W "${data}" ${cur}) )
return 0 ;;
push)
COMPREPLY=( $(compgen -W "$(git branch | grep \* | cut -d ' ' -f2)" ${cur}) )
return 0 ;;
*) ;;
esac

opts=($(git elegant commands))
COMPREPLY=( $(compgen -W "${opts[*]}" -- ${cur}) )
}

0 comments on commit d9b2383

Please sign in to comment.