This commit is contained in:
abs3nt 2024-10-07 18:35:43 -07:00
commit 73101ac33f
Signed by: abs3nt
GPG Key ID: A7BD96A8BAB04C09
11 changed files with 130 additions and 0 deletions

22
completions/_repo Normal file
View File

@ -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

1
functions/aur.zsh Normal file
View File

@ -0,0 +1 @@
repo_aur() { repo_clone "aur" "$1"; }

21
functions/clone.zsh Normal file
View File

@ -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"}"
}

14
functions/core.zsh Normal file
View File

@ -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"
}

2
functions/get.zsh Normal file
View File

@ -0,0 +1,2 @@
repo_get() { repo_clone "git" "$1"; }

11
functions/goto.zsh Normal file
View File

@ -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"}"
}

18
functions/help.zsh Normal file
View File

@ -0,0 +1,18 @@
repo_help() {
cat <<EOF
Usage: repo <command> <repository>
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
}

6
functions/list.zsh Normal file
View File

@ -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
}

18
functions/new.zsh Normal file
View File

@ -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}"
}

6
functions/open.zsh Normal file
View File

@ -0,0 +1,6 @@
repo_open() {
remote=$(git remote get-url origin)
remote=${remote/git@/https:\/\/}
remote=${remote/:/\//}
xdg-open "$remote"
}

11
repo.plugin.zsh Normal file
View File

@ -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