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.
78
9
use warnings;
10use strict;
1112
sub usage($);
1314
# Sanity checks:
15my $GIT_DIR = $ENV{'GIT_DIR'} || ".git";
1617
unless ( -d $GIT_DIR && -d $GIT_DIR . "/objects" &&
18-d $GIT_DIR . "/objects/00" && -d $GIT_DIR . "/refs") {
19usage("Git repository not found.");
20}
2122
usage("") if scalar @ARGV != 2;
2324
my ($src,$dst) = @ARGV;
2526
unless (-f $src || -l $src || -d $src) {
27usage("git rename: bad source '$src'");
28}
2930
if (-e $dst) {
31usage("git rename: destinations '$dst' already exists");
32}
3334
my (@allfiles,@srcfiles,@dstfiles);
3536
$/ = "\0";
37open(F,"-|","git-ls-files","-z")
38or die "Failed to open pipe from git-ls-files: " . $!;
3940
@allfiles = map { chomp; $_; } <F>;
41close(F);
4243
my $safesrc = quotemeta($src);
44@srcfiles = grep /^$safesrc/, @allfiles;
45@dstfiles = @srcfiles;
46s#^$safesrc(/|$)#$dst$1# for @dstfiles;
4748
rename($src,$dst)
49or die "rename failed: $!";
5051
my $rc = system("git-update-cache","--add","--",@dstfiles);
52die "git-update-cache failed to add new name with code $?\n" if $rc;
5354
$rc = system("git-update-cache","--remove","--",@srcfiles);
55die "git-update-cache failed to remove old name with code $?\n" if $rc;
5657
58
sub usage($) {
59my $s = shift;
60print $s, "\n" if (length $s != 0);
61print <<EOT;
62$0 <source> <dest>
63source must exist and be either a file, symlink or directory.
64dest must NOT exist.
6566
Renames source to dest, and updates the git cache to reflect the change.
67Use "git commit" to make record the change permanently.
68EOT
69exit(1);
70}