Commit a15a22d6a1eae8d35a4b68dbd35089f0ca483132

Thomas de Grivel 2023-02-12T18:45:37

wip

diff --git a/bin/shpkg b/bin/shpkg
index 16bb15c..b4aa277 100755
--- a/bin/shpkg
+++ b/bin/shpkg
@@ -4,17 +4,21 @@
 set -e
 
 # Config
+
 : ${SHPKG_DIR:=~/shpkg}
 SHPKG_SHARE_DIR="${SHPKG_DIR}/share/shpkg"
 
 # shpkg
+
 . ${SHPKG_SHARE_DIR}/shpkg.subr
 
 # Usage
+
 usage () {
     echo "Usage: shpkg OPERATION PKG ...
 
 Source directory operations :
+ checkout         shortcut for git checkout
  clone            shortcut for git clone
  fetch            shortcut for git fetch
  pull             shortcut for git pull
@@ -45,75 +49,100 @@ Miscellaneous operations :
 }
 
 # CLI
-if [ "x$1" = "xbuild" ]; then
-    shift
-    verbose 2 shpkg_build "$@"
-elif [ "x$1" = "xclean-all" ]; then
-    shift
-    verbose 2 shpkg_clean_all "$@"
-elif [ "x$1" = "xclean-sources" ]; then
-    shift
-    verbose 2 shpkg_clean_sources "$@"
-elif [ "x$1" = "xclean-build" ]; then
-    shift
-    verbose 2 shpkg_clean_build "$@"
-elif [ "x$1" = "xclean-configure" ]; then
-    shift
-    verbose 2 shpkg_clean_configure "$@"
-elif [ "x$1" = "xclean-fake" ]; then
-    shift
-    verbose 2 shpkg_clean_fake "$@"
-elif [ "x$1" = "xclean-package" ]; then
-    shift
-    verbose 2 shpkg_clean_package "$@"
-elif [ "x$1" = "xclean-sources" ]; then
-    shift
-    verbose 2 shpkg_clean_sources "$@"
-elif [ "x$1" = "xclone" ]; then
-    shift
-    verbose 2 shpkg_clone "$@"
-elif [ "x$1" = "xconfigure" ]; then
-    shift
-    verbose 2 shpkg_configure "$@"
-elif [ "x$1" = "xfake" ]; then
-    shift
-    verbose 2 shpkg_fake "$@"
-elif [ "x$1" = "xfetch" ] ||
-     [ "x$1" = "xf" ]; then
-    shift
-    verbose 2 shpkg_fetch "$@"
-elif [ "x$1" = "xhelp" ] ||
-     [ "x$1" = "xh" ] ||
-     [ "x$1" = "x--help" ] ||
-     [ "x$1" = "x-h" ]; then
-    usage 1
-elif [ "x$1" = "xinstall" ] ||
-     [ "x$1" = "xi" ]; then
-    shift
-    verbose 2 shpkg_install "$@"
-elif [ "x$1" = "xinstalled" ]; then
-    shift
-    verbose 2 shpkg_installed
-elif [ "x$1" = "xlist" ] ||
-     [ "x$1" = "xl" ]; then
-    shift
-    verbose 2 shpkg_list
-elif [ "x$1" = "xpackage" ] ||
-     [ "x$1" = "xpkg" ]; then
-    shift
-    verbose 2 shpkg_package "$@"
-elif [ "x$1" = "xpull" ] ||
-     [ "x$1" = "xp" ]; then
-    shift
-    verbose 2 shpkg_pull "$@"
-elif [ "x$1" = "xuninstall" ]; then
-    shift
-    verbose 2 shpkg_uninstall "$@"
-elif [ "x$1" = "xupdate" ]; then
-    verbose 2 shpkg_update
-elif [ "x$1" = "xupgrade" ]; then
-    shift
-    verbose 2 shpkg_upgrade "$@"
-else
-    usage 1
-fi
+
+main () {
+    if [ "x$1" = "xbuild" ]; then
+        shift
+        verbose 2 shpkg_build "$@"
+    elif [ "x$1" = "xcheckout" ] ||
+         [ "x$1" = "xco" ]; then
+        shift
+        verbose 2 shpkg_checkout "$@"
+    elif [ "x$1" = "xclean-all" ]; then
+        shift
+        verbose 2 shpkg_clean_all "$@"
+    elif [ "x$1" = "xclean-sources" ]; then
+        shift
+        verbose 2 shpkg_clean_sources "$@"
+    elif [ "x$1" = "xclean-build" ]; then
+        shift
+        verbose 2 shpkg_clean_build "$@"
+    elif [ "x$1" = "xclean-configure" ]; then
+        shift
+        verbose 2 shpkg_clean_configure "$@"
+    elif [ "x$1" = "xclean-fake" ]; then
+        shift
+        verbose 2 shpkg_clean_fake "$@"
+    elif [ "x$1" = "xclean-package" ]; then
+        shift
+        verbose 2 shpkg_clean_package "$@"
+    elif [ "x$1" = "xclean-sources" ]; then
+        shift
+        verbose 2 shpkg_clean_sources "$@"
+    elif [ "x$1" = "xclone" ]; then
+        shift
+        verbose 2 shpkg_clone "$@"
+    elif [ "x$1" = "xconfigure" ]; then
+        shift
+        verbose 2 shpkg_configure "$@"
+    elif [ "x$1" = "xfake" ]; then
+        shift
+        verbose 2 shpkg_fake "$@"
+    elif [ "x$1" = "xfetch" ] ||
+         [ "x$1" = "xf" ]; then
+        shift
+        verbose 2 shpkg_fetch "$@"
+    elif [ "x$1" = "xhelp" ] ||
+         [ "x$1" = "xh" ] ||
+         [ "x$1" = "x--help" ] ||
+         [ "x$1" = "x-h" ]; then
+        usage 1
+    elif [ "x$1" = "xinstall" ] ||
+         [ "x$1" = "xi" ]; then
+        shift
+        verbose 2 shpkg_install "$@"
+    elif [ "x$1" = "xinstalled" ]; then
+        shift
+        verbose 2 shpkg_installed
+    elif [ "x$1" = "xlist" ] ||
+         [ "x$1" = "xl" ]; then
+        shift
+        verbose 2 shpkg_list
+    elif [ "x$1" = "xpackage" ] ||
+         [ "x$1" = "xpkg" ]; then
+        shift
+        verbose 2 shpkg_package "$@"
+    elif [ "x$1" = "xpull" ] ||
+         [ "x$1" = "xp" ]; then
+        shift
+        verbose 2 shpkg_pull "$@"
+    elif [ "x$1" = "xuninstall" ]; then
+        shift
+        verbose 2 shpkg_uninstall "$@"
+    elif [ "x$1" = "xupdate" ]; then
+        verbose 2 shpkg_update
+    elif [ "x$1" = "xupgrade" ]; then
+        shift
+        verbose 2 shpkg_upgrade "$@"
+    else
+        usage 1
+    fi
+}
+
+# Logging
+
+redirect_err () {
+    "$@" 2>&1
+}
+
+log () {
+    SHPKG_LOG_DIR="$(dirname "${SHPKG_LOG}")"
+    {
+        if ! [ -d "${SHPKG_LOG_DIR}" ]; then
+            verbose 1 mkdir -p "${SHPKG_LOG_DIR}"
+        fi
+        redirect_err "$@"
+    } | tee "${SHPKG_LOG}"
+}
+
+log main "$@"
diff --git a/share/shpkg/shpkg.subr b/share/shpkg/shpkg.subr
index 4c2d143..55fcb96 100644
--- a/share/shpkg/shpkg.subr
+++ b/share/shpkg/shpkg.subr
@@ -15,24 +15,24 @@ SHPKG_LIB_DIR="${SHPKG_DIR}/lib"
 SHPKG_SHARE_DIR="${SHPKG_DIR}/share/shpkg"
 SHPKG_SRC_DIR="${SHPKG_DIR}/src"
 SHPKG_VAR_DB_DIR="${SHPKG_DIR}/var/db/shpkg"
