bash Git 完成和 PS1 不工作:在 Ubuntu 12.04 上的“sudo -s”或“sudo su”上出现“__git_ps1: command not found”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11662735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:28:45  来源:igfitidea点击:

Git completion and PS1 not working: "__git_ps1: command not found" on "sudo -s" or "sudo su" on Ubuntu 12.04

gitbashbash-completion

提问by Chris Cinelli

I installed git and git-flow completion adding these line to .bashrc of root and a normal_user on a Ubuntu 12.04 machine:

我在 Ubuntu 12.04 机器上安装了 git 和 git-flow 完成,将这些行添加到 root 的 .bashrc 和 normal_user 中:

source /etc/git-completion.bash
source /etc/git-flow-completion.bash
GIT_PS1_SHOWUPSTREAM="verbose"
GIT_PS1_SHOWDIRTYSTATE=true
PS1='\[3[32m\]\u@\h\[3[00m\]:\[3[34m\]\w\[3[31m\]$(__git_ps1)\[3[00m\]$ '

When I log as root or normal_user git completion works. However if I use "sudo -s" or "sudo su" git completion is not working and I continually get "__git_ps1: command not found" each time I press return. I tried to remove the "source" commands and use "apt-get install bash-completion" (bash-completion was already installed). So even without the 2 source I get the exact same behavior.

当我以 root 或 normal_user 身份登录时,git 完成工作。但是,如果我使用“sudo -s”或“sudo su”git完成不起作用,并且每次按回车时我都会不断收到“__git_ps1:找不到命令”。我试图删除“源”命令并使用“apt-get install bash-completion”(bash-completion 已经安装)。因此,即使没有 2 源,我也会得到完全相同的行为。

Anybody knows what the problem is and how to make it work?

任何人都知道问题是什么以及如何使其工作?

回答by Peter van der Does

When you do sudo suit won't source the users .bashrc. The PS1 is inherited from the user you did the sudo sufrom but the new shell doesn't know where it can find ___git_ps1

当您这样做时,sudo su它不会获取 users .bashrc。PS1 继承自您执行此操作的用户sudo su,但新 shell 不知道在哪里可以找到___git_ps1

You need to simulate a login by executing sudo su -l

您需要通过执行来模拟登录 sudo su -l

回答by Aleksey Gorohov

In your case it occurs because the git-prompt.sh file wasn't started at terminal start, it is possible to find contrib/completion/git-prompt.shin the initial git-core files.

在您的情况下,这是因为 git-prompt.sh 文件未在终端启动时启动,因此可能find contrib/completion/git-prompt.sh在初始 git-core 文件中启动。

Probably already is present by the machine, for search:

机器可能已经存在,供搜索:

find ~ -name git-prompt.sh

find ~ -name git-prompt.sh

Can take a lot of time and consequently it is better to specify instead of / search more exact, probably you guess where it is possible to find. When will find, add to .bashrc before your promt change expression by an example as it was made by me with the indication of the ways:

可能会花费很多时间,因此最好指定而不是 / 搜索更准确,您可能会猜到可以找到的位置。什么时候会找到,在你的promt更改表达式之前添加到.bashrc,因为它是由我制作的,并说明了方法:

if [ -f $HOME/git/1.8.0/contrib/completion/git-prompt.sh ]; then

. $HOME/git/1.8.0/contrib/completion/git-prompt.sh

fi

如果 [ -f $HOME/git/1.8.0/contrib/completion/git-prompt.sh ]; 然后

. $HOME/git/1.8.0/contrib/completion/git-prompt.sh

After all do:

毕竟做:

. ~/.bashrc

. ~/.bashrc

回答by Curtis Dunham

The prompt functionality was split out of git-completion.bash into git-prompt.sh on May 22, 2012; you will need to sourcethat one as well. Git 1.7.12 was the first release to see this change. I just had the same issue when updating my git-completion.bash.

提示功能于 2012 年 5 月 22 日从 git-completion.bash 拆分为 git-prompt.sh;你也需要source那个。Git 1.7.12 是第一个看到此更改的版本。我在更新 git-completion.bash 时遇到了同样的问题。

回答by Ray

Assuming you're fine not have git completion when logged in as rootvia sudo su, it's just a little bash kung fu to avoid trying to evaluate __git_ps1.

假设您在以rootvia登录时没有 git 完成sudo su,这只是为了避免尝试评估__git_ps1.

You can place any kind of conditional you want inside the PS1prompt (hence how it can substitute in the branch name when in a git directory). So, just wrap the git stuff in a conditional checking you're not user id 0 (root).

您可以在PS1提示中放置您想要的任何类型的条件(因此它如何在 git 目录中替换分支名称)。因此,只需将 git 内容包装在条件检查中,您就不是用户 ID 0 ( root)。

Replace in your export PS1statement:

在您的出口PS1声明中替换:

$(__git_ps1) 

with

$(if [ $(id -u) -ne 0 ]; then echo  $(__git_ps1) ; fi)

The whole prompt you have in the OP would look now look like this:

您在 OP 中的整个提示现在看起来像这样:

 PS1='\[3[32m\]\u@\h\[3[00m\]:\[3[34m\]\w\[3[31m\]$(if [ $(id -u) -ne 0 ]; then echo  $(__git_ps1) ; fi)\[3[00m\]$ '

Now in a shell you should be able to sudo suwithout the error message.

现在在 shell 中,您应该能够在sudo su没有错误消息的情况下进行操作。

回答by Adam Michael

If you prefer not to add extra flags like -l(or don't want to alias su and the like) you can also just change root's bashrc to not use __git_ps1.

如果您不想添加额外的标志-l(或不想为 su 等命名),您也可以将 root 的 bashrc 更改为不使用__git_ps1.

For example, in my /root/.bashrcI have (I like having root be red):

例如,在我的/root/.bashrc我有(我喜欢 root 是红色的):

export PS1='\[\e[1;31m\][\u@\h \W]# \[\e[0m\]'

Basically, just copy the PS1you have in your ~/.bashrcor similar to /root/.bashrcand delete any references to __git_ps1.

基本上,只需复制PS1您的~/.bashrc或类似的内容/root/.bashrc并删除对 __git_ps1 的任何引用。

Ideally, you rarely do development as root so won't need __git_ps1. If you do (ill advised), you can copy over all of the code needed to execute __git_ps1to /root/.bashrc.

理想情况下,您很少以 root 身份进行开发,因此不需要__git_ps1. 如果您这样做(不明智),您可以将执行所需的所有代码复制__git_ps1/root/.bashrc.

回答by tegularius

Maybe a little late, however you can replace in your PS1:

也许有点晚了,但是您可以在 PS1 中替换:

(__git_ps1)

with

(type -t __git_ps1 >& /dev/null && __git_ps1)

This will disable calling __git_ps1 when it is not available, which you probably wouldn't need as superuser anyway.

这将在 __git_ps1 不可用时禁用调用,无论如何您可能不需要作为超级用户。