diff options
author | Ryan Anderson <ryan@michonline.com> | 2005-07-25 01:26:47 -0400 |
---|---|---|
committer | Junio C Hamano <junkio@cox.net> | 2005-07-27 22:47:06 -0700 |
commit | d36ac03e457ce7e2caeea9aa2530a218ac0ede43 (patch) | |
tree | 32189547914f14bf18f1e5f2dd1f12198e5a2afc /git-rename-script | |
parent | fb6a3d862137c3f77c387729395a0c558950437b (diff) | |
download | git-d36ac03e457ce7e2caeea9aa2530a218ac0ede43.tar.gz git-d36ac03e457ce7e2caeea9aa2530a218ac0ede43.tar.xz |
[PATCH] Add support for directories to git-rename-script.
Oh, and in the process, rewrite it in Perl.
Signed-off-by: Ryan Anderson <ryan@michonline.com>
Signed-off-by: Junio C Hamano <junkio@cox.net>
Diffstat (limited to 'git-rename-script')
-rwxr-xr-x | git-rename-script | 73 |
1 files changed, 68 insertions, 5 deletions
diff --git a/git-rename-script b/git-rename-script index 3952382db..b4d314808 100755 --- a/git-rename-script +++ b/git-rename-script @@ -1,7 +1,70 @@ -#!/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: +my $GIT_DIR = $ENV{'GIT_DIR'} || ".git"; + +unless ( -d $GIT_DIR && -d $GIT_DIR . "/objects" && + -d $GIT_DIR . "/objects/00" && -d $GIT_DIR . "/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); + +$/ = "\0"; +open(F,"-|","git-ls-files","-z") + or die "Failed to open pipe from git-ls-files: " . $!; + +@allfiles = map { chomp; $_; } <F>; +close(F); + +my $safesrc = quotemeta($src); +@srcfiles = grep /^$safesrc/, @allfiles; +@dstfiles = @srcfiles; +s#^$safesrc(/|$)#$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; + + +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); +} |