Skip to content

Commit 7fcf2d3

Browse files
begin-againcoderabbitai[bot]Copilot
authored
Improves shell prompt with git and nvm info (#251)
## Changes Enhances the shell prompt to display Git branch and status information, as well as the currently active Node.js version managed by NVM. - Sources `.git-prompt-support` to provide git status in the prompt. - Modifies `gitlogp` to allow reverse chronological listing. - Uses helper functions to obtain the basename of the nvm symlink target and parent directory to support both Windows and Linux environments. - Changes git status indicators from parenthesis to curly braces. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Enhanced pretty git log with branch filtering and optional reverse commit order. * Added shell helpers to improve prompt display for Node version and symlink targets. * Expanded Git-related prompt support for richer status information. * **Improvements** * Updated prompt git-status badge format for clearer display. * More robust whitespace handling when detecting CHERRY-PICK/REVERT Git states. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent d30a132 commit 7fcf2d3

File tree

4 files changed

+90
-32
lines changed

4 files changed

+90
-32
lines changed

.git-prompt-support

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env bash
12
# bash/zsh git prompt support
23
#
34
# Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
@@ -321,11 +322,11 @@ __git_sequencer_status ()
321322
elif __git_eread "$g/sequencer/todo" todo
322323
then
323324
case "$todo" in
324-
p[\ \ ]|pick[\ \ ]*)
325+
p[[:space:]]|pick[[:space:]]*)
325326
r="|CHERRY-PICKING"
326327
return 0
327328
;;
328-
revert[\ \ ]*)
329+
revert[[:space:]]*)
329330
r="|REVERTING"
330331
return 0
331332
;;

src/bash.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ shopt -s histverify
1414
# Git completion support
1515
# shellcheck disable=SC1091
1616
source "$DEVROOT/dev-tools/.git-completion-support"
17+
# shellcheck disable=SC1091
18+
source "$DEVROOT/dev-tools/.git-prompt-support"
1719

1820
# Command Prompt
1921
# shellcheck disable=SC1091

src/functions.sh

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,34 @@ gitlog() {
9292
' | column -ts '|'
9393
}
9494

95-
95+
# pretty one line git log of current repository with merge and side branch indicators
9696
gitlogp() {
97-
entries=$1
98-
branch="$2"
99-
[ -n "$entries" ] && entries="-${entries#-}"
97+
entries=""
98+
branch=""
99+
reverseFlag=0
100+
101+
# parse named args
102+
while [ $# -gt 0 ]; do
103+
case "$1" in
104+
-n|--entries)
105+
entries="-$2"
106+
shift 2
107+
;;
108+
-b|--branch)
109+
branch="$2"
110+
shift 2
111+
;;
112+
-r|--reverse)
113+
reverseFlag=1
114+
shift
115+
;;
116+
*)
117+
echo "Unknown option: $1"
118+
echo "Usage: gitlogp [-n N] [-b branch] [-r]"
119+
return 1
120+
;;
121+
esac
122+
done
100123

