Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Adding redirect from /git to /cgit for web browsers

As part of the Cgit solution for my network, I added some logic that will redirect web browsers from any /git URLs to the respective /cgit URLs. Maybe someday I will figure how to serve both cgit and git on just /git, but I can deal with this forwarding arrangement for now. Change the httpd directive from ScriptAlias /git/ /usr/libexec/git-core/git-http-backend-custom/ to

ScriptAlias /git/ /usr/sbin/git-http-backend-custom/

And the trailing slash is important! Write new file /usr/sbin/git-http- backend-custom which checks the user agent string and passes git to the real backend utility.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/sh
# File: /usr/sbin/git-http-backend-custom
# Startdate: 2021-04-25
# Part of cgit solution for internal network
export GIT_PROJECT_ROOT PATH_INFO GIT_HTTP_EXPORT_ALL REMOTE_USER GITWEB_CONFIG
unset DEBUG
#DEBUG=1
if echo "${HTTP_USER_AGENT}" | grep -qiE '^git' ;
then
   #printf 'Content-type: text/html\n\n'
   #printf "need to run real git backend\n"
   #cat 1>/dev/null
   /usr/libexec/git-core/git-http-backend "${@}"
else
   printf 'Content-type: text/html\n\n'
   if test -n "${DEBUG}" ;
   then
      printf "<pre>\n"
      env
      echo "${0} ${@}"
      cat
      printf "</pre>\n"
   else
      printf "<meta http-equiv=\"refresh\" content=\"0; URL=%s\" />\n" "/cgit${PATH_INFO}"
      cat 1>/dev/null
   fi
fi
:

Comments