Browse Source

Provide last_hash value as __MKIT_PROJ_GIT_LASTHASH__ token

Note that similar to __MKIT_PROJ_VERSION__, this value cannot be
reliably determined when outside git (e.g. distributor's build dir),
therefore must be kept inside dist source package.

Unlike version, though, user (ie. app developer, ie. mkit.ini
maintainer) most probably does not want to edit it, therefore we're
normally keeping it out of sight.

The value itself is normally just the last git commit's hash (SHA1 these
days), suffixed with ".dirty" mark if there were uncommitted changes
during build.  (Just the same as we do with semver())

If we're outside git and the hash could not be determined, that either
means the source package was mangled with or was built with older
version of mkit.  In either case, the value provided will be 'UNKNOWN'.
Alois Mahdal 8 years ago
parent
commit
2befc0fb36
2 changed files with 30 additions and 0 deletions
  1. 4
    0
      src/include/build.sh
  2. 26
    0
      src/include/facts.sh

+ 4
- 0
src/include/build.sh View File

@@ -79,6 +79,7 @@ _expand_tokens() {
79 79
         echo "s|__MKIT_PROJ_CODENAME__|$(ini 1value project:codename)|g;"
80 80
         echo "s|__MKIT_PROJ_PKGNAME__|$(ini 1value project:pkgname)|g;"
81 81
         echo "s|__MKIT_PROJ_TAGLINE__|$(ini 1value project:tagline)|g;"
82
+        echo "s|__MKIT_PROJ_GIT_LASTHASH__|$(git_lasthash)|g;"
82 83
         echo "s|__MKIT_PROJ_VERSION__|$(semver)|g;"
83 84
         echo "s|__MKIT_SELF_VERSION__|$MKIT_VERSION|g;"
84 85
     } >> "$script"
@@ -179,10 +180,13 @@ dist() {
179 180
     #       rpmstuff, hence -f hack for gzip
180 181
     #
181 182
     local version=$(semver)
183
+    local git_lasthash=$(git_lasthash)
182 184
     local dirname=$MKIT_PROJ_PKGNAME-$version
183 185
     mkdir -p "$dirname"
184 186
     ini values "lists:dist" | xargs -I DIST_ITEM cp -R DIST_ITEM "$dirname"
185 187
     update_version "$version" "$dirname/mkit.ini"
188
+    mkdir -p "$dirname/.mkit"
189
+    echo -n "$git_lasthash" > "$dirname/.mkit/git_lasthash"
186 190
     tar -cf "$dirname.tar" "$dirname"
187 191
     gzip -f "$dirname.tar"      # see above FIXME
188 192
     mkdir -p "$MKIT_LOCAL"

+ 26
- 0
src/include/facts.sh View File

@@ -56,6 +56,32 @@ git_ver2tag() {
56 56
     sed s/^/v/
57 57
 }
58 58
 
59
+git_lasthash() {
60
+    #
61
+    # Show last commit hash (with .dirty suffix if needed)
62
+    #
63
+    # If outside git repo, get it from .mkit/git_lasthash, which
64
+    # should have been put there by dist target.  (I.e., this won't
65
+    # work if you got outside the git repo in other way than dist
66
+    # target, but that's actually expected.)
67
+    #
68
+    if git_present;
69
+    then    # we are in git repo
70
+        local last_hash=$(git rev-parse HEAD)
71
+        if test "$(git diff --shortstat 2>/dev/null)" != "";
72
+        then    # the tree is "dirty", i.e. has been edited
73
+            local suffix=.dirty
74
+        fi
75
+        local suffix=""
76
+        echo -n "$last_hash$suffix"
77
+    else    # we are outside (eg. distributor's build dir')
78
+        grep . .mkit/git_lasthash || {
79
+            echo UNKNOWN
80
+            warn "malformed source, could not determine git hash"
81
+        }
82
+    fi
83
+}
84
+
59 85
 semver() {
60 86
     #
61 87
     # Build semver version string with build metadata