forked from emqx/emqx-rel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-lastest-tag.escript
executable file
·48 lines (41 loc) · 1.79 KB
/
get-lastest-tag.escript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env escript
%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ft=erlang ts=4 sw=4 et
%% -------------------------------------------------------------------
%%
%% nodetool: Helper Script for interacting with live nodes
%%
%% -------------------------------------------------------------------
main(Args) ->
case Args of
["ref"] -> io:format(getRef());
["tag"] -> io:format(latestTag())
end.
comparingFun([C1|R1], [C2|R2]) when is_list(C1), is_list(C2);
is_integer(C1), is_integer(C2) -> C1 < C2 orelse comparingFun(R1, R2);
comparingFun([C1|R1], [C2|R2]) when is_integer(C1), is_list(C2) -> comparingFun(R1, R2);
comparingFun([C1|_R1], [C2|_R2]) when is_list(C1), is_integer(C2) -> true;
comparingFun(_, _) -> false.
sortFun(T1, T2) ->
C = fun(T) ->
[case catch list_to_integer(E) of
I when is_integer(I) -> I;
_ -> E
end || E <- re:split(string:sub_string(T, 2), "[.-]", [{return, list}])]
end,
comparingFun(C(T1), C(T2)).
latestTag() ->
Tag = os:cmd("git describe --abbrev=0 --tags") -- "\n",
LatestTagCommitId = os:cmd(io_lib:format("git rev-parse ~s", [Tag])) -- "\n",
Tags = string:tokens(os:cmd(io_lib:format("git tag -l \"v*\" --points-at ~s", [LatestTagCommitId])), "\n"),
lists:last(lists:sort(fun(T1, T2) -> sortFun(T1, T2) end, Tags)).
branch() ->
case os:getenv("GITHUB_RUN_ID") of
false -> os:cmd("git branch | grep -e '^*' | cut -d' ' -f 2") -- "\n";
_ -> re:replace(os:getenv("GITHUB_REF"), "^refs/heads/|^refs/tags/", "", [global, {return ,list}])
end.
getRef() ->
case re:run(branch(), "master|^dev/|^hotfix/", [{capture, none}]) of
match -> branch();
_ -> latestTag()
end.