获得漂亮的git rev的名字 [英] Get pretty git rev name

查看:165
本文介绍了获得漂亮的git rev的名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道这个问题已被问及很多 ,但我没有找到任何合适的答案。我正在寻找在我的提示符中显示当前git rev的名称,但以一种很好的方式:如果我在分支上,我想要分支的名称;如果它不是分支,我想在我的提示中使用相对名称(即 master〜2 )。



起初,我使用的东西如你可以在这里看到。它工作得很好:我有分支的名字,或者rev SHA1编号。然后,我转向基于 git name-rev 的接近,这很好,但是后来我发现名称有时会混淆:在合并 master 转换为分段,在我的提示中,我在两个分支中都有 master 。鉴于我对此有多依赖,我不能拥有这个。但有 master〜2 而不是SHA1号码真的很棒。

所以我希望有一种方法可以合并两种方法。如果可以显示标签名称,我会接受它(尽管我没有使用它们 - yet )。顶部将是只有git的命令,不会查找git内部文件 - 它们可能会在某些时候改变。



感谢您的阅读!

$

functions / zgitinit#L70rel =nofollow> https://github.com/jcorbin/zsh-git/blob/master/functions/zgitinit#L70

  name = $(git symbolic-ref -q HEAD)
if [$? -eq 0];那么
if [[$ name == refs /(heads | tags)/ *]];然后
name = $ {name#refs /(heads | tags)/}
fi
else
name = $(git name-rev --name-only --no -undefined --always HEAD)
if [$? -ne 0];然后
返回1
elif [[$ name == remotes / *]];然后
name = $ {name#remotes /}
fi
fi

您也可以考虑使用zsh:)

So I know this question have been asked a lot, but I don't find any suitable answers. I'm looking to display the name of the current git rev in my prompt, but in a pretty way : if I'm on a branch, I want the name of the branch; if it's not a branch, I'd like the relative name (ie master~2) in my prompt.

At first, I was using something like what you can see here. It worked well : I had the name of the branch, or the rev SHA1 number. Then I moved to an approached based on git name-rev, which was great at first, but then I noticed that the name was mixed up sometimes : after merging master into staging, on both branches I had master in my prompt. Given how much I rely on this, I can't have this. But having master~2 instead of a SHA1 number was really great.

So I hope there's a way to combine both approaches. If it's possible to display the tag name too, I'll take it (though I don't use them much - yet). And the top would be to have git only commands, no looking up git internal files -- they could change at some point.

Thanks for reading!

解决方案

Taken from zsh-git repository: https://github.com/jcorbin/zsh-git/blob/master/functions/zgitinit#L70

name=$(git symbolic-ref -q HEAD)
if [ $? -eq 0 ]; then
    if [[ $name == refs/(heads|tags)/* ]]; then
        name=${name#refs/(heads|tags)/}
    fi
else
    name=$(git name-rev --name-only --no-undefined --always HEAD)
    if [ $? -ne 0 ]; then
        return 1
    elif [[ $name == remotes/* ]]; then
        name=${name#remotes/}
    fi
fi

You may also consider using zsh :)

这篇关于获得漂亮的git rev的名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