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.
17 $0 [-f] [-n] <source> <destination>
18 $0 [-f] [-n] [-k] <source> ... <destination directory>
23 our ($opt_n, $opt_f, $opt_h, $opt_k, $opt_v);
24 getopts
("hnfkv") || usage
;
28 my $repo = Git
->repository();
30 my (@srcArgs, @dstArgs, @srcs, @dsts);
31 my ($src, $dst, $base, $dstDir);
33 # remove any trailing slash in arguments
34 for (@ARGV) { s/\/*$//; }
36 my $argCount = scalar @ARGV;
37 if (-d
$ARGV[$argCount-1]) {
38 $dstDir = $ARGV[$argCount-1];
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 $subdir_prefix = $repo->wc_subdir();
66 # run in git base directory, so that git-ls-files lists all revisioned files
67 chdir $repo->wc_path();
70 # normalize paths, needed to compare against versioned files and update-index
71 # also, this is nicer to end-users by doing ".//a/./b/.//./c" ==> "a/b/c"
72 for (@srcArgs, @dstArgs) {
73 # prepend git prefix as we run from base directory
74 $_ = $subdir_prefix.$_;
76 s
|/\./|/| while (m|/\
./|);
78 # Also "a/b/../c" ==> "a/c"
79 1 while (s
,(^|/)[^/]+/\.\./,$1,);
82 my (@allfiles,@srcfiles,@dstfiles);
84 my (%overwritten, %srcForDst);
88 @allfiles = $repo->command('ls-files', '-z');
93 while(scalar @srcArgs > 0) {
94 $src = shift @srcArgs;
95 $dst = shift @dstArgs;
99 # Be nicer to end-users by doing ".//a/./b/.//./c" ==> "a/b/c"
101 s
|/\./|/| while (m|/\
./|);
103 # Also "a/b/../c" ==> "a/c"
104 1 while (s
,(^|/)[^/]+/\.\./,$1,);
108 print "Checking rename of '$src' to '$dst'\n";
111 unless (-f
$src || -l
$src || -d
$src) {
112 $bad = "bad source '$src'";
115 $safesrc = quotemeta($src);
116 @srcfiles = grep /^$safesrc(\/|$)/, @allfiles;
118 $overwritten{$dst} = 0;
119 if (($bad eq "") && -e
$dst) {
120 $bad = "destination '$dst' already exists";
122 # only files can overwrite each other: check both source and destination
123 if (-f
$dst && (scalar @srcfiles == 1)) {
124 print "Warning: $bad; will overwrite!\n";
126 $overwritten{$dst} = 1;
129 $bad = "Can not overwrite '$src' with '$dst'";
134 if (($bad eq "") && ($dst =~ /^$safesrc\//)) {
135 $bad = "can not move directory '$src' into itself";
139 if (scalar @srcfiles == 0) {
140 $bad = "'$src' not under version control";
145 if (defined $srcForDst{$dst}) {
146 $bad = "can not move '$src' to '$dst'; already target of ";
147 $bad .= "'".$srcForDst{$dst}."'";
150 $srcForDst{$dst} = $src;
156 print "Warning: $bad; skipping\n";
159 print "Error: $bad\n";
166 # Final pass: rename/move
167 my (@deletedfiles,@addedfiles,@changedfiles);
169 while(scalar @srcs > 0) {
173 if ($opt_n || $opt_v) { print "Renaming $src to $dst\n"; }
175 if (!rename($src,$dst)) {
176 $bad = "renaming '$src' failed: $!";
178 print "Warning: skipped: $bad\n";
186 $safesrc = quotemeta($src);
187 @srcfiles = grep /^$safesrc(\/|$)/, @allfiles;
188 @dstfiles = @srcfiles;
189 s/^$safesrc(\/|$)/$dst$1/ for @dstfiles;
191 push @deletedfiles, @srcfiles;
192 if (scalar @srcfiles == 1) {
193 # $dst can be a directory with 1 file inside
194 if ($overwritten{$dst} ==1) {
195 push @changedfiles, $dstfiles[0];
198 push @addedfiles, $dstfiles[0];
202 push @addedfiles, @dstfiles;
208 print "Changed : ". join(", ", @changedfiles) ."\n";
211 print "Adding : ". join(", ", @addedfiles) ."\n";
214 print "Deleting : ". join(", ", @deletedfiles) ."\n";
219 my ($fd, $ctx) = $repo->command_input_pipe('update-index', '-z', '--stdin');
220 foreach my $fileName (@changedfiles) {
221 print $fd "$fileName\0";
223 git_cmd_try
{ $repo->command_close_pipe($fd, $ctx); }
224 'git-update-index failed to update changed files with code %d';
227 my ($fd, $ctx) = $repo->command_input_pipe('update-index', '--add', '-z', '--stdin');
228 foreach my $fileName (@addedfiles) {
229 print $fd "$fileName\0";
231 git_cmd_try
{ $repo->command_close_pipe($fd, $ctx); }
232 'git-update-index failed to add new files with code %d';
235 my ($fd, $ctx) = $repo->command_input_pipe('update-index', '--remove', '-z', '--stdin');
236 foreach my $fileName (@deletedfiles) {
237 print $fd "$fileName\0";
239 git_cmd_try
{ $repo->command_close_pipe($fd, $ctx); }
240 'git-update-index failed to remove old files with code %d';
245 print "Error: $bad\n";