threads / patch / 1332

patchAdd support for directories to git-rename-script.

Subject: [PATCH] Add support for directories to git-rename-script.

## tl;dr

5 messages between Jul 25, 2005 and Jul 26, 2005. Diffs are folded; open one to read it.

replies: 4people: 2as markdown or json

Ryan Anderson· Jul 25, 2005, 05:26 UTC · lore
Oh, and in the process, rewrite it in Perl.
Signed-off-by: Ryan Anderson <ryan@michonline.com>
---
 git-rename-script |   68 +++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 63 insertions(+), 5 deletions(-)
f46717f9116bba7efb6a10ed99cd2fcea00fe5da
Show changes to git-rename-script +63 −5
diff --git a/git-rename-script b/git-rename-script
--- a/git-rename-script
+++ b/git-rename-script
@@ -1,7 +1,65 @@
-#!/bin/sh
+#!/usr/bin/perl
+#
+# Copyright 2005, Ryan Anderson <ryan@michonline.com>
+#
+# 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);
+}
-- 
Ryan Anderson
  sometimes Pug Majere
Ryan Anderson· Jul 25, 2005, 06:13 UTC · re: Ryan Anderson · lore

[PATCH] Add documentation for git-rename-script

Signed-off-by: Ryan Anderson <ryan@michonline.com>
---
 Documentation/git-rename-script.txt |   34 ++++++++++++++++++++++++++++++++++
 1 files changed, 34 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/git-rename-script.txt
715924203401309ceb2f696e507b8b2c18244d08
Show changes to Documentation/git-rename-script.txt +34 −0
diff --git a/Documentation/git-rename-script.txt b/Documentation/git-rename-script.txt
new file mode 100644
--- /dev/null
+++ b/Documentation/git-rename-script.txt
@@ -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 <torvalds@osdl.org>
+Rewritten by Ryan Anderson <ryan@michonline.com>
+
+Documentation
+--------------
+Documentation by David Greaves, Junio C Hamano and the git-list <git@vger.kernel.org>.
+
+GIT
+---
+Part of the link:git.html[git] suite
+
Junio C Hamano· Jul 25, 2005, 06:21 UTC · re: Ryan Anderson · lore

Re: [PATCH] Add support for directories to git-rename-script.

Ryan Anderson <ryan@michonline.com> writes:
Show 5 quoted lines
> +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>;
> +@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/; }
> +rename($src,$dst)
> +	or die "rename failed: $!";
Making sure leading directories for $dst exists, perhaps?
Otherwise looks good.
> +sub usage($) {

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

Ryan Anderson· Jul 26, 2005, 06:56 UTC · re: Junio C Hamano · lore

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

Also, restore support for the GIT_DIR
Signed-off-by: Ryan Anderson <ryan@michonline.com>
---
 git-rename-script |   26 ++++++++++----------------
 1 files changed, 10 insertions(+), 16 deletions(-)
1ed66638c7ce328d882639447b80099f096c2993
Show changes to git-rename-script +10 −16
diff --git a/git-rename-script b/git-rename-script
--- a/git-rename-script
+++ b/git-rename-script
@@ -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($) {
-- 
Ryan Anderson
  sometimes Pug Majere
Ryan Anderson· Jul 26, 2005, 07:40 UTC · re: Ryan Anderson · lore

[PATCH] 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
Signed-off-by: Ryan Anderson <ryan@michonline.com>
---
 git-rename-script |   26 ++++++++++++++++----------
 1 files changed, 16 insertions(+), 10 deletions(-)
28d2bb7cbb38424c4c6879110bf8aff1e3e5ac42
Show changes to git-rename-script +16 −10
diff --git a/git-rename-script b/git-rename-script
--- a/git-rename-script
+++ b/git-rename-script
@@ -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($) {

← back to recent threads