import psycopg2
 import ConfigParser
 import urllib
+import filecmp
 from util.LockFile import LockFile
 
 class TabRemover(object):
                 THEN 1 ELSE 0 END
 FROM repositories AS r WHERE approved ORDER BY name""")
                f = open("%s.tmp" % self.conf.get("paths", "gitweblist"), "w")
+               accessfile = open("%s.tmp" % self.conf.get("paths", "lighttpdconf"), "w")
+               accessfile.write("alias.url += (\n")
+
                for name, anon, web, description, initialclone, tabwidth, owner, remoterepo in curs:
                        allrepos[name] = 1
                        repopath = "%s/repos/%s.git" % (self.conf.get("paths", "githome"), name)
                                        cf.close()
 
                        anonfile = "%s/git-daemon-export-ok" % repopath
-                       htafile = "%s/.htaccess" % repopath
                        if anon:
                                if not os.path.isfile(anonfile):
                                        open(anonfile, "w").close()
-                               if os.path.isfile(htafile):
-                                       os.remove(htafile)
+                               # When anonymous access is allowed, create an entry so
+                               # we can access it with http git.
+                               accessfile.write(' "/git/%s.git/" => "%s/",' % (name, repopath))
+                               accessfile.write("\n")
                        else:
                                if os.path.isfile(anonfile):
                                        os.remove(anonfile)
-                               if not os.path.isfile(htafile):
-                                       hf = open(htafile, "w")
-                                       hf.write("order allow,deny\ndeny from all\noptions -indexes\n")
-                                       hf.close()
 
                f.close()
                os.chmod("%s.tmp" % self.conf.get("paths", "gitweblist"), 0644)
                os.rename("%s.tmp" % self.conf.get("paths", "gitweblist"), self.conf.get("paths", "gitweblist"))
 
+               accessfile.write(")\n")
+               accessfile.close()
+               # Only rewrite the access file if it is actually different. And if
+               # it is, we need to also reload lighttpd at this point.
+               if os.path.isfile(self.conf.get("paths", "lighttpdconf")) and filecmp.cmp(
+                       self.conf.get("paths", "lighttpdconf"),
+                       "%s.tmp" % self.conf.get("paths", "lighttpdconf")):
+                       # No changes, so just get rid of the temp file
+                       os.remove("%s.tmp" % self.conf.get("paths", "lighttpdconf"))
+               else:
+                       # File changed, so we need to overwrite the old one *and*
+                       # reload lighttpd so the changes take effect.
+                       os.rename("%s.tmp" % self.conf.get("paths", "lighttpdconf"),
+                                         self.conf.get("paths", "lighttpdconf"))
+                       os.system(self.conf.get("webserver", "reloadcommand"))
+
                # Now remove any repositories that have been deleted
                self._removerepos("%s/repos/" % self.conf.get("paths", "githome"), '/', allrepos)