3 # Copyright 2005, Ryan Anderson <ryan@michonline.com>
4 # Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
6 # This file is licensed under the GPL v2, or a later version
7 # at the discretion of Linus Torvalds.
16 $0 [-f] [-n] <source> <destination>
17 $0 [-f] [-n] [-k] <source> ... <destination directory>
22 our ($opt_n, $opt_f, $opt_h, $opt_k, $opt_v);
23 getopts
("hnfkv") || usage
;
27 my $GIT_DIR = `git rev-parse --git-dir`;
28 exit 1 if $?
; # rev-parse would have given "not a git dir" message.
31 my (@srcArgs, @dstArgs, @srcs, @dsts);
32 my ($src, $dst, $base, $dstDir);
34 my $argCount = scalar @ARGV;
35 if (-d
$ARGV[$argCount-1]) {
36 $dstDir = $ARGV[$argCount-1];
37 # remove any trailing slash
39 @srcArgs = @ARGV[0..$argCount-2];
41 foreach $src (@srcArgs) {
44 $dst = "$dstDir/". $base;
50 print "Error: need at least two arguments\n";
54 print "Error: moving to directory '"
56 . "' not possible; not existing\n";
59 @srcArgs = ($ARGV[0]);
60 @dstArgs = ($ARGV[1]);
64 my (@allfiles,@srcfiles,@dstfiles);
66 my (%overwritten, %srcForDst);
69 open(F
, 'git-ls-files -z |')
70 or die "Failed to open pipe from git-ls-files: " . $!;
72 @allfiles = map { chomp; $_; } <F
>;
77 while(scalar @srcArgs > 0) {
78 $src = shift @srcArgs;
79 $dst = shift @dstArgs;
83 # Be nicer to end-users by doing ".//a/./b/.//./c" ==> "a/b/c"
85 s
|/\./|/| while (m|/\
./|);
87 # Also "a/b/../c" ==> "a/c"
88 1 while (s
,(^|/)[^/]+/\.\./,$1,);
92 print "Checking rename of '$src' to '$dst'\n";
95 unless (-f
$src || -l
$src || -d
$src) {
96 $bad = "bad source '$src'";
99 $safesrc = quotemeta($src);
100 @srcfiles = grep /^$safesrc(\/|$)/, @allfiles;
102 $overwritten{$dst} = 0;
103 if (($bad eq "") && -e
$dst) {
104 $bad = "destination '$dst' already exists";
106 # only files can overwrite each other: check both source and destination
107 if (-f
$dst && (scalar @srcfiles == 1)) {
108 print "Warning: $bad; will overwrite!\n";
110 $overwritten{$dst} = 1;
113 $bad = "Can not overwrite '$src' with '$dst'";
118 if (($bad eq "") && ($dst =~ /^$safesrc\//)) {
119 $bad = "can not move directory '$src' into itself";
123 if (scalar @srcfiles == 0) {
124 $bad = "'$src' not under version control";
129 if (defined $srcForDst{$dst}) {
130 $bad = "can not move '$src' to '$dst'; already target of ";
131 $bad .= "'".$srcForDst{$dst}."'";
134 $srcForDst{$dst} = $src;
140 print "Warning: $bad; skipping\n";
143 print "Error: $bad\n";
150 # Final pass: rename/move
151 my (@deletedfiles,@addedfiles,@changedfiles);
153 while(scalar @srcs > 0) {
157 if ($opt_n || $opt_v) { print "Renaming $src to $dst\n"; }
159 if (!rename($src,$dst)) {
160 $bad = "renaming '$src' failed: $!";
162 print "Warning: skipped: $bad\n";
170 $safesrc = quotemeta($src);
171 @srcfiles = grep /^$safesrc(\/|$)/, @allfiles;
172 @dstfiles = @srcfiles;
173 s/^$safesrc(\/|$)/$dst$1/ for @dstfiles;
175 push @deletedfiles, @srcfiles;
176 if (scalar @srcfiles == 1) {
177 # $dst can be a directory with 1 file inside
178 if ($overwritten{$dst} ==1) {
179 push @changedfiles, $dstfiles[0];
182 push @addedfiles, $dstfiles[0];
186 push @addedfiles, @dstfiles;
192 print "Changed : ". join(", ", @changedfiles) ."\n";
195 print "Adding : ". join(", ", @addedfiles) ."\n";
198 print "Deleting : ". join(", ", @deletedfiles) ."\n";
203 open(H
, "| git-update-index -z --stdin")
204 or die "git-update-index failed to update changed files with code $!\n";
205 foreach my $fileName (@changedfiles) {
206 print H
"$fileName\0";
211 open(H
, "| git-update-index --add -z --stdin")
212 or die "git-update-index failed to add new names with code $!\n";
213 foreach my $fileName (@addedfiles) {
214 print H
"$fileName\0";
219 open(H
, "| git-update-index --remove -z --stdin")
220 or die "git-update-index failed to remove old names with code $!\n";
221 foreach my $fileName (@deletedfiles) {
222 print H
"$fileName\0";
229 print "Error: $bad\n";