从 git 中提取 url 推送远程响应

Extract url from git push remote response

当我将我的更改连同通常的统计信息推送到我们的 Bitbucket 服务器时,服务器会以 'remote:' 开头的几行作为响应。其中一行包含 URL 以减少推送的 b运行ch 的差异并创建拉取请求。我目前将 URL 和 copy/paste 突出显示到浏览器 window 中以创建我的拉取请求,但我希望加快该过程。有没有办法提取 URL 并将其传递给 'clip' 以将其作为 git 别名的一部分保存到剪贴板?

我也考虑过尝试使用当前 b运行ch 名称和远程 URL 的组合来重新创建 URL,但是两者之间存在一些差异远程 URL 和拉取请求 URL 所以提取 URL 而不是重新创建它似乎更容易。

Counting objects: 47, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (45/45), done.
Writing objects: 100% (47/47), 12.81 KiB | 0 bytes/s, done.
Total 47 (delta 40), reused 3 (delta 2)
remote:
remote: Create pull request for feature/somefeature:
remote:   https://code.ourbitbucketserver.com/projects/myteam/repos/somerepo/compare/commits?sourceBranch=refs/heads/feature/somefeature
remote:
To https://code.ourbitbucketserver.com/scm/myteam/somerepo.git
 * [new branch]        feature/somefeature -> feature/somefeature
Branch feature/somefeature set up to track remote branch feature/somefeature from origin.

解决方案

bcurrent = "!git rev-parse --abbrev-ref HEAD"
publishold = "!f() { git push -u ${1-origin} $(git bcurrent); }; f"
publishnew = "!f() { git push -u ${1-origin} $(git bcurrent) --progress 2>&1 | awk '/^remote:.*compare/ { system(\"echo \"  \" | clip\") } { print }'; }; f"

bcurrentpublishold 别名是我开始使用的别名以供参考。

接受的答案让我朝着正确的方向前进。我运行遇到了一些我想解释的问题。

  1. git push 通过 stderr 而不是 stdout 输出一些消息(包括 url)。所以我们必须使用 2>&1 重定向 stderr。还添加了 --progress 标志,因此即使未连接到控制台,git 也会输出 stderr,因为我们将输出通过管道传输到 awk。参见 git stderr output can't pipe
  2. clip 不接受命令行参数,必须通过管道输入。
  3. 因为我的 git 别名是一个 shell 函数,所以我不得不在 system 调用中转义双引号。

您可以在别名中使用 awk。以下将完整打印输出并以 URL 作为参数另外调用 clip

echo 'Counting objects: 47, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (45/45), done.
Writing objects: 100% (47/47), 12.81 KiB | 0 bytes/s, done.
Total 47 (delta 40), reused 3 (delta 2)
remote:
remote: Create pull request for feature/somefeature:
remote:   https://code.ourbitbucketserver.com/projects/myteam/repos/somerepo/compare/commits?sourceBranch=refs/heads/feature/somefeature
remote:
To https://code.ourbitbucketserver.com/scm/myteam/somerepo.git
 * [new branch]        feature/somefeature -> feature/somefeature
Branch feature/somefeature set up to track remote branch feature/somefeature from origin.' | awk '/^remote:.*compare/ { system("clip " ) } { print }'