从git push远程响应中提取url

时间:2017-03-21 12:54:56

标签: git bitbucket git-push pull-request

当我将我的更改推送到我们的Bitbucket服务器时,以及通常的统计信息,服务器会以几行前面的'remote:'进行响应。其中一行包含一个URL,几乎没有推送分支的差异并创建一个拉取请求。我目前突出显示URL并将其复制/粘贴到浏览器窗口以创建我的拉取请求,但我希望加快这一过程。有没有办法提取URL并将其传递给'clip'以将其作为git别名的一部分保存到剪贴板?

我还考虑过尝试使用当前分支名称和远程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 \" $2 \" | 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调用中转义双引号。

1 个答案:

答案 0 :(得分:1)

您可以在别名中使用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 " $2) } { print }'