]> git.corax.cc Git - foundry/commitdiff
watchbot: Add support for smart git-over-http protocol
authorMatthias Kruk <m@m10k.eu>
Sat, 22 Jan 2022 09:11:43 +0000 (18:11 +0900)
committerMatthias Kruk <m@m10k.eu>
Sat, 22 Jan 2022 09:11:43 +0000 (18:11 +0900)
Watchbot does not support the smart git-over-http protocol, making
it unable to watch repositories hosted by Github and other services.

This commit adds support for the smart git-over-http protocol to
watchbot.

watchbot.sh

index 8b5c476c6ff0cd12fcb5a63caaca8a6f74ca3208..a1317fe3947ee6bd926ae8b05759df1b1985bc84 100755 (executable)
@@ -37,7 +37,33 @@ watch_to_branch() {
        return 0
 }
 
-fetch_head_remote() {
+fetch_head_smart_http() {
+       local watch="$1"
+
+       local repository
+       local branch
+       local url
+       local re
+       local data
+       local ref
+
+       repository=$(watch_to_repository "$watch")
+       branch=$(watch_to_branch "$watch")
+
+       re="00[0-9a-f]{2}\\K[0-9a-f]{40} refs/heads/$branch"
+       url="$repository/info/refs?service=git-upload-pack"
+
+       if ! data=$(curl --get --silent --location "$url" 2>/dev/null |
+                           grep -oP "$re" --binary-files=text); then
+               return 1
+       fi
+
+       ref="${data%% *}"
+       echo "$ref"
+       return 0
+}
+
+fetch_head_dumb_http() {
        local watch="$1"
 
        local repository
@@ -69,6 +95,21 @@ fetch_head_remote() {
        return 0
 }
 
+fetch_head_remote() {
+       local watch="$1"
+
+       local head
+
+       if ! head=$(fetch_head_smart_http "$watch"); then
+               if ! head=$(fetch_head_dumb_http "$watch"); then
+                       return 1
+               fi
+       fi
+
+       echo "$head"
+       return 0
+}
+
 fetch_head_local() {
        local watch="$1"