Browse Source

Simplify dirt detection code using git_bool()

Alois Mahdal 9 years ago
parent
commit
c4a1197a2f
1 changed files with 12 additions and 16 deletions
  1. 12
    16
      src/include/facts.sh

+ 12
- 16
src/include/facts.sh View File

@@ -91,12 +91,8 @@ git_lasthash() {
91 91
     if git_present;
92 92
     then    # we are in git repo
93 93
         local last_hash=$(git rev-parse HEAD)
94
-        if test "$(git diff --shortstat 2>/dev/null)" != "";
95
-        then    # the tree is "dirty", i.e. has been edited
96
-            local suffix=.dirty
97
-        fi
98
-        local suffix=""
99
-        echo -n "$last_hash$suffix"
94
+        echo -n "$last_hash"
95
+        git_bool dirty && echo -n ".dirty"
100 96
     else    # we are outside (eg. distributor's build dir')
101 97
         grep . .mkit/git_lasthash || {
102 98
             echo UNKNOWN
@@ -160,17 +156,17 @@ semver() {
160 156
         then    # we are at a later commit than the last tag
161 157
             local commit="$(git_fact current_branch).g$(git_fact latest_sha)"
162 158
         fi
163
-        if test "$(git diff --shortstat 2>/dev/null)" != "";
164
-        then    # the tree is "dirty", i.e. has been edited
165
-            local dirty=dirty
166
-        fi
167
-        test -n "$latest_tag" && version=${latest_tag:1}
159
+        local dirty=""
168 160
         local suffix=""
169
-        case "$commit:$dirty" in
170
-            :)       suffix=""                ;;
171
-            :dirty)  suffix="+$dirty"         ;;
172
-            *:)      suffix="+$commit"        ;;
173
-            *:dirty) suffix="+$commit.$dirty" ;;
161
+        git_bool dirty; dirty=$?
162
+        test -n "$latest_tag" && version=${latest_tag:1}
163
+        case "$dirty:$commit" in
164
+            1:)  suffix=""               ;;
165
+            0:)  suffix="+dirty"         ;;
166
+            1:*) suffix="+$commit"       ;;
167
+            0:*) suffix="+$commit.dirty" ;;
168
+            *)   suffix=MKIT_BUG;
169
+                 warn "MKIT_BUG: bad dirt/commit detection" ;;
174 170
         esac
175 171
         test -b "$prerl" && suffix="-$prerl$suffix"
176 172
         version="$version$suffix"