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 # remove any trailing slash in arguments
35 for (@ARGV) { s/\/*$//; }
37 my $argCount = scalar @ARGV;
38 if (-d
$ARGV[$argCount-1]) {
39 $dstDir = $ARGV[$argCount-1];
40 @srcArgs = @ARGV[0..$argCount-2];
42 foreach $src (@srcArgs) {
45 $dst = "$dstDir/". $base;
51 print "Error: need at least two arguments\n";
55 print "Error: moving to directory '"
57 . "' not possible; not existing\n";
60 @srcArgs = ($ARGV[0]);
61 @dstArgs = ($ARGV[1]);
65 # normalize paths, needed to compare against versioned files and update-index
66 # also, this is nicer to end-users by doing ".//a/./b/.//./c" ==> "a/b/c"
67 for (@srcArgs, @dstArgs) {
69 s
|/\./|/| while (m|/\
./|);
71 # Also "a/b/../c" ==> "a/c"
72 1 while (s
,(^|/)[^/]+/\.\./,$1,);
75 my (@allfiles,@srcfiles,@dstfiles);
77 my (%overwritten, %srcForDst);
80 open(F
, 'git-ls-files -z |')
81 or die "Failed to open pipe from git-ls-files: " . $!;
83 @allfiles = map { chomp; $_; } <F
>;
88 while(scalar @srcArgs > 0) {
89 $src = shift @srcArgs;
90 $dst = shift @dstArgs;
94 # Be nicer to end-users by doing ".//a/./b/.//./c" ==> "a/b/c"
96 s
|/\./|/| while (m|/\
./|);
98 # Also "a/b/../c" ==> "a/c"
99 1 while (s
,(^|/)[^/]+/\.\./,$1,);
103 print "Checking rename of '$src' to '$dst'\n";
106 unless (-f
$src || -l
$src || -d
$src) {
107 $bad = "bad source '$src'";
110 $safesrc = quotemeta($src);
111 @srcfiles = grep /^$safesrc(\/|$)/, @allfiles;
113 $overwritten{$dst} = 0;
114 if (($bad eq "") && -e
$dst) {
115 $bad = "destination '$dst' already exists";
117 # only files can overwrite each other: check both source and destination
118 if (-f
$dst && (scalar @srcfiles == 1)) {
119 print "Warning: $bad; will overwrite!\n";
121 $overwritten{$dst} = 1;
124 $bad = "Can not overwrite '$src' with '$dst'";
129 if (($bad eq "") && ($dst =~ /^$safesrc\//)) {
130 $bad = "can not move directory '$src' into itself";
134 if (scalar @srcfiles == 0) {
135 $bad = "'$src' not under version control";
140 if (defined $srcForDst{$dst}) {
141 $bad = "can not move '$src' to '$dst'; already target of ";
142 $bad .= "'".$srcForDst{$dst}."'";
145 $srcForDst{$dst} = $src;
151 print "Warning: $bad; skipping\n";
154 print "Error: $bad\n";
161 # Final pass: rename/move
162 my (@deletedfiles,@addedfiles,@changedfiles);
164 while(scalar @srcs > 0) {
168 if ($opt_n || $opt_v) { print "Renaming $src to $dst\n"; }
170 if (!rename($src,$dst)) {
171 $bad = "renaming '$src' failed: $!";
173 print "Warning: skipped: $bad\n";
181 $safesrc = quotemeta($src);
182 @srcfiles = grep /^$safesrc(\/|$)/, @allfiles;
183 @dstfiles = @srcfiles;
184 s/^$safesrc(\/|$)/$dst$1/ for @dstfiles;
186 push @deletedfiles, @srcfiles;
187 if (scalar @srcfiles == 1) {
188 # $dst can be a directory with 1 file inside
189 if ($overwritten{$dst} ==1) {
190 push @changedfiles, $dstfiles[0];
193 push @addedfiles, $dstfiles[0];
197 push @addedfiles, @dstfiles;
203 print "Changed : ". join(", ", @changedfiles) ."\n";
206 print "Adding : ". join(", ", @addedfiles) ."\n";
209 print "Deleting : ". join(", ", @deletedfiles) ."\n";
214 open(H
, "| git-update-index -z --stdin")
215 or die "git-update-index failed to update changed files with code $!\n";
216 foreach my $fileName (@changedfiles) {
217 print H
"$fileName\0";
222 open(H
, "| git-update-index --add -z --stdin")
223 or die "git-update-index failed to add new names with code $!\n";
224 foreach my $fileName (@addedfiles) {
225 print H
"$fileName\0";
230 open(H
, "| git-update-index --remove -z --stdin")
231 or die "git-update-index failed to remove old names with code $!\n";
232 foreach my $fileName (@deletedfiles) {
233 print H
"$fileName\0";
240 print "Error: $bad\n";