Skip to content

.bash_prompt: better git functions #2 #155

Open
wants to merge 6 commits into from

4 participants

@eins78
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 3, 2012
@necolas necolas 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
0df52ba
Max F. Albrecht .bash_prompt: integrate @necolas changes with upstream style 5c4fdb9
Max F. Albrecht .bash_prompt: safer echo without newline e885e10
Max F. Albrecht .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>.
67b06c5
@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
Owner

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

@nvartolomei
nvartolomei added a note Nov 24, 2012

Never.

@mathiasbynens
Owner

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

@eins78
eins78 added a note Nov 24, 2012

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@eins78
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
Something went wrong with that request. Please try again.