diff --git a/git-recall b/git-recall index 49d9584..89e6013 100644 --- a/git-recall +++ b/git-recall @@ -28,17 +28,29 @@ LESSKEY=false SED_BIN="" # Sed option to use according OS. VERSION="1.1.10" + +# Set keys. +au="`echo -e '\e[A'`" # arrow up +ad="`echo -e '\e[B'`" # arrow down +ec="`echo -e '\e'`" # escape +nl="`echo -e '\n'`" # newline +au_1="k" # arrow up +ad_1="j" # arrow down +nl_1="e" # expand + + # Are we in a git repo? if [[ ! -d ".git" ]] && ! git rev-parse --git-dir &>/dev/null; then echo "abort: not a git repository." 1>&2 exit 1 fi + # Parse options while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in -v | --version ) - echo "$version" + echo "$VERSION" exit ;; -d | --date ) @@ -81,6 +93,7 @@ function uncolored() { REVERSE="" } + # Enable colors if supported by terminal. if [[ -t 1 ]] && [[ -n "$TERM" ]] && which tput &>/dev/null && tput colors &>/dev/null; then ncolors=$(tput colors) @@ -93,11 +106,13 @@ else uncolored fi + # Check if lesskey is installed. if command -v lesskey &> /dev/null; then LESSKEY=true fi + # Set AUTHOR to current user if no param passed or display for all users if param equal to "all". if [[ ! -n $AUTHOR ]]; then AUTHOR=$(git config user.name 2>/dev/null) @@ -105,6 +120,7 @@ elif [[ $AUTHOR = "all" ]]; then AUTHOR=".*" fi + # Fetch changes before. if [[ $FETCH == true ]]; then echo "${GREEN}Fetching changes...${NORMAL}" @@ -113,6 +129,7 @@ if [[ $FETCH == true ]]; then tput ed # clear screen fi + # Log template. GIT_FORMAT="%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset" @@ -121,6 +138,7 @@ GIT_LOG="git log --pretty=format:'${GIT_FORMAT}' --author \"$AUTHOR\" --since \"$SINCE\" --abbrev-commit" + # Change temporary the IFS to store GIT_LOG's output into an array. IFS=$'\n' COMMITS=($(eval ${GIT_LOG} 2>/dev/null)) @@ -132,6 +150,7 @@ CN=$(tput cols) # Screen's number of columns. TN=$(( $NI < $((SN -1)) ? $NI : $((SN -1)))) # Number of lines that we will display. OFFSET=0 #Incremented by one each time a commit's length is higher than teminal width. + # If there is no items, exit. if [[ $NI = 0 ]]; then if [[ $AUTHOR = ".*" ]]; then @@ -141,12 +160,14 @@ if [[ $NI = 0 ]]; then fi fi + # Set correct sed option according OS's type case "$OSTYPE" in darwin*) SED_BIN="sed -E" ;; *) SED_BIN="sed -r" ;; esac + # Create array with uncolred commits (removing escape sequences using sed) for elt in "${COMMITS[@]}" do @@ -162,20 +183,13 @@ do fi done -# Set keys. -au="`echo -e '\e[A'`" # arrow up -au_1="k" # arrow up -ad="`echo -e '\e[B'`" # arrow down -ad_1="j" # arrow down -ec="`echo -e '\e'`" # escape -nl="`echo -e '\n'`" # newline -nl_1="e" # expand # Create a temporary lesskey file to change the keybindings so the user can use the TAB key to quit less. (more convenient) if [[ $LESSKEY = true ]]; then echo "\t quit" | lesskey -o $HOME/.lsh_less_keys_tmp -- - &> /dev/null fi + ## Get commit's diff. function get_diff() { ELT="$(echo "${COMMITS_UNCOL[$CP-1]}")" @@ -188,6 +202,7 @@ function get_diff() { DIFF_LINES_NUMBER=$(( DIFF_LINES_NUMBER + off )) } + ## This function will print the diff according the commit's tip. If the diff is too long, the result will be displayed using 'less'. function print_diff() { get_diff # get commit's diff @@ -227,6 +242,7 @@ function print_diff() { fi } + # Calculate OFFSET to avoid bad redraw. function calculate_offset { tmp=1 @@ -243,6 +259,7 @@ function calculate_offset { done } + { # capture stdout to stderr tput civis # hide cursor. @@ -251,12 +268,11 @@ SI=1 # index END=false # end while loop EXT=0 # Used to extend the number of lines to display. -## Loops, reads inputs and prints commits until user presses 'q' to exit or TAB to show the diff. while ! $END do - END_INDEX=0 # Index for the last item to display # When the number of item is higher than screen's number of lines, OFFSET_2 is recalculated each time we select a new item # Set last index to print. (based on OFFSET) + END_INDEX=0 # Index for the last item to display if [[ $TN == $NI ]]; then END_INDEX=$TN OFFSET_2=$OFFSET @@ -297,7 +313,6 @@ do [[ $TN == $(( SN - 1 )) ]] && tput cuu $(( TN + EXT )) [[ $SI != 1 ]] && tput ed # clear screen ;; - "$ad" | "$ad_1") CP=$(( CP + 1 )) [[ $CP == $(( NI + 1 )) ]] && CP=1 && SI=1 @@ -308,13 +323,11 @@ do [[ $SI != 1 ]] && tput ed # clear screen [[ $SI = 1 ]] && [[ $CP = 1 ]] && tput ed # clear screen ;; - "$nl" | "$nl_1") [[ $TN == $NI ]] && tput cuu $(( TN + OFFSET_2 )) [[ $TN != $NI ]] && tput cuu $(( TN + EXT )) print_diff ;; - "q") si=false END=true