[英]Git completion and PS1 not working: “__git_ps1: command not found” on “sudo -s” or “sudo su” on Ubuntu 12.04
I installed git and git-flow completion adding these line to .bashrc of root and a normal_user on a Ubuntu 12.04 machine: 我安装了git和git-flow完成,将这些行添加到root的.bashrc和Ubuntu 12.04机器上的normal_user:
source /etc/git-completion.bash
source /etc/git-flow-completion.bash
GIT_PS1_SHOWUPSTREAM="verbose"
GIT_PS1_SHOWDIRTYSTATE=true
PS1='\[\033[32m\]\u@\h\[\033[00m\]:\[\033[34m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\$ '
When I log as root or normal_user git completion works. 当我以root身份登录或者normal_user时,git完成工作。 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.
但是,如果我使用“sudo -s”或“sudo su”git完成不起作用,每次按回车时我都会不断收到“__git_ps1:command not found”。 I tried to remove the "source" commands and use "apt-get install bash-completion" (bash-completion was already installed).
我试图删除“source”命令并使用“apt-get install bash-completion”(已经安装了bash-completion)。 So even without the 2 source I get the exact same behavior.
因此,即使没有2源,我也会得到完全相同的行为。
Anybody knows what the problem is and how to make it work? 有人知道问题是什么以及如何使其发挥作用?
When you do sudo su
it won't source the users .bashrc
. 当你做
sudo su
它不会获取用户.bashrc
。 The PS1 is inherited from the user you did the sudo su
from but the new shell doesn't know where it can find ___git_ps1
PS1继承自你做
sudo su
的用户,但新shell不知道它在哪里可以找到___git_ps1
You need to simulate a login by executing sudo su -l
您需要通过执行
sudo su -l
来模拟登录
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.sh
in the initial git-core files. 在您的情况下,它发生是因为git-prompt.sh文件未在终端启动时启动,可以在初始git-core文件中
find contrib/completion/git-prompt.sh
。
Probably already is present by the machine, for search: 可能已经由机器出现,用于搜索:
find ~ -name git-prompt.sh
找到〜-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 ];
if [-f $ HOME / git / 1.8.0 / contrib / completion / git-prompt.sh]; then
然后
.
。 $HOME/git/1.8.0/contrib/completion/git-prompt.sh
$ HOME /混帐/ 1.8.0 /的contrib /完成/ git-prompt.sh
fi
科幻
After all do: 毕竟做了:
.
。 ~/.bashrc
在〜/ .bashrc
The prompt functionality was split out of git-completion.bash into git-prompt.sh on May 22, 2012 ; 2012年5月22日,提示功能从git-completion.bash拆分为git-prompt.sh ; you will need to
source
that one as well. 你将需要
source
的是一个为好。 Git 1.7.12 was the first release to see this change. Git 1.7.12是第一个看到这种变化的版本。 I just had the same issue when updating my git-completion.bash.
我在更新我的git-completion.bash时遇到了同样的问题。
Assuming you're fine not have git completion when logged in as root
via sudo su
, it's just a little bash kung fu to avoid trying to evaluate __git_ps1
. 假设你通过
sudo su
以root
身份登录时没有git完成,这只是一个小小的bash功夫,以避免试图评估__git_ps1
。
You can place any kind of conditional you want inside the PS1
prompt (hence how it can substitute in the branch name when in a git directory). 您可以在
PS1
提示符中放置任何类型的条件(因此在git目录中它可以替换为分支名称)。 So, just wrap the git stuff in a conditional checking you're not user id 0 ( root
). 所以,只需在条件检查中包装git东西,你就不是用户ID 0(
root
)。
Replace in your export PS1
statement: 在您的导出
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='\[\033[32m\]\u@\h\[\033[00m\]:\[\033[34m\]\w\[\033[31m\]$(if [ $(id -u) -ne 0 ]; then echo $(__git_ps1) ; fi)\[\033[00m\]\$ '
Now in a shell you should be able to sudo su
without the error message. 现在在shell中你应该能够在没有错误消息的情况下
sudo su
。
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/.bashrc
I have (I like having root be red): 例如,在我的
/root/.bashrc
我(我喜欢root是红色的):
export PS1='\[\e[1;31m\][\u@\h \W]# \[\e[0m\]'
Basically, just copy the PS1
you have in your ~/.bashrc
or similar to /root/.bashrc
and delete any references to __git_ps1. 基本上,只需复制
~/.bashrc
的PS1
或类似于/root/.bashrc
并删除对__git_ps1的任何引用。
Ideally, you rarely do development as root so won't need __git_ps1
. 理想情况下,您很少以root身份进行开发,因此不需要
__git_ps1
。 If you do (ill advised), you can copy over all of the code needed to execute __git_ps1
to /root/.bashrc
. 如果您这样做(不明智),您可以将执行
__git_ps1
所需的所有代码__git_ps1
到/root/.bashrc
。
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不可用时调用__git_ps1,无论如何您可能不需要超级用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.