]> git.immae.eu Git - perso/Immae/Config/Nix.git/blobdiff - fetch_version
Move Émilia's website
[perso/Immae/Config/Nix.git] / fetch_version
index db0af1b813a3c6e17e320e29b5072d663f31478c..492aff11ddd1d912413743e8658cb19e8c24a6bf 100755 (executable)
@@ -2,7 +2,7 @@
 
 usage() {
   echo "$0 file.json"
-  echo "$0 [-n|--name name] [-b|--branch branch] [-h|--help] (-u|--url) url"
+  echo "$0 [-n|--name name] [-b|--branch branch_or_rev] [-f|--file out_file] [-h|--help] (-u|--url) url"
   exit
 }
 
@@ -17,6 +17,7 @@ while [[ $# -gt 0 ]]; do
       name="$(echo "$content" | jq -r ".meta.name")"
       url="$(echo "$content" | jq -r ".meta.url")"
       branch="$(echo "$content" | jq -r ".meta.branch")"
+      break
       ;;
     -n|--name)
       name=$1
@@ -30,6 +31,10 @@ while [[ $# -gt 0 ]]; do
       branch=$1
       shift
       ;;
+    -f|--file)
+      file=$1
+      shift
+      ;;
     -h|--help)
       usage
       ;;
@@ -67,34 +72,95 @@ fi
 #   printf com ? "%s-%s-g%s\n" : "%s\n", tag, com, sha
 # }'
 
+function get_ref () {
+  case "$1" in
+    refs/*)
+      echo "$1"
+      ;;
+    *)
+      echo "refs/heads/$1"
+      ;;
+  esac
+}
+
+function get_name () {
+  branch="$1"
+  rev="$2"
+  minirev=${rev:0:7}
+
+  case "$branch" in
+    refs/tags/*)
+      b="${branch#refs/tags/}"
+      echo "${b//\//-}"
+      ;;
+    refs/heads/*)
+      b=${branch#refs/heads/}
+      echo "$minirev-${b//\//-}"
+      ;;
+    refs/*)
+      b=${branch#refs/}
+      echo "$minirev-${b//\//-}"
+      ;;
+    *)
+      echo "$minirev-${branch//\//-}"
+      ;;
+  esac
+}
+
 function fetch_github () {
-  rev="$(git ls-remote --refs $url refs/heads/$branch | head -n1 | cut -f1)"
-  sha="$(nix-prefetch-url --unpack $url/archive/$branch.tar.gz)"
+  rev="$(git ls-remote --refs $url $(get_ref $branch) | head -n1 | cut -f1)"
+  sha="$(nix-prefetch-url --unpack $url/archive/$rev.tar.gz)"
   # Différent du git-describe et github-spécifique
   #tag=$(echo "$awk_describe" | awk -f - $url | sed -e "s/^v//")
-  tag=${rev:0:7}-$branch
+  tag=$(get_name $branch $rev)
 }
 
-fetch_github 2>/dev/null
+function fetch_other () {
+  rev="$(git ls-remote --refs $url $(get_ref $branch) | head -n1 | cut -f1)"
+  sha="$(nix-prefetch-git --url $url --rev $(get_ref $branch) | jq -r '.sha256')"
+  tag=$(get_name $branch $rev)
+}
 
-owner=$(echo "$url" | cut -d"/" -f4)
-repo=$(echo "$url" | cut -d"/" -f5)
+case "$url" in
+  https://*github.com/*)
+    fetch_github 2>/dev/null
+    owner=$(echo "$url" | cut -d"/" -f4)
+    repo=$(echo "$url" | cut -d"/" -f5)
 
-F='{
-  "tag": $tag,
-  "meta": {
-    "name": $name,
-    "url": $url,
-    "branch": $branch
-  },
-  "github": {
-    "owner": $owner,
-    "repo": $repo,
-    "rev": $rev,
-    "sha256": $sha,
-    "fetchSubmodules": true
-  }
-}'
+    F='{
+      "tag": $tag,
+      "meta": {
+        "name": $name,
+        "url": $url,
+        "branch": $branch
+      },
+      "github": {
+        "owner": $owner,
+        "repo": $repo,
+        "rev": $rev,
+        "sha256": $sha,
+        "fetchSubmodules": true
+      }
+    }'
+    ;;
+  *)
+    fetch_other 2>/dev/null
+    F='{
+      "tag": $tag,
+      "meta": {
+        "name": $name,
+        "url": $url,
+        "branch": $branch
+      },
+      "git": {
+        "url": $url,
+        "rev": $rev,
+        "sha256": $sha,
+        "fetchSubmodules": true
+      }
+    }'
+    ;;
+esac
 
 jq -n \
   --arg name "$name" \