]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - fetch_version
Move Émilia's website
[perso/Immae/Config/Nix.git] / fetch_version
CommitLineData
177da38b
IB
1#!/bin/bash
2
3usage() {
4 echo "$0 file.json"
6e75ec80 5 echo "$0 [-n|--name name] [-b|--branch branch_or_rev] [-f|--file out_file] [-h|--help] (-u|--url) url"
177da38b
IB
6 exit
7}
8
9branch="master"
10while [[ $# -gt 0 ]]; do
11 a="$1"
12 shift
13 case "$a" in
14 *.json)
15 file=$a
16 content=$(cat $a)
17 name="$(echo "$content" | jq -r ".meta.name")"
18 url="$(echo "$content" | jq -r ".meta.url")"
19 branch="$(echo "$content" | jq -r ".meta.branch")"
6b53d116 20 break
177da38b
IB
21 ;;
22 -n|--name)
23 name=$1
24 shift
25 ;;
26 -u|--url)
27 url=$1
28 shift
29 ;;
30 -b|--branch)
31 branch=$1
32 shift
33 ;;
6b53d116
IB
34 -f|--file)
35 file=$1
36 shift
37 ;;
177da38b
IB
38 -h|--help)
39 usage
40 ;;
41 esac
42done
43if [ -z "$url" ]; then
44 usage
45fi
46if [ -z "$name" ]; then
47 name=$(echo "$url" | cut -d"/" -f5)
48fi
49if [ -z "$file" ]; then
50 file=$name.json
51fi
52
53# function fetch_ledger () {
54# pushd $HOME/projets/ledger >/dev/null 2>/dev/null
55# git fetch origin
56# tag="$(git describe origin/next | sed -e "s/^v//")"
57# rev="$(git show-ref -s refs/remotes/origin/next)"
58# sha="$(nix-prefetch-url --unpack file://<(git archive --format=tar.gz HEAD) 2>/dev/null)"
59# popd >/dev/null 2>/dev/null
60# }
61
62# awk_describe='BEGIN {
63# FS = "[ /^]+"
64# while ("git ls-remote " ARGV[1] "| sort -Vk2" | getline) {
65# if (!sha)
66# sha = substr($0, 1, 7)
67# tag = $3
68# }
69# while ("curl -s " ARGV[1] "/releases/tag/" tag | getline)
70# if ($3 ~ "commits")
71# com = $2
72# printf com ? "%s-%s-g%s\n" : "%s\n", tag, com, sha
73# }'
74
6e75ec80
IB
75function get_ref () {
76 case "$1" in
77 refs/*)
78 echo "$1"
79 ;;
80 *)
81 echo "refs/heads/$1"
82 ;;
83 esac
84}
85
86function get_name () {
87 branch="$1"
88 rev="$2"
89 minirev=${rev:0:7}
90
91 case "$branch" in
92 refs/tags/*)
93 b="${branch#refs/tags/}"
94 echo "${b//\//-}"
95 ;;
96 refs/heads/*)
97 b=${branch#refs/heads/}
98 echo "$minirev-${b//\//-}"
99 ;;
100 refs/*)
101 b=${branch#refs/}
102 echo "$minirev-${b//\//-}"
103 ;;
104 *)
105 echo "$minirev-${branch//\//-}"
106 ;;
107 esac
108}
109
177da38b 110function fetch_github () {
6e75ec80
IB
111 rev="$(git ls-remote --refs $url $(get_ref $branch) | head -n1 | cut -f1)"
112 sha="$(nix-prefetch-url --unpack $url/archive/$rev.tar.gz)"
177da38b
IB
113 # Différent du git-describe et github-spécifique
114 #tag=$(echo "$awk_describe" | awk -f - $url | sed -e "s/^v//")
6e75ec80 115 tag=$(get_name $branch $rev)
177da38b
IB
116}
117
6b53d116 118function fetch_other () {
6e75ec80
IB
119 rev="$(git ls-remote --refs $url $(get_ref $branch) | head -n1 | cut -f1)"
120 sha="$(nix-prefetch-git --url $url --rev $(get_ref $branch) | jq -r '.sha256')"
121 tag=$(get_name $branch $rev)
6b53d116 122}
177da38b 123
6b53d116
IB
124case "$url" in
125 https://*github.com/*)
126 fetch_github 2>/dev/null
127 owner=$(echo "$url" | cut -d"/" -f4)
128 repo=$(echo "$url" | cut -d"/" -f5)
177da38b 129
6b53d116
IB
130 F='{
131 "tag": $tag,
132 "meta": {
133 "name": $name,
134 "url": $url,
135 "branch": $branch
136 },
137 "github": {
138 "owner": $owner,
139 "repo": $repo,
140 "rev": $rev,
141 "sha256": $sha,
142 "fetchSubmodules": true
143 }
144 }'
145 ;;
146 *)
147 fetch_other 2>/dev/null
148 F='{
149 "tag": $tag,
150 "meta": {
151 "name": $name,
152 "url": $url,
153 "branch": $branch
154 },
155 "git": {
156 "url": $url,
157 "rev": $rev,
158 "sha256": $sha,
159 "fetchSubmodules": true
160 }
161 }'
162 ;;
163esac
177da38b
IB
164
165jq -n \
166 --arg name "$name" \
167 --arg owner "$owner" \
168 --arg repo "$repo" \
169 --arg tag "$tag" \
170 --arg rev "$rev" \
171 --arg url "$url" \
172 --arg branch "$branch" \
173 --arg sha "$sha" \
174 "$F" > $file