<commit_before>#! /bin/cat

. ~/jab/bin/first_dir.sh
. ~/jab/environ.d/colour.sh

# functons.sh for git

_git_status_char_regexp="[ MADRCU]"
_git_status_regexp="^${_git_status_char_regexp}${_git_status_char_regexp}"
# sorted by strcmp of function name

# x

# xx

ga () {
    [[ -n $GIT_ADDED ]] && GIT_ADDED="$@" || GIT_ADDED="$GIT_ADDED:""$@"
    if [[ -z "$*" ]]; then
        echo Nothing specified for add >&2
        echo 'Did you mean "gaa" (add all) ?'
        return 1
    else
        git add "$@"
    fi
}

gb () {
    if [[ -z "$*" ]]; then
        git branch 2>&1 | grep -v warning
    else
        git branch 2>&1 | grep -v warning | grep --color "$@";
    fi
}

gc () {
    local _storage=/tmp/gc.sh
    if [[ -z "$@" ]]; then git commit --verbose
    else
        python -c "print 'git commit -m\"$*\"'" > $_storage
        _show_run_storage
    fi
    GIT_ADDED=
}

gd () {
    git diff "$@"
}

gi () {
    git "$@"
}

gp () {
    if ! MSG=$(git push 2>&1); then
        if [[ $MSG =~ set-upstream ]]; then
            $(echo "$MSG" | grep set-upstream)
        else
            echo "$MSG" >&2
            return 1
        fi
    fi
}

gr () {
    git pull --rebase "$@"
}

gs () {
    shift_dir "$@" && shift
    _do_git_status $dir "$@"
}

tc () {
    vim ~/.gitconfig +
}

# xxx

aga () {
    git reset HEAD "$@"
}

gcg () {
    gc; glg
}

gcr () {
    gc; gr
}

gaa () {
    ga .
}

gac () {
    local addable=.
    if [[ -e "$1" ]]; then
        addable=$1
        shift
    fi
    ga $addable && gc "$@"
}

gai () {
    ga --patch "$@"
}

gba () {
    if [[ -z "$*" ]]; then
        git branch -a 2>&1 | grep -v warning
    else
        git branch -a 2>&1 | grep -v warning | grep --color "$@";
    fi
}

gbb () {
    git_branch "$@"
}

gbd () {
    git branch -d "$@"
}

gbD () {
    git branch -D "$@"
}

gbo () {
    local _origin=$(git for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD))
    if [[ -n $_origin ]]; then
        echo $_origin
        return 0
    fi
    echo No origin >&2
    return 1
}

gbv () {
    git blame "$1" | vin
}

gcj () {
    local _storage=/tmp/gcj.sh
    local JIT="git -C ~/jab"
    if [[ -z "$@" ]]; then $JIT commit --all --verbose
    else
        python -c "print '$JIT commit -m\"$*\"'" > $_storage
        _run_storage
    fi
}

gcm () {
    git co master
}

gcp () {
    git cherry-pick -x --allow-empty "$@"
}

gdd () {
    gdi .
}

gdi () {
    git di "$@"
}

gdh () {
    git dh "$@"
}

gdl () {
    git diff --color=always "$@"| fewer
}

gdv () {
    git dv "$@"
}

gfa () {
    git fetch --all
}

_gl () {
    local cmd=$1
    shift
    local _vertical_lines=${LINES:-$(screen_height)}
    local _one_third_of_vertical=$(( $_vertical_lines / 4 ))
    local _lines=${GIT_LOG_LINES:-$_one_third_of_vertical}
    git $cmd "$@" | head -n $_lines
}

gl1 () {
    git log -n1 "$@"
}

gla () {
    _gl lg --author=Alan.Brogan "$@"
}

glg () {
    local _number_of_commits=7
    #if [[ $1 =~ [[:digit:]] && $1 !~ [[:alpha:]] ]]; then
    if [[ $1 =~ ^-?[0-9]+$ ]]; then
        _number_of_commits=$1
        shift
    fi
    # Show full log for quarter of screen
    git log -n$_number_of_commits --stat --color "$@" | head -n $(( LINES / 4 ))
    # Show one line per commit
    GIT_LOG_LINES=$_number_of_commits _gl lg -n$_number_of_commits "$@" | _call_me_alan | sed -e "s/ ago//"
}

