File: git-rename-file

package info (click to toggle)
git-extras 7.4.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 2,120 kB
  • sloc: sh: 4,312; python: 994; makefile: 146
file content (101 lines) | stat: -rwxr-xr-x 2,566 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env bash

set -euo pipefail

# Usage function
usage() {
    cat <<EOF
Usage: git rename-file [OPTIONS] <source> <destination>

Description:
  Rename a file or directory and ensure Git recognizes the change, regardless of filesystem case-sensitivity.
  It combines the functionality of the "mv" command and "git mv". This is particularly useful for renaming files or directories
  to change only their case, which might not be detected by Git on case-insensitive filesystems.

Options:
  -h, --help  Show this help message and exit.

Examples:
  git rename-file old_filename new_filename
  git rename-file old_directory new_directory
EOF
}

# Check for help option
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
    usage
    exit 0
fi

# Check for correct number of arguments
if [ "$#" -ne 2 ]; then
    echo "Error: Incorrect number of arguments."
    echo ""
    usage >&2
    exit 1
fi

# Assign variables
SOURCE="$1"
DESTINATION="$2"
TEMP_NAME="${SOURCE}.temp"

# Function to check if a file or directory exists in a case-sensitive manner
check_case_sensitive_exists() {
    local path="$1"
    local dir
    local base

    dir=$(dirname "$path")
    base=$(basename "$path")

    if [ -e "$dir" ]; then
        if (cd "$dir" && find . -maxdepth 1 -name "$base" | grep -q "$base"); then
            return 0
        fi
    fi
    return 1
}

# Check if source exists and is under version control
if ! check_case_sensitive_exists "$SOURCE"; then
    echo "Error: Source '$SOURCE' does not exist."
    exit 1
fi

if ! git ls-files --error-unmatch "$SOURCE" > /dev/null 2>&1; then
    echo "Error: Source '$SOURCE' is not under version control. If file or directory is new, it must at least be staged."
    exit 1
fi

# Check if destination already exists
if check_case_sensitive_exists "$DESTINATION"; then
    echo "Error: Destination '$DESTINATION' already exists."
    exit 1
fi

# Check if the destination directory exists
DEST_DIR=$(dirname "$DESTINATION")
if ! check_case_sensitive_exists "$DEST_DIR"; then
    echo "Error: Destination directory '$DEST_DIR' does not exist."
    exit 1
fi

# Create a rollback function
rollback() {
    echo "Rolling back changes..."
    if [ -e "$TEMP_NAME" ]; then
        git mv -f "$TEMP_NAME" "$SOURCE"
    fi
}

# Trap errors to trigger rollback
trap 'rollback' ERR

# Move the file to a temporary name within the Git repository
git mv "$SOURCE" "$TEMP_NAME"

# Move the temporary file to the desired destination
git mv "$TEMP_NAME" "$DESTINATION"

echo "Successfully renamed '$SOURCE' to '$DESTINATION'."