101124
fmt='%h|%p|%cd|%an|%(describe:tags)|%s'
102125
datearg="--date=format:%m-%d-%y %H:%M"
@@ -105,7 +128,7 @@ gitlogp() {
105128
git log "$branch" --format="$fmt" "$datearg" $entries
106129
else
107130
git log --format="$fmt" "$datearg" $entries
108-
fi | awk -F'|' '
131+
fi | awk -F'|' -v reverseFlag=$reverseFlag '
109132
{
110133
commit=$1
111134
parents=$2
@@ -124,17 +147,11 @@ gitlogp() {
124147
n=split(parents, arr, " ")
125148
parentCount[commit]=n
126149
127-
for (i=1; i<=n; i++) {
128-
if (arr[i] != "") parentOf[commit]=parentOf[commit] " " arr[i]
129-
}
130150
131-
# mark the *second parent* of merges
132-
if (n > 1) {
133-
markBranch[arr[2]]=1
134-
}
151+
if (n > 1) markBranch[arr[2]]=1
135152
}
136153
END {
137-
# propagate [side] marks backwards
154+
# propagate [side]
138155
changed=1
139156
while (changed) {
140157
changed=0
@@ -150,20 +167,33 @@ gitlogp() {
150167
}
151168
}
152169
153-
for (i=1; i<=NR; i++) {
154-
c=order[i]
155-
156-
# decide marker
157-
if (parentCount[c] > 1) {
158-
flag="[merge]"
159-
} else if (c in markBranch) {
160-
flag="[side]"
161-
} else {
162-
flag=" "
170+
# decide iteration order
171+
if (reverseFlag) {
172+
for (i=NR; i>=1; i--) {
173+
c=order[i]
174+
if (parentCount[c] > 1) {
175+
flag="[merge]"
176+
} else if (c in markBranch) {
177+
flag="[side]"
178+
} else {
179+
flag=" "
180+
}
181+
printf "%-8s %-7s | %-20s | %-14s | %-15s | %-25s | %s\n", \
182+
c, flag, parentsOf[c], dateOf[c], authorOf[c], tagOf[c], subjectOf[c]
183+
}
184+
} else {
185+
for (i=1; i<=NR; i++) {
186+
c=order[i]
187+
if (parentCount[c] > 1) {
188+
flag="[merge]"
189+
} else if (c in markBranch) {
190+
flag="[side]"
191+
} else {
192+
flag=" "
193+
}
194+
printf "%-8s %-7s | %-20s | %-14s | %-15s | %-25s | %s\n", \
195+
c, flag, parentsOf[c], dateOf[c], authorOf[c], tagOf[c], subjectOf[c]
163196
}
164-
165-
printf "%-8s %-7s %-20s %-14s %-15s %-25s %s\n", \
166-
c, flag, parentsOf[c], dateOf[c], authorOf[c], tagOf[c], subjectOf[c]
167197
}
168198
}
169199
'

src/prompt.sh

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,41 @@ Time12h="\T"
3030
PathShort="\w"
3131
Date="\d"
3232

33+
# obtain the folder name of the selected node version (from nvm symlink)
34+
get_symlink_basename() {
35+
# Check if NVM_SYMLINK is set
36+
if [ -z "$NVM_SYMLINK" ]; then
37+
return
38+
fi
39+
40+
# Get the actual target path of the symlink
41+
local target
42+
target=$(readlink "$NVM_SYMLINK")
43+
44+
# Extract the basename of the folder
45+
local version="${target##*/}" # Get the last part after the last '/'
46+
47+
# Echo the extracted version
48+
echo "$version"
49+
}
50+
51+
# Helper: get parent directory name (portable)
52+
get_parent_dirname() {
53+
local dir
54+
dir="${NVM_BIN%/*}"
55+
echo "${dir##*/}"
56+
}
57+
3358
# test if NVM_SYMLINK exists
3459
if [[ -L "$NVM_SYMLINK" ]]; then
3560
# prompt for use in windows
3661
echo " -- windows prompt"
37-
export PS1=$Cyan$Time12h$Color_Off$Purple' <$(basename $(readlink "$NVM_SYMLINK"))>'$Color_Off'$(git branch &>/dev/null;\
62+
export PS1=$Cyan$Time12h$Color_Off$Purple' <$(get_symlink_basename)>'$Color_Off'$(git branch &>/dev/null;\
3863
if [ $? -eq 0 ]; then \
3964
echo "$(echo `git status` | grep "nothing to commit" > /dev/null 2>&1; \
4065
if [ "$?" -eq "0" ]; then \
4166
# Clean repository - nothing to commit
42-
echo "'$Green'"$(__git_ps1 " (%s)"); \
67+
echo "'$Green'"$(__git_ps1 " {%s}"); \
4368
else \
4469
# Changes to working tree
4570
echo "'$Red'"$(__git_ps1 " {%s}"); \
@@ -54,12 +79,12 @@ fi
5479
if [ -d "$NVM_DIR" ]; then
5580
echo " -- linux prompt"
5681
# prompt for use in linux
57-
export PS1=$Cyan$Time12h$Color_Off$Purple' <$(basename $(dirname "$NVM_BIN"))>'$Color_Off'$(git branch &>/dev/null;\
82+
export PS1=$Cyan$Time12h$Color_Off$Purple' <$(get_parent_dirname)>'$Color_Off'$(git branch &>/dev/null;\
5883
if [ $? -eq 0 ]; then \
5984
echo "$(echo `git status` | grep "nothing to commit" > /dev/null 2>&1; \
6085
if [ "$?" -eq "0" ]; then \
6186
# Clean repository - nothing to commit
62-
echo "'$Green'"$(__git_ps1 " (%s)"); \
87+
echo "'$Green'"$(__git_ps1 " {%s}"); \
6388
else \
6489
# Changes to working tree
6590
echo "'$Red'"$(__git_ps1 " {%s}"); \

0 commit comments

Comments
 (0)