{"thread":{"id":"43379","subject":"Re: [StGIT PATCH] Bash snippet to show branch and patch in bash prompt","startedAt":"2006-10-29T23:37:45Z","lastAt":"2006-11-02T11:24:02Z","messageCount":7,"participants":["Catalin Marinas","Eran Tromer","Robin Rosenberg"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"296994","messageId":"20061029233745.24899.1470.stgit@lathund.dewire.com","threadId":"43379","inReplyTo":null,"subject":"[StGIT PATCH] Bash snippet to show branch and patch in bash prompt","fromName":"Robin Rosenberg","fromEmail":"robin.rosenberg@dewire.com","sentAt":"2006-10-29T23:37:45Z","receivedAt":"2006-10-29T23:37:45Z","isPatch":true,"sender":{"key":"robin.rosenberg@dewire.com","avatar":"https://avatars.githubusercontent.com/u/46357?v=4"},"body":"From: Robin Rosenberg <robin.rosenberg@dewire.com>\n\nSigned-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>\n---\n\n contrib/stgbashprompt.sh |   16 ++++++++++++++++\n 1 files changed, 16 insertions(+), 0 deletions(-)\n\ndiff --git a/contrib/stgbashprompt.sh b/contrib/stgbashprompt.sh\nnew file mode 100755\nindex 0000000..792da53\n--- /dev/null\n+++ b/contrib/stgbashprompt.sh\n@@ -0,0 +1,16 @@\n+# include this in your bashrc or copy to /etc/bash_completions.d\n+\n+if [ \"$PS1\" ]; then\n+    # trap 'PS1=\"\\u@\\h [$(stg top)] \\w]\\$ \"' DEBUG\n+    function stgtag\n+    {\n+\tbr=$(stg branch 2>/dev/null)\n+\ttop=$(stg top 2>/dev/null)\n+\tif [[ -n \"$br$top\" ]];then\n+\t    echo \"[$top@$br]\"\n+\t    return\n+\tfi\n+    }\n+    PS1='\\u@\\h$(stgtag)\\w\\$ '\n+\n"},{"id":"296668","messageId":"4545CC6F.90001@tromer.org","threadId":"43379","inReplyTo":"20061029233745.24899.1470.stgit@lathund.dewire.com","subject":"Re: [StGIT PATCH] Bash snippet to show branch and patch in bash prompt","fromName":"Eran Tromer","fromEmail":"git2eran@tromer.org","sentAt":"2006-10-30T09:57:03Z","receivedAt":"2006-10-30T09:57:03Z","isPatch":true,"sender":{"key":"git2eran@tromer.org","avatar":null},"body":"On 2006-10-30 01:37, Robin Rosenberg wrote:\n> +# include this in your bashrc or copy to /etc/bash_completions.d\n> +\n> +if [ \"$PS1\" ]; then\n> +    # trap 'PS1=\"\\u@\\h [$(stg top)] \\w]\\$ \"' DEBUG\n> +    function stgtag\n> +    {\n> +\tbr=$(stg branch 2>/dev/null)\n> +\ttop=$(stg top 2>/dev/null)\n> +\tif [[ -n \"$br$top\" ]];then\n> +\t    echo \"[$top@$br]\"\n> +\t    return\n> +\tfi\n> +    }\n> +    PS1='\\u@\\h$(stgtag)\\w\\$ '\n> +\n> +fi\n\nThat's an annoying 430ms delay at every prompt, on my box. Does StGIT do\nsomething expensive on every invocation?\n\nBen Clifford'd solution is pretty much instantaneous, and the following\nextends it to StGIT (in a less clean but much faster way):\n\n----------------------------------------------\n__prompt_githead() {\n    __PS_GIT=\"$(git-symbolic-ref HEAD 2>/dev/null)\" || exit\n    __PS_GIT=\"$(basename $__PS_GIT)\"\n    echo -n \" $__PS_GIT\"\n    __PS_GIT=$(cat \"${GIT_DIR:-.git}/patches/$__PS_GIT/current\" \\\n               2>/dev/null) || exit\n    echo -n \":$__PS_GIT\"\n}\nPS1='[\\u@\\h \\W$(__prompt_githead)]\\$ '\n----------------------------------------------\n\n"},{"id":"294564","messageId":"tnxlkmy2j9p.fsf@arm.com","threadId":"43379","inReplyTo":"4545CC6F.90001@tromer.org","subject":"Re: [StGIT PATCH] Bash snippet to show branch and patch in bash prompt","fromName":"Catalin Marinas","fromEmail":"catalin.marinas@arm.com","sentAt":"2006-10-30T10:24:50Z","receivedAt":"2006-10-30T10:24:50Z","isPatch":true,"sender":{"key":"catalin.marinas@arm.com","avatar":null},"body":"Eran Tromer <git2eran@tromer.org> wrote:\n> On 2006-10-30 01:37, Robin Rosenberg wrote:\n>> +# include this in your bashrc or copy to /etc/bash_completions.d\n>> +\n>> +if [ \"$PS1\" ]; then\n>> +    # trap 'PS1=\"\\u@\\h [$(stg top)] \\w]\\$ \"' DEBUG\n>> +    function stgtag\n>> +    {\n>> +\tbr=$(stg branch 2>/dev/null)\n>> +\ttop=$(stg top 2>/dev/null)\n>> +\tif [[ -n \"$br$top\" ]];then\n>> +\t    echo \"[$top@$br]\"\n>> +\t    return\n>> +\tfi\n>> +    }\n>> +    PS1='\\u@\\h$(stgtag)\\w\\$ '\n>> +\n>> +fi\n>\n> That's an annoying 430ms delay at every prompt, on my box. Does StGIT do\n> something expensive on every invocation?\n\nWell, there are some forks. For every \"stg\" command, \"git-symbolic-ref\nHEAD\" and \"git-rev-parse --git-dir\" are invoked to get the name of the\nmain branch and the .git directory. There is also the delay of\ninvoking python and loading the command modules in main.py (maybe I\nshould modify this to import the modules on demand, based on what\ncommand was given).\n\nSince the repository format is stable, you could use something like\nthis (it should be faster):\n\ngit_dir=$(git-rev-parse --git-dir 2> /dev/null)\nref=$(git-symbolic-ref HEAD 2> /dev/null)\nbr=${ref##*/}\ntop=$(cat $git_dir/patches/$br/current)\n\n-- \n"},{"id":"298557","messageId":"200610301132.45086.robin.rosenberg@dewire.com","threadId":"43379","inReplyTo":"4545CC6F.90001@tromer.org","subject":"Re: [StGIT PATCH] Bash snippet to show branch and patch in bash prompt","fromName":"Robin Rosenberg","fromEmail":"robin.rosenberg@dewire.com","sentAt":"2006-10-30T10:32:44Z","receivedAt":"2006-10-30T10:32:44Z","isPatch":true,"sender":{"key":"robin.rosenberg@dewire.com","avatar":"https://avatars.githubusercontent.com/u/46357?v=4"},"body":"måndag 30 oktober 2006 10:57 skrev Eran Tromer:\n> That's an annoying 430ms delay at every prompt, on my box. Does StGIT do\n> something expensive on every invocation?\n\nI don't type fast enough to notice really and my machine seems faster, ~300 ms \nper prompt.\n\n>\n> Ben Clifford'd solution is pretty much instantaneous, and the following\n> extends it to StGIT (in a less clean but much faster way):\n>\n> ----------------------------------------------\n> __prompt_githead() {\n>     __PS_GIT=\"$(git-symbolic-ref HEAD 2>/dev/null)\" || exit\n>     __PS_GIT=\"$(basename $__PS_GIT)\"\n>     echo -n \" $__PS_GIT\"\n>     __PS_GIT=$(cat \"${GIT_DIR:-.git}/patches/$__PS_GIT/current\" \\\n>                2>/dev/null) || exit\n>     echo -n \":$__PS_GIT\"\n> }\n> PS1='[\\u@\\h \\W$(__prompt_githead)]\\$ '\n> ----------------------------------------------\n\nThis doesn't work if the branch have a / in the name or if you are in a \nsubdirectory, not the top level. Probably not hard to fix though.\n\n"},{"id":"297835","messageId":"20061030105926.14328.55180.stgit@lathund.dewire.com","threadId":"43379","inReplyTo":"4545CC6F.90001@tromer.org","subject":"[PATCH] Bash snippet to show branch and patch in bash prompt","fromName":"Robin Rosenberg","fromEmail":"robin.rosenberg@dewire.com","sentAt":"2006-10-30T10:59:27Z","receivedAt":"2006-10-30T10:59:27Z","isPatch":true,"sender":{"key":"robin.rosenberg@dewire.com","avatar":"https://avatars.githubusercontent.com/u/46357?v=4"},"body":"From: Robin Rosenberg <robin.rosenberg@dewire.com>\n\nSigned-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>\n---\n\n contrib/stgbashprompt.sh |   18 ++++++++++++++++++\n 1 files changed, 18 insertions(+), 0 deletions(-)\n\ndiff --git a/contrib/stgbashprompt.sh b/contrib/stgbashprompt.sh\nnew file mode 100755\nindex 0000000..a79561e\n--- /dev/null\n+++ b/contrib/stgbashprompt.sh\n@@ -0,0 +1,18 @@\n+# include this in your bashrc or copy to /etc/bash_completions.d\n+\n+if [ \"$PS1\" ]; then\n+    # trap 'PS1=\"\\u@\\h [$(stg top)] \\w]\\$ \"' DEBUG\n+    function stgtag\n+    {\n+\tgit_dir=$(git-rev-parse --git-dir 2> /dev/null)\n+\tref=$(git-symbolic-ref HEAD 2> /dev/null)\n+\tbr=${ref/refs\\/heads\\//}\n+\ttop=$(cat $git_dir/patches/$br/current 2>/dev/null)\n+\tif [[ -n \"$br$top\" ]];then\n+\t    echo \"[$top@$br]\"\n+\t    return\n+\tfi\n+    }\n+    PS1='\\u@\\h$(stgtag)\\w\\$ '\n+\n"},{"id":"298495","messageId":"45461429.4020108@tromer.org","threadId":"43379","inReplyTo":"20061030105926.14328.55180.stgit@lathund.dewire.com","subject":"Re: [PATCH] Bash snippet to show branch and patch in bash prompt","fromName":"Eran Tromer","fromEmail":"git2eran@tromer.org","sentAt":"2006-10-30T15:03:05Z","receivedAt":"2006-10-30T15:03:05Z","isPatch":true,"sender":{"key":"git2eran@tromer.org","avatar":null},"body":"On 2006-10-30 12:59, Robin Rosenberg wrote:\n> From: Robin Rosenberg <robin.rosenberg@dewire.com>\n\n> +    function stgtag\n> +    {\n> +\tgit_dir=$(git-rev-parse --git-dir 2> /dev/null)\n> +\tref=$(git-symbolic-ref HEAD 2> /dev/null)\n\nAbort early if we're not in a git repo:\n\tgit_dir=$(git-rev-parse --git-dir 2> /dev/null) || return\n\tref=$(git-symbolic-ref HEAD 2> /dev/null) || return\n\n\n> +\tbr=${ref/refs\\/heads\\//}\n\nYou want to strip a prefix only, so this is safer:\n\tbr=${ref#refs/heads/}\n\n\n> +\ttop=$(cat $git_dir/patches/$br/current 2>/dev/null)\n\nAll variables should be declared local to avoid polluting the bash\nvariable namespace. Likewise, the function name deserves a couple of\nunderscores.\n\n\n> +\tif [[ -n \"$br$top\" ]];then\n> +\t    echo \"[$top@$br]\"\n\nIt seems better to put the StGIT top after the tag, so that stg push/pop\nshifts less of the prompt, making it easier to see the change visually.\n\n\nCorresponding modified version:\n\n-------------------------------------------\nif [ \"$PS1\" ]; then\n    function __prompt_git()\n    {\n        local git_dir ref br top;\n\tgit_dir=$(git-rev-parse --git-dir 2> /dev/null) || return\n\tref=$(git-symbolic-ref HEAD 2> /dev/null) || return\n\tbr=${ref#refs/heads/}\n\ttop=$(cat $git_dir/patches/$br/current 2>/dev/null) \\\n\t\t&& top=\"#$top\"\n\techo \"[$br$top]\"\n    }\n    PS1='\\u@\\h$(__prompt_git)\\w\\$ '\nfi\n-------------------------------------------\n\nConditionally prepending the \"#\" to $top can be done more concisely via\n${top:+#$top} but I used the more readable version.\n\n"},{"id":"297093","messageId":"tnxd5862ist.fsf@arm.com","threadId":"43379","inReplyTo":"45461429.4020108@tromer.org","subject":"Re: [PATCH] Bash snippet to show branch and patch in bash prompt","fromName":"Catalin Marinas","fromEmail":"catalin.marinas@arm.com","sentAt":"2006-11-02T11:24:02Z","receivedAt":"2006-11-02T11:24:02Z","isPatch":true,"sender":{"key":"catalin.marinas@arm.com","avatar":null},"body":"Eran Tromer <git2eran@tromer.org> wrote:\n> Corresponding modified version:\n\nThanks for your comments. I included them in the patch.\n\n-- \n"}]}