summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Allen <michael@michaelallen.io>2015-02-22 19:18:13 +0000
committerMichael Allen <michael@michaelallen.io>2015-02-22 19:18:13 +0000
commit7f88ce5a93e3f3ea71c187e12dda6105a17ccfaa (patch)
tree58aa84bd0ef6612eec5925da634939df6c95d958
parent2e0fc72580ea71b605aa54b8a6505723618d7d7a (diff)
downloadgit-sonar-7f88ce5a93e3f3ea71c187e12dda6105a17ccfaa.tar.gz
git-sonar-7f88ce5a93e3f3ea71c187e12dda6105a17ccfaa.zip
faster files status strings
-rwxr-xr-xgit-base.sh106
-rwxr-xr-xtest-files.sh54
2 files changed, 98 insertions, 62 deletions
diff --git a/git-base.sh b/git-base.sh
index 1c6ba0c..35e3596 100755
--- a/git-base.sh
+++ b/git-base.sh
@@ -200,51 +200,87 @@ remote_ahead_of_master() {
fi
}
+added="A%{$reset_color%}"
+modified="M%{$reset_color%}"
+deleted="D%{$reset_color%}"
+renamed="R%{$reset_color%}"
+us="U%{$reset_color%}"
+them="T%{$reset_color%}"
+both="B%{$reset_color%}"
+
+staged="%{$fg_bold[green]%}"
+unstaged="%{$fg_bold[red]%}"
+conflicted="%{$fg_bold[yellow]%}"
+untracked="%{$fg_bold[white]%}"
+
porcelain_status() {
echo "$(git status --porcelain 2>/dev/null)"
}
-count_from_porcelain() {
- if is_repo; then
- current_status="$(porcelain_status)"
- pattern="$1"
- echo "$(echo "$current_status" | grep -p "$pattern" | wc -l | grep -oEi '[0-9][0-9]*')"
- else
- echo "0"
+staged_status() {
+ local gitStatus=${1:-"$(porcelain_status)"}
+ local staged_string=""
+ local filesModified="$(echo "$gitStatus" | grep -p "M[A|M|C|D|U|R ] " | wc -l | grep -oEi '[1-9][0-9]*')"
+ local filesAdded="$(echo "$gitStatus" | grep -p "A[A|M|C|D|U|R ] " | wc -l | grep -oEi '[1-9][0-9]*')"
+ local filesDeleted="$(echo "$gitStatus" | grep -p "D[A|M|C|D|U|R ] " | wc -l | grep -oEi '[1-9][0-9]*')"
+ local filesRenamed="$(echo "$gitStatus" | grep -p "R[A|M|C|D|U|R ] " | wc -l | grep -oEi '[1-9][0-9]*')"
+
+ if [ -n "$filesAdded" ]; then
+ staged_string="$staged_string$filesAdded$staged$added"
fi
+ if [ -n "$filesDeleted" ]; then
+ staged_string="$staged_string$filesDeleted$staged$deleted"
+ fi
+ if [ -n "$filesModified" ]; then
+ staged_string="$staged_string$filesModified$staged$modified"
+ fi
+ if [ -n "$filesRenamed" ]; then
+ staged_string="$staged_string$filesRenamed$staged$renamed"
+ fi
+ echo "$staged_string"
}
-untracked_files() {
- echo "$(count_from_porcelain "?? ")"
+conflicted_status() {
+ local gitStatus=${1:-"$(porcelain_status)"}
+ local conflicted_string=""
+ local filesConflictedUs="$(echo "$gitStatus" | grep -p "[A|M|C|D|R ]U " | wc -l | grep -oEi '[1-9][0-9]*')"
+ local filesConflictedThem="$(echo "$gitStatus" | grep -p "U[A|M|C|D|R ] " | wc -l | grep -oEi '[1-9][0-9]*')"
+ local filesConflictedBoth="$(echo "$gitStatus" | grep -p "UU " | wc -l | grep -oEi '[1-9][0-9]*')"
+
+ if [ -n "$filesConflictedUs" ]; then
+ conflicted_string="$conflicted_string$filesConflictedUs$conflicted$us"
+ fi
+ if [ -n "$filesConflictedBoth" ]; then
+ conflicted_string="$conflicted_string$filesConflictedBoth$conflicted$both"
+ fi
+ if [ -n "$filesConflictedThem" ]; then
+ conflicted_string="$conflicted_string$filesConflictedThem$conflicted$them"
+ fi
+ echo "$conflicted_string"
}
-staged_added_changes() {
- echo "$(count_from_porcelain "A[A|M|C|D|U|R ] ")"
-}
-staged_modified_changes() {
- echo "$(count_from_porcelain "M[A|M|C|D|U|R ] ")"
-}
-staged_deleted_changes() {
- echo "$(count_from_porcelain "D[A|M|C|D|U|R ] ")"
-}
-staged_renamed_changes() {
- echo "$(count_from_porcelain "R[A|M|C|D|U|R ] ")"
-}
+unstaged_status() {
+ local gitStatus=${1:-"$(porcelain_status)"}
+ local unstaged_string=""
+ local filesModified="$(echo "$gitStatus" | grep -p "[A|M|C|D|U|R ]M " | wc -l | grep -oEi '[1-9][0-9]*')"
+ local filesDeleted="$(echo "$gitStatus" | grep -p "[A|M|C|D|U|R ]D " | wc -l | grep -oEi '[1-9][0-9]*')"
-unstaged_modified_changes() {
- echo "$(count_from_porcelain "[A|M|C|D|U|R ]M ")"
-}
-unstaged_deleted_changes() {
- echo "$(count_from_porcelain "[A|M|C|D|U|R ]D ")"
+ if [ -n "$filesDeleted" ]; then
+ unstaged_string="$unstaged_string$filesDeleted$unstaged$deleted"
+ fi
+ if [ -n "$filesModified" ]; then
+ unstaged_string="$unstaged_string$filesModified$unstaged$modified"
+ fi
+ echo "$unstaged_string"
}
-conflicted_by_us_changes() {
- echo "$(count_from_porcelain "[A|M|C|D|R ]U ")"
-}
-conflicted_by_them_changes() {
- echo "$(count_from_porcelain "U[A|M|C|D|R ] ")"
-}
-conflicted_both_changes() {
- echo "$(count_from_porcelain "UU ")"
+untracked_status() {
+ local gitStatus=${1:-"$(porcelain_status)"}
+ local untracked_string=""
+ local filesUntracked="$(echo "$gitStatus" | grep -p "?? " | wc -l | grep -oEi '[1-9][0-9]*')"
+
+ if [ -n "$filesUntracked" ]; then
+ untracked_string="$untracked_string$filesUntracked$untracked$added"
+ fi
+ echo "$untracked_string"
}
-
diff --git a/test-files.sh b/test-files.sh
index 1966d03..50ccdcb 100755
--- a/test-files.sh
+++ b/test-files.sh
@@ -19,13 +19,13 @@ test_untracked_files() {
cd_to_tmp
git init --quiet
- assertEquals "0" "$(untracked_files)"
+ assertEquals "" "$(untracked_status)"
touch foo
- assertEquals "1" "$(untracked_files)"
+ assertEquals "1$untracked$added" "$(untracked_status)"
git add .
- assertEquals "0" "$(untracked_files)"
+ assertEquals "" "$(untracked_status)"
rm_tmp
}
@@ -34,7 +34,7 @@ test_unstaged_modified_files() {
cd_to_tmp
git init --quiet
- assertEquals "0" "$(unstaged_modified_changes)"
+ assertEquals "" "$(unstaged_status)"
touch foo
touch bar
@@ -42,10 +42,10 @@ test_unstaged_modified_files() {
git commit -m "foo and bar" >/dev/null
echo "foo" >> foo
- assertEquals "1" "$(unstaged_modified_changes)"
+ assertEquals "1$unstaged$modified" "$(unstaged_status)"
echo "bar" >> bar
- assertEquals "2" "$(unstaged_modified_changes)"
+ assertEquals "2$unstaged$modified" "$(unstaged_status)"
rm_tmp
}
@@ -54,7 +54,7 @@ test_unstaged_deleted_files() {
cd_to_tmp
git init --quiet
- assertEquals "0" "$(unstaged_deleted_changes)"
+ assertEquals "" "$(unstaged_status)"
touch foo
touch bar
@@ -62,10 +62,10 @@ test_unstaged_deleted_files() {
git commit -m "foo and bar" >/dev/null
rm foo
- assertEquals "1" "$(unstaged_deleted_changes)"
+ assertEquals "1$unstaged$deleted" "$(unstaged_status)"
rm bar
- assertEquals "2" "$(unstaged_deleted_changes)"
+ assertEquals "2$unstaged$deleted" "$(unstaged_status)"
rm_tmp
}
@@ -74,15 +74,15 @@ test_staged_added_files() {
cd_to_tmp
git init --quiet
- assertEquals "0" "$(staged_added_changes)"
+ assertEquals "" "$(staged_status)"
touch foo
git add .
- assertEquals "1" "$(staged_added_changes)"
+ assertEquals "1$staged$added" "$(staged_status)"
touch bar
git add .
- assertEquals "2" "$(staged_added_changes)"
+ assertEquals "2$staged$added" "$(staged_status)"
rm_tmp
}
@@ -91,7 +91,7 @@ test_staged_modified_files() {
cd_to_tmp
git init --quiet
- assertEquals "0" "$(staged_modified_changes)"
+ assertEquals "" "$(staged_status)"
touch foo
touch bar
@@ -100,11 +100,11 @@ test_staged_modified_files() {
echo "foo" >> foo
git add .
- assertEquals "1" "$(staged_modified_changes)"
+ assertEquals "1$staged$modified" "$(staged_status)"
echo "bar" >> bar
git add .
- assertEquals "2" "$(staged_modified_changes)"
+ assertEquals "2$staged$modified" "$(staged_status)"
rm_tmp
}
@@ -113,7 +113,7 @@ test_staged_deleted_files() {
cd_to_tmp
git init --quiet
- assertEquals "0" "$(staged_deleted_changes)"
+ assertEquals "" "$(staged_status)"
touch foo
touch bar
@@ -122,11 +122,11 @@ test_staged_deleted_files() {
rm foo
git add .
- assertEquals "1" "$(staged_deleted_changes)"
+ assertEquals "1$staged$deleted" "$(staged_status)"
rm bar
git add .
- assertEquals "2" "$(staged_deleted_changes)"
+ assertEquals "2$staged$deleted" "$(staged_status)"
rm_tmp
}
@@ -135,7 +135,7 @@ test_staged_renamed_files() {
cd_to_tmp
git init --quiet
- assertEquals "0" "$(staged_renamed_changes)"
+ assertEquals "" "$(staged_status)"
touch foo
touch bar
@@ -144,11 +144,11 @@ test_staged_renamed_files() {
mv foo foo2
git add .
- assertEquals "1" "$(staged_renamed_changes)"
+ assertEquals "1$staged$renamed" "$(staged_status)"
mv bar bar2
git add .
- assertEquals "2" "$(staged_renamed_changes)"
+ assertEquals "2$staged$renamed" "$(staged_status)"
rm_tmp
}
@@ -172,11 +172,11 @@ test_conflicted_both_changes() {
git add .
git commit -m "foo2" --quiet
- assertEquals "0" "$(conflicted_both_changes)"
+ assertEquals "" "$(conflicted_status)"
git merge foo2 >/dev/null
- assertEquals "1" "$(conflicted_both_changes)"
+ assertEquals "1$conflicted$both" "$(conflicted_status)"
rm_tmp
}
@@ -200,11 +200,11 @@ test_conflicted_them_changes() {
git add .
git commit -m "foo2" --quiet
- assertEquals "0" "$(conflicted_by_them_changes)"
+ assertEquals "" "$(conflicted_status)"
git merge foo2 >/dev/null
- assertEquals "1" "$(conflicted_by_them_changes)"
+ assertEquals "1$conflicted$them" "$(conflicted_status)"
rm_tmp
}
@@ -228,11 +228,11 @@ test_conflicted_us_changes() {
git add .
git commit -m "delete foo" --quiet
- assertEquals "0" "$(conflicted_by_us_changes)"
+ assertEquals "" "$(conflicted_status)"
git merge foo2 >/dev/null
- assertEquals "1" "$(conflicted_by_us_changes)"
+ assertEquals "1$conflicted$us" "$(conflicted_status)"
rm_tmp
}