gln () {
    _gl lg --name-only "$@"
}

glp () {
    git log -p "$@"
}

gls () {
    _gl log --stat "$@"
}

glt () {
    _gl lt "$@"
}

gmm () {
    git merge master
}

grc () {
    git rebase --continue
}

grp () {
    echo "Pull"
    grr "$@"
    read -p "Push? [Y]" reply
    [[ -z $reply || $reply == "y" || $reply == "Y" ]] && gpp "$@"
}

gpp () {
    _git_stash_and push "$@"
}

grh () {
    git reset HEAD "$@"
}

grc () {
    git rebase --continue
}

gri () {
    if [[ -n $1 ]]; then
        git rebase --interactive "$@"
    else
        git rebase --interactive HEAD~2
    fi
}

grr () {
    _git_stash_and pull --rebase "$@"
}

gsd () {
    shift_dir "$@" && shift
    for f in $(gssd $dir | grep "^ M" | cut -dM -f2)
    do
        git dv $f
    done
    QUESTIONS=$(untracked $dir)
    [[ -n $QUESTIONS ]] && v $QUESTIONS
}

gds () {
    local diff_=${1:-diff}
    git $diff_ --staged "$@"
}

red_one () {
    local red="\033[0;31m"
    local no_colour="\033[0m"
    GSI_MENU="${GSI_MENU}${red}${1}${no_colour}${2}${suffix}"
}

red_two () {
    local red="\033[0;31m"
    local no_colour="\033[0m"
    GSI_MENU="${GSI_MENU}${no_colour}${1}${red}${2}${no_colour}${3}${suffix}"
}

_status_line () {
    [[ -n "$*" ]] && git status -s . || git status -s "$@"
}

_status_chars () {
    git -C $dir status -s -- $1 | sed -e "s/\(..\).*/\1/"
}

_git_modified () {
    [[ $(_status_chars $1) == " M" ]]
}

_git_added () {
    [[ $(_status_chars $1) == " A" ]]
}

_git_untracked () {
    [[ $(_status_chars $1) == "??" ]]
}

_gsi_drop () {
    if [[ $answer =~ [rR] ]]; then
        _git_modified "$1" && git co "$1"
        _git_untracked "$1" && rm -i "$1"
    fi
}

_gvi_drop () {
    if [[ $answer =~ [rR] ]]; then
        _git_modified "$1" && git co "$1"
        _git_untracked "$1" && rm -f "$1"
    fi
}

_gvi_git_dv () {
    local _one="$1"
    shift
    _gvi_other_vim 
    if _git_modified "$1"; then
        git dv $(echo "$1" | cut -dM -f2)
    else
        _gvi_vim "$1"
    fi
}

_gvi_vim () {
    if _git_untracked "$1"; then
        v $(_status_line "$1" | grep "??" | cut -d' ' -f2)
    elif _git_modified "$1"; then
        v $(echo "$1" | cut -dM -f2)
    elif _git_added "$1"; then
        v  $(echo "$1" | cut -dA -f2)
    fi
    _status_line "$1"
}

_gsi_vim () {
    _git_modified $f && git dv $f || git diff $f | vin
}

gsi () {
    gxi _gsi_show_file _gsi_response "$@"
}

gvi () {
    gxi _gvi_show_file _gvi_response "$@"
}

gxi () {
    local _gxi_show_file=$1; shift
    local _gxi_response=$1; shift
    shift_dir "$@" || shift
    while git status -s "$dir"; do
        for f in $(gssd_changes) "$dir"; do
            [[ -n "$f" ]] || continue
            $_gxi_show_file "$f"
            _gxi_request "$f"
            $_gxi_response "$f"
        done
        [[ $answer =~ [qQ] ]] && break
        gc
        git status -v | g -q "working directory clean" && break
        [[ -n $QUESTIONS ]] && v $QUESTIONS
    done
}

gvsd () {
    shift_dir "$@" || shift
    for f in $(gssd $dir); do
        _gvi_vim "$f"
    done
}

