Debian Codegraph @

rpkg.macros

raw
1 function filter_tags {
2 grep -E "^$1[^-]+$"
3 }
4
5 function latest_merged_tag {
6 if [ "$LEGACY_GIT" ]; then
7 git for-each-ref --sort=-v:refname refs/tags | cut -f2 |
8 while read tag; do
9 if [ -z "$(basename "$tag" | filter_tags "$1")" ]; then
10 continue
11 fi
12 if [ "$(git merge-base "$tag" HEAD)"="$(git rev-parse "$tag"^{commit})" ]; then
13 echo "$(basename "$tag")"
14 return
15 fi
16 done
17 else
18 git tag --list --sort=-v:refname "$1*" --merged 2> /dev/null | filter_tags "$1" | head -n 1
19 fi
20 }
21
22 function git_version {
23 declare name="v" lead=0 follow= "$@"
24
25 if [ -z "$name" ]; then
26 log_error "name cannot be empty."
27 return 1
28 fi
29
30 if echo "$lead.$follow" | grep -q '-'; then
31 log_error "lead and follow cannot contain dashes."
32 return 1
33 fi
34
35 if echo "$follow" | grep -q '\.'; then
36 log_error "follow cannot contain dots."
37 return 1
38 fi
39
40 latest_tag="$(latest_merged_tag "$name")"
41 latest_tag_version="$(echo $latest_tag | sed -E -n "s/^$name([^-]+)$/\1/p")"
42
43 if [ -n "$latest_tag" ]; then
44 commit_count="$(git rev-list "$latest_tag"..HEAD | wc -l)"
45 else
46 commit_count="$(git rev-list HEAD 2> /dev/null | wc -l || printf 0)"
47 fi
48
49 if [ "$commit_count" -eq 0 ]; then
50 commit_count_appendix=
51 else
52 commit_count_appendix=".git.$commit_count.$(git rev-parse --short HEAD)"
53 fi
54
55 latest_ctime="$(git_latest_ctime)"
56 if [ "$latest_ctime" -eq 0 ]; then
57 wtree_appendix=
58 else
59 wtree_appendix=".wtree.$(encode_decimal "$latest_ctime")"
60 fi
61
62 if [ -z "$follow" ]; then
63 follow="$(echo "$latest_tag_version" | sed -E -n "s/^.*\.([^.]*)$/\1/p")"
64 fi
65
66 if [ "$lead" = 0 ]; then
67 lead="$(echo "$latest_tag_version" | sed -E -n "s/\.$follow$//p")"
68 fi
69
70 follow=$((follow+1))
71
72 output "${lead}.${follow:-0}${commit_count_appendix}${wtree_appendix}"
73 }
74
75 function git_real_version {
76 real_version="$(git_version "$@" | sed -e "s/\.git.*$//")"
77 output "${real_version}"
78 }
79
80 function git_real_release {
81 git_version > /dev/null
82 commit_sha="$(echo ${commit_count_appendix} | sed -e "s/^.*\.//")"
83 commit_count_no="$(echo ${commit_count_appendix} | sed -e "s/^\.git\.//")"
84 commit_count_no="$(echo ${commit_count_no} | sed -e "s/\.${commit_sha}//")"
85 output "${commit_count_no}.git${commit_sha}${wtree_appendix}"
86 }