gitpages

A collection of scripts to securely webhost and publish git repositories.
git clone https://scm.kuandu.systems/git-raw/gitpages.git
Log | Files | Refs | README | LICENSE

commit f72e4dda131a7698ac36ebe8d083647431d553a5
parent ee29a2a5f393d1396a2445d150cf6d837c8b86cc
Author: Fred Großkopf <fred@kuandu.systems>
Date:   Tue, 21 Apr 2026 23:44:36 +0200

Validates empty or missing -c arg

Diffstat:
Mgitpages.sh | 24++++++++++++++++--------
Mtest-gitpages.sh | 19+++++++++++++++++--
2 files changed, 33 insertions(+), 10 deletions(-)

diff --git a/gitpages.sh b/gitpages.sh @@ -18,7 +18,7 @@ Arguments: ASSETS_DIR Optional: CSS, favicon, etc. directory Options: - -c CONFIG_FILE Path to config (default: \$(dirname "\$0")/$CONFIG_FILE_NAME) + -c CONFIG_FILE Path to config (default: ./$CONFIG_FILE_NAME) -h Show this help Examples: @@ -43,7 +43,7 @@ sed_escape() { printf '%s\n' "$1" | sed 's/[\\/&]/\\&/g'; } # === VALIDATION === check_args() { - case $# in 2 | 3) ;; *) error "usage: $0 SRC DST_DIR [ASSETS_DIR]" ;; esac + case $# in 2 | 3) ;; *) error "usage: $0 [-c CONFIG_FILE] SRC DST_DIR [ASSETS_DIR]" ;; esac } check_html_writable() { @@ -157,11 +157,15 @@ load_config() { # === MAIN LOGIC === main() { CONFIG_FILE="" + CONFIG_FILE_SET=0 # Parse options while getopts ":hc:" opt; do case "$opt" in - c) CONFIG_FILE="$OPTARG" ;; + c) + CONFIG_FILE="$OPTARG" + CONFIG_FILE_SET=1 + ;; h) usage exit 0 @@ -190,10 +194,14 @@ main() { fi # Load config - [ -z "$CONFIG_FILE" ] && CONFIG_FILE="$(dirname "$0")/${CONFIG_FILE_NAME}" + if [ "$CONFIG_FILE_SET" -eq 1 ]; then + [ -n "$CONFIG_FILE" ] || error "-c requires a non-empty argument" + [ -r "$CONFIG_FILE" ] || error "config file '$CONFIG_FILE' does not exist or is not readable" + else + CONFIG_FILE="$(dirname "$0")/$CONFIG_FILE_NAME" + fi load_config "$CONFIG_FILE" - # Unified repo processing if [ -n "$SINGLE_REPO" ]; then repo="$SINGLE_REPO" @@ -203,9 +211,9 @@ main() { generate_index info "Generated single repo: $repo → $DST_DIR" else - # Clear destination (safety-checked earlier) - # shellcheck disable=SC2115 # wants non-posix compatible ${var:?} - [ "$DST_DIR" != "/" ] && rm -rf "$DST_DIR"/* + # Clear destination (safety-checked earlier) + # shellcheck disable=SC2115 # wants non-posix compatible ${var:?} + [ "$DST_DIR" != "/" ] && rm -rf "$DST_DIR"/* for repo in "$SRC_DIR"/*.git; do [ -e "$repo" ] || continue diff --git a/test-gitpages.sh b/test-gitpages.sh @@ -329,8 +329,22 @@ test_single_repo_does_not_remove_others() { # 3. Verify neither repo1 nor repo2 HTML dirs get deleted check_dir_exists "$HTML_DST/repo1" "repo1 HTML still present after single‑repo run" - check_dir_exists "$HTML_DST/repo2" "repo2 HTML still present after single‑repo -run" + check_dir_exists "$HTML_DST/repo2" "repo2 HTML still present after single‑repo run" +} + +test_missing_or_empty_config_file() { + mkdir -p "$RAW_SRC/repo1.git" + (cd "$RAW_SRC/repo1.git" && git init --bare . > /dev/null 2>&1) + + missing_cfg="$TEST_BASE/does-not-exist.conf" + + # Missing file must fail. + assert_fails 13a "missing config file" \ + "$SCRIPT_NAME" -c "$missing_cfg" "$RAW_SRC/repo1.git" "$HTML_DST" + + # Empty path must also fail. + assert_fails 13b "empty config file path" \ + "$SCRIPT_NAME" -c "" "$RAW_SRC/repo1.git" "$HTML_DST" } main() { @@ -352,6 +366,7 @@ main() { test_repo_titles_rewriting test_index_rewriting test_single_repo_does_not_remove_others + test_missing_or_empty_config_file printf '\nALL TESTS PASSED!\n' printf 'Test environment cleaned up.\n'