DEV Community

Discussion on: Open the GitHub project page of a repo from Terminal

Collapse
 
frailwords profile image
Sriram Viswanathan

Just a small & quick modification to add current branch & non-github domain -

alias github=GitHub

function GitHub()
{
    if [ ! -d .git ] ;
        then echo "ERROR: This isnt a git directory" && return false;
    fi

    git_url=`git config --get remote.origin.url`
    git_domain=`echo $git_url | awk -v FS="(@|:)" '{print $2}'`
    git_branch=`git rev-parse --abbrev-ref HEAD 2>/dev/null`

    if [[ $git_url == https://* ]];
    then
        url=${git_domain}/${git_url%.git}/tree/${git_branch}
    else
       if [[ $git_url == git@* ]]
       then
            url="https://${git_domain}/${${git_url#*:}%.git}/tree/${git_branch}"
            echo $url
       else
           echo "ERROR: Remote origin is invalid" && return false;
       fi
    fi
    open $url
}

Enter fullscreen mode Exit fullscreen mode
Collapse
 
engineercoding profile image
Wesley Ameling

The version for Git Bash (windows). It replaces the open command and for some reason it cannot parse a nested variable modification (line 17)

function GitHub()
{
    if [ ! -d .git ] ;
        then echo "ERROR: This isnt a git directory" && return false;
    fi

    git_url=`git config --get remote.origin.url`
    git_domain=`echo $git_url | awk -v FS="(@|:)" '{print $2}'`
    git_branch=`git rev-parse --abbrev-ref HEAD 2>/dev/null`

    if [[ $git_url == https://* ]];
    then
        url=${git_domain}/${git_url%.git}/tree/${git_branch}
    else
       if [[ $git_url == git@* ]]
       then
            cut_off=${git_url#*:}
            url=https://${git_domain}/${cut_off%.git}/tree/${git_branch}
       else
           echo "ERROR: Remote origin is invalid" && return false;
       fi
    fi
    explorer $url
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
n1amr profile image
Amr Alaa
git config --global alias.go "!f() { git-browse $*; }; f"
Enter fullscreen mode Exit fullscreen mode

.../bin/git-browse

#!/bin/bash

action=''
remote_name=origin

while [ $# != 0 ]; do
    case $1 in
        -r)
        [[ $# < 2 ]] && echo "Error: No remote name" && exit 1
        remote_name="$2"
        shift
        ;;

        *)
        if [[ -n "$action" ]]; then
            echo "Only one action is required."
            exit 1
        fi
        action="$1"
        ;;
    esac
    shift
done

if ! git rev-parse 1>/dev/null 2>&1; then
    echo "ERROR: This isnt a git directory"
    exit 1
fi

git_url="$(git config --get remote.${remote_name}.url)"
git_url=${git_url%.git}

git_protocol="$(echo "$git_url" | awk -v FS="(@|:)" '{print $1}')"
if [[ "$git_protocol" == 'http' ]] || [[ "$git_protocol" == 'https' ]]; then
    git_domain="$(echo "$git_url" | awk -F'/' '{print $3}')"
    git_url="$git_url"
elif [[ $git_protocol == 'git' ]]; then
    git_url="${git_url#*@}"
    git_domain="$(echo "$git_url" | awk -F':' '{print $1}')"
    git_url="https://${git_url/:/\/}"
elif [[ "$git_protocol" == 'github' ]] || [[ "$git_protocol" == 'gitlab' ]]; then
    git_domain="${git_protocol}.com"
    git_url="https://${git_protocol}.com/${git_url#*:}"
else
    echo "ERROR: Remote $remote_name is invalid"
    exit 1
fi
url="${git_url}"

git_branch="$(git rev-parse --abbrev-ref HEAD 2> /dev/null)" || git_branch=''

case "$action" in
    '') action='home' ;;
    '-') action='current' ;;
    b) action='branches' ;;
    i) action='issues' ;;
    r) action='releases' ;;
    s) action='settings' ;;
    t) action='tags' ;;
    w) action='wiki' ;;
esac

case "$action" in
    'home') url="$url" ;;
    'branches') url="$url/branches" ;;
    'issues') url="$url/issues" ;;
    'releases') url="$url/releases" ;;
    'tags') url="$url/tags" ;;
    'repo-settings') url="$url/settings/repository" ;;

    'current')
        if [[ -z "$git_branch" ]]; then
            echo "Error: No branch"
            exit 1
        fi
        url="$url/tree/${git_branch}"
    ;;

    'wiki')
        if [[ "$git_domain" == 'gitlab.com' ]]; then
            url="$url/wikis"
        else
            url="$url/wiki"
        fi
    ;;

    'settings')
        if [[ "$git_domain" == 'gitlab.com' ]]; then
            url="$url/edit"
        else
            url="$url/settings"
        fi
    ;;

    *)
        echo "ERROR: Unknown action $action"
        exit 1
    ;;
esac

echo "Opening $url"
sensible-browser "$url"
Enter fullscreen mode Exit fullscreen mode