Commit fa06acf60267

Vincent Demeester <vincent@sbr.pm>
2026-01-26 13:54:00
refactor(lazyworktree): source upstream shell functions
Instead of duplicating shell functions in the module, source them from the package's share/lazyworktree/functions.shell. This removes the need for worktreeDirectory and shellWrapperName options as the upstream provides worktree_jump and _worktree_jump functions. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 88c6b99
Changed files (1)
home
home/modules/lazyworktree.nix
@@ -16,12 +16,6 @@ let
       "Library/Application Support"
     else
       config.xdg.configHome;
-
-  worktreeDir =
-    if cfg.worktreeDirectory != null then
-      cfg.worktreeDirectory
-    else
-      "${config.home.homeDirectory}/.local/share/worktrees";
 in
 {
   options.programs.lazyworktree = {
@@ -29,16 +23,6 @@ in
 
     package = lib.mkPackageOption pkgs "lazyworktree" { nullable = true; };
 
-    worktreeDirectory = lib.mkOption {
-      type = types.nullOr types.str;
-      default = null;
-      example = "~/worktrees";
-      description = ''
-        Override the default worktree root directory.
-        Defaults to `~/.local/share/worktrees`.
-      '';
-    };
-
     settings = lib.mkOption {
       type = yamlFormat.type;
       default = { };
@@ -69,15 +53,6 @@ in
 
     enableBashIntegration = lib.mkEnableOption "bash integration";
 
-    shellWrapperName = lib.mkOption {
-      type = types.str;
-      default = "lwt";
-      example = "wt";
-      description = ''
-        Name of the shell wrapper function for jumping to worktrees.
-      '';
-    };
-
     aliases = lib.mkOption {
       type = types.attrsOf types.str;
       default = { };
@@ -104,122 +79,23 @@ in
 
     programs =
       let
-        # Helper to extract repo slug from git remote
-        gitRepoSlugFunction = ''
-          _lwt_git_repo_slug() {
-              local dir="$1"
-              local url slug
-
-              url=$(cd "$dir" 2>/dev/null && git remote get-url origin 2>/dev/null) || return 1
-              slug=$(echo "$url" | sed -E 's#^.*[:/]([^/]+/[^/]+)(\.git)?$#\1#')
-              [[ -n "$slug" ]] || return 1
-              echo "$slug"
-          }
-        '';
-
-        # Shell function to jump to worktrees with directory change on exit
-        shellWrapperFunction = ''
-          ${cfg.shellWrapperName}() {
-              local dir="$1"; shift
-              local repo slug
-
-              if [[ -z "$dir" || ! -d "$dir" ]]; then
-                  echo >&2 "${cfg.shellWrapperName}: invalid directory: $dir"
-                  return 1
-              fi
-
-              slug=$(_lwt_git_repo_slug "$dir" 2>/dev/null)
-              repo="''${slug:-$(basename "$dir")}"
-
-              local wt_root="${worktreeDir}/$repo"
-
-              # Direct jump if worktree name provided
-              if [[ -n "$1" && -d "$wt_root/$1" ]]; then
-                  cd "$wt_root/$1" || return 1
-                  return
-              fi
-
-              cd "$dir" || return 1
-
-              local tmp selected
-              tmp=$(mktemp "''${TMPDIR:-/tmp}/lazyworktree.selection.XXXXXX") || return 1
-              lazyworktree --output-selection="$tmp"
-              local rc=$?
-              if [[ $rc -ne 0 ]]; then
-                  rm -f "$tmp"
-                  return $rc
-              fi
-
-              if [[ -s "$tmp" ]]; then
-                  selected=$(<"$tmp")
-                  [[ -n "$selected" && -d "$selected" ]] && cd "$selected" || true
-              fi
-              rm -f "$tmp"
-          }
-        '';
-
-        # Function to jump to last selected worktree
-        goLastFunction = ''
-          ${cfg.shellWrapperName}_go_last() {
-              local dir="$1"
-              local repo slug last_selected selected
-
-              if [[ -z "$dir" || ! -d "$dir" ]]; then
-                  echo >&2 "${cfg.shellWrapperName}_go_last: invalid directory: $dir"
-                  return 1
-              fi
-
-              slug=$(_lwt_git_repo_slug "$dir" 2>/dev/null)
-              repo="''${slug:-$(basename "$dir")}"
-
-              last_selected="${worktreeDir}/$repo/.last-selected"
-              if [[ -f "$last_selected" ]]; then
-                  selected=$(<"$last_selected")
-                  if [[ -n "$selected" && -d "$selected" ]]; then
-                      cd "$selected" || return 1
-                      return
-                  fi
-              fi
-
-              echo >&2 "No last selected worktree found"
-              return 1
-          }
-        '';
-
-        shellFunctions = ''
+        # Source upstream shell functions from the package
+        sourceUpstream = ''
           # lazyworktree shell integration
-          ${gitRepoSlugFunction}
-          ${shellWrapperFunction}
-          ${goLastFunction}
-        '';
-
-        # Zsh completion for alias functions (completes worktree names)
-        zshAliasCompletion = ''
-          _lwt_complete_worktrees() {
-              local dir="$1"
-              local repo slug wt_root
-
-              slug=$(_lwt_git_repo_slug "$dir" 2>/dev/null)
-              repo="''${slug:-$(basename "$dir")}"
-
-              wt_root="${worktreeDir}/$repo"
-              [[ -d "$wt_root" ]] || return
-
-              local -a dirs
-              dirs=(''${wt_root}/*(/:t))
-              _describe 'worktree' dirs
-          }
+          source ${cfg.package}/share/lazyworktree/functions.shell
         '';
 
+        # Alias definitions that wrap worktree_jump
         aliasDefinitions = lib.concatStringsSep "\n" (
           lib.mapAttrsToList (name: dir: ''
-            ${name}() { ${cfg.shellWrapperName} ${dir} "$@" }
+            ${name}() { worktree_jump ${dir} "$@"; }
           '') cfg.aliases
         );
 
+        # Zsh completion setup for aliases
         zshAliasCompletions = lib.concatStringsSep "\n" (
           lib.mapAttrsToList (name: dir: ''
-            _${name}() { _lwt_complete_worktrees ${dir} }
+            _${name}() { _worktree_jump ${dir}; }
             compdef _${name} ${name}
           '') cfg.aliases
         );
@@ -227,13 +103,12 @@ in
       in
       {
         bash.initExtra = mkIf cfg.enableBashIntegration ''
-          ${shellFunctions}
+          ${sourceUpstream}
           ${aliasDefinitions}
         '';
 
         zsh.initContent = mkIf cfg.enableZshIntegration ''
-          ${shellFunctions}
-          ${zshAliasCompletion}
+          ${sourceUpstream}
           ${aliasDefinitions}
           ${zshAliasCompletions}
         '';