diff --git a/GitThat.sh b/GitThat.sh new file mode 100755 index 0000000..f792f59 --- /dev/null +++ b/GitThat.sh @@ -0,0 +1,71 @@ +#!/bin/bash + +show_GitThat_help() { + echo "Usage: $0 [-h] [-d directory] [-o output_file] [-s]" + echo + echo " -h Show this help message and exit" + echo " -d Directory to search (default: current directory)" + echo " -o File to save the output (default: stdout)" + echo " -s Search all subdirectories (recursive)" +} + +# Default values +BASE_DIR="." +OUTPUT_FILE="" +RECURSIVE=false + +# Parse options +while getopts ":hd:o:s" opt; do + case $opt in + h) + show_GitThat_help + exit 0 + ;; + d) + BASE_DIR="$OPTARG" + ;; + o) + OUTPUT_FILE="$OPTARG" + ;; + s) + RECURSIVE=true + ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + show_GitThat_help + exit 1 + ;; + :) + echo "Option -$OPTARG requires an argument." >&2 + show_GitThat_help + exit 1 + ;; + esac +done + +# Determine find depth option +if [ "$RECURSIVE" = true ]; then + FIND_OPTS="-type d -name .git" +else + FIND_OPTS="-maxdepth 2 -type d -name .git" +fi + +# Run the search and collect results +RESULTS="" +while IFS= read -r gitdir; do + repo_dir="$(dirname "$gitdir")" + url=$(git -C "$repo_dir" config --get remote.origin.url) + if [ -n "$url" ]; then + RESULTS+="$repo_dir: $url"$'\n' + else + RESULTS+="$repo_dir: No remote.origin.url found"$'\n' + fi +done < <(find "$BASE_DIR" $FIND_OPTS) + +# Output results +if [ -n "$OUTPUT_FILE" ]; then + echo "$RESULTS" > "$OUTPUT_FILE" +else + echo "$RESULTS" +fi +