|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +# Usage function |
| 6 | +usage() { |
| 7 | + cat <<EOF |
| 8 | +Usage: git rename-file [OPTIONS] <source> <destination> |
| 9 | +
|
| 10 | +Description: |
| 11 | + Rename a file or directory and ensure Git recognizes the change, regardless of filesystem case-sensitivity. |
| 12 | + It combines the functionality of the "mv" command and "git mv". This is particularly useful for renaming files or directories |
| 13 | + to change only their case, which might not be detected by Git on case-insensitive filesystems. |
| 14 | +
|
| 15 | +Options: |
| 16 | + -h, --help Show this help message and exit. |
| 17 | +
|
| 18 | +Examples: |
| 19 | + git rename-file old_filename new_filename |
| 20 | + git rename-file old_directory new_directory |
| 21 | +EOF |
| 22 | +} |
| 23 | + |
| 24 | +# Check for help option |
| 25 | +if [[ "$1" == "-h" || "$1" == "--help" ]]; then |
| 26 | + usage |
| 27 | + exit 0 |
| 28 | +fi |
| 29 | + |
| 30 | +# Check for correct number of arguments |
| 31 | +if [ "$#" -ne 2 ]; then |
| 32 | + echo "Error: Incorrect number of arguments." |
| 33 | + echo "" |
| 34 | + usage >&2 |
| 35 | + exit 1 |
| 36 | +fi |
| 37 | + |
| 38 | +# Assign variables |
| 39 | +SOURCE="$1" |
| 40 | +DESTINATION="$2" |
| 41 | +TEMP_NAME="${SOURCE}.temp" |
| 42 | + |
| 43 | +# Function to check if a file or directory exists in a case-sensitive manner |
| 44 | +check_case_sensitive_exists() { |
| 45 | + local path="$1" |
| 46 | + local dir |
| 47 | + local base |
| 48 | + |
| 49 | + dir=$(dirname "$path") |
| 50 | + base=$(basename "$path") |
| 51 | + |
| 52 | + if [ -e "$dir" ]; then |
| 53 | + if (cd "$dir" && find . -maxdepth 1 -name "$base" | grep -q "$base"); then |
| 54 | + return 0 |
| 55 | + fi |
| 56 | + fi |
| 57 | + return 1 |
| 58 | +} |
| 59 | + |
| 60 | +# Check if source exists and is under version control |
| 61 | +if ! check_case_sensitive_exists "$SOURCE"; then |
| 62 | + echo "Error: Source '$SOURCE' does not exist." |
| 63 | + exit 1 |
| 64 | +fi |
| 65 | + |
| 66 | +if ! git ls-files --error-unmatch "$SOURCE" > /dev/null 2>&1; then |
| 67 | + echo "Error: Source '$SOURCE' is not under version control. If file or directory is new, it must at least be staged." |
| 68 | + exit 1 |
| 69 | +fi |
| 70 | + |
| 71 | +# Check if destination already exists |
| 72 | +if check_case_sensitive_exists "$DESTINATION"; then |
| 73 | + echo "Error: Destination '$DESTINATION' already exists." |
| 74 | + exit 1 |
| 75 | +fi |
| 76 | + |
| 77 | +# Check if the destination directory exists |
| 78 | +DEST_DIR=$(dirname "$DESTINATION") |
| 79 | +if ! check_case_sensitive_exists "$DEST_DIR"; then |
| 80 | + echo "Error: Destination directory '$DEST_DIR' does not exist." |
| 81 | + exit 1 |
| 82 | +fi |
| 83 | + |
| 84 | +# Create a rollback function |
| 85 | +rollback() { |
| 86 | + echo "Rolling back changes..." |
| 87 | + if [ -e "$TEMP_NAME" ]; then |
| 88 | + git mv -f "$TEMP_NAME" "$SOURCE" |
| 89 | + fi |
| 90 | +} |
| 91 | + |
| 92 | +# Trap errors to trigger rollback |
| 93 | +trap 'rollback' ERR |
| 94 | + |
| 95 | +# Move the file to a temporary name within the Git repository |
| 96 | +git mv "$SOURCE" "$TEMP_NAME" |
| 97 | + |
| 98 | +# Move the temporary file to the desired destination |
| 99 | +git mv "$TEMP_NAME" "$DESTINATION" |
| 100 | + |
| 101 | +echo "Successfully renamed '$SOURCE' to '$DESTINATION'." |
0 commit comments