From f053a0f70aa921a38d28eb5b2b323c4197687648 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Wed, 3 Jul 2024 23:35:52 +0200 Subject: [PATCH] home/git: bundle homebrewn external git commands --- home/home.nix | 2 +- home/programs/{git.nix => git/default.nix} | 22 +++++++++++-- .../git/scripts/git-switch-interactive.sh | 11 +++++++ home/programs/git/scripts/git-tcommit.sh | 31 +++++++++++++++++++ 4 files changed, 63 insertions(+), 3 deletions(-) rename home/programs/{git.nix => git/default.nix} (85%) create mode 100644 home/programs/git/scripts/git-switch-interactive.sh create mode 100755 home/programs/git/scripts/git-tcommit.sh diff --git a/home/home.nix b/home/home.nix index b6898dc..068d7a6 100644 --- a/home/home.nix +++ b/home/home.nix @@ -14,7 +14,7 @@ in { ./programs/direnv.nix ./programs/gdb.nix ./programs/gh.nix - ./programs/git.nix + ./programs/git ./programs/gpg.nix ./programs/less.nix ./programs/neovim.nix diff --git a/home/programs/git.nix b/home/programs/git/default.nix similarity index 85% rename from home/programs/git.nix rename to home/programs/git/default.nix index 598ceca..ac5efe9 100644 --- a/home/programs/git.nix +++ b/home/programs/git/default.nix @@ -1,4 +1,7 @@ -{ pkgs, ... }: +{ config, pkgs, lib, ... }: +let + cfg = config.programs.git; +in { programs.git = { enable = true; @@ -30,7 +33,6 @@ graphv = "log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all"; forcepush = "push --force-with-lease --force-if-includes"; authors = "shortlog --summary --numbered --email"; - switch-interactive = "!cat <(git branch) <(git branch -r) | grep -v '^\\*\\|HEAD ->' | ${pkgs.fzf}/bin/fzf --reverse --info=inline --preview 'echo {} | xargs git show --color' | sed 's|\\s*.*/||' | xargs git switch"; si = "switch-interactive"; rebase-author = "rebase -i -x \"git commit --amend --reset-author -CHEAD\""; }; @@ -147,4 +149,20 @@ "shell.nix" ]; }; + + home.packages = [ + (pkgs.writeShellApplication { + name = "git-tcommit"; + runtimeInputs = with pkgs; [ cfg.package coreutils ]; + text = lib.fileContents ./scripts/git-tcommit.sh; + }) + (pkgs.writeShellApplication { + name = "git-switch-interactive"; + runtimeInputs = with pkgs; [ cfg.package fzf gnused coreutils ]; + text = lib.fileContents ./scripts/git-switch-interactive.sh; + excludeShellChecks = [ + "SC2001" # (style): See if you can use ${variable//search/replace} instead. (sed invocation) + ]; + }) + ]; } diff --git a/home/programs/git/scripts/git-switch-interactive.sh b/home/programs/git/scripts/git-switch-interactive.sh new file mode 100644 index 0000000..c2d7717 --- /dev/null +++ b/home/programs/git/scripts/git-switch-interactive.sh @@ -0,0 +1,11 @@ +set -euo pipefail + +if [ -n "${1:-}" ]; then + git switch "$1" + exit 0 +fi + +BRANCHES=$(cat <(git branch) <(git branch --remotes) | grep --invert-match '^\*\|HEAD ->' | sed 's|^\s*||') +CHOSEN_BRANCH=$(fzf --reverse --info=inline --preview 'git show --color {}' <<<"$BRANCHES") +CLEAN_BRANCH_NAME=$(sed 's|^\s*.*/||' <<<"$CHOSEN_BRANCH") +git switch "$CLEAN_BRANCH_NAME" \ No newline at end of file diff --git a/home/programs/git/scripts/git-tcommit.sh b/home/programs/git/scripts/git-tcommit.sh new file mode 100755 index 0000000..991343d --- /dev/null +++ b/home/programs/git/scripts/git-tcommit.sh @@ -0,0 +1,31 @@ +set -euo pipefail + +HOUR_SHIFT="$1" +shift + +if [[ $HOUR_SHIFT == -* ]]; then + HOUR_SHIFT="${HOUR_SHIFT#*-}" + OPERATOR="-" +else + OPERATOR="+" +fi + +DATE=$(date -d "now ${OPERATOR} ${HOUR_SHIFT} hours") + +while true; do + echo "Commiting with date: ${DATE} (${OPERATOR}${HOUR_SHIFT}h)" + read -rp "Do you want to proceed? (y/n) " yn + + case $yn in + [yY] ) + break;; + [nN] ) + exit;; + * ) echo invalid response;; + esac +done + +export GIT_COMMITTER_DATE="${DATE}" +export GIT_AUTHOR_DATE="${DATE}" + +git commit "$@"