【问题标题】:Extract url from git push remote response从 git push 远程响应中提取 url
【发布时间】:2017-03-21 12:54:55
【问题描述】:

当我将更改连同通常的统计信息一起推送到我们的 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"

bcurrent 和 publishold 别名是我开始参考的。

接受的答案使我朝着正确的方向前进。在此过程中,我遇到了一些我想解释的问题。

  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 调用中转义双引号。

【问题讨论】:

标签: git bitbucket git-push pull-request


【解决方案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 }'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-20
    • 2013-04-08
    • 2014-05-04
    • 2021-12-18
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多