Skip to contentSkip to conversation

Add support for directories to git-rename-script.

PATCH In review Ryan Anderson 2 participants 5 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add support for directories to git-rename-script.

Oh, and in the process, rewrite it in Perl.

---

 git-rename-script |   68 +++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 63 insertions(+), 5 deletions(-)

f46717f9116bba7efb6a10ed99cd2fcea00fe5da

git-rename-script +63 -5
@@ -1,7 +1,65 @@
-#!/bin/sh
+#!/usr/bin/perl
+#
+# Copyright 2005, Ryan Anderson <[email protected]>
+#
+# This file is licensed under the GPL v2, or a later version
+# at the discretion of Linus Torvalds.
 
-. git-sh-setup-script || die "Not a git archive"
 
-[ -f "$1" ] || [ -h "$1" ] || die "git rename: bad source"
-[ -e "$2" ] && die "git rename: destination already exists"
-mv -- "$1" "$2" && git-update-cache --add --remove -- "$1" "$2"
+use warnings;
+use strict;
+
+sub usage($);
+
+# Sanity checks:
+unless ( -d ".git" && -d ".git/objects" && 
+	-d ".git/objects/00" && -d ".git/refs") {
+	usage("Git repository not found.");
+}
+
+usage("") if scalar @ARGV != 2;
+
+my ($src,$dst) = @ARGV;
+
+unless (-f $src || -l $src || -d $src) {
+	usage("git rename: bad source '$src'");
+}
+
+if (-e $dst) {
+	usage("git rename: destinations '$dst' already exists");
+}
+
+my (@allfiles,@srcfiles,@dstfiles);
+
+open(F,"-|","git-ls-files")
+	or die "Failed to open pipe from git-ls-files: " . $!;
+
+@allfiles = <F>;
+close(F);
+chomp for @allfiles;
+
+
+@srcfiles = grep /^$src/, @allfiles;
+@dstfiles = @srcfiles;
+s#^$src(/|$)#$dst$1# for @dstfiles;
+
+rename($src,$dst)
+	or die "rename failed: $!";
+
+system("git-update-cache","--remove","--",@srcfiles);
+system("git-update-cache","--add","--",@dstfiles);
+
+
+sub usage($) {
+	my $s = shift;
+	print $s, "\n" if (length $s != 0);
+	print <<EOT;
+$0 <source> <dest>
+source must exist and be either a file, symlink or directory.
+dest must NOT exist.
+
+Renames source to dest, and updates the git cache to reflect the change.
+Use "git commit" to make record the change permanently.
+EOT
+	exit(1);
+}
Signature · 2 lines
Ryan Anderson
  sometimes Pug Majere
#1 lore raw

PATCH 1/1 Add documentation for git-rename-script

---

 Documentation/git-rename-script.txt |   34 ++++++++++++++++++++++++++++++++++
 1 files changed, 34 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/git-rename-script.txt

715924203401309ceb2f696e507b8b2c18244d08

Documentation/git-rename-script.txt new file +34 -0
new file mode 100644
@@ -0,0 +1,34 @@
+
+git-rename-script(1)
+=====================
+v0.1, May 2005
+
+NAME
+----
+git-rename-script - Script used to rename a file, directory or symlink.
+
+
+SYNOPSIS
+--------
+'git-rename-script' <source> <destination>
+
+DESCRIPTION
+-----------
+This script is used to rename a file, directory or symlink.
+
+The index is updated after successful completion, but the change must still be
+committed.
+
+Author
+------
+Written by Linus Torvalds <[email protected]>
+Rewritten by Ryan Anderson <[email protected]>
+
+Documentation
+--------------
+Documentation by David Greaves, Junio C Hamano and the git-list <[email protected]>.
+
+GIT
+---
+Part of the link:git.html[git] suite
+
#2 lore raw
In reply to Ryan Anderson

git-rename-script line 38

Quoted 5 lines · Ryan Anderson
git-rename-script +5 -0
+open(F,"-|","git-ls-files")
+	or die "Failed to open pipe from git-ls-files: " . $!;
+
+@allfiles = <F>;
+close(F);

We can afford to handle funny characters in the filename right without pain in Perl, so let's do it right:

    use vars qw($/);
    $/ = "\0";
    open F, '-|', 'git-ls-files', '-z'
    	or die "Failed to open pipe from git-ls-files: " . $!;
    @allfiles = map { chomp; $_} <F>;
Quoted 3 lines

+@srcfiles = grep /^$src/, @allfiles;
+@dstfiles = @srcfiles;
+s#^$src(/|$)#$dst$1# for @dstfiles;

Be careful with these regexps. Perhaps something like this?

    @dstfiles = @srcfiles = grep /^\Q$src\E/, @allfiles;
    s#^\Q$src\E(/|$)#$dst$1# for @dstfiles;

or perhaps a bit more readable:

    my $qsrc = quotemeta($src);
    @dstfiles = @srcfiles = grep /^$qsrc/, @allfiles;
    for (@dstfiles) { s/^$qsrc(/|$)/$dst$1/; }

git-rename-script line 47

