#!/usr/bin/env bash

# Try this vim command:
#
# :map \ghu :let $l=line(".")<cr>:let $p=@%<cr>:!echo -n "$(git hub url $p $l)" \| xsel -b<cr><cr>

set -e

command:url() {
  set -- "${command_arguments[@]}"

  local path= line=
  if inside-git-repo && [ $# -gt 0 ] && [ -e "$1" ]; then
    path="$1"
    path="${path%/}"
    [ -n "$2" ] && line="$2"
    command_arguments=()
  fi

  get-args '?owner:get-user/repo:get-repo'

  url="https://github.com/$owner/$repo"

  if [ -n "$path" ]; then
    branch="$(git rev-parse --abbrev-ref HEAD)"
    if "$raw_output"; then
      quiet_output=false
      url+="/raw/$branch/$path"
    else
      url+="/blob/$branch/$path"
    fi
    if [ -n "$line" ]; then
      url+="#L$line"
    fi
  fi

  if pipeline; then
    echo -n "$url"
  else
    echo "$url"
  fi

  msg_ok=0
}

# vim: set lisp:
