From 41cd22ea330c90a42786e6fbebf6ad8a916bdb2a Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Fri, 21 Jun 2024 14:47:48 +0200 Subject: [PATCH] Add better-git-branch script --- home/programs/terminal/tools/git/aliases.nix | 8 ++- .../better-git-branch/better-git-branch.sh | 56 +++++++++++++++++++ .../git/bin/better-git-branch/default.nix | 4 ++ .../terminal/tools/git/bin/default.nix | 9 +++ 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 home/programs/terminal/tools/git/bin/better-git-branch/better-git-branch.sh create mode 100644 home/programs/terminal/tools/git/bin/better-git-branch/default.nix create mode 100644 home/programs/terminal/tools/git/bin/default.nix diff --git a/home/programs/terminal/tools/git/aliases.nix b/home/programs/terminal/tools/git/aliases.nix index b9968bf..7972a97 100644 --- a/home/programs/terminal/tools/git/aliases.nix +++ b/home/programs/terminal/tools/git/aliases.nix @@ -1,4 +1,10 @@ { + pkgs, + ... +}: let + + scriptPkgs = (import ./bin {inherit pkgs;}); +in { programs.git = { aliases = { quickclone = "clone --single-branch --depth=1"; @@ -48,7 +54,7 @@ bD = "branch --delete --force"; bm = "branch --move"; bM = "branch --move --force"; - bb = "!~/.local/bin/scripts/cli/better-git-branch"; + bb = "!${scriptPkgs.better-git-branch}/bin/better-git-branch"; r = "rebase"; ri = "rebase -i"; diff --git a/home/programs/terminal/tools/git/bin/better-git-branch/better-git-branch.sh b/home/programs/terminal/tools/git/bin/better-git-branch/better-git-branch.sh new file mode 100644 index 0000000..5d4f4da --- /dev/null +++ b/home/programs/terminal/tools/git/bin/better-git-branch/better-git-branch.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +# Source: https://gist.github.com/schacon/e9e743dee2e92db9a464619b99e94eff + +# Colors +RED='\033[0;31m' +GREEN='\033[0;32m' +NO_COLOR='\033[0m' +BLUE='\033[0;34m' +YELLOW='\033[0;33m' +NO_COLOR='\033[0m' + +width1=5 +width2=6 +width3=30 +width4=20 +width5=20 + +# Function to count commits +count_commits() { + local branch="$1" + local base_branch="$2" + local ahead_behind + + ahead_behind=$(git rev-list --left-right --count "$base_branch"..."$branch") + echo "$ahead_behind" +} + +# Main script +main_branch=$(git rev-parse HEAD) + +printf "${GREEN}%-${width1}s ${RED}%-${width2}s ${BLUE}%-${width3}s ${YELLOW}%-${width4}s ${NO_COLOR}%-${width5}s\n" "Ahead" "Behind" "Branch" "Last Commit" " " + +# Separator line for clarity +printf "${GREEN}%-${width1}s ${RED}%-${width2}s ${BLUE}%-${width3}s ${YELLOW}%-${width4}s ${NO_COLOR}%-${width5}s\n" "-----" "------" "------------------------------" "-------------------" " " + +format_string="%(objectname:short)@%(refname:short)@%(committerdate:relative)" +IFS=$'\n' + +for branchdata in $(git for-each-ref --sort=-authordate --format="$format_string" refs/heads/ --no-merged); do + sha=$(echo "$branchdata" | cut -d '@' -f1) + branch=$(echo "$branchdata" | cut -d '@' -f2) + time=$(echo "$branchdata" | cut -d '@' -f3) + if [ "$branch" != "$main_branch" ]; then + # Get branch description + description=$(git config branch."$branch".description) + + # Count commits ahead and behind + ahead_behind=$(count_commits "$sha" "$main_branch") + ahead=$(echo "$ahead_behind" | cut -f2) + behind=$(echo "$ahead_behind" | cut -f1) + + # Display branch info + # shellcheck disable=SC2086 + printf "${GREEN}%-${width1}s ${RED}%-${width2}s ${BLUE}%-${width3}s ${YELLOW}%-${width4}s ${NO_COLOR}%-${width5}s\n" $ahead $behind $branch "$time" "$description" + fi +done diff --git a/home/programs/terminal/tools/git/bin/better-git-branch/default.nix b/home/programs/terminal/tools/git/bin/better-git-branch/default.nix new file mode 100644 index 0000000..80b4517 --- /dev/null +++ b/home/programs/terminal/tools/git/bin/better-git-branch/default.nix @@ -0,0 +1,4 @@ +{pkgs, ...}: + pkgs.writeShellScriptBin "better-git-branch" '' + ${builtins.readFile ./better-git-branch.sh} + '' diff --git a/home/programs/terminal/tools/git/bin/default.nix b/home/programs/terminal/tools/git/bin/default.nix new file mode 100644 index 0000000..8cf9cb0 --- /dev/null +++ b/home/programs/terminal/tools/git/bin/default.nix @@ -0,0 +1,9 @@ +{ + pkgs, + ... +}: let + packages = { + better-git-branch = pkgs.callPackage ./better-git-branch {}; + }; +in + packages