From 08f0f74da3cd335f0acf58dc1f6370848dc1dbd8 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Paul=20H=C3=A4nsch?= <paul@plutz.net>
Date: Wed, 31 May 2023 22:27:12 +0200
Subject: [PATCH] enable work without git revisioning

---
 handlers/60_edit.sh               |  2 +-
 handlers/60_move_rename_delete.sh | 17 ++++++++++++++---
 index.cgi                         | 11 ++++++++---
 macros/revisions                  |  2 +-
 4 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/handlers/60_edit.sh b/handlers/60_edit.sh
index 6d4e1ea..9d1a404 100755
--- a/handlers/60_edit.sh
+++ b/handlers/60_edit.sh
@@ -43,7 +43,7 @@ elif [ "$edit_action" = update ]; then
     REDIRECT "${_BASE}${PATH_INFO%\[edit\]}/[edit]"
   fi
 
-  if [ "$(which git)" ]; then
+  if [ "$REV_PAGES" = true ]; then
     git -C "$_DATA" add \
       -- "pages/$edit_page/#page.md"
     git -C "$_DATA" commit -qm \
diff --git a/handlers/60_move_rename_delete.sh b/handlers/60_move_rename_delete.sh
index be84ca8..bbe994e 100755
--- a/handlers/60_move_rename_delete.sh
+++ b/handlers/60_move_rename_delete.sh
@@ -105,11 +105,14 @@ if [ "$action" = rename -a "$newname" ]; then
     printf 'Refresh: %i\r\n' 4
     theme_error 403
     return 0
-  else
+  elif [ "$REV_PAGES" = true ]; then
     git -C "$_DATA" mv "pages/$oldname" "pages/$newname"
     git -C "$_DATA" commit -m 'Page # '"$oldname"' # renamed to # '"$newname"' # by user @ '"$USER_NAME"' @' \
         -- "pages/$oldname" "pages/$newname"
     REDIRECT "$_BASE${newname}"
+  else
+    mv -- "$_DATA/pages/$oldname" "$_DATA/pages/$newname"
+    REDIRECT "$_BASE${newname}"
   fi
 elif [ "$action" = move -a "$newlocation" ]; then
   oldname="${PATH_INFO%\[*\]}"
@@ -131,11 +134,14 @@ elif [ "$action" = move -a "$newlocation" ]; then
     printf 'Refresh: %i\r\n' 4
     theme_error 403
     return 0
-  else
+  elif [ "$REV_PAGES" = true ]; then
     git -C "$_DATA" mv "pages/${oldname}" "pages/${newname}"
     git -C "$_DATA" commit -m 'Page # '"$oldname"' # moved to # '"$newname"' # by user @ '"$USER_NAME"' @' \
         -- "pages/${oldname}" "pages/${newname}"
     REDIRECT "$_BASE${newname}"
+  else
+    mv -- "$_DATA/pages/$oldname" "$_DATA/pages/$newname"
+    REDIRECT "$_BASE${newname}"
   fi
 elif [ "$action" = delete ]; then
   oldname="${PATH_INFO%\[*\]}"
@@ -143,13 +149,18 @@ elif [ "$action" = delete ]; then
     printf 'Refresh: %i\r\n' 4
     theme_error 403
     return 0
-  else
+  elif [ "$REV_PAGES" = true ]; then
     git -C "$_DATA" rm "pages/${oldname}/#page.md"
     git -C "$_DATA" commit -m 'Page # '"$oldname"' # deleted by user @ '"$USER_NAME"' @' \
         -- "pages/${oldname}/#page.md"
     rm -r -- "$_DATA/pages/${oldname}"/\#*
     rmdir -- "$_DATA/pages/${oldname}/" || true
     REDIRECT ./
+  else
+    rm -- "$_DATA/pages/${oldname}/#page.md"
+    rm -r -- "$_DATA/pages/${oldname}"/\#*
+    rmdir -- "$_DATA/pages/${oldname}/" || true
+    REDIRECT ./
   fi
 elif [ "$action" = cancel ]; then
   REDIRECT ./
diff --git a/index.cgi b/index.cgi
index 8479bd9..61a8bb0 100755
--- a/index.cgi
+++ b/index.cgi
@@ -6,8 +6,13 @@
 . "${_EXEC}/tools.sh"
 . "${_EXEC}/acl.sh"
 
+REV_PAGES=${REV_PAGES:-true}
 REV_ATTACHMENTS=${REV_ATTACHMENTS:-false}
 WIKI_THEME="${WIKI_THEME:-default}"
+
+which git >/dev/null || REV_PAGES=false
+[ "$REV_PAGES" != true ] && REV_ATTACHMENTS=false
+
 . "${_EXEC}/themes/${WIKI_THEME}.sh"
 
 # Renew session cookie, only if cookie already set
@@ -23,11 +28,10 @@ wiki_text() {
   cat -- "$mdfile"
 }
 
-if [ "$(which git)" -a ! -f "$_DATA/.gitignore" ]; then
+if [ "$REV_PAGES" = true -a ! -f "$_DATA/.gitignore" ]; then
   cat >"$_DATA/.gitignore" <<-EOF
 	users.db
 	serverkey
-	$([ "$REV_ATTACHMENTS" = true ] || printf %s "**/#attachments/")
 	**/#cache/
 	**/#page.lock
 	**/#page.*.cache
@@ -35,6 +39,8 @@ if [ "$(which git)" -a ! -f "$_DATA/.gitignore" ]; then
 	**/#page:*.*.cache
 	**/#page:*.*.cache.*
 	EOF
+  [ "$REV_ATTACHMENTS" != true ] \
+  && printf '**/#attachments/\n' >>"$_DATA/.gitignore"
   git init "$_DATA"
   git -C "$_DATA" add .gitignore
   git -C "$_DATA" commit -m 'initialization' -- .gitignore
@@ -43,7 +49,6 @@ fi 1>&2
 for handler in "$_EXEC"/handlers/*; do
   . "$handler" && break
 done
-
 if [ $? != 0 ]; then
   export ERROR_MSG="The presented URL schema cannot be handled"
   theme_error 400
diff --git a/macros/revisions b/macros/revisions
index 9a5730f..4271149 100755
--- a/macros/revisions
+++ b/macros/revisions
@@ -9,7 +9,7 @@ page_abs="$(page_abs "$page")"
 
 if ! acl_read "$page_abs"; then
   return 0
-elif [ ! "$(which git)" ]; then
+elif [ "$REV_PAGES" != true ]; then
   printf '<div class="macro revisions error">GIT is not available to handle revisioning.</div>'
 fi
 
-- 
2.39.5