Quoted 2 lines
git-rename-script +2 -0
+rename($src,$dst)
+	or die "rename failed: $!";

Making sure leading directories for $dst exists, perhaps?

Otherwise looks good.

Quoted 1 line

+sub usage($) {

This is just a style thing, but do you really care to force a scalar contect when "usage" is used?

#3 lore raw

PATCH 1/1 Make git-rename-script behave much better when faced with input contain Perl regular expression metacharacters.

Also, restore support for the GIT_DIR

---

 git-rename-script |   26 ++++++++++----------------
 1 files changed, 10 insertions(+), 16 deletions(-)

1ed66638c7ce328d882639447b80099f096c2993

git-rename-script +10 -16
@@ -12,11 +12,8 @@ use strict;
 sub usage($);
 
 # Sanity checks:
-my $GIT_DIR = $$ENV{'GIT_DIR'};
-$GIT_DIR = ".git" unless defined $GIT_DIR;
-
-unless ( -d $GIT_DIR && -d $GIT_DIR . "/objects" && 
-	-d $GIT_DIR . "/objects/00" && -d $GIT_DIR . "/refs") {
+unless ( -d ".git" && -d ".git/objects" && 
+	-d ".git/objects/00" && -d ".git/refs") {
 	usage("Git repository not found.");
 }
 
@@ -34,26 +31,23 @@ if (-e $dst) {
 
 my (@allfiles,@srcfiles,@dstfiles);
 
-$/ = "\0";
-open(F,"-|","git-ls-files","-z")
+open(F,"-|","git-ls-files")
 	or die "Failed to open pipe from git-ls-files: " . $!;
 
-@allfiles = map { chomp; $_; } <F>;
+@allfiles = <F>;
 close(F);
+chomp for @allfiles;
+
 
-my $safesrc = quotemeta($src);
-@srcfiles = grep /^$safesrc/, @allfiles;
+@srcfiles = grep /^$src/, @allfiles;
 @dstfiles = @srcfiles;
-s#^$safesrc(/|$)#$dst$1# for @dstfiles;
+s#^$src(/|$)#$dst$1# for @dstfiles;
 
 rename($src,$dst)
 	or die "rename failed: $!";
 
-my $rc = system("git-update-cache","--add","--",@dstfiles);
-die "git-update-cache failed to add new name with code $?\n" if $rc;
-
-$rc = system("git-update-cache","--remove","--",@srcfiles);
-die "git-update-cache failed to remove old name with code $?\n" if $rc;
+system("git-update-cache","--remove","--",@srcfiles);
+system("git-update-cache","--add","--",@dstfiles);
 
 
 sub usage($) {
Signature · 2 lines
Ryan Anderson
  sometimes Pug Majere
#4 lore raw

PATCH 1/1 Make git-rename-script behave better with input containing Perl regexp metachars.

Make git-rename-script behave much better when faced with input contain Perl regular expression metacharacters.

Also, restore support for the GIT_DIR

---

 git-rename-script |   26 ++++++++++++++++----------
 1 files changed, 16 insertions(+), 10 deletions(-)

28d2bb7cbb38424c4c6879110bf8aff1e3e5ac42

git-rename-script +16 -10
@@ -12,8 +12,11 @@ use strict;
 sub usage($);
 
 # Sanity checks:
-unless ( -d ".git" && -d ".git/objects" && 
-	-d ".git/objects/00" && -d ".git/refs") {
+my $GIT_DIR = $$ENV{'GIT_DIR'};
+$GIT_DIR = ".git" unless defined $GIT_DIR;
+
+unless ( -d $GIT_DIR && -d $GIT_DIR . "/objects" && 
+	-d $GIT_DIR . "/objects/00" && -d $GIT_DIR . "/refs") {
 	usage("Git repository not found.");
 }
 
@@ -31,23 +34,26 @@ if (-e $dst) {
 
 my (@allfiles,@srcfiles,@dstfiles);
 
-open(F,"-|","git-ls-files")
+$/ = "\0";
+open(F,"-|","git-ls-files","-z")
 	or die "Failed to open pipe from git-ls-files: " . $!;
 
-@allfiles = <F>;
+@allfiles = map { chomp; $_; } <F>;
 close(F);
-chomp for @allfiles;
-
 
-@srcfiles = grep /^$src/, @allfiles;
+my $safesrc = quotemeta($src);
+@srcfiles = grep /^$safesrc/, @allfiles;
 @dstfiles = @srcfiles;
-s#^$src(/|$)#$dst$1# for @dstfiles;
+s#^$safesrc(/|$)#$dst$1# for @dstfiles;
 
 rename($src,$dst)
 	or die "rename failed: $!";
 
-system("git-update-cache","--remove","--",@srcfiles);
-system("git-update-cache","--add","--",@dstfiles);
+my $rc = system("git-update-cache","--add","--",@dstfiles);
+die "git-update-cache failed to add new name with code $?\n" if $rc;
+
+$rc = system("git-update-cache","--remove","--",@srcfiles);
+die "git-update-cache failed to remove old name with code $?\n" if $rc;
 
 
 sub usage($) {