commit 73101ac33fa1d93e87dc075060f15a776d710bdf Author: abs3nt Date: Mon Oct 7 18:35:43 2024 -0700 initial diff --git a/completions/_repo b/completions/_repo new file mode 100644 index 0000000..7928860 --- /dev/null +++ b/completions/_repo @@ -0,0 +1,22 @@ +#compdef repo + +_arguments -s \ + '1: :->cmds' \ + '2: :->args' \ + '*::options:->opts' && return 0 + +cmds=( + 'get:Clone a repository' + 'open:Open the repository in a browser' + 'aur:Clone an AUR repository' + 'list:List all repositories' + 'go:Navigate to a repository' + 'goto:Navigate to a repository' + 'new:Create a new repository' + 'create:Create a new repository' + 'help:Show help message' +) + +if [[ $state == cmds ]]; then + _describe -t commands "repo commands" cmds +fi diff --git a/functions/aur.zsh b/functions/aur.zsh new file mode 100644 index 0000000..4713d8d --- /dev/null +++ b/functions/aur.zsh @@ -0,0 +1 @@ +repo_aur() { repo_clone "aur" "$1"; } diff --git a/functions/clone.zsh b/functions/clone.zsh new file mode 100644 index 0000000..b35a0a9 --- /dev/null +++ b/functions/clone.zsh @@ -0,0 +1,21 @@ +repo_clone() { + if [ -z "$2" ]; then + echo "Error: Repository path is required" + return 1 + fi + + local repo_prefix="$1" + cleaned=$(clean_repo_path "$2") + output_path="$REPO_BASE_DIR/$cleaned" + suffix=".git" + mkdir -p "${output_path%"$suffix"}" + + if [ ! -d "$output_path/.git" ]; then + repourl=$(echo "$repo_prefix@$cleaned" | sed -e "s/\//:/1") + echo "Cloning $repourl to ${output_path%"$suffix"}..." + git clone "$repourl" "${output_path%"$suffix"}" + else + echo "Repository already exists: ${output_path%"$suffix"}" + fi + post_repo_clone "${output_path%"$suffix"}" +} diff --git a/functions/core.zsh b/functions/core.zsh new file mode 100644 index 0000000..289c2bb --- /dev/null +++ b/functions/core.zsh @@ -0,0 +1,14 @@ +clean_repo_path() { + stripped=$1 + stripped="${stripped#http://}" + stripped="${stripped#https://}" + stripped="${stripped#git@}" + stripped="${stripped#ssh://}" + stripped="${stripped#aur@}" + stripped=$(echo "$stripped" | sed -e "s/:/\//1") + + if [[ ! "$stripped" =~ "/" ]]; then + stripped="github.com/$stripped" + fi + echo "$stripped" +} diff --git a/functions/get.zsh b/functions/get.zsh new file mode 100644 index 0000000..1272e30 --- /dev/null +++ b/functions/get.zsh @@ -0,0 +1,2 @@ +repo_get() { repo_clone "git" "$1"; } + diff --git a/functions/goto.zsh b/functions/goto.zsh new file mode 100644 index 0000000..0431595 --- /dev/null +++ b/functions/goto.zsh @@ -0,0 +1,11 @@ +repo_goto() { + if [ -z "$1" ]; then + echo "Error: Repository path is required" + return 1 + fi + + cleaned=$(clean_repo_path "$1") + output_path="$REPO_BASE_DIR/$cleaned" + suffix=".git" + post_repo_goto "${output_path%"$suffix"}" +} diff --git a/functions/help.zsh b/functions/help.zsh new file mode 100644 index 0000000..9ed14d3 --- /dev/null +++ b/functions/help.zsh @@ -0,0 +1,18 @@ +repo_help() { + cat < + +Commands: + get Clone a repository to the repos directory + open Open the current repository in the browser + aur Clone an AUR repository + list List all repositories + go|goto Navigate to a repository + new|create Create a new repository + help Show this help message + +Examples: + repo get github.com/user/repo + repo open +EOF +} diff --git a/functions/list.zsh b/functions/list.zsh new file mode 100644 index 0000000..dd4e666 --- /dev/null +++ b/functions/list.zsh @@ -0,0 +1,6 @@ +list_repos() { + find "$REPO_BASE_DIR" -type d -name ".git" | while read -r git_dir; do + parent_dir=$(dirname "$git_dir") + echo "${parent_dir##*/}" | sed 's/\./_/g' + done +} diff --git a/functions/new.zsh b/functions/new.zsh new file mode 100644 index 0000000..dbf0c92 --- /dev/null +++ b/functions/new.zsh @@ -0,0 +1,18 @@ +repo_new() { + if [ -z "$1" ]; then + echo "Error: Repository path is required" + return 1 + fi + + cleaned=$(clean_repo_path "$1") + output_path="$REPO_BASE_DIR/$cleaned" + mkdir -p "${output_path%.git}" + + if [ ! -d "$output_path/.git" ]; then + git init "${output_path%.git}" + else + echo "Repository already exists: ${output_path%.git}" + fi + + post_repo_new "${output_path%.git}" +} diff --git a/functions/open.zsh b/functions/open.zsh new file mode 100644 index 0000000..95fc095 --- /dev/null +++ b/functions/open.zsh @@ -0,0 +1,6 @@ +repo_open() { + remote=$(git remote get-url origin) + remote=${remote/git@/https:\/\/} + remote=${remote/:/\//} + xdg-open "$remote" +} diff --git a/repo.plugin.zsh b/repo.plugin.zsh new file mode 100644 index 0000000..6c42df9 --- /dev/null +++ b/repo.plugin.zsh @@ -0,0 +1,11 @@ +REPO_BASE_DIR="${REPO_BASE_DIR:-$HOME/repos}" + +for script in "${0:A:h}/functions/"*.zsh; do + source "$script" +done + +if [[ -d "${0:A:h}/completion" ]]; then + fpath=("${0:A:h}/completion" $fpath) + autoload -Uz compinit && compinit +fi +