# adapted from https://pickard.cc/posts/git-identity-home-manager/

{
  pkgs,
  ...
}:

let
  # put a shell script into the nix store
  gitIdentity = pkgs.writeShellScriptBin "git-identity" (builtins.readFile ./git-identity);
in
{
  # we will use the excellent fzf in our `git-identity` script, so let's make sure it's available
  # let's add the gitIdentity script to the path as well
  home.packages = with pkgs; [
    gitIdentity
    fzf
    tig
  ];

  programs.git = {
    enable = true;
    package = pkgs.gitAndTools.gitFull;

    extraConfig = {
      # extremely important, otherwise git will attempt to guess a default user identity. see `man git-config` for more details
      user.useConfigOnly = true;

      # the `inuits` identity
      user.inuits.name = "Lander Van den Bulcke";
      user.inuits.email = "landervdb@inuits.eu";
      user.inuits.signingKey = "CA5B1C34E649BF92";

      # the `olly` identity
      user.olly.name = "Lander Van den Bulcke";
      user.olly.email = "landervdb@o11y.eu";
      user.olly.signingKey = "CA5B1C34E649BF92";

      # the `personal` identity
      user.personal.name = "Lander Van den Bulcke";
      user.personal.email = "landervandenbulcke@gmail.com";
      user.personal.signingKey = "CA5B1C34E649BF92";

      # editor
      core.editor = "nvim";

      # line up default branch with github/codeberg/forgejo
      init.defaultBranch = "main";

      # always rebase xx
      pull.rebase = true;

      push.autoSetupRemote = true;
      rebase.autosquash = true;
      commit.verbose = true;
      diff.algorithm = "histogram";
      transfer.fsckobjects = true;
      fetch.fsckobjects = true;
      receive.fsckObjects = true;
      rerere.enabled = true;

      # sign your commits!
      commit.gpgsign = true;
    };
    # This is optional, as `git identity` will call the `git-identity` script by itself, however
    # setting it up explicitly as an alias gives you autocomplete
    aliases = {
      identity = "! git-identity";
      id = "! git-identity";
    };
  };
}
