]>
Commit | Line | Data |
---|---|---|
d36ac03e RA |
1 | #!/usr/bin/perl |
2 | # | |
3 | # Copyright 2005, Ryan Anderson <ryan@michonline.com> | |
4 | # | |
5 | # This file is licensed under the GPL v2, or a later version | |
6 | # at the discretion of Linus Torvalds. | |
399144f2 | 7 | |
399144f2 | 8 | |
d36ac03e RA |
9 | use warnings; |
10 | use strict; | |
11 | ||
12 | sub usage($); | |
13 | ||
14 | # Sanity checks: | |
15 | my $GIT_DIR = $ENV{'GIT_DIR'} || ".git"; | |
16 | ||
17 | unless ( -d $GIT_DIR && -d $GIT_DIR . "/objects" && | |
18 | -d $GIT_DIR . "/objects/00" && -d $GIT_DIR . "/refs") { | |
19 | usage("Git repository not found."); | |
20 | } | |
21 | ||
22 | usage("") if scalar @ARGV != 2; | |
23 | ||
24 | my ($src,$dst) = @ARGV; | |
25 | ||
26 | unless (-f $src || -l $src || -d $src) { | |
27 | usage("git rename: bad source '$src'"); | |
28 | } | |
29 | ||
30 | if (-e $dst) { | |
31 | usage("git rename: destinations '$dst' already exists"); | |
32 | } | |
33 | ||
34 | my (@allfiles,@srcfiles,@dstfiles); | |
35 | ||
36 | $/ = "\0"; | |
37 | open(F,"-|","git-ls-files","-z") | |
38 | or die "Failed to open pipe from git-ls-files: " . $!; | |
39 | ||
40 | @allfiles = map { chomp; $_; } <F>; | |
41 | close(F); | |
42 | ||
43 | my $safesrc = quotemeta($src); | |
44 | @srcfiles = grep /^$safesrc/, @allfiles; | |
45 | @dstfiles = @srcfiles; | |
46 | s#^$safesrc(/|$)#$dst$1# for @dstfiles; | |
47 | ||
48 | rename($src,$dst) | |
49 | or die "rename failed: $!"; | |
50 | ||
51 | my $rc = system("git-update-cache","--add","--",@dstfiles); | |
52 | die "git-update-cache failed to add new name with code $?\n" if $rc; | |
53 | ||
54 | $rc = system("git-update-cache","--remove","--",@srcfiles); | |
55 | die "git-update-cache failed to remove old name with code $?\n" if $rc; | |
56 | ||
57 | ||
58 | sub usage($) { | |
59 | my $s = shift; | |
60 | print $s, "\n" if (length $s != 0); | |
61 | print <<EOT; | |
62 | $0 <source> <dest> | |
63 | source must exist and be either a file, symlink or directory. | |
64 | dest must NOT exist. | |
65 | ||
66 | Renames source to dest, and updates the git cache to reflect the change. | |
67 | Use "git commit" to make record the change permanently. | |
68 | EOT | |
69 | exit(1); | |
70 | } |