From 39fa2a2fc51399649f14b5feb874334afb857be6 Mon Sep 17 00:00:00 2001
From: Oliver Davies <oliver@oliverdavies.dev>
Date: Wed, 17 Apr 2024 08:48:28 +0100
Subject: [PATCH] Make clone work with HTTPS URL

---
 lib/shared/modules/zsh.nix | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/lib/shared/modules/zsh.nix b/lib/shared/modules/zsh.nix
index 30538bf3..985f9bd6 100644
--- a/lib/shared/modules/zsh.nix
+++ b/lib/shared/modules/zsh.nix
@@ -11,6 +11,17 @@
       clone() {
         repo_url="$1"
 
+        # Extract the remote domain (e.g. github.com) from the repo URL.
+        domain="''${repo_url#*://}"
+        if [[ "''${domain}" == *@*:* ]]; then
+            # SSH repo URL: domain ends at the colon.
+            domain="''${domain#*@}"
+            domain="''${domain%%:*}"
+        else
+            # HTTPS repo URL: domain ends at the slash.
+            domain="''${domain%%/*}"
+        fi
+
         # TODO: make it work with multi-level URLS - e.g. https://gitlab.com/a/b/c/d.git
 
         user_and_repo_name="''${repo_url}"
@@ -28,12 +39,6 @@
           [[ -z "$user" ]] && user="$USER"
         fi
 
-        # Extract the remote domain (e.g. github.com) from the repo URL.
-        # TODO: make it work with HTTPS URLs.
-        #  Cloning https://github.com/opdavies/oliverdavies.uk.git returns "https" as the domain.
-        domain="''${repo_url#*@}"
-        domain="''${domain%:*}"
-
         repo_name="''${user_and_repo_name##*/}"
         repo_name="''${repo_name%.git}"