git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Add support for directories to git-rename-script.
@ 2005-07-25  5:26 Ryan Anderson
  2005-07-25  6:13 ` [PATCH] Add documentation for git-rename-script Ryan Anderson
  2005-07-25  6:21 ` [PATCH] Add support for directories to git-rename-script Junio C Hamano
  0 siblings, 2 replies; 5+ messages in thread
From: Ryan Anderson @ 2005-07-25  5:26 UTC (permalink / raw)
  To: Linus Torvalds; +Cc: git


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
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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH] Add documentation for git-rename-script
  2005-07-25  5:26 [PATCH] Add support for directories to git-rename-script Ryan Anderson
@ 2005-07-25  6:13 ` Ryan Anderson
  2005-07-25  6:21 ` [PATCH] Add support for directories to git-rename-script Junio C Hamano
  1 sibling, 0 replies; 5+ messages in thread
From: Ryan Anderson @ 2005-07-25  6:13 UTC (permalink / raw)
  To: Linus Torvalds; +Cc: git


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
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
+

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Add support for directories to git-rename-script.
  2005-07-25  5:26 [PATCH] Add support for directories to git-rename-script Ryan Anderson
  2005-07-25  6:13 ` [PATCH] Add documentation for git-rename-script Ryan Anderson
@ 2005-07-25  6:21 ` Junio C Hamano
  2005-07-26  6:56   ` [PATCH] Make git-rename-script behave much better when faced with input contain Perl regular expression metacharacters Ryan Anderson
  1 sibling, 1 reply; 5+ messages in thread
From: Junio C Hamano @ 2005-07-25  6:21 UTC (permalink / raw)
  To: Ryan Anderson; +Cc: git

Ryan Anderson <ryan@michonline.com> writes:

> +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?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH] Make git-rename-script behave much better when faced with input contain Perl regular expression metacharacters.
  2005-07-25  6:21 ` [PATCH] Add support for directories to git-rename-script Junio C Hamano
@ 2005-07-26  6:56   ` Ryan Anderson
  2005-07-26  7:40     ` [PATCH] Make git-rename-script behave better with input containing Perl regexp metachars Ryan Anderson
  0 siblings, 1 reply; 5+ messages in thread
From: Ryan Anderson @ 2005-07-26  6:56 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git


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
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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH] Make git-rename-script behave better with input containing Perl regexp metachars.
  2005-07-26  6:56   ` [PATCH] Make git-rename-script behave much better when faced with input contain Perl regular expression metacharacters Ryan Anderson
@ 2005-07-26  7:40     ` Ryan Anderson
  0 siblings, 0 replies; 5+ messages in thread
From: Ryan Anderson @ 2005-07-26  7:40 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

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
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($) {

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2005-07-26  7:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-07-25  5:26 [PATCH] Add support for directories to git-rename-script Ryan Anderson
2005-07-25  6:13 ` [PATCH] Add documentation for git-rename-script Ryan Anderson
2005-07-25  6:21 ` [PATCH] Add support for directories to git-rename-script Junio C Hamano
2005-07-26  6:56   ` [PATCH] Make git-rename-script behave much better when faced with input contain Perl regular expression metacharacters Ryan Anderson
2005-07-26  7:40     ` [PATCH] Make git-rename-script behave better with input containing Perl regexp metachars Ryan Anderson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).