+SHPKG_LOG_DIR="${SHPKG_DIR}/var/log/shpkg"
 
 SHPKG_PREFIX="${SHPKG_DIR}"
+SHPKG_LOG="${SHPKG_LOG_DIR}/shpkg_${SHPKG_DATE}.log"
 
 # Console output
 
 verbose__puts () {
-    {
-        if [ "x$1" = "x1" ]; then
-            printf '\33[0;34m'
-        elif [ "x$1" = "x2" ]; then
-            printf '\33[0;35m'
-        else
-            err 1 "bad verbose level"
-        fi
-        shift
-        echo -n "$@"
-        printf '\33[0m\n'
-    }
+    if [ "x$1" = "x1" ]; then
+        printf '\33[0;34m'
+    elif [ "x$1" = "x2" ]; then
+        printf '\33[0;35m'
+    else
+        err 1 "bad verbose level"
+    fi
+    shift
+    echo -n "$@"
+    printf '\33[0m\n'
 }
 
 verbose () {
@@ -161,6 +161,21 @@ shpkg_src_parent_dir () {
 
 # Source repository
 
+shpkg_checkout () {
+    TREE="$1"
+    shift
+    for SHPKG_REPO; do
+        SHPKG_REPO_DIR="$(shpkg_dir "${SHPKG_REPO}")"
+        if [ "x${SHPKG_REPO_DIR}" = "x" ]; then
+            err 1 "shpkg_checkout: unknown repo: $SHPKG_REPO"
+        fi
+        ( verbose 1 cd "${SHPKG_SRC_DIR}/${SHPKG_REPO_DIR}" &&
+              verbose 1 git fetch origin "$TREE" &&
+              verbose 1 git checkout "$TREE" &&
+              verbose 1 git submodule update; )
+    done
+}
+
 shpkg_clone () {
     for SHPKG_REPO; do
         SHPKG_REPO_DIR="$(shpkg_dir "${SHPKG_REPO}")"
@@ -190,7 +205,7 @@ shpkg_fetch () {
             err 1 "shpkg_fetch: unknown repo: $SHPKG_REPO"
         fi
         ( verbose 1 cd "${SHPKG_SRC_DIR}/${SHPKG_REPO_DIR}" &&
-              verbose 1 git fetch
+              verbose 1 git fetch --recurse-submodules --tags
         )
     done
 }