From e286bb61897651e85b2035d02b0d4136cf53f897 Mon Sep 17 00:00:00 2001 From: abs3nt Date: Tue, 1 Jul 2025 15:07:16 -0700 Subject: [PATCH] fish --- fish-repotool/README.md | 59 ++++++++++++++++++++ fish-repotool/conf.d/repotool.fish | 9 +++ fish-repotool/functions/repotool.fish | 79 +++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 fish-repotool/README.md create mode 100644 fish-repotool/conf.d/repotool.fish create mode 100644 fish-repotool/functions/repotool.fish diff --git a/fish-repotool/README.md b/fish-repotool/README.md new file mode 100644 index 0000000..da766c4 --- /dev/null +++ b/fish-repotool/README.md @@ -0,0 +1,59 @@ +# Fish Repotool Plugin + +A Fish shell plugin for the repotool repository management system. + +## Installation + +### Using Fisher + +```fish +fisher install path/to/fish-repotool +``` + +### Manual Installation + +1. Copy the `conf.d/repotool.fish` file to your fish config directory: + ```fish + cp conf.d/repotool.fish ~/.config/fish/conf.d/ + ``` + +2. Copy the `functions/repotool.fish` file to your fish functions directory: + ```fish + cp functions/repotool.fish ~/.config/fish/functions/ + ``` + +## Configuration + +Set the `REPOTOOL_PATH` environment variable to specify where your repositories are stored: + +```fish +set -gx REPOTOOL_PATH "$HOME/my-repos" +``` + +If not set, it defaults to `$HOME/repo`. + +## Usage + +The plugin provides a `repo` alias that sources the repotool function with the following commands: + +- `repo get ` - Clone a repository if not found +- `repo worktree list` - List existing worktrees +- `repo worktree get ` - Create or go to a worktree +- `repo worktree root` - Return to the root directory +- `repo worktree remove ` - Remove a worktree +- `repo open` - Open the current repository in web browser + +## Hooks + +The plugin supports hooks that can be placed in `$REPOTOOL_PATH/.hooks/`: + +- `before__cd.fish` - Executed before changing directory +- `after__cd.fish` - Executed after changing directory + +Hooks can also have `.sh` extension or no extension at all. + +## Requirements + +- Fish shell 4.0 or later +- `jq` for JSON parsing +- The repotool binary installed at `$REPOTOOL_PATH/.bin/repotool` \ No newline at end of file diff --git a/fish-repotool/conf.d/repotool.fish b/fish-repotool/conf.d/repotool.fish new file mode 100644 index 0000000..7219675 --- /dev/null +++ b/fish-repotool/conf.d/repotool.fish @@ -0,0 +1,9 @@ +#!/usr/bin/env fish + +# Set default REPOTOOL_PATH if not already set +if not set -q REPOTOOL_PATH + set -gx REPOTOOL_PATH "$HOME/repo" +end + +# Create alias that sources the main repotool function +alias repo="source $REPOTOOL_PATH/.shell/repotool.fish" \ No newline at end of file diff --git a/fish-repotool/functions/repotool.fish b/fish-repotool/functions/repotool.fish new file mode 100644 index 0000000..5c26cd5 --- /dev/null +++ b/fish-repotool/functions/repotool.fish @@ -0,0 +1,79 @@ +#!/usr/bin/env fish + +# Set default REPOTOOL_PATH if not already set +if not set -q REPOTOOL_PATH + set -gx REPOTOOL_PATH "$HOME/repo" +end + +function _activate_hook + set -l hook_name $argv[1] + set -l response $argv[2] + + if test -f "$REPOTOOL_PATH/.hooks/$hook_name.fish" + source "$REPOTOOL_PATH/.hooks/$hook_name.fish" $response + return 0 + else if test -f "$REPOTOOL_PATH/.hooks/$hook_name.sh" + source "$REPOTOOL_PATH/.hooks/$hook_name.sh" $response + return 0 + else if test -f "$REPOTOOL_PATH/.hooks/$hook_name" + source "$REPOTOOL_PATH/.hooks/$hook_name" $response + return 0 + end + + return 1 +end + +function _parse_json + set -l response $argv[1] + set -l key $argv[2] + echo "$response" | jq -r ".$key" +end + +function _handle_response + set -l response $argv[1] + + # Validate JSON + if not echo "$response" | jq . >/dev/null 2>&1 + # Not valid JSON, write to stderr + echo "$response" >&2 + return + end + + # Check if response has an echo field + set -l echo_msg (echo "$response" | jq -r '.echo // empty') + if test -n "$echo_msg" + echo "$echo_msg" + end + + # Get hook name from response + set -l hook_name (echo "$response" | jq -r '.hook // empty') + + # Handle before hook if hook name is provided + if test -n "$hook_name" + _activate_hook "before_{$hook_name}_cd" "$response" + end + + # Check if response has a cd field + set -l cd_path (echo "$response" | jq -r '.cd // empty') + if test -n "$cd_path" + cd "$cd_path" + end + + # Handle after hook if hook name is provided + if test -n "$hook_name" + _activate_hook "after_{$hook_name}_cd" "$response" + end +end + +set TOOL_BIN "$REPOTOOL_PATH/.bin/repotool" + +# Pass all arguments to repotool and handle response +set -l response ($TOOL_BIN $argv) +set -l exit_code $status + +if test $exit_code -ne 0 + echo "Command failed with exit code $exit_code" >&2 + return $exit_code +end + +_handle_response "$response" \ No newline at end of file