Skip to content

Commit

Permalink
Merge pull request #61 from tiktok/feat-sparo-zsh-plugin
Browse files Browse the repository at this point in the history
sparo zsh plugin
  • Loading branch information
chengcyber authored Apr 5, 2024
2 parents f3f1e79 + 0dcb97d commit 7babbfa
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
57 changes: 57 additions & 0 deletions zsh-plugin/sparo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# sparo zsh plugin

This "sparo" zsh plugin provides many aliases just like [git zsh plugin](https://github.com/ohmyzsh/ohmyzsh/blob/master/plugins/git/README.md) does.

## Install & Update

```shell
sh -c "$(curl -fsSL https://raw.githubusercontent.com/tiktok/sparo/main/zsh-plugin/sparo/install.sh)" "" main
```

To use it, add `sparo` to the plugins array in your zshrc file:

**~/.zshrc**

```shell
plugins=(... sparo)
```

Or, manually load it by appending the following code to your zshrc file:

**~/.zshrc**

```shell
source $ZSH/custom/plugins/sparo/sparo.plugin.zsh
```

## Aliases

| Alias | Command
| :--------------------- | :-------------------------------------------------- |
| `sa` | `sparo add` |
| `sb` | `sparo branch` |
| `sco` | `sparo checkout` |
| `scm` | `sparo checkout $(git_main_branch)` |
| `scl` | `sparo clone` |
| `sc` | `sparo commit` |
| `scmsg` | `sparo commit --message` |
| `sc!` | `sparo commit --amend` |
| `sd` | `sparo diff` |
| `sf` | `sparo fetch` |
| `sfo` | `sparo fetch origin` |
| `sl` | `sparo pull` |
| `sp` | `sparo push` |
| `spf!` | `sparo push --force` |
| `spf` | `sparo push --force-with-lease --force-if-includes` |
| `srb` | `sparo rebase` |
| `srba` | `sparo rebase --abort` |
| `srbc` | `sparo rebase --continue` |
| `srbi` | `sparo rebase --interactive` |
| `sst` | `sparo status` |


## Functions

| Command | Description |
| :--------------------- | :----------------------------------------------------------------------------- |
| `git_main_branch` | Returns the name of the main branch: `main` if it exists, `master` otherwise. |
33 changes: 33 additions & 0 deletions zsh-plugin/sparo/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

BRANCH=${1:-main}

# check ZSH environment
if [ -z "$ZSH" ]; then
echo "ZSH environment variable does not defined."
exit 1
fi

# target folder
plugin_dir="$ZSH/custom/plugins/sparo"

mkdir -p "$plugin_dir"

# download sparo.plugin.zsh to plugin dir
echo "Downloading sparo.plugin.zsh to $plugin_dir from $BRANCH branch..."
curl -L "https://raw.githubusercontent.com/tiktok/sparo/$BRANCH/zsh-plugin/sparo/sparo.plugin.zsh" -o "$plugin_dir/sparo.plugin.zsh"

cat <<EOF
Sparo plugin installed!
It's necessary to do the following changes in your .zshrc file:
1. Add "sparo" in the plugins list if you are using Oh-My-Zsh
plugins=(... sparo)
OR, 2. Manually load this file by appending the following code to your .zshrc file.
source $ZSH/custom/plugins/sparo/sparo.plugin.zsh
EOF
44 changes: 44 additions & 0 deletions zsh-plugin/sparo/sparo.plugin.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
if typeset -f git_main_branch > /dev/null; then
# Reuse git_main_branch if it's defined in git plugin
else
# https://github.com/ohmyzsh/ohmyzsh/blob/1d09c6bb0a950756a65b02457842933e3aa493eb/plugins/git/git.plugin.zsh#L34
# Check if main exists and use instead of master
function git_main_branch() {
command git rev-parse --git-dir &>/dev/null || return
local ref
for ref in refs/{heads,remotes/{origin,upstream}}/{main,trunk,mainline,default,master}; do
if command git show-ref -q --verify $ref; then
echo ${ref:t}
return 0
fi
done

# If no main branch was found, fall back to master but return error
echo master
return 1
}
fi

# Aliases
# (order should follow README)

alias sa='sparo add'
alias sb='sparo branch'
alias sco='sparo checkout'
alias scm='sparo checkout $(git_main_branch)'
alias scl='sparo clone'
alias sc='sparo commit'
alias scmsg='sparo commit --message'
alias sc!='sparo commit --amend'
alias sd='sparo diff'
alias sf='sparo fetch'
alias sfo='sparo fetch origin'
alias sl='sparo pull'
alias sp='sparo push'
alias spf!='sparo push --force'
alias spf='sparo push --force-with-lease --force-if-includes'
alias srb='sparo rebase'
alias srba='sparo rebase --abort'
alias srbc='sparo rebase --continue'
alias srbi='sparo rebase --interactive'
alias sst='sparo status'

0 comments on commit 7babbfa

Please sign in to comment.