From 4aae19e729814521a00449af628f386f5af44ee3 Mon Sep 17 00:00:00 2001 From: Felix Schneider <99918022+trueberryless@users.noreply.github.com> Date: Thu, 22 Jan 2026 19:05:49 +0100 Subject: [PATCH] feat: git co author extension tool --- modules/git-tools.nix | 61 ++++++++++++++++++++++++++++++++++++++++ modules/home-manager.nix | 4 +++ 2 files changed, 65 insertions(+) create mode 100644 modules/git-tools.nix diff --git a/modules/git-tools.nix b/modules/git-tools.nix new file mode 100644 index 0000000..53a050c --- /dev/null +++ b/modules/git-tools.nix @@ -0,0 +1,61 @@ +{ pkgs, ... }: + +let + git-ucommit = pkgs.writeShellScriptBin "git-ucommit" '' + args=() + message="" + username="" + + while [[ $# -gt 0 ]]; do + case "$1" in + -u|--user) + username="$2" + shift 2 + ;; + -m|--message) + message="$2" + shift 2 + ;; + *) + args+=("$1") + shift + ;; + esac + done + + if [[ -n "$username" ]]; then + user_json=$(${pkgs.gh}/bin/gh api users/"$username") + name=$(echo "$user_json" | ${pkgs.jq}/bin/jq -r '.name // .login') + id=$(echo "$user_json" | ${pkgs.jq}/bin/jq -r '.id') + + co_author="Co-authored-by: $name <$id+$username@users.noreply.github.com>" + + if [[ -n "$message" ]]; then + # Use interpret-trailers on the string directly + final_msg=$(echo -e "$message" | ${pkgs.git}/bin/git interpret-trailers --trailer "$co_author") + ${pkgs.git}/bin/git commit "''${args[@]}" -m "$final_msg" + else + # If no -m was provided, we let git open the editor with the trailer pre-filled + ${pkgs.git}/bin/git commit "''${args[@]}" --trailer "$co_author" -e + fi + else + # Fallback to standard commit if no username is provided + if [[ -n "$message" ]]; then + ${pkgs.git}/bin/git commit "''${args[@]}" -m "$message" + else + ${pkgs.git}/bin/git commit "''${args[@]}" + fi + fi + ''; +in +{ + home.packages = [ + pkgs.gh + pkgs.jq + git-ucommit + ]; + + home.shellAliases = { + gcommit = "git-ucommit"; + }; +} diff --git a/modules/home-manager.nix b/modules/home-manager.nix index 0334fe7..9ae612c 100644 --- a/modules/home-manager.nix +++ b/modules/home-manager.nix @@ -6,6 +6,10 @@ home.stateVersion = "25.11"; programs.home-manager.enable = true; + imports = [ + ./git-tools.nix + ]; + home.file = { ".gitconfig".source = ../dotfiles/git/config; ".gitignore".source = ../dotfiles/git/ignore; -- 2.51.2