# 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 = {
      user =
        let
          name = "Lander Van den Bulcke";
          signingKey = "CA5B1C34E649BF92";

          mkIdentity = email: {
            name = name;
            signingKey = signingKey;
            email = email;
          };
        in
        {
          # extremely important, otherwise git will attempt to guess a default user identity. see `man git-config` for more details
          useConfigOnly = true;
          personal = mkIdentity "landervandenbulcke@gmail.com";
          inuits = mkIdentity "landervdb@inuits.eu";
          olly = mkIdentity "landervdb@o11y.eu";
          basf = mkIdentity "lander.van-den-bulcke@partners.basf.com";
        };

      # 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;

      # diff3 merge
      merge.conflictStyle = "diff3";
    };

    # 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";
    };

    # use difftastic
    difftastic = {
      enable = true;
      enableAsDifftool = true;
    };
  };

  # use mergiraf
  programs.mergiraf.enable = true;
}
