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.
|
|
|
|
|
2006-06-28 12:17:07 +02:00
|
|
|
BEGIN { unshift @INC, '@@INSTLIBDIR@@'; }
|
2005-10-23 18:15:34 +02:00
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
use Getopt::Std;
|
2006-06-24 04:34:53 +02:00
|
|
|
use Git;
|
2005-10-23 18:15:34 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
our ($opt_n, $opt_f, $opt_h, $opt_k, $opt_v);
|
|
|
|
getopts("hnfkv") || usage;
|
|
|
|
usage() if $opt_h;
|
|
|
|
@ARGV >= 1 or usage;
|
|
|
|
|
2006-06-24 04:34:53 +02:00
|
|
|
my $repo = Git->repository();
|
2006-03-01 18:16:36 +01:00
|
|
|
|
2005-10-23 18:15:34 +02:00
|
|
|
my (@srcArgs, @dstArgs, @srcs, @dsts);
|
|
|
|
my ($src, $dst, $base, $dstDir);
|
|
|
|
|
git-mv: fixes for path handling
Moving a directory ending in a slash was not working as the
destination was not calculated correctly.
E.g. in the git repo,
git-mv t/ Documentation
gave the error
Error: destination 'Documentation' already exists
To get rid of this problem, strip trailing slashes from all arguments.
The comment in cg-mv made me curious about this issue; Pasky, thanks!
As result, the workaround in cg-mv is not needed any more.
Also, another bug was shown by cg-mv. When moving files outside of
a subdirectory, it typically calls git-mv with something like
git-mv Documentation/git.txt Documentation/../git-mv.txt
which triggers the following error from git-update-index:
Ignoring path Documentation/../git-mv.txt
The result is a moved file, removed from git revisioning, but not
added again. To fix this, the paths have to be normalized not have ".."
in the middle. This was already done in git-mv, but only for
a better visual appearance :(
Signed-off-by: Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-03-01 19:09:23 +01:00
|
|
|
# remove any trailing slash in arguments
|
|
|
|
for (@ARGV) { s/\/*$//; }
|
|
|
|
|
2005-10-23 18:15:34 +02:00
|
|
|
my $argCount = scalar @ARGV;
|
|
|
|
if (-d $ARGV[$argCount-1]) {
|
|
|
|
$dstDir = $ARGV[$argCount-1];
|
|
|
|
@srcArgs = @ARGV[0..$argCount-2];
|
git-mv: fixes for path handling
Moving a directory ending in a slash was not working as the
destination was not calculated correctly.
E.g. in the git repo,
git-mv t/ Documentation
gave the error
Error: destination 'Documentation' already exists
To get rid of this problem, strip trailing slashes from all arguments.
The comment in cg-mv made me curious about this issue; Pasky, thanks!
As result, the workaround in cg-mv is not needed any more.
Also, another bug was shown by cg-mv. When moving files outside of
a subdirectory, it typically calls git-mv with something like
git-mv Documentation/git.txt Documentation/../git-mv.txt
which triggers the following error from git-update-index:
Ignoring path Documentation/../git-mv.txt
The result is a moved file, removed from git revisioning, but not
added again. To fix this, the paths have to be normalized not have ".."
in the middle. This was already done in git-mv, but only for
a better visual appearance :(
Signed-off-by: Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-03-01 19:09:23 +01:00
|
|
|
|
2005-10-23 18:15:34 +02:00
|
|
|
foreach $src (@srcArgs) {
|
|
|
|
$base = $src;
|
|
|
|
$base =~ s/^.*\///;
|
|
|
|
$dst = "$dstDir/". $base;
|
|
|
|
push @dstArgs, $dst;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2006-03-01 18:16:36 +01:00
|
|
|
if ($argCount < 2) {
|
|
|
|
print "Error: need at least two arguments\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if ($argCount > 2) {
|
2005-10-23 18:15:34 +02:00
|
|
|
print "Error: moving to directory '"
|
|
|
|
. $ARGV[$argCount-1]
|
2006-03-01 18:16:36 +01:00
|
|
|
. "' not possible; not existing\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 = "";
|
|
|
|
}
|
|
|
|
|
2006-06-24 04:34:53 +02:00
|
|
|
my $subdir_prefix = $repo->wc_subdir();
|
2006-03-03 17:23:32 +01:00
|
|
|
|
|
|
|
# run in git base directory, so that git-ls-files lists all revisioned files
|
2006-06-24 04:34:53 +02:00
|
|
|
chdir $repo->wc_path();
|
|
|
|
$repo->wc_chdir('');
|
2006-03-03 17:23:32 +01:00
|
|
|
|
git-mv: fixes for path handling
Moving a directory ending in a slash was not working as the
destination was not calculated correctly.
E.g. in the git repo,
git-mv t/ Documentation
gave the error
Error: destination 'Documentation' already exists
To get rid of this problem, strip trailing slashes from all arguments.
The comment in cg-mv made me curious about this issue; Pasky, thanks!
As result, the workaround in cg-mv is not needed any more.
Also, another bug was shown by cg-mv. When moving files outside of
a subdirectory, it typically calls git-mv with something like
git-mv Documentation/git.txt Documentation/../git-mv.txt
which triggers the following error from git-update-index:
Ignoring path Documentation/../git-mv.txt
The result is a moved file, removed from git revisioning, but not
added again. To fix this, the paths have to be normalized not have ".."
in the middle. This was already done in git-mv, but only for
a better visual appearance :(
Signed-off-by: Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-03-01 19:09:23 +01:00
|
|
|
# normalize paths, needed to compare against versioned files and update-index
|
|
|
|
# also, this is nicer to end-users by doing ".//a/./b/.//./c" ==> "a/b/c"
|
|
|
|
for (@srcArgs, @dstArgs) {
|
2006-03-03 17:23:32 +01:00
|
|
|
# prepend git prefix as we run from base directory
|
|
|
|
$_ = $subdir_prefix.$_;
|
git-mv: fixes for path handling
Moving a directory ending in a slash was not working as the
destination was not calculated correctly.
E.g. in the git repo,
git-mv t/ Documentation
gave the error
Error: destination 'Documentation' already exists
To get rid of this problem, strip trailing slashes from all arguments.
The comment in cg-mv made me curious about this issue; Pasky, thanks!
As result, the workaround in cg-mv is not needed any more.
Also, another bug was shown by cg-mv. When moving files outside of
a subdirectory, it typically calls git-mv with something like
git-mv Documentation/git.txt Documentation/../git-mv.txt
which triggers the following error from git-update-index:
Ignoring path Documentation/../git-mv.txt
The result is a moved file, removed from git revisioning, but not
added again. To fix this, the paths have to be normalized not have ".."
in the middle. This was already done in git-mv, but only for
a better visual appearance :(
Signed-off-by: Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-03-01 19:09:23 +01:00
|
|
|
s|^\./||;
|
|
|
|
s|/\./|/| while (m|/\./|);
|
|
|
|
s|//+|/|g;
|
|
|
|
# Also "a/b/../c" ==> "a/c"
|
|
|
|
1 while (s,(^|/)[^/]+/\.\./,$1,);
|
|
|
|
}
|
|
|
|
|
2005-10-23 18:15:34 +02:00
|
|
|
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
|
|
|
|
2006-06-24 04:34:53 +02:00
|
|
|
{
|
|
|
|
local $/ = "\0";
|
|
|
|
@allfiles = $repo->command('ls-files', '-z');
|
|
|
|
}
|
2005-10-23 18:15:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
my ($i, $bad);
|
|
|
|
while(scalar @srcArgs > 0) {
|
|
|
|
$src = shift @srcArgs;
|
|
|
|
$dst = shift @dstArgs;
|
|
|
|
$bad = "";
|
|
|
|
|
2006-02-19 08:42:03 +01:00
|
|
|
for ($src, $dst) {
|
|
|
|
# Be nicer to end-users by doing ".//a/./b/.//./c" ==> "a/b/c"
|
|
|
|
s|^\./||;
|
|
|
|
s|/\./|/| while (m|/\./|);
|
|
|
|
s|//+|/|g;
|
|
|
|
# Also "a/b/../c" ==> "a/c"
|
|
|
|
1 while (s,(^|/)[^/]+/\.\./,$1,);
|
|
|
|
}
|
|
|
|
|
2005-10-23 18:15:34 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-28 11:54:05 +01:00
|
|
|
if (($bad eq "") && ($dst =~ /^$safesrc\//)) {
|
2005-10-23 18:15:34 +02:00
|
|
|
$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: $!";
|
2005-11-27 22:11:33 +01:00
|
|
|
if ($opt_k) {
|
|
|
|
print "Warning: skipped: $bad\n";
|
|
|
|
$bad = "";
|
|
|
|
next;
|
|
|
|
}
|
2005-11-27 22:04:14 +01:00
|
|
|
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) {
|
2006-06-24 04:34:53 +02:00
|
|
|
my ($fd, $ctx) = $repo->command_input_pipe('update-index', '-z', '--stdin');
|
2005-11-23 11:19:41 +01:00
|
|
|
foreach my $fileName (@changedfiles) {
|
2006-06-24 04:34:53 +02:00
|
|
|
print $fd "$fileName\0";
|
2005-11-23 11:19:41 +01:00
|
|
|
}
|
2006-06-24 04:34:53 +02:00
|
|
|
git_cmd_try { $repo->command_close_pipe($fd, $ctx); }
|
|
|
|
'git-update-index failed to update changed files with code %d';
|
2005-11-27 21:58:52 +01:00
|
|
|
}
|
|
|
|
if (@addedfiles) {
|
2006-06-24 04:34:53 +02:00
|
|
|
my ($fd, $ctx) = $repo->command_input_pipe('update-index', '--add', '-z', '--stdin');
|
2005-11-23 11:19:41 +01:00
|
|
|
foreach my $fileName (@addedfiles) {
|
2006-06-24 04:34:53 +02:00
|
|
|
print $fd "$fileName\0";
|
2005-11-23 11:19:41 +01:00
|
|
|
}
|
2006-06-24 04:34:53 +02:00
|
|
|
git_cmd_try { $repo->command_close_pipe($fd, $ctx); }
|
|
|
|
'git-update-index failed to add new files with code %d';
|
2005-11-27 21:58:52 +01:00
|
|
|
}
|
|
|
|
if (@deletedfiles) {
|
2006-06-24 04:34:53 +02:00
|
|
|
my ($fd, $ctx) = $repo->command_input_pipe('update-index', '--remove', '-z', '--stdin');
|
2005-11-23 11:19:41 +01:00
|
|
|
foreach my $fileName (@deletedfiles) {
|
2006-06-24 04:34:53 +02:00
|
|
|
print $fd "$fileName\0";
|
2005-11-23 11:19:41 +01:00
|
|
|
}
|
2006-06-24 04:34:53 +02:00
|
|
|
git_cmd_try { $repo->command_close_pipe($fd, $ctx); }
|
|
|
|
'git-update-index failed to remove old files with code %d';
|
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);
|
|
|
|
}
|