【发布时间】:2021-12-30 11:37:42
【问题描述】:
在我的上一个项目中,我使用 GitHub,因此我能够使用命令从 CLI 创建 PRs gh pr create -> https://cli.github.com/manual/gh_pr_create
我实际上是通过 CLI 完成所有操作的。
现在有了 Bitbucket,我想知道是否有办法做到这一点?
【问题讨论】:
标签: git github command-line-interface bitbucket
在我的上一个项目中,我使用 GitHub,因此我能够使用命令从 CLI 创建 PRs gh pr create -> https://cli.github.com/manual/gh_pr_create
我实际上是通过 CLI 完成所有操作的。
现在有了 Bitbucket,我想知道是否有办法做到这一点?
【问题讨论】:
标签: git github command-line-interface bitbucket
有几个通过 CURL 使用 bitbucket API 的示例,但我认为您正在寻找更简单的方法。
我有以下custom git command,它旨在与 GitHub 一起使用,但您可以通过操纵 URL 的形状轻松修改它以与 BitBucket 一起使用。不幸的是,我没有有效的 BitBucket 帐户,所以我没有经过测试的命令可以提供给您。
# usage
git create-pr
#!/usr/bin/env bash
# Opens the "Open Pull Request" GitHub page for a repo/branch in your browser.
# based on git-open by Paul Irish (https://github.com/paulirish/git-open/)
#
# git create-pr
# git create-pr [remote] [branch]
# are we in a git repo?
git rev-parse --is-inside-work-tree &>/dev/null
if [[ $? != 0 ]]; then
echo "Not a git repository." 1>&2
exit 1
fi
# assume origin if not provided
# fallback to upstream if neither is present.
remote="origin"
if [ -n "$1" ]; then
if [ "$1" == "issue" ]; then
currentBranch=$(git symbolic-ref -q --short HEAD)
regex='^issue'
if [[ $currentBranch =~ $regex ]]; then
issue=${currentBranch#*#}
else
echo "'git open issue' expect branch naming to be issues/#123" 1>&2
exit 1
fi
else
remote="$1"
fi
fi
remote_url="remote.${remote}.url"
giturl=$(git config --get "$remote_url")
if [ -z "$giturl" ]; then
echo "$remote_url not set." 1>&2
exit 1
fi
# get current branch
if [ -z "$2" ]; then
branch=$(git symbolic-ref -q --short HEAD)
else
branch="$2"
fi
# Make # and % characters url friendly
# github.com/paulirish/git-open/pull/24
branch=${branch//%/%25} && branch=${branch//#/%23}
# URL normalization
# GitHub
giturl=${giturl/git\@github\.com\:/https://github.com/}
# handle SSH protocol (links like ssh://git@github.com/user/repo)
giturl=${giturl/#ssh\:\/\/git\@github\.com\//https://github.com/}
providerUrlDifference=compare
giturl=${giturl%\.git}
giturl="${giturl}/${providerUrlDifference}/${branch}?expand=1"
# get current open browser command
case $( uname -s ) in
Darwin) open=open;;
MINGW*) open=start;;
CYGWIN*) open=cygstart;;
MSYS*) open="powershell.exe –NoProfile Start";;
*) open=${BROWSER:-xdg-open};;
esac
# open it in a browser
$open "$giturl" &> /dev/null
exit $?
【讨论】: