Skip to content

Instantly share code, notes, and snippets.

@cppcooper

cppcooper/history-fun.bash

Last active Mar 26, 2020
Embed
What would you like to do?
bash-env/functions.bash - history purge commands - https://bitbucket.org/cppcooper/bash-env/src/master/
function phgit(){
purgehistory_core '(git ((add|reset|diff|log|checkout( HEAD| --)) (?!(`|\$))|commit|status|tag|push|branch))'
}
function reloadhistory() {
history -c
history -r
}
function purgehistory_core() {
if [ -z "$1" ]; then
core_regex="#\d{10}\s[^#\s].*"
else
core_regex="#\d{10}\s(?!$1)[^#\s].*"
fi
if [ ! -z "$phc_debug" ]; then
echo "debugging"
echo "$core_regex"
fi
error(){
case "$1" in
"0") echo -e "purgehistory_core: Error 0: ~/.bash_history is empty"
;; "1") echo -e "purgehistory_core: Error 1: ~/.tmphist_after is empty"
;; "2") echo -e "Error 2: purgehistory_core: Critical failue.\nCould not run pcregrep (probably). Will not try again this session."; fi
export HISTPURGEFAIL="2"
;; "3") echo -e "purgehistory_core: Error 3: ~/.tmphist_before is empty"
;; "4") echo -e "purgehistory_core: Error 4: failed: cp ~/.bash_history ~/.tmphist_before\nPerhaps ~/.tmphist_before already exists."
;; "5") echo -e "purgehistory_core: Error 5: \$lastCMD is empty"
;; "5")if [ -z "$SHELLSTARTED" ]; then export SHELLSTARTED="1"
else
echo -e "purgehistory_core: Error 5: Purging .bash_history failed. Empty prompt"; fi
;; "6") echo -e "purgehistory_core: Error 6: blockon_w function call failed"
;; "7")if [ -z "$HISTPURGEFAIL" ]; then
echo -e "purgehistory_core: Error 7: this shouldn't happen, but history purge failed.. and also didn't otherwise this message wouldn't have appeared. Perhaps gremlins changed the value between when your CPU executed this line and when the error function was called."
else :;fi #nothing happens. error 2 proc'd and now we don't want to spam what would be here
;; *) echo "Not an error(yet), a typo" ;;
esac
unset -f error
releaselock "$lockname"
return $1
}
if [ -z "$HISTPURGEFAIL" ]; then
lockname=".historypurge"
if blockon_w 2 newlock $lockname; then
if [ ! -z "$lastCMD" ]; then
if [[ ! -s ~/.bash_history ]]; then
error 0; return $?
elif cp ~/.bash_history ~/.tmphist_before; then
if [[ -s ~/.tmphist_before ]]; then
# remove non-entries & duplicates
if cat ~/.tmphist_before | pcre2grep -M "$core_regex" | tac | awk '!seen[$0]++' | tac > ~/.tmphist_after; then
if [ ! -z "$phc_debug" ]; then :
elif [[ -s ~/.tmphist_after ]]; then
mv ~/.tmphist_after ~/.bash_history
rm -f ~/.tmphist_before
reloadhistory
else error 1; return $?; fi
else error 2; return $?; fi
else error 3; return $?; fi
else error 4; return $?; fi
else error 5; return $?; fi
unset -f error
releaselock $lockname
return $?
else error 6
fi
else error 7; return $?; fi
echo "major fuck up, like actually.. there were so many returns before now."
}
# for more fun https://bitbucket.org/cppcooper/bash-env/src/master/.bash-shell-profile.bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.