Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolodiamante committed Apr 29, 2021
0 parents commit ed66f5d
Show file tree
Hide file tree
Showing 8 changed files with 358 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#
# EditorConfig for consistent coding styles.
# Ref: https://editorconfig.org
#

root = true

[*]
indent_style = space
indent_size = 2
tab_width = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#
# Don't commit…
#

# Custom files
assets/

# temp files
*.swp
.DS_Store
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2021 Nicolò Diamante <hello@nicolodiamante.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
88 changes: 88 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<p align="center"><a href="#"><img src="https://user-images.githubusercontent.com/48920263/113856355-68e06f80-97a1-11eb-9117-3a882d0b64c5.png" draggable="false" ondragstart="return false;" alt="gitio Title" title="gitio" /></a></p>

Git URLs can get pretty long since we might have long names for repositories
and files. A long URL will look messy, sometimes difficult to read. Short URLs
look better, especially when we need to share them on emails and socials. To
hand over this problem, this plugin—inspired by a service provided by GitHub
([Git.io][gitio])—shorten URLs using curl.
<br/><br/>

## Getting Started

Download the repository via curl:

```shell
sh -c "$(curl -fsSL https://raw.githubusercontent.com/nicolodiamante/gitio/HEAD/bootstrap.sh)"
```

Alternatively, clone manually:

```shell
git clone https://github.com/nicolodiamante/gitio.git ~
```

Head over into the directory and then:

```shell
cd utils && source install.sh
```

The script will look for the `zshrc` and then append the full path to the
`gitio/script` directory to the `fpath`:

```shell
fpath=(~/gitio/script $fpath)
autoload -Uz gitio
```

### Install via [Oh My Zsh][ohmyzsh]

```shell
git clone https://github.com/nicolodiamante/gitio.git $ZSH_CUSTOM/plugins/gitio
```

- Add to your `zshrc` plugins array `plugins=(... gitio)`
- Restart the shell to activate
<br/><br/>

## How to Use `gitio`

- `gitio`: inside a git repository, it will search for the URL within the config
file inside the `.git` directory.

- `gitio [URL]`: shortens the URL but without a vanity name given there is no
control of the output.

- `gitio [URL] [vanity-name]`: shortens and changes the URL with the vanity name
given. Instead of typing the entire URL you can type only the `username/repo`.

- `gitio --retrieve [URL]`: retrieves the original URL from the shortened URL.

- `gitio --open [URL]`: opens the URL in the default browser (works only with
GitHub URLs).

For full gitio options, see `gitio --help`.
<br/><br/>

## Notes

### Resources

- [The GitHub Blog][git-blog]

### Contribution

Please report any issues or bugs to the [issues page][issues]. Suggestions for
improvements are welcome!<br/><br/>

<p align="center"><a href="#"><img src="https://user-images.githubusercontent.com/48920263/113406768-5a164900-93ac-11eb-94a7-09377a52bf53.png" draggable="false" ondragstart="return false;" /></a></p>

<p align="center"><a href="https://nicolodiamante.com" target="_blank"><img src="https://user-images.githubusercontent.com/48920263/113433823-31a84200-93e0-11eb-9ffb-9111b389ef2f.png" draggable="false" ondragstart="return false;" alt="Nicol&#242; Diamante Portfolio" title="Nicol&#242; Diamante" width="11px" /></a></p>

<p align="center"><a href="https://github.com/nicolodiamante/gitio/blob/main/LICENSE.md" target="_blank"><img src="https://user-images.githubusercontent.com/48920263/110947109-06ca5100-8340-11eb-99cf-8d245044b8a3.png" draggable="false" ondragstart="return false;" alt="The MIT License" title="The MIT License (MIT)" width="90px" /></a></p>

<!-- Link labels: -->
[gitio]: https://git.io
[ohmyzsh]: https://github.com/robbyrussell/oh-my-zsh/
[git-blog]: https://github.blog/2011-11-10-git-io-github-url-shortener/
[issues]: https://github.com/nicolodiamante/gitio/issues
31 changes: 31 additions & 0 deletions bootstrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/sh