_gsi_response () {
    if [[ $answer =~ [fF] ]]; then gc; _gxi_request "$1"; fi
    [[ $answer =~ [aA] ]] && ga "$1"
    [[ $answer =~ [cC] ]] && git commit
    [[ $answer =~ [rR] ]] && _gsi_drop "$1"
    if _git_modified "$1" ; then
        [[ $answer =~ [dD] ]] && git di "$1"
        [[ $answer =~ [iI] ]] && gai "$1"
    fi
    [[ $answer =~ [vV] ]] && _gsi_vim "$1"
}

_gvi_response () {
    if [[ $answer =~ [fF] ]]; then 
        gc
        _gxi_request "$1"
    else
        [[ $answer =~ [aA] ]] && ga "$1"
        [[ $answer =~ [cC] ]] && git commit
        [[ $answer =~ [rR] ]] && _gvi_drop "$1"
        if _git_modified "$1" ; then
            [[ $answer =~ [dD] ]] && git diff "$1" | vin
            [[ $answer =~ [iI] ]] && gai "$1"
            [[ $answer =~ [pP] ]] && gap "$1"
        fi
        [[ $answer =~ [vV] ]] && _gvi_vim "$1"
        [[ $answer =~ [wW] ]] && _gvi_git_dv "$1"
    fi
}

# xxxx

gaic () {
    gaii .; gc "$@"
}

gaii () {
    ga --interactive "$@"
}

gbdr () {
    git push origin --delete $1
}

gcmr () {
    gcm
    git pull --rebase
    bump show
}

gcpa () {
    git cherry-pick --abort
}

gcac () {
    ga .
    gcpc
}

gcpc () {
    git cherry-pick --continue
}

gcpe () {
    git cherry-pick --edit "$@"
}

gcrg () {
    gc; gr; glg
}

gdsi () {
    gds di "$@"
}

gdsv () {
    gds dv "$@"
}

ggai () {
    g -q $1 $2 && gai $2 || echo fuck off<commit_msg>Alphabetise<commit_after>#! /bin/cat

. ~/jab/bin/first_dir.sh
. ~/jab/environ.d/colour.sh

# functons.sh for git

_git_status_char_regexp="[ MADRCU]"
_git_status_regexp="^${_git_status_char_regexp}${_git_status_char_regexp}"
# sorted by strcmp of function name

# x

# xx

ga () {
    [[ -n $GIT_ADDED ]] && GIT_ADDED="$@" || GIT_ADDED="$GIT_ADDED:""$@"
    if [[ -z "$*" ]]; then
        echo Nothing specified for add >&2
        echo 'Did you mean "gaa" (add all) ?'
        return 1
    else
        git add "$@"
    fi
}

gb () {
    if [[ -z "$*" ]]; then
        git branch 2>&1 | grep -v warning
    else
        git branch 2>&1 | grep -v warning | grep --color "$@";
    fi
}

gc () {
    local _storage=/tmp/gc.sh
    if [[ -z "$@" ]]; then git commit --verbose
    else
        python -c "print 'git commit -m\"$*\"'" > $_storage
        _show_run_storage
    fi
    GIT_ADDED=
}

gd () {
    git diff "$@"
}

gi () {
    git "$@"
}

gp () {
    if ! MSG=$(git push 2>&1); then
        if [[ $MSG =~ set-upstream ]]; then
            $(echo "$MSG" | grep set-upstream)
        else
            echo "$MSG" >&2
            return 1
        fi
    fi
}

gr () {
    git pull --rebase "$@"
}

gs () {
    shift_dir "$@" && shift
    _do_git_status $dir "$@"
}

tc () {
    vim ~/.gitconfig +
}

# xxx

aga () {
    git reset HEAD "$@"
}

gcg () {
    gc; glg
}

gcr () {
    gc; gr
}

gaa () {
    ga .
}

gac () {
    local addable=.
    if [[ -e "$1" ]]; then
        addable=$1
        shift
    fi
    ga $addable && gc "$@"
}

gai () {
    ga --patch "$@"
}

gba () {
    if [[ -z "$*" ]]; then
        git branch -a 2>&1 | grep -v warning
    else
        git branch -a 2>&1 | grep -v warning | grep --color "$@";
    fi
}

gbb () {
    git_branch "$@"
}

gbd () {
    git branch -d "$@"
}

gbD () {
    git branch -D "$@"
}

gbo () {
    local _origin=$(git for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD))
    if [[ -n $_origin ]]; then
        echo $_origin
        return 0
    fi
    echo No origin >&2
    return 1
}

