* command prompt script for current branch @ 2008-02-06 19:52 Stephen Sinclair 2008-02-06 20:42 ` Junio C Hamano 2008-02-06 21:09 ` command prompt script for current branch Jakub Narebski 0 siblings, 2 replies; 16+ messages in thread From: Stephen Sinclair @ 2008-02-06 19:52 UTC (permalink / raw) To: git [-- Attachment #1: Type: text/plain, Size: 1339 bytes --] Hello, Attached is a script I quickly wrote up yesterday. Sometimes I've found I started working only to realize I was on the wrong branch. (Now that I'm accustomed to working with git, I make many small branches, so it happens.) So I made this small script to simply write the name of the currently checked out branch, as well as information on how many commits it differs from the remote tracking branch, if any. It also appends an exclamation mark if I have uncommitted changes. I stuck it in my PS1 environment variable, so that it would be part of my command prompt. I tried to choose git commands that would be relatively quick, since this will be run for every single command prompt. If I've chosen suboptimal ways of doing this, please let me know. There's a few too many 'grep' and 'sed' commands for my liking, so maybe there is a way to get the same information more directly instead of parsing git command output. Anyways, if it's of general interest, I'd be happy to make a patch for /contrib. For my tiny git repos, this seems to have pretty much no negative impact on my work flow. When I cd'd to my git.git clone there was about a 1/2-second delay the first time, but subsequent command prompts incurred no visible delay. I've no idea how fast it would be for something as big as the kernel, for example. Steve [-- Attachment #2: curgitbranch.sh --] [-- Type: application/x-sh, Size: 1960 bytes --] ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: command prompt script for current branch 2008-02-06 19:52 command prompt script for current branch Stephen Sinclair @ 2008-02-06 20:42 ` Junio C Hamano 2008-02-06 22:14 ` [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress Robin Rosenberg 2008-02-06 21:09 ` command prompt script for current branch Jakub Narebski 1 sibling, 1 reply; 16+ messages in thread From: Junio C Hamano @ 2008-02-06 20:42 UTC (permalink / raw) To: Stephen Sinclair; +Cc: git "Stephen Sinclair" <radarsat1@gmail.com> writes: > ... Sometimes I've > found I started working only to realize I was on the wrong branch. See contrib/completion/git-completion.bash if you are a bash user. ^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress 2008-02-06 20:42 ` Junio C Hamano @ 2008-02-06 22:14 ` Robin Rosenberg 2008-02-06 22:23 ` Robin Rosenberg ` (2 more replies) 0 siblings, 3 replies; 16+ messages in thread From: Robin Rosenberg @ 2008-02-06 22:14 UTC (permalink / raw) To: Junio C Hamano; +Cc: Stephen Sinclair, git Hi Junio, I'm not sure whether you didn't like it or not or just lost it. Here is the vastly enhanced prompt again, rebased. -- robin >From 76aa8bae8491c1ffbd6e3f5c99ab014ef87794c8 Mon Sep 17 00:00:00 2001 From: Shawn O. Pearce <spearce@spearce.org> Date: Tue, 4 Sep 2007 03:13:01 -0400 Subject: This patch makes the git prompt (when enabled) show if a merge or a rebase is unfinished. It also detects if a bisect is being done as well as detached checkouts. Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com> --- contrib/completion/git-completion.bash | 37 ++++++++++++++++++++++++++++--- 1 files changed, 33 insertions(+), 4 deletions(-) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 0d33f9a..4ea727b 100755 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -64,12 +64,41 @@ __gitdir () __git_ps1 () { - local b="$(git symbolic-ref HEAD 2>/dev/null)" - if [ -n "$b" ]; then + local g="$(git rev-parse --git-dir 2>/dev/null)" + if [ -n "$g" ]; then + local r + local b + if [ -d "$g/../.dotest" ] + then + r="|AM/REBASE" + b="$(git symbolic-ref HEAD 2>/dev/null)" + elif [ -f "$g/.dotest-merge/interactive" ] + then + r="|REBASE-i" + b="$(cat $g/.dotest-merge/head-name)" + elif [ -d "$g/.dotest-merge" ] + then + r="|REBASE-m" + b="$(cat $g/.dotest-merge/head-name)" + elif [ -f "$g/MERGE_HEAD" ] + then + r="|MERGING" + b="$(git symbolic-ref HEAD 2>/dev/null)" + else + if [ -f $g/BISECT_LOG ] + then + r="|BISECTING" + fi + if ! b="$(git symbolic-ref HEAD 2>/dev/null)" + then + b="$(cut -c1-7 $g/HEAD)..." + fi + fi + if [ -n "$1" ]; then - printf "$1" "${b##refs/heads/}" + printf "$1" "${b##refs/heads/}$r" else - printf " (%s)" "${b##refs/heads/}" + printf " (%s)" "${b##refs/heads/}$r" fi fi } -- 1.5.4.rc4.25.g81cc ^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress 2008-02-06 22:14 ` [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress Robin Rosenberg @ 2008-02-06 22:23 ` Robin Rosenberg 2008-02-06 22:31 ` Junio C Hamano 2008-02-06 23:21 ` [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress Jakub Narebski 2 siblings, 0 replies; 16+ messages in thread From: Robin Rosenberg @ 2008-02-06 22:23 UTC (permalink / raw) To: Junio C Hamano; +Cc: Stephen Sinclair, git, Shawn O. Pearce Actually I wrote the patch and Shawn sent back a sligtly better version, i.e.: From: Robin Rosenberg <robin.rosenberg@dewire.com> -- robin ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress 2008-02-06 22:14 ` [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress Robin Rosenberg 2008-02-06 22:23 ` Robin Rosenberg @ 2008-02-06 22:31 ` Junio C Hamano 2008-02-07 0:23 ` Robin Rosenberg 2008-02-06 23:21 ` [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress Jakub Narebski 2 siblings, 1 reply; 16+ messages in thread From: Junio C Hamano @ 2008-02-06 22:31 UTC (permalink / raw) To: Robin Rosenberg; +Cc: Shawn O. Pearce, Stephen Sinclair, git Robin Rosenberg <robin.rosenberg.lists@dewire.com> writes: > I'm not sure whether you didn't like it or not or just lost it. Here > is the vastly enhanced prompt again, rebased. Most likely "lost", and I appreciate a reminder like this. > From 76aa8bae8491c1ffbd6e3f5c99ab014ef87794c8 Mon Sep 17 00:00:00 2001 > From: Shawn O. Pearce <spearce@spearce.org> > Date: Tue, 4 Sep 2007 03:13:01 -0400 > Subject: > > This patch makes the git prompt (when enabled) show if a merge or a > rebase is unfinished. It also detects if a bisect is being done as > well as detached checkouts. > > Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com> But this looks funny, without Subject: and also S-o-b by the original author (if Shawn is indeed the original author). It appears that the patch originally was from Shawn and after discussion petered out around Oct 1st 2007 there wasn't a resend for inclusion. Is this improved/revised in any way from the one in the thread? http://thread.gmane.org/gmane.comp.version-control.git/57271/focus=57536 ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress 2008-02-06 22:31 ` Junio C Hamano @ 2008-02-07 0:23 ` Robin Rosenberg 2008-02-07 6:34 ` Shawn O. Pearce 0 siblings, 1 reply; 16+ messages in thread From: Robin Rosenberg @ 2008-02-07 0:23 UTC (permalink / raw) To: Junio C Hamano; +Cc: Shawn O. Pearce, Stephen Sinclair, git onsdagen den 6 februari 2008 skrev Junio C Hamano: > Robin Rosenberg <robin.rosenberg.lists@dewire.com> writes: > > > I'm not sure whether you didn't like it or not or just lost it. Here > > is the vastly enhanced prompt again, rebased. > > Most likely "lost", and I appreciate a reminder like this. > > > From 76aa8bae8491c1ffbd6e3f5c99ab014ef87794c8 Mon Sep 17 00:00:00 2001 > > From: Shawn O. Pearce <spearce@spearce.org> > > Date: Tue, 4 Sep 2007 03:13:01 -0400 > > Subject: > > > > This patch makes the git prompt (when enabled) show if a merge or a > > rebase is unfinished. It also detects if a bisect is being done as > > well as detached checkouts. > > > > Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com> > > But this looks funny, without Subject: and also S-o-b by the > original author (if Shawn is indeed the original author). It > appears that the patch originally was from Shawn and after > discussion petered out around Oct 1st 2007 there wasn't a resend > for inclusion. Is this improved/revised in any way from the one > in the thread? No. I sent a patch and Shawn modified it somewhat and posted the full modified patch. Here is the same patch again (to make sure you get the right one). It has a better comment than the one I sent recently. -- robin >From 82a5b8d5b043ffc7c1950b391ec7ae69d575640b Mon Sep 17 00:00:00 2001 From: Robin Rosenberg <robin.rosenberg@dewire.com> Date: Sun, 30 Sep 2007 02:20:45 +0200 Subject: [PATCH] Improve bash prompt to detect various states like an unfinished merge This patch makes the git prompt (when enabled) show if a merge or a rebase is unfinished. It also detects if a bisect is being done as well as detached checkouts. An uncompleted git-am cannot be distinguised from a rebase (the non-interactive version). Instead of having an even longer prompt we simply ignore that and hope the power users that use git-am knows the difference. Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com> --- contrib/completion/git-completion.bash | 37 ++++++++++++++++++++++++++++--- 1 files changed, 33 insertions(+), 4 deletions(-) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 0d33f9a..4ea727b 100755 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -64,12 +64,41 @@ __gitdir () __git_ps1 () { - local b="$(git symbolic-ref HEAD 2>/dev/null)" - if [ -n "$b" ]; then + local g="$(git rev-parse --git-dir 2>/dev/null)" + if [ -n "$g" ]; then + local r + local b + if [ -d "$g/../.dotest" ] + then + r="|AM/REBASE" + b="$(git symbolic-ref HEAD 2>/dev/null)" + elif [ -f "$g/.dotest-merge/interactive" ] + then + r="|REBASE-i" + b="$(cat $g/.dotest-merge/head-name)" + elif [ -d "$g/.dotest-merge" ] + then + r="|REBASE-m" + b="$(cat $g/.dotest-merge/head-name)" + elif [ -f "$g/MERGE_HEAD" ] + then + r="|MERGING" + b="$(git symbolic-ref HEAD 2>/dev/null)" + else + if [ -f $g/BISECT_LOG ] + then + r="|BISECTING" + fi + if ! b="$(git symbolic-ref HEAD 2>/dev/null)" + then + b="$(cut -c1-7 $g/HEAD)..." + fi + fi + if [ -n "$1" ]; then - printf "$1" "${b##refs/heads/}" + printf "$1" "${b##refs/heads/}$r" else - printf " (%s)" "${b##refs/heads/}" + printf " (%s)" "${b##refs/heads/}$r" fi fi } -- 1.5.4.rc4.25.g81cc ^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress 2008-02-07 0:23 ` Robin Rosenberg @ 2008-02-07 6:34 ` Shawn O. Pearce 2008-02-08 11:26 ` Steffen Prohaska 0 siblings, 1 reply; 16+ messages in thread From: Shawn O. Pearce @ 2008-02-07 6:34 UTC (permalink / raw) To: Robin Rosenberg; +Cc: Junio C Hamano, Stephen Sinclair, git Robin Rosenberg <robin.rosenberg.lists@dewire.com> wrote: > I sent a patch and Shawn modified it somewhat and posted the full modified > patch. Here is the same patch again (to make sure you get the right one). It > has a better comment than the one I sent recently. > > -- robin > > From 82a5b8d5b043ffc7c1950b391ec7ae69d575640b Mon Sep 17 00:00:00 2001 > From: Robin Rosenberg <robin.rosenberg@dewire.com> > Date: Sun, 30 Sep 2007 02:20:45 +0200 > Subject: [PATCH] Improve bash prompt to detect various states like an unfinished merge > > This patch makes the git prompt (when enabled) show if a merge or a > rebase is unfinished. It also detects if a bisect is being done as > well as detached checkouts. > > An uncompleted git-am cannot be distinguised from a rebase (the > non-interactive version). Instead of having an even longer prompt > we simply ignore that and hope the power users that use git-am knows > the difference. > > Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com> For what it's worth: Acked-by: Shawn O. Pearce <spearce@spearce.org> -- Shawn. ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress 2008-02-07 6:34 ` Shawn O. Pearce @ 2008-02-08 11:26 ` Steffen Prohaska 2008-02-08 12:25 ` Johannes Schindelin 0 siblings, 1 reply; 16+ messages in thread From: Steffen Prohaska @ 2008-02-08 11:26 UTC (permalink / raw) To: Robin Rosenberg Cc: Junio C Hamano, Shawn O. Pearce, Stephen Sinclair, Git Mailing List On Feb 7, 2008, at 7:34 AM, Shawn O. Pearce wrote: > Robin Rosenberg <robin.rosenberg.lists@dewire.com> wrote: >> I sent a patch and Shawn modified it somewhat and posted the full >> modified >> patch. Here is the same patch again (to make sure you get the >> right one). It >> has a better comment than the one I sent recently. >> >> -- robin >> >> From 82a5b8d5b043ffc7c1950b391ec7ae69d575640b Mon Sep 17 00:00:00 >> 2001 >> From: Robin Rosenberg <robin.rosenberg@dewire.com> >> Date: Sun, 30 Sep 2007 02:20:45 +0200 >> Subject: [PATCH] Improve bash prompt to detect various states like >> an unfinished merge >> >> This patch makes the git prompt (when enabled) show if a merge or a >> rebase is unfinished. It also detects if a bisect is being done as >> well as detached checkouts. >> >> An uncompleted git-am cannot be distinguised from a rebase (the >> non-interactive version). Instead of having an even longer prompt >> we simply ignore that and hope the power users that use git-am knows >> the difference. >> >> Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com> > > For what it's worth: > > Acked-by: Shawn O. Pearce <spearce@spearce.org> This improved prompt is great. I already miss it each time I switch back from next to master. Steffen ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress 2008-02-08 11:26 ` Steffen Prohaska @ 2008-02-08 12:25 ` Johannes Schindelin 2008-02-08 13:12 ` Steffen Prohaska 0 siblings, 1 reply; 16+ messages in thread From: Johannes Schindelin @ 2008-02-08 12:25 UTC (permalink / raw) To: Steffen Prohaska Cc: Robin Rosenberg, Junio C Hamano, Shawn O. Pearce, Stephen Sinclair, Git Mailing List Hi, On Fri, 8 Feb 2008, Steffen Prohaska wrote: > This improved prompt is great. I already miss it each time I switch > back from next to master. Maybe we (as in msysgit) should install the git-completion.bash script into /share/git-core/? Ciao, Dscho ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress 2008-02-08 12:25 ` Johannes Schindelin @ 2008-02-08 13:12 ` Steffen Prohaska 2008-02-08 14:06 ` [PATCH/RFC] Make git-completion.bash a first-class citizen Johannes Schindelin 0 siblings, 1 reply; 16+ messages in thread From: Steffen Prohaska @ 2008-02-08 13:12 UTC (permalink / raw) To: Johannes Schindelin Cc: Robin Rosenberg, Junio C Hamano, Shawn O. Pearce, Stephen Sinclair, Git Mailing List On Feb 8, 2008, at 1:25 PM, Johannes Schindelin wrote: > On Fri, 8 Feb 2008, Steffen Prohaska wrote: > >> This improved prompt is great. I already miss it each time I switch >> back from next to master. > > Maybe we (as in msysgit) should install the git-completion.bash script > into /share/git-core/? Yes. Steffen ^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH/RFC] Make git-completion.bash a first-class citizen 2008-02-08 13:12 ` Steffen Prohaska @ 2008-02-08 14:06 ` Johannes Schindelin 0 siblings, 0 replies; 16+ messages in thread From: Johannes Schindelin @ 2008-02-08 14:06 UTC (permalink / raw) To: Steffen Prohaska Cc: Robin Rosenberg, Junio C Hamano, Shawn O. Pearce, Stephen Sinclair, Git Mailing List This moves git-completion.bash out of contrib/, and installs it to $sharedir/git-completion/ Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- On Fri, 8 Feb 2008, Steffen Prohaska wrote: > On Feb 8, 2008, at 1:25 PM, Johannes Schindelin wrote: > > > On Fri, 8 Feb 2008, Steffen Prohaska wrote: > > > > > This improved prompt is great. I already miss it each time > > > I switch back from next to master. > > > > Maybe we (as in msysgit) should install the > > git-completion.bash script into /share/git-core/? > > Yes. How about this? Makefile | 4 ++++ .../git-completion.bash => git-completion.bash | 0 2 files changed, 4 insertions(+), 0 deletions(-) rename contrib/completion/git-completion.bash => git-completion.bash (100%) diff --git a/Makefile b/Makefile index de8c752..77efc3f 100644 --- a/Makefile +++ b/Makefile @@ -168,6 +168,7 @@ gitexecdir = $(bindir) sharedir = $(prefix)/share template_dir = $(sharedir)/git-core/templates htmldir=$(sharedir)/doc/git-doc +completiondir = $(sharedir)/git-completion ifeq ($(prefix),/usr) sysconfdir = /etc else @@ -784,6 +785,7 @@ bindir_SQ = $(subst ','\'',$(bindir)) mandir_SQ = $(subst ','\'',$(mandir)) infodir_SQ = $(subst ','\'',$(infodir)) gitexecdir_SQ = $(subst ','\'',$(gitexecdir)) +completiondir_SQ = $(subst ','\'',$(completiondir)) template_dir_SQ = $(subst ','\'',$(template_dir)) htmldir_SQ = $(subst ','\'',$(htmldir)) prefix_SQ = $(subst ','\'',$(prefix)) @@ -1067,6 +1069,8 @@ install: all $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(gitexecdir_SQ)' $(INSTALL) $(ALL_PROGRAMS) '$(DESTDIR_SQ)$(gitexecdir_SQ)' $(INSTALL) git$X '$(DESTDIR_SQ)$(bindir_SQ)' + $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(completiondir_SQ)' + $(INSTALL) git-completion.bash '$(DESTDIR_SQ)$(completiondir_SQ)' $(MAKE) -C templates DESTDIR='$(DESTDIR_SQ)' install $(MAKE) -C perl prefix='$(prefix_SQ)' DESTDIR='$(DESTDIR_SQ)' install ifndef NO_TCLTK diff --git a/contrib/completion/git-completion.bash b/git-completion.bash similarity index 100% rename from contrib/completion/git-completion.bash rename to git-completion.bash -- 1.5.4.1261.g18335 ^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress 2008-02-06 22:14 ` [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress Robin Rosenberg 2008-02-06 22:23 ` Robin Rosenberg 2008-02-06 22:31 ` Junio C Hamano @ 2008-02-06 23:21 ` Jakub Narebski 2008-02-06 23:44 ` Mike Hommey 2 siblings, 1 reply; 16+ messages in thread From: Jakub Narebski @ 2008-02-06 23:21 UTC (permalink / raw) To: Robin Rosenberg; +Cc: Junio C Hamano, Stephen Sinclair, git Robin Rosenberg <robin.rosenberg.lists@dewire.com> writes: > This patch makes the git prompt (when enabled) show if a merge or a > rebase is unfinished. It also detects if a bisect is being done as > well as detached checkouts. [...] > + if [ -d "$g/../.dotest" ] [...] > + elif [ -f "$g/.dotest-merge/interactive" ] [...] > + elif [ -d "$g/.dotest-merge" ] Hmmm... is it time to ressurect "git explain" / "git info" / "git state" command idea, as to not need to harcode info about state in scripts, putting it in only one place? -- Jakub Narebski Poland ShadeHawk on #git ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress 2008-02-06 23:21 ` [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress Jakub Narebski @ 2008-02-06 23:44 ` Mike Hommey 0 siblings, 0 replies; 16+ messages in thread From: Mike Hommey @ 2008-02-06 23:44 UTC (permalink / raw) To: Jakub Narebski; +Cc: Robin Rosenberg, Junio C Hamano, Stephen Sinclair, git On Wed, Feb 06, 2008 at 03:21:40PM -0800, Jakub Narebski wrote: > Robin Rosenberg <robin.rosenberg.lists@dewire.com> writes: > > > This patch makes the git prompt (when enabled) show if a merge or a > > rebase is unfinished. It also detects if a bisect is being done as > > well as detached checkouts. > [...] > > + if [ -d "$g/../.dotest" ] > [...] > > + elif [ -f "$g/.dotest-merge/interactive" ] > [...] > > + elif [ -d "$g/.dotest-merge" ] > > Hmmm... is it time to ressurect "git explain" / "git info" / "git state" > command idea, as to not need to harcode info about state in scripts, > putting it in only one place? Maybe git status could deal with that job. Mike ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: command prompt script for current branch 2008-02-06 19:52 command prompt script for current branch Stephen Sinclair 2008-02-06 20:42 ` Junio C Hamano @ 2008-02-06 21:09 ` Jakub Narebski 2008-02-06 22:13 ` Stephen Sinclair 1 sibling, 1 reply; 16+ messages in thread From: Jakub Narebski @ 2008-02-06 21:09 UTC (permalink / raw) To: Stephen Sinclair; +Cc: git "Stephen Sinclair" <radarsat1@gmail.com> writes: > Attached is a script I quickly wrote up yesterday. Sometimes I've > found I started working only to realize I was on the wrong branch. > (Now that I'm accustomed to working with git, I make many small > branches, so it happens.) First, it is much better to put such script inline, in the body of your email. This makes commenting about script body much easier. And if you have to attach it, for example because your mailer mangles whitespace (which is not the case here), make sure that attachement uses "text/plain", and if possible "inline" disposition, to better _view_ the code without need to save it to temporary file. I would take an exeption and comment on your code, even though you make it hard to do so. Second, there are numerous examples on how to create git-aware prompt. Those include code in contrib/completion/git-completion.bash in git sources: # 4) Consider changing your PS1 to also show the current branch: # PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ ' # # The argument to __git_ps1 will be displayed only if you # are currently in a git repository. The %s token will be # the name of the current branch. and the _git_ps1 file in sources of "Git In Nutshell", with git repository at http://git.jonas.iki.fi/git_guides.git; this code by yours truly is heavily commented to make it easier to understand. I'm not sure if it is included in "Git In Nutshell" (http://jonas.iki.fi/git_guides). I have put the _git_ps1 code at the end of this post. > So I made this small script to simply write the name of the currently > checked out branch, as well as information on how many commits it > differs from the remote tracking branch, if any. > It also appends an exclamation mark if I have uncommitted changes. I > stuck it in my PS1 environment variable, so that it would be part of > my command prompt. Third, the information on how many commits it differes from coupled remote tracking branch is not that fast to get; additionally you don't always _have_ coupled remote tracking branch. For me the number of commits current branch differs from origin repo, and "dirtiness" of branch are not important, while working with detached HEAD and marking branches which are StGit controlled is. > -- >8 -- > #!/bin/bash > > # exit if not a git repo > if ! (git branch >/dev/null 2>&1); then > exit > fi There are much better ways to do this; it is usually much better to use plumbing (engine) commands in scripts, not porcelain, which is meant for user interaction. See code in _git_ps1 Besides, why do you run git-branch _twice_? > BRANCH=$(git branch | grep \* | sed s,\*\ ,, ) There is much better way to do this with plumbing. > REMOTE=$(git config --list | grep branch.$BRANCH.remote 2>/dev/null | cut -f2 -d=) > MERGE=$(git config --list | grep branch.$BRANCH.merge 2>/dev/null | sed s,^.*=refs/heads/,,) Why do you use "git config --list" then grep for key, instead of asking git-config to return results for given key with "git config --get" or "git config --get-all"? By the way, when you use grep you should quote '.', as '.' means any character in regular expression. You do not need put regular expression (parameter to grep) in double quotes _only_ because refnames (and branchnames) cannot contain spaces. Your code wouldn't be able to deal with being on detached HEAD. > # exit if no branch found > if [ "${BRANCH}"x == x ]; then > exit > fi This is mixture of coding for portability with bash-isms; either use 'test' instead of '[', or use '-z' operator. > echo -n ' '[$BRANCH > > # dirty state > if [ "$(git-diff-index --name-only HEAD)"x != x ]; then > echo -n \! > fi Err... if you want to check if there are differences, it would be better to use '--quiet' (which implies '--exit-code') and check for exit code instead of checking if there was any output. BTW. there is slight inconsistency in using dashed (git-diff-index) and dashless (git branch) of diff commands. In "external" script which does not source git shell script "library", it is better to use dashless form. > # exit if no remote tracking branch found > if [ ${REMOTE}x == x -a ${MERGE}x == x ]; then > echo ] > exit > fi > > # Uncomment this line to ignore remote tracking branches > # echo ]; exit > > # calc number of revs between remote and local > FWDREVS=$(git rev-list $REMOTE/$MERGE..$BRANCH 2>/dev/null 2>&1 | wc -l) > > # and the other way, in case it is not up to date > BACKREVS=$(git rev-list $BRANCH..$REMOTE/$MERGE 2>/dev/null 2>&1 | wc -l) > > echo -n ' '-\> $REMOTE/$MERGE > > if [ $FWDREVS -gt 0 ]; then > echo -n +$FWDREVS > fi > > if [ $BACKREVS -gt 0 ]; then > echo -n -$BACKREVS > fi > > echo ] Why do you use sequence of "echo -n", instead of setting variables, perhaps with the help of some more advanced constructs like in example below, then using one "echo" to output it all? -- >8 -- function _git_ps1() { # 'git_dir' is absolute path to git repository # 'rel' is path relative to top dir in repository # 'br' is current branch name, or 'HEAD' if we are on detached HEAD local git_dir rel br # first call to git-rev-parse also checks if we are inside git # repository; if we are not in git repository, use default prompt, # provided as an argument rel=$(git rev-parse --show-prefix 2>/dev/null) || \ { echo "$@" ; return; } rel=${rel%\/} # get branch name, strip 'refs/heads/' prefix, # and use 'HEAD' for detached HEAD (no branch name) br=$(git symbolic-ref HEAD 2>/dev/null) br=${br#refs/heads/} br=${br:-HEAD} # path to top dir of git repository loc=${PWD%/$rel} # the following code is important only if you use StGit, # to note if you are on StGit controlled branch; # use second part of conditional if you don't use StGit git_dir=$(git rev-parse --git-dir) if [ "$br" -a -e "$git_dir/patches/$br" ]; then echo "$br:${loc/*\/}${rel:+/$rel}# " else echo "$br:${loc/*\/}${rel:+/$rel}> " fi } ## bash: ## set PS1 only if we are in interactive shell (PS1 is set) # [ "$PS1" ] && PS1='$(_git_ps1 "\u@\h:\w> ")' ## zsh: # function precmd() { # PS1=`_git_ps1 '%m:%~%# '` # } # vim:filetype=sh -- Jakub Narebski Poland ShadeHawk on #git ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: command prompt script for current branch 2008-02-06 21:09 ` command prompt script for current branch Jakub Narebski @ 2008-02-06 22:13 ` Stephen Sinclair 2008-02-06 22:56 ` Jakub Narebski 0 siblings, 1 reply; 16+ messages in thread From: Stephen Sinclair @ 2008-02-06 22:13 UTC (permalink / raw) To: Jakub Narebski; +Cc: git > > Attached is a script I quickly wrote up yesterday. Sometimes I've > > found I started working only to realize I was on the wrong branch. > > (Now that I'm accustomed to working with git, I make many small > > branches, so it happens.) > > First, it is much better to put such script inline, in the body of > your email. This makes commenting about script body much easier. > And if you have to attach it, for example because your mailer mangles > whitespace (which is not the case here), make sure that attachement > uses "text/plain", and if possible "inline" disposition, to better > _view_ the code without need to save it to temporary file. > > I would take an exeption and comment on your code, even though you > make it hard to do so. Thank you. Since there's already a solution for this in contrib, (didn't see it, sorry), I'll just take your recommendations on coding style for any future stuff I do. Steve ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: command prompt script for current branch 2008-02-06 22:13 ` Stephen Sinclair @ 2008-02-06 22:56 ` Jakub Narebski 0 siblings, 0 replies; 16+ messages in thread From: Jakub Narebski @ 2008-02-06 22:56 UTC (permalink / raw) To: Stephen Sinclair; +Cc: git On Wed, 6 Feb 2008, Stephen Sinclair wrote: Please provide attribution (who write what) when quoting, like below: > Jakub Narebski wrote: >> "Stephen Sinclair" <radarsat1@gmail.com> writes: >>> Attached is a script I quickly wrote up yesterday. Sometimes I've >>> found I started working only to realize I was on the wrong branch. >>> (Now that I'm accustomed to working with git, I make many small >>> branches, so it happens.) >> >> First, it is much better to put such script inline, in the body of >> your email. This makes commenting about script body much easier. >> And if you have to attach it, for example because your mailer mangles >> whitespace (which is not the case here), make sure that attachement >> uses "text/plain", and if possible "inline" disposition, to better >> _view_ the code without need to save it to temporary file. >> >> I would take an exeption and comment on your code, even though you >> make it hard to do so. > > Thank you. Since there's already a solution for this in contrib, > (didn't see it, sorry), I'll just take your recommendations on coding > style for any future stuff I do. First, the code in contrib doesn't do all your code did. It doesn't show if workarea is "dirty" (although I'm not sure if this is very useful), nor does it show number of commits since or to mainline (origin) version, although with working on (with) feature/topic branches which does not have corresponding remote tracking branch it seems not useful. On the other hand code in _git_ps1 (taken from one of the "git prompt" posts floating on git mailing list, and available in archives, together with /usr/share/stgit/contrib/stgbashprompt.sh from StGit, Quilt-like patch management layer on top of Git) shows also name of repository and path in repository, and also if the branch is managed by StGit. Second, I think you have learned something. Both on rules regarding discussion, netiquette and sending contributions on this mailing list, and about how to write scripts for/around git. (At least I hope so). -- Jakub Narebski Poland ^ permalink raw reply [flat|nested] 16+ messages in thread
end of thread, other threads:[~2008-02-08 14:07 UTC | newest] Thread overview: 16+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2008-02-06 19:52 command prompt script for current branch Stephen Sinclair 2008-02-06 20:42 ` Junio C Hamano 2008-02-06 22:14 ` [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress Robin Rosenberg 2008-02-06 22:23 ` Robin Rosenberg 2008-02-06 22:31 ` Junio C Hamano 2008-02-07 0:23 ` Robin Rosenberg 2008-02-07 6:34 ` Shawn O. Pearce 2008-02-08 11:26 ` Steffen Prohaska 2008-02-08 12:25 ` Johannes Schindelin 2008-02-08 13:12 ` Steffen Prohaska 2008-02-08 14:06 ` [PATCH/RFC] Make git-completion.bash a first-class citizen Johannes Schindelin 2008-02-06 23:21 ` [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress Jakub Narebski 2008-02-06 23:44 ` Mike Hommey 2008-02-06 21:09 ` command prompt script for current branch Jakub Narebski 2008-02-06 22:13 ` Stephen Sinclair 2008-02-06 22:56 ` Jakub Narebski
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).