# Determines the current user's shell.
[[ "$(basename -- "$SHELL")" == "zsh" ]] || exit 1

SOURCE=https://github.com/nicolodiamante/gitio
TARBALL="${SOURCE}/tarball/master"
TARGET="${HOME}/gitio"
TAR_CMD="tar -xzv -C "${TARGET}" --strip-components 1 --exclude .gitignore"

INSTALL=./utils/install.sh

is_executable() {
type "$1" > /dev/null 2>&1
}

# Checks which executable is available then downloads and installs.
if is_executable "git"; then
CMD="git clone ${SOURCE} ${TARGET}"
elif is_executable "curl"; then
CMD="curl -L ${TARBALL} | ${TAR_CMD}"
elif is_executable "wget"; then
CMD="wget --no-check-certificate -O - ${TARBALL} | ${TAR_CMD}"
fi

if [[ -z "$CMD" ]]; then
echo 'No git, curl or wget available. Aborting!'
else
echo 'Installing gitio...'
mkdir -p "${TARGET}" && eval "${CMD}" && cd "${TARGET}" && source "${INSTALL}"
fi
3 changes: 3 additions & 0 deletions gitio.plugin.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/zsh

fpath+="${0:h}/script"
163 changes: 163 additions & 0 deletions script/gitio
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#
# gitio — Shorten GitHub URLs.
#

