Skip to content

Commit 1ad11bf

Browse files
committed
Enhance script functionality and improve error handling
* feat: Support parsing command-line arguments with validation * fix: Correctly handle config file loading and logging * docs: Update documentation for argument parsing and usage * style: Standardize variable usage with braces for consistency * refactor: Improve script structure and readability * chore: Update .gitignore to include temporary files * test: Remove obsolete temporary testing files
1 parent f686fc1 commit 1ad11bf

File tree

3 files changed

+72
-59
lines changed

3 files changed

+72
-59
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ prompt.md
55
history.md
66
.test-logs
77
tests/.logs
8+
tests/.tmp
89
bashdb-5.0-1.1.2
910
changes.0.2.0.sh

src/giv.sh

Lines changed: 71 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ IFS='
1919
get_script_dir() {
2020
# $1: path to script (may be $0 or ${BASH_SOURCE[0]})
2121
script="$1"
22-
case "$script" in
23-
/*) dir=$(dirname "$script") ;;
24-
*) dir=$(cd "$(dirname "$script")" 2>/dev/null && pwd) ;;
22+
case "${script}" in
23+
/*) dir=$(dirname "${script}") ;;
24+
*) dir=$(cd "$(dirname "${script}")" 2>/dev/null && pwd) ;;
2525
esac
26-
printf '%s\n' "$dir"
26+
printf '%s\n' "${dir}"
2727
}
2828

2929
# Detect if sourced (works in bash, zsh, dash, sh)
@@ -41,7 +41,7 @@ else
4141
_SCRIPT_PATH="$0"
4242
fi
4343

44-
SCRIPT_DIR="$(get_script_dir "$_SCRIPT_PATH")"
44+
SCRIPT_DIR="$(get_script_dir "${_SCRIPT_PATH}")"
4545
PROMPT_DIR="${SCRIPT_DIR}/../templates"
4646
GIV_TMPDIR=""
4747

@@ -57,7 +57,7 @@ config_file=""
5757
is_config_loaded=false
5858
debug=false
5959
dry_run=""
60-
template_dir="$PROMPT_DIR"
60+
template_dir="${PROMPT_DIR}"
6161
output_file=''
6262
todo_pattern=''
6363
todo_files="*todo*"
@@ -82,7 +82,21 @@ changelog_file='CHANGELOG.md'
8282
release_notes_file='RELEASE_NOTES.md'
8383
announce_file='ANNOUNCEMENT.md'
8484

85-
# Parse global flags and detect subcommand/target/pattern
85+
# Parses command-line arguments passed to the script and sets corresponding
86+
# variables or flags based on the provided options. Handles validation and
87+
# error reporting for invalid or missing arguments.
88+
#
89+
# Usage:
90+
# parse_args "$@"
91+
#
92+
# Globals:
93+
# May set or modify global variables depending on parsed arguments.
94+
#
95+
# Arguments:
96+
# All command-line arguments passed to the script.
97+
#
98+
# Returns:
99+
# 0 if parsing is successful, non-zero on error.
86100
parse_args() {
87101

88102
# Restore original arguments for main parsing
@@ -149,18 +163,18 @@ parse_args() {
149163
# -------------------------------------------------------------------
150164

151165
# Always attempt to source config file if it exists; empty config_file is a valid state.
152-
if [ -n "$config_file" ] && [ -f "$config_file" ]; then
166+
if [ -n "${config_file}" ] && [ -f "${config_file}" ]; then
153167
# shellcheck disable=SC1090
154-
. "$config_file"
168+
. "${config_file}"
155169
is_config_loaded=true
156-
print_debug "Loaded config file: $config_file"
170+
print_debug "Loaded config file: ${config_file}"
157171
# Override defaults with config file values
158-
model=${GIV_MODEL:-$model}
159-
model_mode=${GIV_MODEL_MODE:-$model_mode}
160-
api_model=${GIV_API_MODEL:-$api_model}
161-
api_url=${GIV_API_URL:-$api_url}
162-
api_key=${GIV_API_KEY:-$api_key}
163-
elif [ ! -f "$config_file" ] && [ "${config_file}" != "${PWD}/.env" ]; then
172+
model=${GIV_MODEL:-${model}}
173+
model_mode=${GIV_MODEL_MODE:-${model_mode}}
174+
api_model=${GIV_API_MODEL:-${api_model}}
175+
api_url=${GIV_API_URL:-${api_url}}
176+
api_key=${GIV_API_KEY:-${api_key}}
177+
elif [ ! -f "${config_file}" ] && [ "${config_file}" != "${PWD}/.env" ]; then
164178
print_warn "config file ${config_file} not found."
165179
fi
166180

@@ -205,7 +219,7 @@ parse_args() {
205219
esac
206220
fi
207221

208-
if [ -z "$REVISION" ]; then
222+
if [ -z "${REVISION}" ]; then
209223
# If no target specified, default to current working tree
210224
print_debug "Debug: No target specified, defaulting to current working tree."
211225
REVISION="--current"
@@ -223,7 +237,7 @@ parse_args() {
223237
shift
224238
done
225239

226-
print_debug "Target and pattern parsed: $REVISION, $PATHSPEC"
240+
print_debug "Target and pattern parsed: ${REVISION}, ${PATHSPEC}"
227241

228242
# 4. Remaining args: global options
229243
while [ $# -gt 0 ]; do
@@ -344,26 +358,26 @@ parse_args() {
344358
print_debug " GIV_API_MODEL: ${GIV_API_MODEL:-}"
345359
print_debug " GIV_API_URL: ${GIV_API_URL:-}"
346360
print_debug "Parsed options:"
347-
print_debug " Debug: $debug"
348-
print_debug " Dry Run: $dry_run"
349-
print_debug " Subcommand: $subcmd"
350-
print_debug " Revision: $REVISION"
351-
print_debug " Pathspec: $PATHSPEC"
352-
print_debug " Template Directory: $template_dir"
353-
print_debug " Config File: $config_file"
354-
print_debug " Config Loaded: $is_config_loaded"
355-
print_debug " Output File: $output_file"
356-
print_debug " TODO Files: $todo_files"
357-
print_debug " TODO Pattern: $todo_pattern"
358-
print_debug " Version File: $version_file"
359-
print_debug " Version Pattern: $version_pattern"
360-
print_debug " Model: $model"
361-
print_debug " Model Mode: $model_mode"
362-
print_debug " API Model: $api_model"
363-
print_debug " API URL: $api_url"
364-
print_debug " Output Mode: $output_mode"
365-
print_debug " Output Version: $output_version"
366-
print_debug " Prompt File: $prompt_file"
361+
print_debug " Debug: ${debug}"
362+
print_debug " Dry Run: ${dry_run}"
363+
print_debug " Subcommand: ${subcmd}"
364+
print_debug " Revision: ${REVISION}"
365+
print_debug " Pathspec: ${PATHSPEC}"
366+
print_debug " Template Directory: ${template_dir}"
367+
print_debug " Config File: ${config_file}"
368+
print_debug " Config Loaded: ${is_config_loaded}"
369+
print_debug " Output File: ${output_file}"
370+
print_debug " TODO Files: ${todo_files}"
371+
print_debug " TODO Pattern: ${todo_pattern}"
372+
print_debug " Version File: ${version_file}"
373+
print_debug " Version Pattern: ${version_pattern}"
374+
print_debug " Model: ${model}"
375+
print_debug " Model Mode: ${model_mode}"
376+
print_debug " API Model: ${api_model}"
377+
print_debug " API URL: ${api_url}"
378+
print_debug " Output Mode: ${output_mode}"
379+
print_debug " Output Version: ${output_version}"
380+
print_debug " Prompt File: ${prompt_file}"
367381
}
368382
# -------------------------------------------------------------------
369383
# Helper Functions
@@ -379,7 +393,7 @@ get_available_releases() {
379393
# Update the script to a specific release version (or latest if not specified)
380394
run_update() {
381395
version="${1:-latest}"
382-
if [ "$version" = "latest" ]; then
396+
if [ "${version}" = "latest" ]; then
383397
latest_version=$(get_available_releases | head -n 1)
384398
printf 'Updating giv to version %s...\n' "${latest_version}"
385399
curl -fsSL https://raw.githubusercontent.com/giv-cli/giv/main/install.sh | sh -- --version "${latest_version}"
@@ -470,15 +484,14 @@ cmd_message() {
470484
print_debug "Generating commit message for ${commit_id}"
471485

472486
# Handle both --current and --cached (see argument parsing section for details).
473-
if [ "$commit_id" = "--current" ] || [ "$commit_id" = "--cached" ]; then
487+
if [ "${commit_id}" = "--current" ] || [ "${commit_id}" = "--cached" ]; then
474488
hist=$(portable_mktemp "commit_history_XXXXXX.md")
475-
build_history "$hist" "$commit_id" "$todo_pattern" "$PATHSPEC"
476-
print_debug "Generated history file $hist"
489+
build_history "${hist}" "${commit_id}" "${todo_pattern}" "${PATHSPEC}"
490+
print_debug "Generated history file ${hist}"
477491
pr=$(portable_mktemp "commit_message_prompt_XXXXXX.md")
478-
printf '%s' "$(build_prompt "${PROMPT_DIR}/message_prompt.md" "$hist")" >"$pr"
479-
print_debug "Generated prompt file $pr"
480-
res=$(generate_response "$pr" "$model_mode")
481-
res=$(generate_response "$pr" "$model_mode")
492+
build_prompt "${PROMPT_DIR}/message_prompt.md" "${hist}" >"${pr}"
493+
print_debug "Generated prompt file ${pr}"
494+
res=$(generate_response "${pr}" "${model_mode}")
482495
if [ $? -ne 0 ]; then
483496
printf 'Error: Failed to generate AI response.\n' >&2
484497
exit 1
@@ -487,33 +500,33 @@ cmd_message() {
487500
return
488501
fi
489502
# Detect exactly two- or three-dot ranges (A..B or A...B)
490-
if echo "$commit_id" | grep -qE '\.\.\.?'; then
491-
print_debug "Detected commit range syntax: $commit_id"
503+
if echo "${commit_id}" | grep -qE '\.\.\.?'; then
504+
print_debug "Detected commit range syntax: ${commit_id}"
492505

493506
# Confirm Git accepts it as a valid range
494-
if ! git rev-list "$commit_id" >/dev/null 2>&1; then
495-
print_error "Invalid commit range: $commit_id"
507+
if ! git rev-list "${commit_id}" >/dev/null 2>&1; then
508+
print_error "Invalid commit range: ${commit_id}"
496509
exit 1
497510
fi
498511

499512
# Use symmetric-difference for three-dot, exclusion for two-dot
500-
case "$commit_id" in
513+
case "${commit_id}" in
501514
*...*)
502-
print_debug "Processing three-dot range: $commit_id"
503-
git --no-pager log --pretty=%B --left-right "$commit_id" | sed '${/^$/d;}'
515+
print_debug "Processing three-dot range: ${commit_id}"
516+
git --no-pager log --pretty=%B --left-right "${commit_id}" | sed '${/^$/d;}'
504517
;;
505518
*..*)
506-
print_debug "Processing two-dot range: $commit_id"
507-
git --no-pager log --reverse --pretty=%B "$commit_id" | sed '${/^$/d;}'
519+
print_debug "Processing two-dot range: ${commit_id}"
520+
git --no-pager log --reverse --pretty=%B "${commit_id}" | sed '${/^$/d;}'
508521
;;
509522
*) ;;
510523
esac
511524
return
512525
fi
513526

514-
print_debug "Processing single commit: $commit_id"
515-
if ! git rev-parse --verify "$commit_id" >/dev/null 2>&1; then
516-
printf 'Error: Invalid commit ID: %s\n' "$commit_id" >&2
527+
print_debug "Processing single commit: ${commit_id}"
528+
if ! git rev-parse --verify "${commit_id}" >/dev/null 2>&1; then
529+
printf 'Error: Invalid commit ID: %s\n' "${commit_id}" >&2
517530
exit 1
518531
fi
519532
git --no-pager log -1 --pretty=%B "${commit_id}" | sed '${/^$/d;}'

tests/.tmp/tmp.4ifJ9Hiotv

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)