summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Allen <michael@michaelallen.io>2015-03-10 16:21:40 +0000
committerMichael Allen <michael@michaelallen.io>2015-03-10 16:21:40 +0000
commit18c874f385e991552f71b93b9dc68bcdfd7d9c19 (patch)
tree6003bb228d81fedfc37be4b50743c05f7c876d24
parent82f5bd7c9c9118f2e5b623860beee1c76e03cec0 (diff)
downloadgit-sonar-18c874f385e991552f71b93b9dc68bcdfd7d9c19.tar.gz
git-sonar-18c874f385e991552f71b93b9dc68bcdfd7d9c19.zip
try to tell if a dir is dirty
Diffstat (limited to '')
-rwxr-xr-xgit-base.sh46
-rwxr-xr-xtest-files.sh27
2 files changed, 73 insertions, 0 deletions
diff --git a/git-base.sh b/git-base.sh
index 2debafa..fbd7ef9 100755
--- a/git-base.sh
+++ b/git-base.sh
@@ -205,6 +205,52 @@ unstaged="%{$fg_bold[red]%}"
conflicted="%{$fg_bold[yellow]%}"
untracked="%{$fg_bold[white]%}"
+is_dirty() {
+ if ! git rev-parse; then
+ #not in repo, thus not dirty
+ return 1
+ else
+ #in repo, might be dirty
+ if [[ -n "$(git ls-files --exclude-standard --others)" ]]; then
+ #untracked files thus dirty
+ return 0
+ else
+ #no untracked files
+ if git show HEAD --; then
+ #has a commit hash, thus not on an initial commit
+ if ! git diff --quiet --ignore-submodules HEAD --; then
+ #has differences thus dirty
+ return 0
+ else
+ return 1
+ fi
+ else
+ #no commit hash, thus can't use HEAD.
+ #As it's inital commit we can just list the files.
+ if [[ -n "$(ls -a -1 | grep -Ev '(\.|\.\.|\.git)')" ]]; then
+ #files listed and no commit hash, thus changes
+ return 0
+ else
+ return 1
+ fi
+ fi
+ fi
+ fi
+ {
+ #returns 1 if not a git repo
+ git rev-parse && {
+ #returns 1 if untracked files
+ git ls-files --exclude-standard --others && ! {
+ git show HEAD -- ||
+ #returns 1 if unstaged or staged files
+ git diff --quiet --ignore-submodules HEAD --
+ }
+ }
+ }
+ exitCode="$?"
+ return "$exitCode"
+}
+
porcelain_status() {
echo "$(git status --porcelain 2>/dev/null)"
}
diff --git a/test-files.sh b/test-files.sh
index 2d39023..270ee3b 100755
--- a/test-files.sh
+++ b/test-files.sh
@@ -237,4 +237,31 @@ test_conflicted_us_changes() {
rm_tmp
}
+test_is_dirty() {
+ cd_to_tmp
+
+ assertFalse "not in repo" is_dirty
+
+ git init --quiet
+ assertFalse "in repo and clean" is_dirty
+
+ touch foo
+ assertTrue "untracked files" is_dirty
+
+ git add .
+ assertTrue "staged addition files" is_dirty
+
+ git commit -m "inital commit" --quiet
+
+ assertFalse "commited and clean" is_dirty
+
+ echo "foo" >> foo
+ assertTrue "modified file unstaged" is_dirty
+
+ git add .
+ assertTrue "modified file staged" is_dirty
+
+ rm_tmp
+}
+
. ./shunit/shunit2