gbv () {
    git blame "$1" | vin
}

gcj () {
    local _storage=/tmp/gcj.sh
    local JIT="git -C ~/jab"
    if [[ -z "$@" ]]; then $JIT commit --all --verbose
    else
        python -c "print '$JIT commit -m\"$*\"'" > $_storage
        _run_storage
    fi
}

gcm () {
    git co master
}

gcp () {
    git cherry-pick -x --allow-empty "$@"
}

gdd () {
    gdi .
}

gdi () {
    git di "$@"
}

gdh () {
    git dh "$@"
}

gdl () {
    git diff --color=always "$@"| fewer
}

gdv () {
    git dv "$@"
}

gfa () {
    git fetch --all
}

_gl () {
    local cmd=$1
    shift
    local _vertical_lines=${LINES:-$(screen_height)}
    local _one_third_of_vertical=$(( $_vertical_lines / 4 ))
    local _lines=${GIT_LOG_LINES:-$_one_third_of_vertical}
    git $cmd "$@" | head -n $_lines
}

gl1 () {
    git log -n1 "$@"
}

gla () {
    _gl lg --author=Alan.Brogan "$@"
}

glg () {
    local _number_of_commits=7
    #if [[ $1 =~ [[:digit:]] && $1 !~ [[:alpha:]] ]]; then
    if [[ $1 =~ ^-?[0-9]+$ ]]; then
        _number_of_commits=$1
        shift
    fi
    # Show full log for quarter of screen
    git log -n$_number_of_commits --stat --color "$@" | head -n $(( LINES / 4 ))
    # Show one line per commit
    GIT_LOG_LINES=$_number_of_commits _gl lg -n$_number_of_commits "$@" | _call_me_alan | sed -e "s/ ago//"
}

gln () {
    _gl lg --name-only "$@"
}

glp () {
    git log -p "$@"
}

gls () {
    _gl log --stat "$@"
}

glt () {
    _gl lt "$@"
}

gmm () {
    git merge master
}

grc () {
    git rebase --continue
}

grp () {
    echo "Pull"
    grr "$@"
    read -p "Push? [Y]" reply
    [[ -z $reply || $reply == "y" || $reply == "Y" ]] && gpp "$@"
}

gpp () {
    _git_stash_and push "$@"
}

grh () {
    git reset HEAD "$@"
}

grc () {
    git rebase --continue
}

gri () {
    if [[ -n $1 ]]; then
        git rebase --interactive "$@"
    else
        git rebase --interactive HEAD~2
    fi
}

grr () {
    _git_stash_and pull --rebase "$@"
}

gsd () {
    shift_dir "$@" && shift
    for f in $(gssd $dir | grep "^ M" | cut -dM -f2)
    do
        git dv $f
    done
    QUESTIONS=$(untracked $dir)
    [[ -n $QUESTIONS ]] && v $QUESTIONS
}

gds () {
    local diff_=${1:-diff}
    git $diff_ --staged "$@"
}

red_one () {
    local red="\033[0;31m"
    local no_colour="\033[0m"
    GSI_MENU="${GSI_MENU}${red}${1}${no_colour}${2}${suffix}"
}

red_two () {
    local red="\033[0;31m"
    local no_colour="\033[0m"
    GSI_MENU="${GSI_MENU}${no_colour}${1}${red}${2}${no_colour}${3}${suffix}"
}

_status_line () {
    [[ -n "$*" ]] && git status -s . || git status -s "$@"
}

_status_chars () {
    git -C $dir status -s -- $1 | sed -e "s/\(..\).*/\1/"
}

_git_modified () {
    [[ $(_status_chars $1) == " M" ]]
}

_git_added () {
    [[ $(_status_chars $1) == " A" ]]
}

_git_untracked () {
    [[ $(_status_chars $1) == "??" ]]
}

