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 my $subdir_prefix = `git rev-parse --show-prefix`;
66 chomp($subdir_prefix);
68 # run in git base directory, so that git-ls-files lists all revisioned files
71 # normalize paths, needed to compare against versioned files and update-index
72 # also, this is nicer to end-users by doing ".//a/./b/.//./c" ==> "a/b/c"
73 for (@srcArgs, @dstArgs) {
74 # prepend git prefix as we run from base directory
75 $_ = $subdir_prefix.$_;
77 s|/\./|/| while (m|/\./|);
79 # Also "a/b/../c" ==> "a/c"
80 1 while (s,(^|/)[^/]+/\.\./,$1,);
83 my (@allfiles,@srcfiles,@dstfiles);
85 my (%overwritten, %srcForDst);
88 open(F, 'git-ls-files -z |')
89 or die "Failed to open pipe from git-ls-files: " . $!;
91 @allfiles = map { chomp; $_; } <F>;
96 while(scalar @srcArgs > 0) {
97 $src = shift @srcArgs;
98 $dst = shift @dstArgs;
102 # Be nicer to end-users by doing ".//a/./b/.//./c" ==> "a/b/c"
104 s|/\./|/| while (m|/\./|);
106 # Also "a/b/../c" ==> "a/c"
107 1 while (s,(^|/)[^/]+/\.\./,$1,);
111 print "Checking rename of '$src' to '$dst'\n";
114 unless (-f $src || -l $src || -d $src) {
115 $bad = "bad source '$src'";
118 $safesrc = quotemeta($src);
119 @srcfiles = grep /^$safesrc(\/|$)/, @allfiles;
121 $overwritten{$dst} = 0;
122 if (($bad eq "") && -e $dst) {
123 $bad = "destination '$dst' already exists";
125 # only files can overwrite each other: check both source and destination
126 if (-f $dst && (scalar @srcfiles == 1)) {
127 print "Warning: $bad; will overwrite!\n";
129 $overwritten{$dst} = 1;
132 $bad = "Can not overwrite '$src' with '$dst'";
137 if (($bad eq "") && ($dst =~ /^$safesrc\//)) {
138 $bad = "can not move directory '$src' into itself";
142 if (scalar @srcfiles == 0) {
143 $bad = "'$src' not under version control";
148 if (defined $srcForDst{$dst}) {
149 $bad = "can not move '$src' to '$dst'; already target of ";
150 $bad .= "'".$srcForDst{$dst}."'";
153 $srcForDst{$dst} = $src;
159 print "Warning: $bad; skipping\n";
162 print "Error: $bad\n";
169 # Final pass: rename/move
170 my (@deletedfiles,@addedfiles,@changedfiles);
172 while(scalar @srcs > 0) {
176 if ($opt_n || $opt_v) { print "Renaming $src to $dst\n"; }
178 if (!rename($src,$dst)) {
179 $bad = "renaming '$src' failed: $!";
181 print "Warning: skipped: $bad\n";
189 $safesrc = quotemeta($src);
190 @srcfiles = grep /^$safesrc(\/|$)/, @allfiles;
191 @dstfiles = @srcfiles;
192 s/^$safesrc(\/|$)/$dst$1/ for @dstfiles;
194 push @deletedfiles, @srcfiles;
195 if (scalar @srcfiles == 1) {
196 # $dst can be a directory with 1 file inside
197 if ($overwritten{$dst} ==1) {
198 push @changedfiles, $dstfiles[0];
201 push @addedfiles, $dstfiles[0];
205 push @addedfiles, @dstfiles;
211 print "Changed : ". join(", ", @changedfiles) ."\n";
214 print "Adding : ". join(", ", @addedfiles) ."\n";
217 print "Deleting : ". join(", ", @deletedfiles) ."\n";
222 open(H, "| git-update-index -z --stdin")
223 or die "git-update-index failed to update changed files with code $!\n";
224 foreach my $fileName (@changedfiles) {
225 print H "$fileName\0";
230 open(H, "| git-update-index --add -z --stdin")
231 or die "git-update-index failed to add new names with code $!\n";
232 foreach my $fileName (@addedfiles) {
233 print H "$fileName\0";
238 open(H, "| git-update-index --remove -z --stdin")
239 or die "git-update-index failed to remove old names with code $!\n";
240 foreach my $fileName (@deletedfiles) {
241 print H "$fileName\0";
248 print "Error: $bad\n";