如何查看当前分支的拉取请求 URL?

How can I view the pull request URL for the current branch?

推送更改后,我看到这样的文字:

Writing objects: 100% (5/5), 478 bytes | 239.00 KiB/s, done.
Total 5 (delta 4), reused 0 (delta 0)
remote:
remote: Create pull request for my-branch => master-branch
remote:   https://bitbucket.org/my-company/repo/pull-requests/12345

但如果我当时不做 PR,文本滚出视野,URL 就消失了。我怎样才能简单地生成或重新查看 URL 而不进行更改并再次 committing/pushing?

此拉取请求 URL 不是 git 功能,而是 BitBucket 服务器上的挂钩脚本生成的消息。

在 BitBucket 服务器上,您可以全局禁用它:How do I disable the remote create pull request message when pushing changes?。在 BitBucket 云上,您无法禁用它。

获取此消息的一个方法是使用 --dry-run 选项模拟 git pull,例如:

git pull --dry-run 

但如果这还不足以触发挂钩,可能唯一的方法就是通过 BitBucket 网络界面。

您在此处列出的 URL 是针对已存在的拉取请求 - "View pull request" 文本是那里的赠品。现有的拉取请求在 URL 中有一个 ID,您需要指定该 ID;如果您不知道 ID,则需要从 GUI 获取 URL(https://bitbucket.org/owner/repo/pull-requests/ 可能是最容易找到它的地方)。

如果您的分支还没有拉取请求,则挂钩生成的 "Create pull requests" link 是 https://bitbucket.org/owner/repo/pull-requests/new?source=branchname&t=1.

我能够使用这样的批处理文件生成 URL:

@echo off

setlocal 
for /f "tokens=*" %%a in ( 
    'git rev-parse --abbrev-ref HEAD'
) do ( 
    set branch=%%a
    set url=https://bitbucket.org/my-company/repo/pull-requests/new?source=%%a^^^&t=1
) 

echo %url%
endlocal 

它只是获取当前的 git 分支并放入一个字符串,然后回显该字符串。

Bash版本:

#!/bin/bash

org="myOrg"
branch=$(git rev-parse --abbrev-ref HEAD)
repo_dir=$(git rev-parse --show-toplevel)
repo=$(basename ${repo_dir})
url="https://bitbucket.org/${org}/${repo}/pull-requests/new?source=${branch}&t=1"

echo $url

要使用网络浏览器(bitbucket 网络界面)创建拉取请求,请转到您分支机构的 branch 网页,或您的 bitbucket 存储库的 branches 页面。

使用问题中的例子:

remote: Create pull request for my-branch => master-branch
remote:   https://bitbucket.org/my-company/repo/pull-requests/12345

我们可以去 https://bitbucket.org/my-company/repo/branch/my-branch,有一个“创建拉取请求”link。

或者我们可以去 https://bitbucket.org/my-company/repo/branches, 有一个名为“拉取请求”的列,对于没有现有拉取请求的分支,有一个“创建”link.