Skip to content

Commit a5c45d7

Browse files
committed
Document what we're doing with $BASH_FULL_HISTFILE
1 parent 734d294 commit a5c45d7

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

files/tree/etc/bash.bashrc.d/history.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ function readfromhistory() {
1616
history -r
1717
}
1818

19-
# based on whether BASH_FULL_HISTFILE has been set by direnv, adjust
20-
# BASH_FULLHIST and make sure it's loaded
19+
# Implement per-directory bash history file:
20+
# If you set BASH_FULL_HISTFILE for a subdir, and then cd to somewhere
21+
# under that dir, direnv changes the value of BASH_FULL_HISTFILE, and
22+
# our job is to detect this change after the `cd` was executed, and
23+
# make sure this new history file is loaded, before the prompt
24+
# returns.
2125
function choosehistoryfile() {
2226
local BASH_FULLHIST="${BASH_FULL_HISTFILE:-$HOME/.bash_fullhistory}"
2327
if [ "$BASH_FULL_HISTFILE_LAST" != "$BASH_FULLHIST" ] ; then

files/tree/etc/bash.bashrc.d/prompt.sh

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
# -*- Mode: shell-script -*-
22
# shellcheck shell=bash
33

4-
# We've long appended our bash history upon a command running, using
5-
# various methods. `trap '...' DEBUG` was causing us to have to write
6-
# lots of code just to make sure the command being tested was a user
7-
# command and not some poor shell command, and pay careful attention
8-
# to make sure it didn't slow down the shell too much. Another probel
9-
# with `trap '...' DEBUG` method of writing .bash_fullhistory is that
10-
# if a command doesn't actually run (eg, #blah), then it doesn't get
11-
# echoed yet to the history file. Problem with PROMPT_COMMAND by
12-
# itself is that it doesn't write to the history until the command
13-
# returns. Like
4+
# We long ago overrode default bash behaviour of only writing the
5+
# history file when a shell exits in a controlled fashion, instead
6+
# choosing to attempt to append the history the moment a command has
7+
# been entered, using various methods (we also do away with any
8+
# BASH_HISTSIZE etc for the stored file -- any rotation of that file
9+
# is left to the user to do manually. I've got 1.6million lines in my
10+
# history file, and it doesn't affect shell loading time. We only
11+
# read the last 32k of them when starting a new shell).
12+
#
13+
# But there's a few ways to do this. `trap '...' DEBUG` was causing
14+
# us to have to write lots of code just to make sure the command being
15+
# tested was a user command and not some poor shell command, and pay
16+
# careful attention to make sure it didn't slow down the shell too
17+
# much. Another problem with `trap '...' DEBUG` method of writing
18+
# .bash_fullhistory is that if a command doesn't actually run (eg,
19+
# #blah), then it doesn't get echoed yet to the history file. Problem
20+
# with PROMPT_COMMAND by itself is that it doesn't write to the
21+
# history until the command returns. Like
1422
# https://superuser.com/questions/175799/does-bash-have-a-hook-that-is-run-before-executing-a-command
1523
# We were using $PS0 for a while, via signal handlers per
1624
# https://github.com/rcaloras/bash-preexec/issues/28 but that causes
@@ -73,9 +81,14 @@ function handleprompt () {
7381
statusbeep $retcode 1$SECONDS 1$CMD_START_SECONDS
7482
fi
7583

76-
# we need to make sure the right history file is loaded, after the
77-
# previous command, which might have been a `cd` triggering a new
78-
# history file to be set, before the prompt returns. Do it now.
84+
# Implement per-directory bash history file:
85+
# We have a mechanism to load/save different history files per
86+
# subdirectory that you're in, eg by using `direnv`, to set
87+
# BASH_FULL_HISTFILE. If you set it for a subdir, and then
88+
# navigate to somewhere under that dir, direnv changes the value
89+
# of BASH_FULL_HISTFILE, and our job is to detect this change
90+
# after `cd` was run, and make sure this new history file is
91+
# loaded, before the prompt returns.
7992
choosehistoryfile
8093

8194
if [ -n "$EXTRA_PROMPT_SOURCE" ] && [ -e "$EXTRA_PROMPT_SOURCE" ] ; then

0 commit comments

Comments
 (0)