git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2] gitweb: Refactor reading and parsing config file into read_config_file

From
Jakub Narebski <jnareb@gmail.com>
Date
May 25, 2011, 16:35 UTC
Message-ID
<1306341328-11108-2-git-send-email-jnareb@gmail.com>
In-Reply-To
<1306341328-11108-1-git-send-email-jnareb@gmail.com>

Beside being obvious reduction of duplicated code, this is enables us to easily call site-wide config file in per-installation config file.

The actual update to documentation is left for next commit, because of possible exclusive alternative (possible other next commit) of always reading system-wide config file and relying on per-instalation config file overriding system-wide defaults.

Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
I think it makes code more clear, both version A xor B ;-)
 gitweb/gitweb.perl |   28 ++++++++++++++++++++--------
 1 files changed, 20 insertions(+), 8 deletions(-)
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index e4ede13..ce92d67 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -635,18 +635,30 @@ sub filter_snapshot_fmts {
 # if it is true then gitweb config would be run for each request.
 our $per_request_config = 1;
 
+# read and parse gitweb config file given by its parameter.
+# returns true on success, false on recoverable error, allowing
+# to chain this subroutine, using first file that exists.
+# dies on errors during parsing config file, as it is unrecoverable.
+sub read_config_file {
+	my $filename = shift;
+	return unless defined $filename;
+	# die if there are errors parsing config file
+	if (-e $filename) {
+		do $filename;
+		die $@ if $@;
+		return 1;
+	}
+	return;
+}
+
 our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM);
 sub evaluate_gitweb_config {
 	our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
 	our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
-	# die if there are errors parsing config file
-	if (-e $GITWEB_CONFIG) {
-		do $GITWEB_CONFIG;
-		die $@ if $@;
-	} elsif (-e $GITWEB_CONFIG_SYSTEM) {
-		do $GITWEB_CONFIG_SYSTEM;
-		die $@ if $@;
-	}
+
+	# use first config file that exists
+	read_config_file($GITWEB_CONFIG) or
+	read_config_file($GITWEB_CONFIG_SYSTEM);
 }
 
 # Get loadavg of system, to compare against $maxload.
-- 
1.7.5
Previous: Jakub NarebskiNext: Junio C Hamano
Message 2 of 9 in “gitweb: Improve handling of configuration files”
  1. 0/2 gitweb: Improve handling of configuration filesJakub Narebski, May 25, 2011
  2. 1/2 gitweb: Refactor reading and parsing config file into read_config_fileJakub Narebski, May 25, 2011
  3. Junio C HamanoMay 25, 2011
  4. 2/2 gitweb: Mention read_config_file in gitweb/READMEJakub Narebski, May 25, 2011
  5. 2/2 gitweb: Use /etc/gitweb.conf even if gitweb_conf.perl existJakub Narebski, May 25, 2011
  6. Junio C HamanoMay 25, 2011
  7. 2/2 gitweb: Introduce common system-wide settings for convenienceJakub Narebski, May 26, 2011
  8. J.H.May 25, 2011
  9. Junio C HamanoMay 25, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.