_gsi_drop () {
    if [[ $answer =~ [rR] ]]; then
        _git_modified "$1" && git co "$1"
        _git_untracked "$1" && rm -i "$1"
    fi
}

_gvi_drop () {
    if [[ $answer =~ [rR] ]]; then
        _git_modified "$1" && git co "$1"
        _git_untracked "$1" && rm -f "$1"
    fi
}

_gvi_git_dv () {
    local _one="$1"
    shift
    _gvi_other_vim 
    if _git_modified "$1"; then
        git dv $(echo "$1" | cut -dM -f2)
    else
        _gvi_vim "$1"
    fi
}

_gvi_vim () {
    if _git_untracked "$1"; then
        v $(_status_line "$1" | grep "??" | cut -d' ' -f2)
    elif _git_modified "$1"; then
        v $(echo "$1" | cut -dM -f2)
    elif _git_added "$1"; then
        v  $(echo "$1" | cut -dA -f2)
    fi
    _status_line "$1"
}

_gsi_vim () {
    _git_modified $f && git dv $f || git diff $f | vin
}

gsi () {
    gxi _gsi_show_file _gsi_response "$@"
}

gvi () {
    gxi _gvi_show_file _gvi_response "$@"
}

gxi () {
    local _gxi_show_file=$1; shift
    local _gxi_response=$1; shift
    shift_dir "$@" || shift
    while git status -s "$dir"; do
        for f in $(gssd_changes) "$dir"; do
            [[ -n "$f" ]] || continue
            $_gxi_show_file "$f"
            _gxi_request "$f"
            $_gxi_response "$f"
        done
        [[ $answer =~ [qQ] ]] && break
        gc
        git status -v | g -q "working directory clean" && break
        [[ -n $QUESTIONS ]] && v $QUESTIONS
    done
}

gvsd () {
    shift_dir "$@" || shift
    for f in $(gssd $dir); do
        _gvi_vim "$f"
    done
}

_gsi_response () {
    if [[ $answer =~ [fF] ]]; then gc; _gxi_request "$1"; fi
    [[ $answer =~ [aA] ]] && ga "$1"
    [[ $answer =~ [cC] ]] && git commit
    [[ $answer =~ [rR] ]] && _gsi_drop "$1"
    if _git_modified "$1" ; then
        [[ $answer =~ [dD] ]] && git di "$1"
        [[ $answer =~ [iI] ]] && gai "$1"
    fi
    [[ $answer =~ [vV] ]] && _gsi_vim "$1"
}

_gvi_response () {
    if [[ $answer =~ [fF] ]]; then 
        gc
        _gxi_request "$1"
    else
        [[ $answer =~ [aA] ]] && ga "$1"
        [[ $answer =~ [cC] ]] && git commit
        [[ $answer =~ [rR] ]] && _gvi_drop "$1"
        if _git_modified "$1" ; then
            [[ $answer =~ [dD] ]] && git diff "$1" | vin
            [[ $answer =~ [iI] ]] && gai "$1"
            [[ $answer =~ [pP] ]] && gap "$1"
        fi
        [[ $answer =~ [vV] ]] && _gvi_vim "$1"
        [[ $answer =~ [wW] ]] && _gvi_git_dv "$1"
    fi
}

# xxxx

gaic () {
    gaii .; gc "$@"
}

gaii () {
    ga --interactive "$@"
}

gbdr () {
    git push origin --delete $1
}

gcac () {
    ga .
    gcpc
}

gcmr () {
    gcm
    git pull --rebase
    bump show
}

gcpa () {
    git cherry-pick --abort
}

gcpc () {
    git cherry-pick --continue
}

gcpe () {
    git cherry-pick --edit "$@"
}

gcrg () {
    gc; gr; glg
}

gdsi () {
    gds di "$@"
}

gdsv () {
    gds dv "$@"
}

ggai () {
    g -q $1 $2 && gai $2 || echo fuck off
{'fraction_non_alphanumeric': 0.20615266785805464, 'fraction_numerical': 0.01416900689303038, 'mean_word_length': 1.9000555247084954, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4005191', 'n_tokens_mistral': 6775, 'n_tokens_neox': 6486, 'n_words': 1571}