From b5414a38415f084d889fa99f890ed6a61aab8bb8 Mon Sep 17 00:00:00 2001 From: Haseeb Majid Date: Wed, 1 Nov 2023 14:47:45 +0000 Subject: [PATCH] feat: add new plugin git-worktree (#678) Co-authored-by: Haseeb Majid --- plugins/default.nix | 1 + plugins/git/git-worktree.nix | 82 +++++++++++++++++++ .../test-sources/plugins/git/git-worktree.nix | 29 +++++++ 3 files changed, 112 insertions(+) create mode 100644 plugins/git/git-worktree.nix create mode 100644 tests/test-sources/plugins/git/git-worktree.nix diff --git a/plugins/default.nix b/plugins/default.nix index 70193c07..f8b48676 100644 --- a/plugins/default.nix +++ b/plugins/default.nix @@ -34,6 +34,7 @@ ./git/diffview.nix ./git/fugitive.nix + ./git/git-worktree.nix ./git/gitblame.nix ./git/gitgutter.nix ./git/gitmessenger.nix diff --git a/plugins/git/git-worktree.nix b/plugins/git/git-worktree.nix new file mode 100644 index 00000000..72824a38 --- /dev/null +++ b/plugins/git/git-worktree.nix @@ -0,0 +1,82 @@ +{ + pkgs, + config, + lib, + ... +}: +with lib; let + cfg = config.plugins.git-worktree; + helpers = import ../helpers.nix {inherit lib;}; +in { + options = { + plugins.git-worktree = { + enable = mkEnableOption "git-worktree"; + + package = helpers.mkPackageOption "git-worktree" pkgs.vimPlugins.git-worktree-nvim; + + enableTelescope = mkEnableOption "telescope integration"; + + changeDirectoryCommand = helpers.defaultNullOpts.mkStr "cd" '' + The vim command used to change to the new worktree directory. + Set this to `tcd` if you want to only change the `pwd` for the current vim Tab. + ''; + + updateOnChange = helpers.defaultNullOpts.mkBool true '' + If set to true updates the current buffer to point to the new work tree if the file is found in the new project. + Otherwise, the following command will be run. + ''; + + updateOnChangeCommand = helpers.defaultNullOpts.mkStr "e ." '' + The vim command to run during the `update_on_change` event. + Note, that this command will only be run when the current file is not found in the new worktree. + This option defaults to `e .` which opens the root directory of the new worktree. + ''; + + clearJumpsOnChange = helpers.defaultNullOpts.mkBool true '' + If set to true every time you switch branches, your jumplist will be cleared so that you don't + accidentally go backward to a different branch and edit the wrong files. + ''; + + autopush = helpers.defaultNullOpts.mkBool false '' + When creating a new worktree, it will push the branch to the upstream then perform a `git rebase`. + ''; + }; + }; + + config = let + setupOptions = with cfg; { + enabled = cfg.enable; + change_directory_command = cfg.changeDirectoryCommand; + update_on_change = cfg.updateOnChange; + update_on_change_command = cfg.updateOnChangeCommand; + clearjumps_on_change = cfg.clearJumpsOnChange; + inherit autopush; + }; + in + mkIf cfg.enable { + assertions = [ + { + assertion = cfg.enableTelescope -> config.plugins.telescope.enable; + message = ''Nixvim: The git-worktree telescope integration needs telescope to function as intended''; + } + ]; + + extraPlugins = with pkgs.vimPlugins; [ + cfg.package + plenary-nvim + ]; + + extraPackages = [pkgs.git]; + + extraConfigLua = let + telescopeCfg = ''require("telescope").load_extension("git_worktree")''; + in '' + require('git-worktree').setup(${helpers.toLuaObject setupOptions}) + ${ + if cfg.enableTelescope + then telescopeCfg + else "" + } + ''; + }; +} diff --git a/tests/test-sources/plugins/git/git-worktree.nix b/tests/test-sources/plugins/git/git-worktree.nix new file mode 100644 index 00000000..d9624a46 --- /dev/null +++ b/tests/test-sources/plugins/git/git-worktree.nix @@ -0,0 +1,29 @@ +{ + empty = { + plugins.git-worktree.enable = true; + }; + + telescopeEnabled = { + plugins.telescope = { + enable = true; + }; + + plugins.git-worktree = { + enable = true; + + enableTelescope = true; + changeDirectoryCommand = "tcd"; + updateOnChange = true; + updateOnChangeCommand = "e ."; + clearJumpsOnChange = true; + }; + }; + + telescopeDisabled = { + plugins.git-worktree = { + enable = true; + + enableTelescope = false; + }; + }; +}