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
     if git_present;
91
     if git_present;
92
     then    # we are in git repo
92
     then    # we are in git repo
93
         local last_hash=$(git rev-parse HEAD)
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
     else    # we are outside (eg. distributor's build dir')
96
     else    # we are outside (eg. distributor's build dir')
101
         grep . .mkit/git_lasthash || {
97
         grep . .mkit/git_lasthash || {
102
             echo UNKNOWN
98
             echo UNKNOWN
160
         then    # we are at a later commit than the last tag
156
         then    # we are at a later commit than the last tag
161
             local commit="$(git_fact current_branch).g$(git_fact latest_sha)"
157
             local commit="$(git_fact current_branch).g$(git_fact latest_sha)"
162
         fi
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
         local suffix=""
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
         esac
170
         esac
175
         test -b "$prerl" && suffix="-$prerl$suffix"
171
         test -b "$prerl" && suffix="-$prerl$suffix"
176
         version="$version$suffix"
172
         version="$version$suffix"