From 329f48882efad923b85b19c0c86e42a25537e689 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Thu, 4 Apr 2024 20:16:56 +0200 Subject: [PATCH] Add git configuration --- home/programs/terminal/default.nix | 1 + home/programs/terminal/tools/default.nix | 5 ++ home/programs/terminal/tools/git/aliases.nix | 81 ++++++++++++++++++++ home/programs/terminal/tools/git/default.nix | 58 ++++++++++++++ home/programs/terminal/tools/git/gh.nix | 16 ++++ home/programs/terminal/tools/git/ignores.nix | 39 ++++++++++ hosts/vbox_nix/default.nix | 1 + options/home/default.nix | 4 + options/home/git.nix | 18 +++++ 9 files changed, 223 insertions(+) create mode 100644 home/programs/terminal/tools/default.nix create mode 100644 home/programs/terminal/tools/git/aliases.nix create mode 100644 home/programs/terminal/tools/git/default.nix create mode 100644 home/programs/terminal/tools/git/gh.nix create mode 100644 home/programs/terminal/tools/git/ignores.nix create mode 100644 options/home/git.nix diff --git a/home/programs/terminal/default.nix b/home/programs/terminal/default.nix index 5159b60..5615862 100644 --- a/home/programs/terminal/default.nix +++ b/home/programs/terminal/default.nix @@ -1,5 +1,6 @@ _: { imports = [ ./shell + ./tools ]; } diff --git a/home/programs/terminal/tools/default.nix b/home/programs/terminal/tools/default.nix new file mode 100644 index 0000000..42cb526 --- /dev/null +++ b/home/programs/terminal/tools/default.nix @@ -0,0 +1,5 @@ +_: { + imports = [ + ./git + ]; +} diff --git a/home/programs/terminal/tools/git/aliases.nix b/home/programs/terminal/tools/git/aliases.nix new file mode 100644 index 0000000..b9968bf --- /dev/null +++ b/home/programs/terminal/tools/git/aliases.nix @@ -0,0 +1,81 @@ +{ + programs.git = { + aliases = { + quickclone = "clone --single-branch --depth=1"; + bareclone = "!sh -c 'git clone --bare \"$0\" \"$1\"/.bare; echo \"gitdir: ./.bare\" > \"$1/.git\"'"; + bareinit = "!sh -c 'git init --bare \"$0\"/.bare; echo \"gitdir: ./.bare\" > \"$0/.git\"'"; + cleanup = "!default_branch=$(git remote show origin | awk '/HEAD branch/ {print $NF}'); git remote prune origin && git checkout -q $default_branch && git for-each-ref refs/heads/ '--format=%(refname:short)' | while read branch; do mergeBase=$(git merge-base $default_branch $branch) && [[ $(git cherry $default_branch $(git commit-tree $(git rev-parse $branch^{tree}) -p $mergeBase -m _)) == '-'* ]] && git branch -D $branch; done"; + + m = "merge"; + f = "fetch"; + + p = "push"; + pu = "!git push --set-upstream origin `git symbolic-ref --short HEAD`"; + pf = "push --force"; + pl = "pull"; + + s = "status --short --branch"; + ss = "status"; + + ch = "checkout"; + chb = "checkout -b"; + + undo = "reset --soft HEAD~"; + redo = "reset HEAD@{1}"; + unstage = "restore --staged"; + + c = "commit"; + ca = "commit --ammend"; + ci = "commit --interactive"; + cm = "commit --message"; + cv = "commit --verbose"; + + a = "add"; + aa = "add --all"; + ap = "add --patch"; + au = "add --update"; + + d = "diff"; + dc = "diff --cached"; + ds = "diff --staged"; + dw = "diff --word-diff"; + dcm = "!sh -c 'git diff $0~ $0'"; + linediff = "!sh -c 'git diff --unified=0 $1 $2 | grep -Po \"(?<=^\\+)(?!\\+\\+).*\" '"; + + b = "branch"; + ba = "branch --all"; + bd = "branch --delete"; + bD = "branch --delete --force"; + bm = "branch --move"; + bM = "branch --move --force"; + bb = "!~/.local/bin/scripts/cli/better-git-branch"; + + r = "rebase"; + ri = "rebase -i"; + rc = "rebase --continue"; + + l = "log --oneline --decorate --all --graph"; + lp = "log --patch"; + lo = "log --pretty=oneline --abbrev-commit --graph"; + lg = "log --all --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --"; + loog = "log --format=fuller --show-signature --all --color --decorate --graph"; + + make-patch = "diff --no-prefix --relative"; + + set-upstream = "!git branch --set-upstream-to=origin/`git symbolic-ref --short HEAD`"; + + staash = "stash --all"; + stash-staged = "!sh -c 'git stash --keep-index; git stash push -m \"staged\" --keep-index; git stash pop stash@{1}'"; + + 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'"; + show-merge = "!sh -c 'merge=$(git find-merge $0 $1) && [ -n \"$merge\" ] && git show $merge'"; + + tracked-files = "ls-tree --full-tree --name-only -r HEAD"; + tracked-text-files = "!git tracked-files | while IFS= read -r file; do mime_type=$(file -b --mime-type \"$file \"); [[ $mime_type == text/* ]] && echo \"$file \"; done"; + total-lines = "!git tracked-text-files | xargs cat | wc -l"; + total-files = "!git tracked-files | wc -l"; + total-commits = "!git log --oneline | wc -l"; + comitter-lines = "!git log --author=\"$1\" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf \"added lines: %s, removed lines: %s, total lines: %s\\n\", add, subs, loc }' #"; + }; + }; +} diff --git a/home/programs/terminal/tools/git/default.nix b/home/programs/terminal/tools/git/default.nix new file mode 100644 index 0000000..3ffa9c2 --- /dev/null +++ b/home/programs/terminal/tools/git/default.nix @@ -0,0 +1,58 @@ +{ osConfig, pkgs, ... }: +let + myGitConf = osConfig.myOptions.home-manager.git; +in +{ + imports = [ + ./gh.nix + ./ignores.nix + ./aliases.nix + ]; + + # TODO: Figure out how to manage gpg keys properly in nix/home-manager + # (right now, I'm importing my keys manually) + + programs.git = { + enable = true; + package = pkgs.gitAndTools.gitFull; + + userName = "ItsDrike"; + userEmail = "itsdrike@protonmail.com"; + + signing = { + signByDefault = myGitConf.signing.enabled; + key = myGitConf.signing.key; + }; + + delta.enable = true; + + extraConfig = { + init.defaultBranch = "main"; + merge.conflictstyle = "diff3"; + delta.line-numbers = true; + + push = { + default = "current"; + followTags = true; + }; + + url = { + "https://github.com/".insteadOf = "github:"; + "ssh://git@github.com/".pushInsteadOf = "github:"; + "https://gitlab.com/".insteadOf = "gitlab:"; + "ssh://git@gitlab.com/".pushInsteadOf = "gitlab:"; + "https://aur.archlinux.org/".insteadOf = "aur:"; + "ssh://aur@aur.archlinux.org/".pushInsteadOf = "aur:"; + "https://git.sr.ht/".insteadOf = "srht:"; + "ssh://git@git.sr.ht/".pushInsteadOf = "srht:"; + "https://codeberg.org/".insteadOf = "codeberg:"; + "ssh://git@codeberg.org/".pushInsteadOf = "codeberg:"; + }; + }; + }; + + home.packages = with pkgs; [ + gist # Manage github gists + act # Run github actions locally + ]; +} diff --git a/home/programs/terminal/tools/git/gh.nix b/home/programs/terminal/tools/git/gh.nix new file mode 100644 index 0000000..f10dc7f --- /dev/null +++ b/home/programs/terminal/tools/git/gh.nix @@ -0,0 +1,16 @@ +{ pkgs, ... }: { + programs.gh = { + enable = true; + gitCredentialHelper.enable = false; + extensions = with pkgs; [ + gh-dash # dashboard with pull requess and issues + gh-eco # explore the ecosystem + gh-cal # contributions calendar terminal viewer + # TODO: Include meiji163/gh-notify + ]; + settings = { + git_protocol = "ssh"; + prompt = "enabled"; + }; + }; +} diff --git a/home/programs/terminal/tools/git/ignores.nix b/home/programs/terminal/tools/git/ignores.nix new file mode 100644 index 0000000..b18e5cc --- /dev/null +++ b/home/programs/terminal/tools/git/ignores.nix @@ -0,0 +1,39 @@ +{ + programs.git.ignores = [ + # Python: + "__pycache__/" + "*.py[cod]" + "$py.class" + "venv" + ".venv" + ".pytest_cache" + ".mypy_cache" + + # C + ".tags" + "tags" + "*~" + "*.o" + "*.so" + "*.cmake" + "CmakeCache.txt" + "CMakeFiles/" + "cmake-build-debug/" + "compile_commands.json" + ".ccls*" + + # JavaScript + TypeScript + "node_modules/" + + # Editors + ".vscode/" + ".idea/" + ".replit" + ".spyproject/" + ".spyderproject" + ".neoconf.json" + + # Custom attributes for folders on Mac OS + ".DS_Store" + ]; +} diff --git a/hosts/vbox_nix/default.nix b/hosts/vbox_nix/default.nix index aedc8eb..8898eda 100644 --- a/hosts/vbox_nix/default.nix +++ b/hosts/vbox_nix/default.nix @@ -30,6 +30,7 @@ home-manager = { enabled = true; stateVersion = "23.11"; + git.signing.key = "FA2745890B7048C0"; }; }; } diff --git a/options/home/default.nix b/options/home/default.nix index 616182d..2ed801c 100644 --- a/options/home/default.nix +++ b/options/home/default.nix @@ -1,6 +1,10 @@ { lib, ... }: with lib; let in { + imports = [ + ./git.nix + ]; + options.myOptions.home-manager = { enabled = mkOption { type = types.bool; diff --git a/options/home/git.nix b/options/home/git.nix new file mode 100644 index 0000000..9cff09b --- /dev/null +++ b/options/home/git.nix @@ -0,0 +1,18 @@ +{ lib, ... }: with lib; let +in +{ + options.myOptions.home-manager.git = { + signing = { + enabled = mkOption { + type = types.bool; + default = true; + description = "Should commits and tags be sgined by default?"; + }; + key = mkOption { + type = types.str; + default = ""; + description = "The defaul GPG key fingerprint for signing."; + }; + }; + }; +}