Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

.bash_prompt: better git functions #2 #155

Open
wants to merge 6 commits into
from

Conversation

Projects
None yet
4 participants
Contributor

eins78 commented Nov 24, 2012

Duplicate, original closed itself: 😟
PR #143

BTW: if you want to use git outside of your prompt, you can just source the functions provided by git itself
Here the relevant functions are extracted because it would be overkill to source it all.

necolas and others added some commits Nov 4, 2012

bash: Update custom Bash prompt
Use git commands to directly enquire about the state of the working
tree. Where possible, git plumbing commands are used. In some instances,
git porcelain commands are used (where plumbing commands fail on freshly
initialized repos) but there is no reliance on string data in their
output.

The previous method relied on specific strings in the output of commands
(`status` and `branch`) which was very unreliable. Those commands make
no guarantees about the nature of the text they return and should not be
used for scripting purposes.

Enhancements included in this update:

* Display the short SHA of a commit when there is no branch name.
* Display the `$` character when there are stashed changes.

Conflicts:
	bash/bash_prompt
.bash_prompt: call 'git' directly, without aliases
Don't call `git`, call `command git`.

Otherwise the prompt could be slow if you aliased a git helper like hub, see <https://github.com/defunkt/hub#is-your-shell-prompt-slow>.

@mathiasbynens mathiasbynens and 2 others commented on an outdated diff Nov 24, 2012

+ echo $branch_name
+}
+
+# Git status information
+function prompt_git() {
+ local git_info git_state uc us ut st
+
+ if ! is_git_repo; then
+ return 1
+ fi
+
+ git_info=$(get_git_branch)
+
+ # Check for uncommitted changes in the index
+ if ! `command git diff --quiet --ignore-submodules --cached`; then
+ uc=""
@mathiasbynens

mathiasbynens Nov 24, 2012

Owner

I don’t get it. When does uc get a value other than the empty string?

@nvartolomei

nvartolomei Nov 24, 2012

Contributor

Never.

@mathiasbynens

mathiasbynens Nov 24, 2012

Owner

That’s what I’m not getting :) Then why bother?

@eins78

eins78 Nov 24, 2012

Contributor

I set to empty string to match your original style, but left it in so it is easier to adapt for other people.
The original style only showed a * when branch is dirty.

The new code checks for much more, if you want to assign a symbol, just pick one.

Compare with @necolas version: https://github.com/necolas/dotfiles/blob/master/bash/bash_prompt#L79-L96

Contributor

eins78 commented Nov 24, 2012

I added a comment to clarify how to use the different checks with symbols.
If you fill out all the symbols like @necolas did, your branch could look like:

master+*?$

(if your branch has staged, changed but unstaged, untracked and stashed files)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment