diff --git a/Commands.md b/Commands.md index fa596142..5600559e 100644 --- a/Commands.md +++ b/Commands.md @@ -392,6 +392,19 @@ Type `exit`, `quit`, or `q` to end the repl session. Any arguments to git repl will be taken as the first command to execute in the repl. +### CONFIGURATION + +Commands entered in a repl session will be saved to a history file and be available in +future sessions, similar to a shell or programming language repl. By default, +there is one global history file, ~/.git_repl_history. You can specify that your projects +each have their own independent history file. This file will be saved in .git_repl_history +at the top level of the repo, and will need to be added to the repo or global .gitignore. + +```bash +# remove the --global flag to configure only an individual project to have its own history file +git config --global git-extras.repl.use-local-history "true" +``` + ```bash $ git repl git version 2.34.1 diff --git a/bin/git-repl b/bin/git-repl index a706de37..8887b6b3 100755 --- a/bin/git-repl +++ b/bin/git-repl @@ -4,6 +4,21 @@ git version echo "git-extras version ""$(git-extras -v)" echo "Type 'ls' to ls files below current directory; '!command' to execute any command or just 'subcommand' to execute any git subcommand; 'quit', 'exit', 'q', ^D, or ^C to exit the git repl." +HISTCONTROL=ignoredups +use_local_history=$(git config --get --default 'false' git-extras.repl.use-local-history) +if [[ "$use_local_history" == "true" ]]; then + HISTFILE="$(git rev-parse --show-toplevel)/.git_repl_history" +else + HISTFILE=~/.git_repl_history +fi + +# file doesn't exist, is empty, or contains only whitespace +if [[ ! -f "$HISTFILE" ]] || [[ ! -s "$HISTFILE" ]] || ! grep -q '[^[:space:]]' "$HISTFILE"; then + # `history -r` .... `history -a` are not happy with an empty initial file + echo '!echo welcome to git-repl!' >> "$HISTFILE" +fi +history -r + while true; do # Current branch cur=$(git symbolic-ref HEAD 2> /dev/null | cut -d/ -f3-) @@ -33,8 +48,10 @@ while true; do test $? -ne 0 && break fi - # History - history -s "$cmd" + # Add command to history if it is not all whitespace + if [[ ! "$cmd" =~ ^[[:space:]]*$ ]]; then + history -s "$cmd" + fi # Built-in commands case $cmd in @@ -43,6 +60,8 @@ while true; do quit|exit|q) break;; esac + history -a + if [[ $cmd == !* ]]; then # shellcheck disable=SC2086 eval ${cmd:1} diff --git a/man/git-repl.1 b/man/git-repl.1 index b3721f44..d77513ae 100644 --- a/man/git-repl.1 +++ b/man/git-repl.1 @@ -1,6 +1,6 @@ -.\" generated with Ronn-NG/v0.9.1 -.\" http://github.com/apjanke/ronn-ng/tree/0.9.1 -.TH "GIT\-REPL" "1" "September 2024" "" "Git Extras" +.\" generated with Ronn-NG/v0.10.1 +.\" http://github.com/apjanke/ronn-ng/tree/0.10.1 +.TH "GIT\-REPL" "1" "January 2026" "" "Git Extras" .SH "NAME" \fBgit\-repl\fR \- git read\-eval\-print\-loop .SH "SYNOPSIS" @@ -31,6 +31,14 @@ Equivalent of 'git ls\-files'\. exit|quit|q .P Ends the repl session\. +.SH "CONFIGURATION" +Commands entered in a repl session will be saved to a history file and be available in future sessions, similar to a shell or programming language repl\. By default, there is one global history file, ~/\.git_repl_history\. You can specify that your projects each have their own independent history file\. This file will be saved in \.git_repl_history at the top level of the repo, and will need to be added to the repo or global \.gitignore\. +.IP "" 4 +.nf + # remove the \-\-global flag to configure only an individual project to have its own history file + git config \-\-global git\-extras\.repl\.use\-local\-history "true" +.fi +.IP "" 0 .SH "EXAMPLES" .nf $ git repl diff --git a/man/git-repl.html b/man/git-repl.html index 3f9750e6..99e88168 100644 --- a/man/git-repl.html +++ b/man/git-repl.html @@ -1,8 +1,8 @@
- - + +