与 Gerrit 一起使用时如何为 Git 配置特定的上游推送引用规范?

发布于 2024-11-29 22:11:35 字数 1199 浏览 1 评论 0原文

我正在使用 Gerrit Code Review 设置 Git,并正在寻找一种方法来进行必要的操作对于 Git 新手来说,Git 命令相对简单。

我目前用于启动新功能分支的命令本质上是(假设 Gerrit 是origin):

git checkout baseline
git pull
git checkout -b work1234
git push -u origin work1234

这将启动一个从某些基线分支的新工作包work1234 code>,最后的推送在 Gerrit 中创建分支并设置上游。所以 .git/config 看起来像这样:

[branch "work1234"]
        remote = origin
        merge = refs/heads/work1234

现在,Gerrit 希望将新提交的审核推送到特殊的 refspec,例如 refs/for/work1234。我可以手动执行此操作:

git push origin work1234:refs/for/work1234

我想做的是找到某种方法来设置 .git/config ,以便简单的 git Push 将当前分支推送到Gerrit 需要的遥控器上的 refspec。我查看了以下 git config 区域:

  • ..* 似乎没有任何用于设置推送引用规范的特定选项
  • branch Push.default - 我有点想要这里的上游
  • remote..push - 我尝试过refs/heads/*:refs/for/* 这里但是 git push 在这种情况下总是想推送所有本地分支,而我只是想要当前分支

如果我不能让 Git 自己完成此操作,我将编写一个完整指定引用规范的小包装脚本。不过,如果 Git 能够原生推送到正确的位置就更好了。

I'm setting up Git with Gerrit Code Review and am looking for a way to make the necessary Git commands relatively straightforward for users who might be new to Git.

The commands I currently have for starting a new feature branch are essentially (assuming Gerrit is origin):

git checkout baseline
git pull
git checkout -b work1234
git push -u origin work1234

This starts a new work package work1234 branched from some baseline, and the final push creates the branch in Gerrit and sets the upstream. So .git/config looks something like:

[branch "work1234"]
        remote = origin
        merge = refs/heads/work1234

Now, Gerrit wants new commits for review to be pushed to a special refspec, refs/for/work1234 for example. I can do this manually with:

git push origin work1234:refs/for/work1234

What I would like to do is find some way to set up .git/config so that a plain git push will push the current branch to the refspec on the remote that Gerrit requires. I have looked at the following git config areas:

  • branch.<name>.* - doesn't seem to have any specific option for setting the push refspec
  • push.default - I sort of want upstream here
  • remote.<name>.push - I tried refs/heads/*:refs/for/* here but git push always wants to push all local branches in this case, while I just want the current branch

If I can't make Git do this by itself, I'll write a small wrapper script that fully specifies the refspecs. However, it would be better if Git could push to the right place natively.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

物价感观 2024-12-06 22:11:35

我最终编写了一个新的 git-submit 脚本:

#!/bin/sh -e

if [ -z "$1" ]; then
    REMOTE=origin
else
    REMOTE=$1
fi

BRANCH=`git symbolic-ref HEAD`
case $BRANCH in
    refs/heads/*)
        BRANCH=`basename $BRANCH`
        ;;
    *)
        echo "I can't figure out which branch you are on."
        exit 1
        ;;
esac

git push $REMOTE HEAD:refs/for/$BRANCH

我将此脚本放在 /usr/local/libexec/git-core/git-submit 中,现在有一个命令可以执行向 Gerrit 提交新代码以供审核:

$ git submit

如果 Gerrit 不是 origin 远程,则根据需要使用 git Submit

I ended up writing a new git-submit script:

#!/bin/sh -e

if [ -z "$1" ]; then
    REMOTE=origin
else
    REMOTE=$1
fi

BRANCH=`git symbolic-ref HEAD`
case $BRANCH in
    refs/heads/*)
        BRANCH=`basename $BRANCH`
        ;;
    *)
        echo "I can't figure out which branch you are on."
        exit 1
        ;;
esac

git push $REMOTE HEAD:refs/for/$BRANCH

I put this script in /usr/local/libexec/git-core/git-submit and now there's one command to submit new code to Gerrit for review:

$ git submit

If Gerrit is not the origin remote, then use git submit <remote> as appropriate.

反差帅 2024-12-06 22:11:35

我发现了两个选项:

1)查看我制作的项目,ggh: https://github.com/hobbs /ggh

2) 您可以在遥控器中配置默认​​的推送引用规范。我只弄清楚如何使用硬编码的远程分支来执行此操作,仍在尝试找出如何使用通配符远程引用规范,以便它始终推送到您正在跟踪的远程:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = ssh://gerrit/repo
    push =  HEAD:refs/for/master

Two options I've found:

1) Check out a project I made, ggh: https://github.com/hobbs/ggh

2) You can configure the default push refspec in your remote. I've only figured out how to do this with a hardcoded remote branch, still trying to figure out how to wildcard the remote refspec so that it always pushes to the remote you're tracking:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = ssh://gerrit/repo
    push =  HEAD:refs/for/master
﹎☆浅夏丿初晴 2024-12-06 22:11:35

这是 Greg Hewgill 答案的改进。

在路径上的某个位置创建一个脚本,例如在您的配置文件 bin 文件夹中(如果需要,即使在 Windows 上也创建一个脚本):

$ mkdir ~/bin
$ vi ~/bin/git-submit

包含以下内容:

#!/bin/sh -e

if [ -z "$1" ]; then
    REMOTE=origin
else
    REMOTE=$1
fi

BRANCH=`git symbolic-ref HEAD`
case $BRANCH in
    refs/heads/*)
        BRANCH=`basename $BRANCH`
        ;;
    *)
        echo "I can't figure out which branch you are on."
        exit 1
        ;;
esac

REMOTE_BRANCH=`git config --get "branch.$BRANCH.merge"`
if [ -z $REMOTE_BRANCH ]
then
    echo "There is no tracking information for the current branch."
    echo "If you wish to set tracking information for this branch you can do so with:"
    echo ""
    echo "    git branch --set-upstream $BRANCH <remote>/<branch>"
    echo ""
    exit 1
fi

git push $REMOTE HEAD:refs/for/$REMOTE_BRANCH

假设您创建一个远程跟踪分支,例如:

$ git checkout -b MyBranch origin/master

您可以简单地调用:

$ git submit

... 推送到在这种情况下,refs/for/master

Here is an improvement of Greg Hewgill answer.

Create a script somewhere on the path, like on your profile bin folder (create one if necessary, even on Windows):

$ mkdir ~/bin
$ vi ~/bin/git-submit

With the following content:

#!/bin/sh -e

if [ -z "$1" ]; then
    REMOTE=origin
else
    REMOTE=$1
fi

BRANCH=`git symbolic-ref HEAD`
case $BRANCH in
    refs/heads/*)
        BRANCH=`basename $BRANCH`
        ;;
    *)
        echo "I can't figure out which branch you are on."
        exit 1
        ;;
esac

REMOTE_BRANCH=`git config --get "branch.$BRANCH.merge"`
if [ -z $REMOTE_BRANCH ]
then
    echo "There is no tracking information for the current branch."
    echo "If you wish to set tracking information for this branch you can do so with:"
    echo ""
    echo "    git branch --set-upstream $BRANCH <remote>/<branch>"
    echo ""
    exit 1
fi

git push $REMOTE HEAD:refs/for/$REMOTE_BRANCH

Supposing you create a remote tracking branch like:

$ git checkout -b MyBranch origin/master

You can simply call:

$ git submit

... to push to refs/for/master in this case.

暗恋未遂 2024-12-06 22:11:35
git config push.default upstream
git config branch.work1234.merge refs/for/work1234
git config branch.work1234.remote origin

或 .git/config:

[push]
        default = upstream
[branch "work1234"]
        merge = refs/for/work1234
        remote = origin

我不确定是否有办法在任何地方放置通配符以使其适用于所有分支。

git config push.default upstream
git config branch.work1234.merge refs/for/work1234
git config branch.work1234.remote origin

or .git/config:

[push]
        default = upstream
[branch "work1234"]
        merge = refs/for/work1234
        remote = origin

I'm not sure if there's a way to put a wildcard in there anywhere to make it work for all branches.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文