72 lines
1.6 KiB
Bash
Executable File
72 lines
1.6 KiB
Bash
Executable File
#!/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> Directory to search (default: current directory)"
|
|
echo " -o <output_file> 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
|
|
|