nixos-configuration/home-manager/modules/git.nix
2023-11-08 23:35:55 +00:00

156 lines
5.3 KiB
Nix

{ nixosConfig, pkgs, ... }:
{
programs = {
git = {
enable = true;
userName = nixosConfig.jalr.git.user.name;
userEmail = nixosConfig.jalr.git.user.email;
signing = {
key = nixosConfig.jalr.gpg.defaultKey;
signByDefault = nixosConfig.jalr.git.signByDefault;
};
extraConfig = {
init.defaultBranch = "main";
core.pager = "${pkgs.diff-so-fancy}/bin/diff-so-fancy | less --tabs=4 -RFX";
diff.sops.textconv = "${pkgs.sops}/bin/sops -d";
pull.ff = "only";
alias.find-merge = "!sh -c 'commit=$0 && branch=\${1:-HEAD} && (git rev-list $commit..$branch --ancestry-path | cat -n; git rev-list $commit..$branch --first-parent | cat -n) | sort -k2 -s | uniq -f1 -d | sort -n | tail -1 | cut -f2'";
alias.show-merge = "!sh -c 'merge=$(git find-merge $0 $1) && [ -n \"$merge\" ] && git show $merge'";
};
};
fish = {
shellAbbrs = {
ga = "git add";
gam = "git commit --amend";
gap = "git add --patch";
gb = "git branch";
gbd = "git branch --delete";
gbl = "git blame";
gbm = "git branch --move";
gc = "git clean";
gcl = "git clone";
gcm = "git commit --verbose";
gco = "git checkout";
gd = "git diff";
gdc = "git diff --cached";
gf = "git fetch";
ginit = "git init";
gl = "git log";
gpll = "git pull";
gpsh = "git push";
grb = "git rebase --autostash";
grbi = "git rebase --autostash --interactive --autosquash \(git merge-base HEAD origin/master\)";
gr = "git restore";
grs = "git restore --staged";
grst = "git reset";
gs = "git status";
gsh = "git show";
gsm = "git submodule";
gsmi = "git submodule init";
gsmu = "git submodule update";
gsp = "git stash pop";
gst = "git stash";
gsw = "git switch";
gswc = "git switch -c";
gwl = "git worktree list";
gwr = "git worktree remove";
};
functions = {
#function gwa -d 'git worktree add'
gwa = {
description = "Add worktree";
body = ''
set -l dir (basename $argv[1])
git worktree add ../$dir -b (whoami)/$argv[1] && cd ../$dir
'';
};
# function git -d 'git wrapper function' --wraps git
git = {
description = "git wrapper function";
wraps = "git";
body = ''
if test (count $argv) -eq 0
command git
return $status
else
switch $argv[1]
case push
command git $argv
set -l git_result $status
if [ $git_result -eq 128 ]
set set_upstream (command git push 3>&1 1>&2 2>&3 3>&- | grep -o 'git push --set-upstream.*$')
if [ ! -z "$set_upstream" ]
echo "set upstream: $set_upstream"
commandline $set_upstream
commandline -f repaint
end
end
return $git_result
case '*'
command git $argv
return $status
end
end
'';
};
git_pick-commit_merge-base_origin_master = {
description = "fuzzy find a commit hash";
body = ''
git log --oneline (git merge-base HEAD origin/master)..HEAD | ${pkgs.fzf}/bin/fzf --preview='git show (echo {} | cut -d" " -f 1)' --preview-window=top:75% | cut -d" " -f 1
'';
};
gfix = {
description = "git commit --fixup with fuzzy find commmit picker";
body = ''
set commit (git_pick-commit_merge-base_origin_master)
commandline "git commit --fixup=$commit"
'';
};
".g" = {
description = "change directory to repository root";
body = ''
set git_repo_dir (git rev-parse --show-toplevel 2>/dev/null)
if [ "$git_repo_dir" ]
cd $git_repo_dir
else
return 1
end
'';
};
"..g" = {
description = "change directory to superproject working tree";
body = ''
set git_repo_dir (git rev-parse --show-superproject-working-tree 2>/dev/null)
if [ "$git_repo_dir" ]
cd $git_repo_dir
else
return 1
end
'';
};
"gwc" = {
description = "change directory to common repository";
body = ''
set repo_dir (git rev-parse --show-superproject-working-tree)
if ! [ "$repo_dir" ]
set repo_dir (git rev-parse --show-toplevel)
end
set common_git_dir (git -C $repo_dir rev-parse --path-format=absolute --git-common-dir)
set common_dir (dirname $common_git_dir)
if [ -d "$common_dir" ]
if [ "$argv[1]" ]
cd $common_dir/(realpath --relative-to=(git rev-parse --show-toplevel) $argv[1])
else
cd $common_dir
end
else
return 1
end
'';
};
};
};
};
}