gitio() {
# Use colors (if you can call them so).
if [[ -t 1 ]]; then
BOLD=$(printf '\033[1m')
RESET=$(printf '\033[m')
else
BOLD=""
RESET=""
fi

# Check if any parameters are given, then get URL from git config if present.
if [[ "$#" -eq 0 && -s ".git/config" ]]; then
GIT_CONFIG_URL="$(cat .git/config | grep "url" | awk '{print $3}')"
fi

# Checks if URL contains http(s).
if [[ -n "$GIT_CONFIG_URL" ]]; then
local URL="${GIT_CONFIG_URL}"
elif [[ ! "$1" =~ ^(http|https)://github.com/ ]]; then
local URL="https://github.com/${1}"
else
local URL="${1}"
fi

# Checks for any parameter, or show help.
if [[ "$#" -eq 0 && -z "$GIT_CONFIG_URL" ]]; then
echo 'gitio: no URL specified!'
echo "gitio: for more information see 'gitio --help'"
return 0
fi

# Show `help` message.
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
<<EOF
Usage: gitio [options] URL vanity-name
Shorten, squeeze, change GitHub URLs using curl.

-o, --open opens the URL in the default browser
-r, --retrieve retrieves the complete URL from the shortened URL
-h, --help display this help and exit

Examples:
gitio https://github.com/username/repo vanity-name => https://git.io/vanity-name
gitio username/repo vanity-name => https://git.io/vanity-name
gitio -r https://git.io/vanity-name => https://github.com/username/repo

Note: your vanity-name can only contain lowercase letters, numbers
and dashes. If 'gitio' is launched inside a git repository it will
search for the URL within the config file. However, you should make
sure to be inside a git repo or otherwise provide the URL and the
vanity-name. It's even possible to just type the username and repo
name instead of the whole URL (see examples above).

Report any translation bugs to <https://github.com/nicolodiamante/gitio/issues/>
Full documentation <https://github.com/nicolodiamante/gitio/>
EOF
return 0
fi

# Retrieves the complete URL from the shortened URL.
if [[ "$1" == "-r" || "$1" == "--retrieve" ]]; then
if [[ -n "$2" && "$2" =~ ^(http|https)://git.io/ ]]; then

# Grep the `Location: headers` of the URL that was fetched last.
local GITHUB_URL="$(curl -w '%{url_effective}' -s -o /dev/null "$2")"

# Checks the status of the URL given.
HTTP_CODE_STATUS="$(curl -w '%{http_code}' -s -o /dev/null "$2")"

if [[ -n "$2" && "$HTTP_CODE_STATUS" -eq 200 && -n "$GITHUB_URL" ]]; then
# Copy to clipboard if possible.
case "$OSTYPE" in
darwin*) echo "${GITHUB_URL}" | pbcopy ;;
linux*) echo "${GITHUB_URL}" | xclip -selection c ;;
esac
# Prints the source URL.
echo "gitio: source: ${GITHUB_URL}"
echo 'gitio: URL copied to clipboard!'
return 0
else
echo 'gitio: URL using bad/illegal format or missing URL'
return 1
fi
elif [[ "$2" =~ ^(http|https)://git && "$HTTP_CODE_STATUS" -eq 200 ]]; then
echo 'gitio: only allowed git.io URL!'
return 1
else
echo 'gitio: URL using bad/illegal format or missing URL'
return 1
fi
fi

# Open in the default browser.
if [[ "$1" == "-o" || "$1" == "--open" ]]; then
if [[ -n "$2" && "$2" =~ ^(http|https)://github.com/ || \
"$2" =~ ^(http|https)://git.io/ ]]; then

if [[ "$HTTP_CODE_STATUS" -eq 200 ]]; then
# Open the URL in the browser.
case "$OSTYPE" in
darwin*) open "$2" ;;
linux*) xdg-open "$2" ;;
*) python -m webbrowser "$2" ;;
esac
return 0
else
echo 'gitio: cannot open URL in the browser!'
return 1
fi
else
echo 'gitio: URL using bad/illegal format or missing URL'
return 1
fi
fi

# Checks if the vanity name is valid.
if [[ -n "$2" ]]; then
local VANITY="$(echo "$2" | tr "[:upper:]" "[:lower:]")"

if [[ ! "$VANITY" =~ ^[0-9a-z\-]+$ ]]; then
echo "gitio: ${BOLD}${2}${RESET} invalid characters!"
return 1
fi
fi

# Make the request to git.io
if [[ "$1" =~ ^(http|https)://github.com/ ]]; then
# Checks if URL is valid before send the request to git.io
local URL_CODE_STATUS="$(curl -w '%{http_code}' -s -o /dev/null "$URL")"

if [[ -n "$URL" && "$URL_CODE_STATUS" -eq 200 && -n "$VANITY" ]]; then
local REQUEST="$(curl -is https://git.io/ -F "url=$URL" -F "code=$VANITY")"
elif [[ -n "$URL" && "$URL_CODE_STATUS" -eq 200 ]]; then
local REQUEST="$(curl -is https://git.io/ -F "url=$URL")"
else
echo 'gitio: check the URL provided is correct'
echo 'gitio: can not connect to the server'
return 1
fi
else
echo 'gitio: URL using bad/illegal format or missing URL'
return 1
fi

# Get the shortened URL.
local GITIO_URL="$(echo "$REQUEST" | awk '/^Location/ {print $2}')"

# Copy to clipboard if possible.
if [[ -n "$GITIO_URL" ]]; then
case "$OSTYPE" in
darwin*) echo "${GITIO_URL}" | pbcopy ;;
linux*) echo "${GITIO_URL}" | xclip -selection c ;;
esac

# Prints the shortened URL.
echo "gitio: URL shortened: ${GITIO_URL}"
echo 'gitio: URL copied to clipboard!'
fi
}
27 changes: 27 additions & 0 deletions utils/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh

#
# Install gitio
#

# Determines the current user's shell, if `zsh` then installs.
[[ "$(basename -- "$SHELL")" == "zsh" ]] || exit 1

# Defines the PATHs.
SCRIPT="${HOME}/gitio/script"
ZSHRC="${ZDOTDIR:-${XDG_CONFIG_HOME/zsh:-$HOME}}/.zshrc"

if [[ -d "$SCRIPT" && -f "$ZSHRC" ]]; then
# Append to `zshrc`.
cat << EOF >> ${ZSHRC}
# gitio
fpath=(~/gitio/script \$fpath)
autoload -Uz gitio
EOF
echo 'zsh: appended gitio to zshrc.'

# Reload shell.
source "${ZSHRC}"
else
echo 'zsh: zshrc not found!'
fi

0 comments on commit ed66f5d

Please sign in to comment.