From 3c12e2f7463ca3acd58f61ab5e78f0379cbc59c1 Mon Sep 17 00:00:00 2001
From: Daniel Lundin <daniel@arity.se>
Date: Wed, 27 Dec 2023 00:43:23 +0100
Subject: [PATCH] git: add git-st script

---
 .gitconfig |  7 ++++++-
 bin/git-st | 31 +++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100755 bin/git-st

diff --git a/.gitconfig b/.gitconfig
index 6142a4a..6deb7af 100644
--- a/.gitconfig
+++ b/.gitconfig
@@ -39,7 +39,12 @@ sw = branchless switch
 s = branchless switch -i
 new = !git fetch -u origin main:main && git branchless switch -d origin/main
 
-gerrit-clone = "!f() { git clone ssh://gerrit-ssh.shelman.io:2222/$1 && (cd $1 && mkdir -p `git rev-parse --git-dir`/hooks/ && scp -O -P 2222 gerrit-ssh.shelman.io:hooks/commit-msg `git rev-parse --git-dir`/hooks/commit-msg && chmod +x `git rev-parse --git-dir`/hooks/commit-msg && git branchless init); }; f"
+gerrit-clone = "!f() { \
+  git clone ssh://gerrit-ssh.shelman.io:2222/$1 \
+  && (cd $1 && mkdir -p `git rev-parse --git-dir`/hooks/ \
+  && scp -O -P 2222 gerrit-ssh.shelman.io:hooks/commit-msg `git rev-parse --git-dir`/hooks/commit-msg \
+  && chmod +x `git rev-parse --git-dir`/hooks/commit-msg \
+  && git branchless init); }; f"
 
 gerrit-switch = !(ssh `git remote get-url origin | cut -d / -f -3` gerrit query --format=JSON --current-patch-set status:open project:`git remote get-url origin | cut -d / -f 4-` \
     | jq -sr 'map(select(.number) | \"\\(.number) @ \\(.currentPatchSet.ref) - \\(.subject)\")|join(\"\\n\")' \
diff --git a/bin/git-st b/bin/git-st
new file mode 100755
index 0000000..a982342
--- /dev/null
+++ b/bin/git-st
@@ -0,0 +1,31 @@
+#!/usr/bin/env bash
+
+git branchless smartlog || true
+printf '\n'
+
+awk -vOFS='' '
+    NR==FNR {
+        all[i++] = $0;
+        difffiles[$1] = $0;
+        next;
+    }
+    ! ($2 in difffiles) {
+        print; next;
+    }
+    {
+        gsub($2, difffiles[$2]);
+        print;
+    }
+    END {
+        if (NR != FNR) {
+            # Had diff output
+            exit;
+        }
+        # Had no diff output, just print lines from git status -sb
+        for (i in all) {
+            print all[i];
+        }
+    }
+' \
+    <(git diff --color --stat=$(($(tput cols) - 3)) HEAD | sed '$d; s/^ //')\
+    <(git -c color.status=always status -sb)