lyze / posh-git-sh Goto Github PK
View Code? Open in Web Editor NEWBash/ZSH version of the posh-git command prompt
License: GNU General Public License v3.0
Bash/ZSH version of the posh-git command prompt
License: GNU General Public License v3.0
When you run posh-git-sh on default OS X terminal with bash and open new tab it doesn't keep the current working directory. To fix the issue you have to add the following line to your .bash_profile
export PROMPT_COMMAND="$PROMPT_COMMAND;update_terminal_cwd;"
Hi, I got an error when using this script.
please take a look at it.
alexya@Alex-MacPro [08:52:19] [~/Source/Git/posh-git-sh] output=''
[output_error_info ?]
-> $ ls
README.md git-prompt.sh
after investigating, I think it may happen in git-prompt.sh
# Find how many commits we are ahead/behind our upstream
if [ -z "$legacy" ]; then
local output
output=$(git rev-list --count --left-right $upstream...HEAD 2>/dev/null)
return_code=$?
IFS=$' \t\n' read -r __POSH_BRANCH_BEHIND_BY __POSH_BRANCH_AHEAD_BY <<< $output
else
local output
output=$(git rev-list --left-right $upstream...HEAD 2>/dev/null)
return_code=$?
# produce equivalent output to --count for older versions of git
while IFS=$' \t\n' read -r commit; do
case "$commit" in
"<*") (( __POSH_BRANCH_BEHIND_BY++ )) ;;
">*") (( __POSH_BRANCH_AHEAD_BY++ )) ;;
esac
done <<< $output
fi
the line local output
will print error information output=''
in shell. (I don't know the reason)
The fix is to change to
local output=$(git rev-list --count --left-right $upstream...HEAD 2>/dev/null)
and then, the shell is good. e.g.
alexya@Alex-MacPro [08:56:56] [~/Source/Git/posh-git-sh] [output_error_info ≡]
-> $ ls
README.md git-prompt.sh
I don't have the permission to create a branch, so, could u please take a look at it?
My macos version is 10.10.1
git version is 2.7.2
shell is oh-my-zsh
As all of you know, now macbook comes with zsh, not bash.
But the instructions are not so clear for zsh.
I had to install Homebrew first, which took some time, but it was pretty good. (I had to enter computer password a few times)
So, I guess, it would be just nice to write a section for those who uses zsh, not bash.
Hello.
How can I add git status in .zshrc?
First of all, @lyze, huge thank you for porting this extension to bash! I've been using Powershell with Git on Windows for the better part of my career. Now I'm transitioning to Hyper with native Bash on Windows in Windows 10 - and I'm happy that I can continue using this excellent syntax.
I followed your instructions precisely:
And consequently the prompt is shown with the Git Posh syntax, on Hyper:
However, do you notice the ╔
in the start of the prompt line?
My goal is to use Hyper (terminal emulator) with the native Bash on Windows (which is Bash version 4.3). However, if I'm just loading Bash by itself, this is what I'm getting:
As you can see the repo status is all messed up, but I don't mind because it displays correctly on Hyper. But can you see that ☺
character in the start of the prompt line?
The comments/documentation in the CONFIG OPTIONS
section of the script say that GIT_PS1_SHOWUPSTREAM
and GIT_PS1_SHOWSTASHSTATE
can be set to configure the script, but those were removed from code in e4141d4 and replaced with reading options from git config
. GIT_PS1_SHOWCOLORHINTS
is also not used anywhere.
I get the following repo information output
( (\[\]\[\e[1;33m\] [\[\]\[\e[1;36m\]master\[\]\[\e[1;33m\]]\[\]\[\e[0;34m\]\[\]\[\e[m\] ))]$
I had a problem where __git_ps1 would always output (branche_name), even though I included your file. This is fixed if I change the name of __git_ps1 to something like __git_ps1_new.
I don't know how or where, but something was rewriting the function call.
(now that I got it working it's a lovely piece of work, you deserve more recognition for this!)
Before I used MacOSX default terminal
tool.
Today, I downloaded the tool iterm2
. But the posh-git-sh
worked partly. (see the screenshot)
Any help will be much appreciated. thx.
Write some basic acceptance tests. Perhaps they also compare against posh-git output. It would be nice to compare the colors as well.
I have a Bash warning popping up, but only when my working directory is inside a git repo that is checked out to a specific version tag:
bash: warning: command substitution: ignored null byte in input
To reproduce, it would go something like this in an empty directory:
git init
echo "hello world" > foo.txt
git add .
git commit --message "foo bar"
git tag v1.2.3
git checkout v1.2.3
After that, every time you press Enter and get a new prompt, the warning will show.
The warning shows up even with an extremely basic prompt configured, such as: PROMPT_COMMAND='__posh_git_ps1 "hello " "\nworld \\$ "'
I have $BASH_VERSION
5.0.11(1)-release
and git
is 2.24.0
Thank you very much for porting this extension to bash.
I just tried to install it yesterday and everything is working fine, except that I have a strange $17
at the end of my prompt when it's using git-prompt
nicolas@X260-NICOLAS:~
$ cd Source/java/slipdb/
nicolas@X260-NICOLAS:~/Source/java/slipdb [feat/filter-object ≡]$17
$ rm src/ -r
nicolas@X260-NICOLAS:~/Source/java/slipdb [feat/filter-object ≡ +0 ~0 -106]$17
$ git reset --hard
HEAD is now at 39b76c6 implement our filter object idea
nicolas@X260-NICOLAS:~/Source/java/slipdb [feat/filter-object ≡]$17
$
Command prompt configuration:
PROMPT_COMMAND='__posh_git_ps1 "\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] " "\n\$ ";'$PROMPT_COMMAND
I usually enable the failglob
shell option by default in bash via shopt -s failglob
. This results in errors being displayed when combined with using __posh_git_ps1()
in my PROMPT_COMMAND
.
Here is an example:
~/p/dev [main ≡ +0 ~3 -0 !] $ shopt -s failglob
bash: no match: \[\e[m\]
bash: no match: \[\033[0;93m\]
bash: no match: \[\033[0;93m\]
bash: no match: \[\033[0;93m\]
bash: no match: \[\033[0;96m\]
bash: no match: \[\033[0;92m\]
bash: no match: \[\033[0;91m\]
bash: no match: \[\033[0;93m\]
bash: no match: \[\033[0;32m\]
bash: no match: \[\033[0;32m\]
bash: no match: \[\033[0;31m\]
bash: no match: \[\033[0;91m\]
bash: no match: \[\033[0;31m\]
bash: no match: \[\033[0;96m\]
bash: no match: \[\e[0m\]
~/p/dev [main ≡ +0 ~3 -0 !] $ shopt -u failglob
~/p/dev [main ≡ +0 ~3 -0 !] $
I was able to resolve the issue by changing these lines in the __posh_color()
function:
From:
echo \\["$1"\\]
To:
(shopt -u failglob && echo \\["$1"\\])
However, I'm not sure if this might break other use cases. I could look into making sure it is robust.
Let me know if you're interested in a pull request with the fix + tests.
Using conda, when an environment different to root is used, the name of the environment is prepended in the system command:
(environment-name) user@linux ~ $
This behavior is lost when using posh-git-sh is in use.
Regardless the active conda environment, the command always prompt looks this way:
user@linux ~ $
The Git Status information still displayed correctly
Original posh-git
master ↑1 +0 ~1 -0 !
Posh-git-sh:
master ↑1 +0 ~1 -0
Currently the symbol used to show if there's anything in stash is a '$' - that's also the default ending character for most default prompt setups. As a result there's a doubled '$' character when there's something in stash.
It would be great to change that to something else (a '*' maybe?) and even better to have this parametrized.
Hi,
When you activate a Python virtualenv, posh-git-sh got the priority and I don't see the name of my virtual env on the prompt.
It is a bit annoying because I don't know if I am in my virtualenv or not.
I don't have this issue using the original powershell post-git version
toto@computer_name:~ $ mkdir -p ~/toto_test; cd ~/toto_test
toto@computer_name:~/toto_test $ git init
toto@computer_name:~/toto_test $ python -m venv .venv
toto@computer_name:~/toto_test $ source ./.venv/bin/activate
I should get something like that:
(.venv) toto@computer_name: ~/toto_test [main =]$
I got this instead
toto@computer_name: ~/toto_test [main =]$
I couldn't find a place to open an issue on the npm site, so I'm opening one here.
When I install posh-git-sh on my mac using npm ("sudo npm install -g posh-git" followed by "posh-git-install" for local user), the installation puts in the following lines:
source ~/posh-git.sh
PROMPT_COMMAND='__git_ps1 "\u@\h: \w" "\$ ":'$PROMPT_COMMAND
And I get the following error:
-bash: __git_ps1: command not found
The issue is that posh-git.sh does NOT have a function by that name. Instead, it is called __posh_git_ps1(){...}. I've updated my .bashrc to call the right function, but it would be nice if it is corrected in the npm install.
Thanks,
Yasir
Hey,
I just installed it and it seems that it does the trick, but it is very slow compared to powershell's posh-git. After each pressed 'enter' in the console I have to wait ca 3 sec or more to actually give me a new (re-counted) line. Is this normal behavior?
I have a problem when the prompt string is a few characters longer than the window width and a branch name is displayed.
The screenshot below illustrates the issue.
What you can see is the branch name is displayed, then the three stacked lines, but then the closing square bracket ]
is shown in blue (instead of yellow) near the beginning of the first line. Also, the input cursor is shown close after the closing square bracket and dollar sign: ] $ |
. The yellow pipe |
is my cursor, yellow is the intended colour by the way. Also, the working directory is intended that way, it's there so the dir is quite long. Instead, I could have chosen a very long branch name for similar results.
In my ~/.bash_profile
I have set the following.
export PROMPT_COMMAND='__posh_git_ps1 "$prefix" "$suffix";'$PROMPT_COMMAND
where $prefix
is my username@hostname workingdir
and $suffix
is $
, both with some colour specification, but nothing more.
When the Terminal window is a little smaller, the problem does not occur; see screenshot below for a terminal window of 77 characters wide. The one above is 80 characters wide. The problem occurs when the windows is 78, 79 or 80 characters wide. Narrower or wider windows behave correctly.
For very narrow Terminal windows (or very long working directories) the problems become worse, especially when the prompt string should wrap around more than 2 lines. Try resizing the Terminal window in a similar situation as my current screenshots show, with quite a long working directory.
I'd like to add this to awesome-zsh-plugins, but it doesn't have a license file.
Please add a license file - if you don't have one in mind, I recommend using choosealicense.com to help choose an appropriate one.
Thanks!
Before we begin, I'm no expert with zsh
(or any heavy shell customizations really), but since macOS Catalina made it the default shell on me, I figured I would just live with it. I'm also using plain-old Terminal.app, not iTerm2 or anything else.
I followed the instructions in the README, which at the time said to define precmd
as follows:
precmd() {
__posh_git_ps1 '\u@\h:\w ' '$ '
}
I pasted that into my .zshrc
file and reloaded my terminal. However, this is what happened:
posh-git-sh
worked, but prompt expansion wasn't. It took a bit of digging online before I found this comment on StackOverflow, showing that zsh
prompt expansions are totally different from bash
, which I'm guessing uses backslashes. Here's what worked for me:
precmd() {
__posh_git_ps1 '%n@%m:%~ ' '$ '
}
I can submit a quick PR if it turns out the instructions need to be changed for zsh
users. Thanks!
Greetings, I just wanted to drop in a quick feature request to not show any status symbol for the current branch if an upstream for it does not exist. It's just a nice reminder of whether I pushed up the branch to the remote yet or not.
Thanks for the great script, I was going crazy without this after getting so used to posh-git on Windows!
I am having a problem with understanding how to get this setup for a mac terminal bash environment. It was easy to setup the first part but I find this section to be entirely confusing.
a. If you are using bash, __git_ps1 can be used for PROMPT_COMMAND with two parameters,
and , which are strings you would put in $PS1 before and after the status string generated by the git-prompt machinery. For example, the followingPROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\$ "'$PROMPT_COMMAND
will show username, at-sign, host, colon, cwd, then various status strings, followed by dollar and space, as your prompt. Optionally, you can supply a third argument with a printf format string to fine-tune the output of the branch status.
Hi there!
My predicament is the following: I'm a Windows user who's recently been forced to use Linux for work. One of the biggest things I was missing was the posh-git Powershell scripts included in the Github desktop application, so I was happy when I found this Linux port. However, I can't quite get it to work.
First, I tried following the instructions in the readme. However, setting PROMPT_COMMAND in .bashrc like instructed gave me the following error message:
bash: printf: missing unicode digit for \u
Which would then repeat for every command prompt line.
I then found the alternate instructions to replace PS1 in the git-prompt.sh script itself. I did so, adding the following line near the end of .bashrc:
PS1=$PS1'$(__git_ps1 " [(%s)]")\$ '
Which worked, sort of. It always just printed the current branch and nothing else. I eventually figured out I was dealing with the same problem as #9, so I worked around that by renaming the __git_ps1 function to __git_ps1_2, which finally gave me the actual output, except with broken color commands:
user@machine:~/path/to/repo [( [(\[\]\[\e[1;33m\] [\[\]\[\e[1;36m\]master\[\]\[\e[1;32m\] +1 ~0 -0\[\]\[\e[1;33m\] |\[\]\[\e[0;31m\] +0 ~4 -0\[\]\[\e[1;33m\]]\[\]\[\e[0;34m\]$\[\]\[\e[m\])])]$
Which is obviously not ideal. What am I doing wrong?
I'd appreciate any help very much.
I want to use posh-git-sh in shell 'oh-my-zsh', but I failed. could you please give me some help?
I met an issue related to git status, please see the thread in stackoverflow.com.
I want to double check with you, will posh-git-sh script update the local repository status such as git fetch
when I ran git checkout master
to swtich branches?
From the design of git, when running git checkout <branch_name>
it will not fetch the information from remote repository. But one of my computer can get updated staus but another computer failed. I think it may be that I mis-config posh-git-sh in the computer.
But I don't know where I made mistake. What I did these days is to make my script install oh-my-zsh and posh-git-sh automaticaly. https://github.com/alexya/envconfig/blob/master/setup.sh
Any help will be much appreciated.
One feature I like about posh-git is the numeric indicator of how many commits I am behind or ahead after doing a fetch.
Have you considered adding such? Would you entertain a pull request?
Example:
C:\source\DataMigrator [develop ↓4]>
myuser@host:~/source/DataMigrator [develop ↓]$
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.