changeset 160:961186621cc8

Add optional rhg (Mercurial Rust) support Configure in hgrc and write a custom `hg prompt` that's faster that the prompt extension on a Raspberry Pi because it doesn't initialise Python on each call
author IBBoard <dev@ibboard.co.uk>
date Sat, 11 Nov 2023 20:29:16 +0000
parents 5c19b4fd04c0
children 912d1c33382f
files bashrc hgrc
diffstat 2 files changed, 41 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/bashrc	Wed Aug 23 21:00:28 2023 +0100
+++ b/bashrc	Sat Nov 11 20:29:16 2023 +0000
@@ -58,7 +58,41 @@
 	VCS_EXTRA=$(__git_ps1 "%s")
 
 	if [[ "x$VCS_EXTRA" == "x" ]]; then
-		VCS_EXTRA=$(hg prompt "{branch}{ {status|modified}}{ u{inout}}" 2>/dev/null)
+		if ! command -v rhg &> /dev/null; then
+			VCS_EXTRA=$(hg prompt "{branch}{ {status|modified}}{ u{inout}}" 2>/dev/null)
+		else
+			# If we have rhg then bypass hgprompt for a faster experience on slow machines
+
+			# 24h cache - I normally work on my own, so upstream shouldn't change frequently
+			# and we still have hgprompt installed for wrapping commits to update outgoing
+			HG_CACHE_TIMEOUT=86400
+			HG_ROOT=$(rhg root 2>/dev/null)
+			if [[ "x$HG_ROOT" != "x" ]]; then
+				HG_BRANCH=$(<"${HG_ROOT}"/.hg/branch)
+				HG_MODIFIED=$([ "$(rhg status -mard | head -c1)" ] && echo " *")
+				HG_INCOMING_PATH="${HG_ROOT}"/.hg/prompt/cache/incoming
+				HG_OUTGOING_PATH="${HG_ROOT}"/.hg/prompt/cache/outgoing
+				if [[ $(($(date +%s) - $(date -r "${HG_INCOMING_PATH}" +%s 2>/dev/null) )) -gt $HG_CACHE_TIMEOUT ]]; then
+					hg incoming --quiet --config ui.interactive=False > "$HG_INCOMING_PATH"
+					hg outgoing --quiet --config ui.interactive=False > "$HG_OUTGOING_PATH"
+				fi
+				HG_IN=$(wc -l < "$HG_INCOMING_PATH")
+				HG_OUT=$(wc -l < "$HG_OUTGOING_PATH")
+				if [[ $HG_IN = "0" && $HG_OUT = "0" ]]; then
+					HG_INOUT=" u="
+				else
+					HG_INOUT=" u"
+					if [[ $HG_OUT != "0" ]]; then
+						HG_INOUT="${HG_INOUT}+${HG_OUT}"
+					fi
+					if [[ $HG_IN != "0" ]]; then
+
+						HG_INOUT="${HG_INOUT}-${HG_IN}"
+					fi
+				fi
+				VCS_EXTRA="${HG_BRANCH}${HG_MODIFIED}${HG_INOUT}"
+			fi
+		fi
 	fi
 	if [[ "x$VCS_EXTRA" != "x" ]]; then
 		echo -n " ($VCS_EXTRA)"
--- a/hgrc	Wed Aug 23 21:00:28 2023 +0100
+++ b/hgrc	Sat Nov 11 20:29:16 2023 +0000
@@ -38,3 +38,9 @@
 %include ~/.hgrc-certs
 
 %include ~/.hgrc-auth
+
+[rhg]
+on-unsupported = fallback
+fallback-executable = /usr/bin/hg
+allowed-extensions = rebase, absorb
+ignored-extensions = mercurial_keyring, bookmarks, prompt, extdiff, pager, hgext.convert, rebase, color, progress
\ No newline at end of file