Skip to content

Instantly share code, notes, and snippets.

@tung-dang
Last active December 7, 2018 10:54
Show Gist options
  • Select an option

  • Save tung-dang/8b2304d99526904165438be7b90cdd76 to your computer and use it in GitHub Desktop.

Select an option

Save tung-dang/8b2304d99526904165438be7b90cdd76 to your computer and use it in GitHub Desktop.
Global functions to open git repo quickly.
#!/bin/sh
#############################################
# Some public functions in Open Bitbucket Cloud, Bitbucket Server or Github repo quickly:
# - git_create_pr
# - git_open_repo
# - git_view_current_commit_in_web
#############################################
BITBUCKET_CLOUD_URL='https://bitbucket.org'
BITBUCKET_SERVER_URL='https://stash.atlassian.com'
GITHUB_URL='https://github.com'
_get_bb_project() {
project=$(basename `git rev-parse --show-toplevel`)
teamName=$(git config --get remote.origin.url | awk -F[:/] '{ print $2 }')
echo "$BITBUCKET_CLOUD_URL/$teamName/$project"
}
_get_stash_project() {
project=$(git config --get remote.origin.url | awk -F[:/] '{ print $6 }')
repo=$(basename `git rev-parse --show-toplevel`)
echo "$BITBUCKET_SERVER_URL/projects/$project/repos/$repo"
}
_get_github_project() {
project=$(git config --get remote.origin.url | awk -F[:/] '{ print $2 }')
repo=$(basename `git rev-parse --show-toplevel`)
echo "$GITHUB_URL/$project/$repo"
}
# Create PR in Bitbucket Cloud or Bitbucket Server
git_create_pr() {
origin=$(git config --get remote.origin.url)
branchName=$(git rev-parse --abbrev-ref HEAD)
url=''
if [[ "$origin" = *bitbucket* ]]; then
url="$(_get_bb_project)/pull-requests/new?source=$branchName"
elif [[ "$origin" = *github* ]]; then
url="$(_get_github_project)/compare/master...$branchName"
else
url="$(_get_stash_project)/pull-requests?create&targetBranch=refs%2Fheads%2Fmaster&sourceBranch=refs%2Fheads%2F$branchName"
fi
open "$url"
}
# Open Project UL in Bitbucket Cloud or Bitbucket Server
git_open_repo() {
origin=$(git config --get remote.origin.url)
url=""
if [[ "$origin" = *bitbucket* ]]; then
url="$(_get_bb_project)/src/master/"
elif [[ "$origin" = *github* ]]; then
url="$(_get_github_project)/"
else
url="$(_get_stash_project)/browse"
fi
open "$url"
}
# View current commit in local and open it in Web UI
git_view_current_commit_in_web() {
origin=$(git config --get remote.origin.url)
commit=$(git rev-parse HEAD);
url=""
if [[ "$origin" = *bitbucket* ]]; then
url="$(_get_bb_project)/commits/$commit"
elif [[ "$origin" = *github* ]]; then
url="$(_get_github_project)/commit/$commit"
else
url="$(_get_stash_project)/commits/$commit"
fi
open "$url"
}
@tung-dang
Copy link
Copy Markdown
Author

If you help better idea, please comment. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment