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 $repo = Git
->repository();
29 my (@srcArgs, @dstArgs, @srcs, @dsts);
30 my ($src, $dst, $base, $dstDir);
32 # remove any trailing slash in arguments
33 for (@ARGV) { s/\/*$//; }
35 my $argCount = scalar @ARGV;
36 if (-d
$ARGV[$argCount-1]) {
37 $dstDir = $ARGV[$argCount-1];
38 @srcArgs = @ARGV[0..$argCount-2];
40 foreach $src (@srcArgs) {
43 $dst = "$dstDir/". $base;
49 print "Error: need at least two arguments\n";
53 print "Error: moving to directory '"
55 . "' not possible; not existing\n";
58 @srcArgs = ($ARGV[0]);
59 @dstArgs = ($ARGV[1]);
63 my $subdir_prefix = $repo->wc_subdir();
65 # run in git base directory, so that git-ls-files lists all revisioned files
66 chdir $repo->wc_path();
69 # normalize paths, needed to compare against versioned files and update-index
70 # also, this is nicer to end-users by doing ".//a/./b/.//./c" ==> "a/b/c"
71 for (@srcArgs, @dstArgs) {
72 # prepend git prefix as we run from base directory
73 $_ = $subdir_prefix.$_;
75 s
|/\./|/| while (m|/\
./|);
77 # Also "a/b/../c" ==> "a/c"
78 1 while (s
,(^|/)[^/]+/\.\./,$1,);
81 my (@allfiles,@srcfiles,@dstfiles);
83 my (%overwritten, %srcForDst);
87 @allfiles = $repo->command('ls-files', '-z');
92 while(scalar @srcArgs > 0) {
93 $src = shift @srcArgs;
94 $dst = shift @dstArgs;
98 # Be nicer to end-users by doing ".//a/./b/.//./c" ==> "a/b/c"
100 s
|/\./|/| while (m|/\
./|);
102 # Also "a/b/../c" ==> "a/c"
103 1 while (s
,(^|/)[^/]+/\.\./,$1,);
107 print "Checking rename of '$src' to '$dst'\n";
110 unless (-f
$src || -l
$src || -d
$src) {
111 $bad = "bad source '$src'";
114 $safesrc = quotemeta($src);
115 @srcfiles = grep /^$safesrc(\/|$)/, @allfiles;
117 $overwritten{$dst} = 0;
118 if (($bad eq "") && -e
$dst) {
119 $bad = "destination '$dst' already exists";
121 # only files can overwrite each other: check both source and destination
122 if (-f
$dst && (scalar @srcfiles == 1)) {
123 print "Warning: $bad; will overwrite!\n";
125 $overwritten{$dst} = 1;
128 $bad = "Can not overwrite '$src' with '$dst'";
133 if (($bad eq "") && ($dst =~ /^$safesrc\//)) {
134 $bad = "can not move directory '$src' into itself";
138 if (scalar @srcfiles == 0) {
139 $bad = "'$src' not under version control";
144 if (defined $srcForDst{$dst}) {
145 $bad = "can not move '$src' to '$dst'; already target of ";
146 $bad .= "'".$srcForDst{$dst}."'";
149 $srcForDst{$dst} = $src;
155 print "Warning: $bad; skipping\n";
158 print "Error: $bad\n";
165 # Final pass: rename/move
166 my (@deletedfiles,@addedfiles,@changedfiles);
168 while(scalar @srcs > 0) {
172 if ($opt_n || $opt_v) { print "Renaming $src to $dst\n"; }
174 if (!rename($src,$dst)) {
175 $bad = "renaming '$src' failed: $!";
177 print "Warning: skipped: $bad\n";
185 $safesrc = quotemeta($src);
186 @srcfiles = grep /^$safesrc(\/|$)/, @allfiles;
187 @dstfiles = @srcfiles;
188 s/^$safesrc(\/|$)/$dst$1/ for @dstfiles;
190 push @deletedfiles, @srcfiles;
191 if (scalar @srcfiles == 1) {
192 # $dst can be a directory with 1 file inside
193 if ($overwritten{$dst} ==1) {
194 push @changedfiles, $dstfiles[0];
197 push @addedfiles, $dstfiles[0];
201 push @addedfiles, @dstfiles;
207 print "Changed : ". join(", ", @changedfiles) ."\n";
210 print "Adding : ". join(", ", @addedfiles) ."\n";
213 print "Deleting : ". join(", ", @deletedfiles) ."\n";
218 my ($fd, $ctx) = $repo->command_input_pipe('update-index', '-z', '--stdin');
219 foreach my $fileName (@changedfiles) {
220 print $fd "$fileName\0";
222 git_cmd_try
{ $repo->command_close_pipe($fd, $ctx); }
223 'git-update-index failed to update changed files with code %d';
226 my ($fd, $ctx) = $repo->command_input_pipe('update-index', '--add', '-z', '--stdin');
227 foreach my $fileName (@addedfiles) {
228 print $fd "$fileName\0";
230 git_cmd_try
{ $repo->command_close_pipe($fd, $ctx); }
231 'git-update-index failed to add new files with code %d';
234 my ($fd, $ctx) = $repo->command_input_pipe('update-index', '--remove', '-z', '--stdin');
235 foreach my $fileName (@deletedfiles) {
236 print $fd "$fileName\0";
238 git_cmd_try
{ $repo->command_close_pipe($fd, $ctx); }
239 'git-update-index failed to remove old files with code %d';
244 print "Error: $bad\n";