-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rename-file command (#1149)
- Loading branch information
1 parent
e1a4e71
commit c32869d
Showing
11 changed files
with
314 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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'." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
.\" generated with Ronn-NG/v0.8.0 | ||
.\" http://github.com/apjanke/ronn-ng/tree/0.8.0 | ||
.TH "GIT\-RENAME\-FILE" "1" "July 2024" "" "Git Extras" | ||
.SH "NAME" | ||
\fBgit\-rename\-file\fR \- Rename a file or directory and ensure Git recognizes the change, regardless of filesystem case-sensitivity. | ||
.SH "SYNOPSIS" | ||
\fBgit\-rename\-file\fR [OPTIONS] <source> <destination> | ||
.SH "DESCRIPTION" | ||
The \fBgit\-rename\-file\fR command renames a file or directory and ensures Git recognizes the change, regardless of filesystem case-sensitivity. It combines the functionality of the \fBmv\fR command and \fBgit mv\fR. | ||
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. | ||
.SH "OPTIONS" | ||
\fB\-h\fR, \fB\-\-help\fR | ||
.RS | ||
Show usage information. | ||
.RE | ||
.SH "EXAMPLES" | ||
Rename a file: | ||
.RS | ||
\fBgit\-rename\-file old_filename new_filename\fR | ||
.RE | ||
Rename a directory: | ||
.RS | ||
\fBgit\-rename\-file old_directory new_directory\fR | ||
.RE | ||
.SH "AUTHOR" | ||
Written by Zachary Miller <\fI\%[email protected]\fR> | ||
.SH "REPORTING BUGS" | ||
<\fI\%https://github.com/tj/git-extras/issues\fR> | ||
.SH "SEE ALSO" | ||
<\fI\%https://github.com/tj/git-extras\fR> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
git-rename-file(1) -- Rename a file or directory and ensure Git recognizes the change, regardless of filesystem case-sensitivity. | ||
================================================ | ||
|
||
## SYNOPSIS | ||
|
||
`git-rename-file` [OPTIONS] <source> <destination> | ||
|
||
## DESCRIPTION | ||
|
||
The `git-rename-file` command renames a file or directory and ensures 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 usage information. | ||
|
||
## EXAMPLES | ||
|
||
Rename a file: | ||
|
||
```sh | ||
git-rename-file old_filename new_filename | ||
``` | ||
|
||
Rename a directory: | ||
|
||
```sh | ||
git-rename-file old_directory new_directory | ||
``` | ||
|
||
## AUTHOR | ||
|
||
Written by Zachary Miller <<[email protected]>> | ||
|
||
## REPORTING BUGS | ||
|
||
<https://github.com/tj/git-extras/issues> | ||
|
||
## SEE ALSO | ||
|
||
<https://github.com/tj/git-extras> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters