just a dummy repo for a dummy package

facts.sh 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #!/bin/bash
  2. # MKit - simple install helper
  3. # See LICENSE file for copyright and license details.
  4. mkit_import ini
  5. git_bool() {
  6. #
  7. # Get git bool (ie. exit status counts) $1
  8. #
  9. local bool_name=$1 # name of boolean to get
  10. git_present || warn "can't give bool outside git repo: $bool_name"
  11. case "$bool_name" in
  12. dirty_files)
  13. git diff-files | grep -qm 1 .
  14. ;;
  15. dirty_index)
  16. git diff-index HEAD | grep -qm 1 .
  17. ;;
  18. dirty)
  19. git_bool dirty_files || git_bool dirty_index
  20. ;;
  21. async)
  22. local status_desc # status description (in square brackets)
  23. status_desc=$(git status -sb | grep '^##.*' | grep -o '\[[^]]*\]$')
  24. grep -qw behind <<<"$status_desc" && return 0
  25. grep -qw ahead <<<"$status_desc" && return 0
  26. return 1
  27. ;;
  28. *)
  29. warn "unknown git bool asked: $bool_name"
  30. return 2
  31. ;;
  32. esac
  33. }
  34. git_fact() {
  35. #
  36. # Get git fact $1
  37. #
  38. local fact_name=$1 # name of fact to get
  39. git_present || warn "can't give fact outside git repo: $fact_name"
  40. case "$fact_name" in
  41. latest_tag)
  42. git log --format="%d" \
  43. | sed 's/,/\n/g' \
  44. | sed 's/^[[:blank:]]*//' \
  45. | grep -E '^\(?tag' \
  46. | tr -cd '[[:digit:]].v\n' \
  47. | grep . -m 1
  48. ;;
  49. latest_version)
  50. git_fact latest_tag | git_tag2ver
  51. ;;
  52. current_branch)
  53. git rev-parse --abbrev-ref HEAD
  54. ;;
  55. reldiff)
  56. git log --oneline "$(git_fact latest_tag)..HEAD" --name-only
  57. ;;
  58. latest_sha)
  59. git log -1 --pretty=format:%h HEAD
  60. ;;
  61. latest_cdate)
  62. git log -1 --format=%ct HEAD
  63. ;;
  64. *)
  65. warn "unknown git fact asked: $fact_name"
  66. ;;
  67. esac
  68. }
  69. git_present() {
  70. #
  71. # True if we're in a git repo
  72. #
  73. git rev-parse HEAD >&/dev/null
  74. }
  75. git_tag2ver() {
  76. #
  77. # Convert tag to version
  78. #
  79. sed s/^v//
  80. }
  81. git_ver2tag() {
  82. #
  83. # Convert version to tag
  84. #
  85. sed s/^/v/
  86. }
  87. git_lasthash() {
  88. #
  89. # Show last commit hash (with .dirty suffix if needed)
  90. #
  91. # We can't do it outside git repo (or without git) but we should
  92. # not be asked to; targets that don't require git should make use
  93. # of cache built by dist target.
  94. #
  95. local last_hash # last commit hash
  96. git_present || {
  97. echo UNKNOWN_HASH
  98. warn "no git present; could not determine last hash"
  99. return 3
  100. }
  101. last_hash=$(git rev-parse HEAD)
  102. echo -n "$last_hash"
  103. git_bool dirty && echo -n ".dirty"
  104. }
  105. git_lastsummary() {
  106. #
  107. # Show last commit summary
  108. #
  109. # We can't do it outside git repo (or without git) but we should
  110. # not be asked to; targets that don't require git should make use
  111. # of cache built by dist target.
  112. #
  113. git_present || {
  114. echo UNKNOWN_SUMMARY
  115. warn "no git present; could not determine last summary"
  116. return 3
  117. }
  118. git_bool dirty && {
  119. echo "(index is dirty)"
  120. return
  121. }
  122. git log -1 --format=oneline HEAD \
  123. | cut -d' ' -f2-
  124. }
  125. make_bmeta() {
  126. #
  127. # Compose build metadata string
  128. #
  129. local is_tagged=T # T if tagged (clean, no metadata), F if devel
  130. local brname # current branch name
  131. local ghash # current commit short hash
  132. if ! git describe --tags --exact-match HEAD >&/dev/null;
  133. then # we are at a later commit than the last tag
  134. is_tagged=F
  135. brname=$(git_fact current_branch | sed 's/[^[:alnum:]]/_/g')
  136. ghash=$(git_fact latest_sha)
  137. fi
  138. {
  139. if test "$is_tagged" == F; then
  140. test -n "$stamp" && echo "t$stamp"
  141. echo "$brname"
  142. echo "g$ghash"
  143. test -n "$MKIT_UPSTREAM" && echo "u$MKIT_UPSTREAM"
  144. git_bool async && "$MKIT_ASYNC" && echo async
  145. "$MKIT_IN_CI" && test -z "$MKIT_UPSTREAM" && git_bool async && {
  146. warn "branch is out-of sync with upstream; git hash might be misleading"
  147. warn " (hint: if this is due to CI auto-rebase, set MKIT_UPSTREAM to original shorthash)"
  148. }
  149. fi
  150. git_bool dirty && echo dirty
  151. } | paste -sd.
  152. }
  153. make_suffix() {
  154. local bmeta
  155. bmeta=$(make_bmeta)
  156. test -n "$bmeta" || return 0
  157. echo "+$bmeta"
  158. }
  159. semver() {
  160. #
  161. # Build proper SemVer version string
  162. #
  163. # Build version string from available info using following
  164. # logic:
  165. #
  166. # 1. Use version from last git tag (or mkit.ini if there is no
  167. # tag, which is possible on new project)
  168. # 2. if set, add project:prerl (from mkit.ini) as pre-release ID
  169. # (afer dash)
  170. # 3. if we are at a later commit than the last tag, add branch
  171. # name and commit sha1 to build metadata (after plus sign)
  172. # 4. if the tree is "dirty", i.e. has uncommited changes,
  173. # add "dirty" to build metadata
  174. #
  175. # The version is compatible with SemVer 2.0.0.
  176. #
  177. # Examples:
  178. #
  179. # foo v1.0.7 # all clear; proper release
  180. # foo v1.0.7-beta # mkit.ini: project:prerl="beta"
  181. # foo v1.0.7-beta+g1aef811.master # ^^ + some commits after
  182. # foo v1.0.7-beta+gf14fc4f.api2 # ^^ + on a feature branch
  183. # foo v1.0.7-beta+gf14fc4f.api2.dirty # ^^ + tree edited
  184. # foo v1.0.7-beta+dirty # tag OK but tree edited
  185. # foo v1.0.7+dirty # ^^ but no pre-release id
  186. #
  187. # Note that versions with "dirty" should be perceived as kind of
  188. # dangerous outside developer's own machine. Versions with sha1 are
  189. # safer but must not be released.
  190. #
  191. # FIXME: Using project:prerl for release IDs may not be compatible with
  192. # release strategy implemented in release.sh
  193. #
  194. local xyz # base version string
  195. local prerl # pre-release keyword (from mkit.ini, eg. 'beta')
  196. local latest_tag # latest git tag
  197. local stamp # timestamp or nothing (see $MKIT_TSTAMP)
  198. local suffix # version suffix
  199. prerl=$(ini 1value project:prerl)
  200. case $MKIT_TSTAMP in
  201. none) stamp= ;;
  202. btime) stamp=$(date -u +%Y%m%d%H%M%S) ;;
  203. ctime) stamp=$(date -d @"$(git_fact latest_cdate)" -u +%Y%m%d%H%M%S) ;;
  204. esac
  205. grep ":" <<<"$prerl" \
  206. && warn "colon in project:prerl may corrupt version data: $prerl"
  207. git_present || {
  208. echo UNKNOWN_VERSION
  209. warn "no git present; could not determine SemVer"
  210. return 3
  211. }
  212. latest_tag=$(git_fact latest_tag)
  213. case $latest_tag in
  214. v*) xyz=${latest_tag:1} ;;
  215. "") warn "no tags, using base version from mkit.ini (ok for new project)"
  216. xyz=$(ini 1value project:version) ;;
  217. *) warn "bad form of last tag, using base version from mkit.ini: tag is '$latest_tag'"
  218. xyz=$(ini 1value project:version) ;;
  219. esac
  220. suffix=$(make_suffix)
  221. test -n "$prerl" && suffix="-$prerl$suffix"
  222. echo "$xyz$suffix"
  223. }