From 6b6e511387b436a648441b0251eace9476a1a256 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Wed, 15 May 2013 00:28:32 +0200 Subject: [PATCH 1/3] git: litle optimisation (do not concat $end everywhere) --- liquidprompt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/liquidprompt b/liquidprompt index 9cc75a7..afa163e 100755 --- a/liquidprompt +++ b/liquidprompt @@ -752,19 +752,19 @@ _lp_git_branch_color() has_lines=$(git diff --numstat 2>/dev/null | awk 'NF==3 {plus+=$1; minus+=$2} END {printf("+%d/-%d\n", plus, minus)}') if [[ "$has_commit" -gt "0" ]] ; then # Changes to commit and commits to push - ret="${LP_COLOR_CHANGES}${branch}${NO_COL}(${LP_COLOR_DIFF}$has_lines${NO_COL},${LP_COLOR_COMMITS}$has_commit${NO_COL})${end}" + ret="${LP_COLOR_CHANGES}${branch}${NO_COL}(${LP_COLOR_DIFF}$has_lines${NO_COL},${LP_COLOR_COMMITS}$has_commit${NO_COL})" else - ret="${LP_COLOR_CHANGES}${branch}${NO_COL}(${LP_COLOR_DIFF}$has_lines${NO_COL})${end}" # changes to commit + ret="${LP_COLOR_CHANGES}${branch}${NO_COL}(${LP_COLOR_DIFF}$has_lines${NO_COL})" # changes to commit fi else if [[ "$has_commit" -gt "0" ]] ; then # some commit(s) to push - ret="${LP_COLOR_COMMITS}${branch}${NO_COL}(${LP_COLOR_COMMITS}$has_commit${NO_COL})${end}" + ret="${LP_COLOR_COMMITS}${branch}${NO_COL}(${LP_COLOR_COMMITS}$has_commit${NO_COL})" else - ret="${LP_COLOR_UP}${branch}${end}" # nothing to commit or push + ret="${LP_COLOR_UP}${branch}" # nothing to commit or push fi fi - echo -ne "$ret" + echo -ne "$ret$end" fi } From 0c1e66d12a586c76c56aaf2fe1070725e0af9fac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Wed, 15 May 2013 00:33:28 +0200 Subject: [PATCH 2/3] git: optimize changed lines extraction - use "git diff --shortstat" to replace 3 different calls to "git diff" - drop usage of awk --- liquidprompt | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/liquidprompt b/liquidprompt index afa163e..5918d71 100755 --- a/liquidprompt +++ b/liquidprompt @@ -715,14 +715,6 @@ _lp_git_branch_color() branch=$(_lp_git_branch) if [[ -n "$branch" ]] ; then - local GD - git diff --quiet >/dev/null 2>&1 - GD=$? - - local GDC - git diff --cached --quiet >/dev/null 2>&1 - GDC=$? - local end="$NO_COL" if git status 2>/dev/null | grep -q '\(# Untracked\)'; then end="$LP_COLOR_CHANGES$LP_MARK_UNTRACKED$end" @@ -747,9 +739,14 @@ _lp_git_branch_color() fi fi fi - if [[ "$GD" -eq 1 || "$GDC" -eq "1" ]] ; then - local has_line - has_lines=$(git diff --numstat 2>/dev/null | awk 'NF==3 {plus+=$1; minus+=$2} END {printf("+%d/-%d\n", plus, minus)}') + + local shortstat=$(LANG=C git diff --shortstat 2>/dev/null) + if [[ -n "$shortstat" ]] ; then + #has_lines=$(git diff --numstat 2>/dev/null | awk 'NF==3 {plus+=$1; minus+=$2} END {printf("+%d/-%d\n", plus, minus)}') + local has_lines=${shortstat/*changed, /+} + has_lines=${has_lines/ insertions(+), /\/-} + has_lines=${has_lines/ del*/} + if [[ "$has_commit" -gt "0" ]] ; then # Changes to commit and commits to push ret="${LP_COLOR_CHANGES}${branch}${NO_COL}(${LP_COLOR_DIFF}$has_lines${NO_COL},${LP_COLOR_COMMITS}$has_commit${NO_COL})" From e4626c4e0fb7709e2c3aa06de79cef5dfd246356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Thu, 16 May 2013 19:12:42 +0200 Subject: [PATCH 3/3] _lp_git_branch: optimize code paths --- liquidprompt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/liquidprompt b/liquidprompt index 5918d71..a5d1d30 100755 --- a/liquidprompt +++ b/liquidprompt @@ -694,11 +694,11 @@ _lp_git_branch() local branch="$(git symbolic-ref HEAD 2>/dev/null)" if [[ $? -ne 0 || -z "$branch" ]] ; then # In detached head state, use commit instead - branch="$(git rev-parse --short HEAD 2>/dev/null)" + # No escape needed + git rev-parse --short HEAD 2>/dev/null + else + _lp_escape "${branch#refs/heads/}" fi - [[ $? -ne 0 || -z "$branch" ]] && return - branch="${branch#refs/heads/}" - echo $(_lp_escape "$branch") } # Set a color depending on the branch state: