Skip to content

Commit

Permalink
Enhance git-repl (tj#1160)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattscarpenter authored Sep 23, 2024
1 parent 16d1445 commit 5ca9360
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 80 deletions.
12 changes: 7 additions & 5 deletions Commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,16 @@ Git read-eval-print-loop. Lets you run `git` commands without typing 'git'.
Commands can be prefixed with an exclamation mark (!) to be interpreted as
a regular command.

Type `exit` or `quit` to end the repl session.
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.

```bash
$ git repl
git version 2.9.2
git-extras version 3.0.0
type 'ls' to ls files below current directory,
'!command' to execute any command or just 'subcommand' to execute any git subcommand
git version 2.34.1
git-extras version 7.3.0
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.

git (master)> ls-files
History.md
Expand Down
33 changes: 23 additions & 10 deletions bin/git-repl
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,36 @@

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"
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."

while true; do
# Current branch
cur=$(git symbolic-ref HEAD 2> /dev/null | cut -d/ -f3-)

# Prompt
if test -n "$cur"; then
prompt="git ($cur)> "
cur_string=" ($cur)"
else
prompt="git> "
cur_string=""
fi
if test -n "$exit_status" && test "$exit_status" -ne 0; then
es_string=$' \e[31m['"$exit_status"$']\e[0m'
else
es_string=""
fi
prompt="git$cur_string$es_string> "

# Readline
read -e -r -p "$prompt" cmd

# EOF
test $? -ne 0 && break
# Use arguments as a command if any are provided.
if [ $# -ne 0 ]; then
cmd=$*
set --
echo "$prompt" "$cmd" # It is as though you had entered a command.
else
# Readline
read -e -r -p "$prompt" cmd
# Check for EOF, and end the program if so (handles ^D).
test $? -ne 0 && break
fi

# History
history -s "$cmd"
Expand All @@ -28,16 +40,17 @@ while true; do
case $cmd in
ls) cmd=ls-files;;
"") continue;;
quit|exit) break;;
quit|exit|q) break;;
esac

if [[ $cmd == !* ]]; then
eval ${cmd:1}
eval ${cmd:1}
elif [[ $cmd == git* ]]; then
eval $cmd
else
eval git "$cmd"
fi
exit_status=$?
done

echo
50 changes: 16 additions & 34 deletions man/git-repl.1
Original file line number Diff line number Diff line change
@@ -1,56 +1,42 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "GIT\-REPL" "1" "October 2017" "" "Git Extras"
.
.\" 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"
.SH "NAME"
\fBgit\-repl\fR \- git read\-eval\-print\-loop
.
.SH "SYNOPSIS"
\fBgit\-repl\fR
.
.P
\fBgit\-repl\fR [command\|\.\|\.\|\.]
.SH "DESCRIPTION"
Git read\-eval\-print\-loop\. Lets you run \fBgit\fR commands without typing \'git\'\.
.
Git read\-eval\-print\-loop\. Lets you run \fBgit\fR commands without typing 'git'\.
.P
Commands can be prefixed with an exclamation mark (!) to be interpreted as a regular shell command\.
.P
Commands can be prefixed with an exclamation mark (!) to be interpreted as a regular command\.
.
Any arguments to git repl will be taken collectively as the first command to execute in the repl\.
.P
Type \fBexit\fR or \fBquit\fR to end the repl session\.
.
Type \fBexit\fR, \fBquit\fR, or \fBq\fR to end the repl session\. Ctrl\-D and Ctrl\-C will also work\.
.SH "COMMANDS"
<command>
.
.P
Interpreted as \fBgit <command>\fR\.
.
.P
!<command>
.
.P
Interpreted as \fB<command>\fR (not through \fBgit\fR)\.
.
.P
ls
.
.P
Equivalent of \'git ls\-files\'\.
.
Equivalent of 'git ls\-files'\.
.P
exit|quit
.
exit|quit|q
.P
Ends the repl session\.
.
.SH "EXAMPLES"
.
.nf

$ git repl
git version 2\.9\.2
git\-extras version 3\.0\.0
type \'ls\' to ls files below current directory,
\'!command\' to execute any command or just \'subcommand\' to execute any git subcommand
git version 2\.34\.1
git\-extras version 7\.3\.0
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\.

git (master)> ls\-files
History\.md
Expand All @@ -67,14 +53,10 @@ git (master)> !echo Straight from the shell!
Straight from the shell!

git (master)> quit
.
.fi
.
.SH "AUTHOR"
Written by Tj Holowaychuk <\fItj@vision\-media\.ca\fR>
.
Written by Tj Holowaychuk <\fItj@vision\-media\.ca\fR>\. Updated by Wyatt S Carpenter to add display of the previous command's exit status, 'q', and the ability to pass in the initial command as arguments\.
.SH "REPORTING BUGS"
<\fIhttps://github\.com/tj/git\-extras/issues\fR>
.
.SH "SEE ALSO"
<\fIhttps://github\.com/tj/git\-extras\fR>
51 changes: 28 additions & 23 deletions man/git-repl.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 13 additions & 8 deletions man/git-repl.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ git-repl(1) -- git read-eval-print-loop

`git-repl`

`git-repl` [command...]

## DESCRIPTION

Git read-eval-print-loop. Lets you run `git` commands without typing 'git'.

Commands can be prefixed with an exclamation mark (!) to be interpreted as
a regular command.
a regular shell command.

Any arguments to git repl will be taken collectively as the first command
to execute in the repl.

Type `exit` or `quit` to end the repl session.
Type `exit`, `quit`, or `q` to end the repl session. Ctrl-D and Ctrl-C
will also work.

## COMMANDS

Expand All @@ -28,18 +34,17 @@ git-repl(1) -- git read-eval-print-loop

Equivalent of 'git ls-files'.

exit|quit
exit\|quit\|q

Ends the repl session.


## EXAMPLES

$ git repl
git version 2.9.2
git-extras version 3.0.0
type 'ls' to ls files below current directory,
'!command' to execute any command or just 'subcommand' to execute any git subcommand
git version 2.34.1
git-extras version 7.3.0
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.

git (master)> ls-files
History.md
Expand All @@ -59,7 +64,7 @@ git-repl(1) -- git read-eval-print-loop

## AUTHOR

Written by Tj Holowaychuk &lt;<[email protected]>&gt;
Written by Tj Holowaychuk &lt;<[email protected]>&gt;. Updated by Wyatt S Carpenter to add display of the previous command's exit status, 'q', and the ability to pass in the initial command as arguments.

## REPORTING BUGS

Expand Down

0 comments on commit 5ca9360

Please sign in to comment.