2005-10-23 18:15:34 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
|
|
|
# Copyright 2005, Ryan Anderson <ryan@michonline.com>
|
|
|
|
# Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
|
|
|
|
#
|
|
|
|
# This file is licensed under the GPL v2, or a later version
|
|
|
|
# at the discretion of Linus Torvalds.
|
|
|
|
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
use Getopt::Std;
|
|
|
|
|
|
|
|
sub usage() {
|
|
|
|
print <<EOT;
|
2005-11-27 21:58:52 +01:00
|
|
|
$0 [-f] [-n] <source> <destination>
|
|
|
|
$0 [-f] [-n] [-k] <source> ... <destination directory>
|
2005-10-23 18:15:34 +02:00
|
|
|
EOT
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Sanity checks:
|
|
|
|
my $GIT_DIR = $ENV{'GIT_DIR'} || ".git";
|
|
|
|
|
|
|
|
unless ( -d $GIT_DIR && -d $GIT_DIR . "/objects" &&
|
|
|
|
-d $GIT_DIR . "/objects/" && -d $GIT_DIR . "/refs") {
|
2005-11-27 21:58:52 +01:00
|
|
|
print "Error: git repository not found.";
|
|
|
|
exit(1);
|
2005-10-23 18:15:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
our ($opt_n, $opt_f, $opt_h, $opt_k, $opt_v);
|
|
|
|
getopts("hnfkv") || usage;
|
|
|
|
usage() if $opt_h;
|
|
|
|
@ARGV >= 1 or usage;
|
|
|
|
|
|
|
|
my (@srcArgs, @dstArgs, @srcs, @dsts);
|
|
|
|
my ($src, $dst, $base, $dstDir);
|
|
|
|
|
|
|
|
my $argCount = scalar @ARGV;
|
|
|
|
if (-d $ARGV[$argCount-1]) {
|
|
|
|
$dstDir = $ARGV[$argCount-1];
|
2005-11-01 23:46:27 +01:00
|
|
|
# remove any trailing slash
|
|
|
|
$dstDir =~ s/\/$//;
|
2005-10-23 18:15:34 +02:00
|
|
|
@srcArgs = @ARGV[0..$argCount-2];
|
|
|
|
|
|
|
|
foreach $src (@srcArgs) {
|
|
|
|
$base = $src;
|
|
|
|
$base =~ s/^.*\///;
|
|
|
|
$dst = "$dstDir/". $base;
|
|
|
|
push @dstArgs, $dst;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ($argCount != 2) {
|
|
|
|
print "Error: moving to directory '"
|
|
|
|
. $ARGV[$argCount-1]
|
|
|
|
. "' not possible; not exisiting\n";
|
2005-11-27 21:58:52 +01:00
|
|
|
exit(1);
|
2005-10-23 18:15:34 +02:00
|
|
|
}
|
|
|
|
@srcArgs = ($ARGV[0]);
|
|
|
|
@dstArgs = ($ARGV[1]);
|
|
|
|
$dstDir = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
my (@allfiles,@srcfiles,@dstfiles);
|
|
|
|
my $safesrc;
|
2005-10-25 14:20:45 +02:00
|
|
|
my (%overwritten, %srcForDst);
|
2005-10-23 18:15:34 +02:00
|
|
|
|
|
|
|
$/ = "\0";
|
|
|
|
open(F,"-|","git-ls-files","-z")
|
|
|
|
or die "Failed to open pipe from git-ls-files: " . $!;
|
|
|
|
|
|
|
|
@allfiles = map { chomp; $_; } <F>;
|
|
|
|
close(F);
|
|
|
|
|
|
|
|
|
|
|
|
my ($i, $bad);
|
|
|
|
while(scalar @srcArgs > 0) {
|
|
|
|
$src = shift @srcArgs;
|
|
|
|
$dst = shift @dstArgs;
|
|
|
|
$bad = "";
|
|
|
|
|
|
|
|
if ($opt_v) {
|
|
|
|
print "Checking rename of '$src' to '$dst'\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
unless (-f $src || -l $src || -d $src) {
|
|
|
|
$bad = "bad source '$src'";
|
|
|
|
}
|
|
|
|
|
2005-11-23 12:04:23 +01:00
|
|
|
$safesrc = quotemeta($src);
|
|
|
|
@srcfiles = grep /^$safesrc(\/|$)/, @allfiles;
|
|
|
|
|
2005-10-23 18:15:34 +02:00
|
|
|
$overwritten{$dst} = 0;
|
|
|
|
if (($bad eq "") && -e $dst) {
|
|
|
|
$bad = "destination '$dst' already exists";
|
2005-11-23 12:04:23 +01:00
|
|
|
if ($opt_f) {
|
|
|
|
# only files can overwrite each other: check both source and destination
|
|
|
|
if (-f $dst && (scalar @srcfiles == 1)) {
|
|
|
|
print "Warning: $bad; will overwrite!\n";
|
|
|
|
$bad = "";
|
|
|
|
$overwritten{$dst} = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$bad = "Can not overwrite '$src' with '$dst'";
|
|
|
|
}
|
2005-10-23 18:15:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($bad eq "") && ($src eq $dstDir)) {
|
|
|
|
$bad = "can not move directory '$src' into itself";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($bad eq "") {
|
|
|
|
if (scalar @srcfiles == 0) {
|
|
|
|
$bad = "'$src' not under version control";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-25 14:20:45 +02:00
|
|
|
if ($bad eq "") {
|
|
|
|
if (defined $srcForDst{$dst}) {
|
|
|
|
$bad = "can not move '$src' to '$dst'; already target of ";
|
|
|
|
$bad .= "'".$srcForDst{$dst}."'";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$srcForDst{$dst} = $src;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-23 18:15:34 +02:00
|
|
|
if ($bad ne "") {
|
|
|
|
if ($opt_k) {
|
|
|
|
print "Warning: $bad; skipping\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
print "Error: $bad\n";
|
2005-11-27 21:58:52 +01:00
|
|
|
exit(1);
|
2005-10-23 18:15:34 +02:00
|
|
|
}
|
|
|
|
push @srcs, $src;
|
|
|
|
push @dsts, $dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Final pass: rename/move
|
|
|
|
my (@deletedfiles,@addedfiles,@changedfiles);
|
2005-11-27 22:04:14 +01:00
|
|
|
$bad = "";
|
2005-10-23 18:15:34 +02:00
|
|
|
while(scalar @srcs > 0) {
|
|
|
|
$src = shift @srcs;
|
|
|
|
$dst = shift @dsts;
|
|
|
|
|
|
|
|
if ($opt_n || $opt_v) { print "Renaming $src to $dst\n"; }
|
|
|
|
if (!$opt_n) {
|
2005-11-27 22:04:14 +01:00
|
|
|
if (!rename($src,$dst)) {
|
|
|
|
$bad = "renaming '$src' failed: $!";
|
|
|
|
last;
|
|
|
|
}
|
2005-10-23 18:15:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$safesrc = quotemeta($src);
|
|
|
|
@srcfiles = grep /^$safesrc(\/|$)/, @allfiles;
|
|
|
|
@dstfiles = @srcfiles;
|
|
|
|
s/^$safesrc(\/|$)/$dst$1/ for @dstfiles;
|
|
|
|
|
|
|
|
push @deletedfiles, @srcfiles;
|
|
|
|
if (scalar @srcfiles == 1) {
|
2005-11-23 12:04:23 +01:00
|
|
|
# $dst can be a directory with 1 file inside
|
2005-10-23 18:15:34 +02:00
|
|
|
if ($overwritten{$dst} ==1) {
|
2005-11-23 12:04:23 +01:00
|
|
|
push @changedfiles, $dstfiles[0];
|
|
|
|
|
2005-10-23 18:15:34 +02:00
|
|
|
} else {
|
2005-11-23 12:04:23 +01:00
|
|
|
push @addedfiles, $dstfiles[0];
|
2005-10-23 18:15:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
push @addedfiles, @dstfiles;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($opt_n) {
|
2005-11-27 21:58:52 +01:00
|
|
|
if (@changedfiles) {
|
2005-10-23 18:15:34 +02:00
|
|
|
print "Changed : ". join(", ", @changedfiles) ."\n";
|
2005-11-27 21:58:52 +01:00
|
|
|
}
|
|
|
|
if (@addedfiles) {
|
2005-10-23 18:15:34 +02:00
|
|
|
print "Adding : ". join(", ", @addedfiles) ."\n";
|
2005-11-27 21:58:52 +01:00
|
|
|
}
|
|
|
|
if (@deletedfiles) {
|
2005-10-23 18:15:34 +02:00
|
|
|
print "Deleting : ". join(", ", @deletedfiles) ."\n";
|
2005-11-27 21:58:52 +01:00
|
|
|
}
|
2005-10-23 18:15:34 +02:00
|
|
|
}
|
2005-11-27 21:58:52 +01:00
|
|
|
else {
|
|
|
|
if (@changedfiles) {
|
2005-11-23 11:19:41 +01:00
|
|
|
open(H, "| git-update-index -z --stdin")
|
|
|
|
or die "git-update-index failed to update changed files with code $!\n";
|
|
|
|
foreach my $fileName (@changedfiles) {
|
|
|
|
print H "$fileName\0";
|
|
|
|
}
|
|
|
|
close(H);
|
2005-11-27 21:58:52 +01:00
|
|
|
}
|
|
|
|
if (@addedfiles) {
|
2005-11-23 11:19:41 +01:00
|
|
|
open(H, "| git-update-index --add -z --stdin")
|
|
|
|
or die "git-update-index failed to add new names with code $!\n";
|
|
|
|
foreach my $fileName (@addedfiles) {
|
|
|
|
print H "$fileName\0";
|
|
|
|
}
|
|
|
|
close(H);
|
2005-11-27 21:58:52 +01:00
|
|
|
}
|
|
|
|
if (@deletedfiles) {
|
2005-11-23 11:19:41 +01:00
|
|
|
open(H, "| git-update-index --remove -z --stdin")
|
|
|
|
or die "git-update-index failed to remove old names with code $!\n";
|
|
|
|
foreach my $fileName (@deletedfiles) {
|
|
|
|
print H "$fileName\0";
|
|
|
|
}
|
|
|
|
close(H);
|
2005-11-27 21:58:52 +01:00
|
|
|
}
|
2005-10-23 18:15:34 +02:00
|
|
|
}
|
2005-11-27 22:04:14 +01:00
|
|
|
|
|
|
|
if ($bad ne "") {
|
|
|
|
print "Error: $bad\n";
|
|
|
|
exit(1);
|
|
|
|
}
|