git/git-rename.perl

71 lines
1.6 KiB
Plaintext
Raw Normal View History

#!/usr/bin/perl
#
# Copyright 2005, Ryan Anderson <ryan@michonline.com>
#
# This file is licensed under the GPL v2, or a later version
# at the discretion of Linus Torvalds.
use warnings;
use strict;
sub usage($);
# Sanity checks:
my $GIT_DIR = $ENV{'GIT_DIR'} || ".git";
unless ( -d $GIT_DIR && -d $GIT_DIR . "/objects" &&
Create object subdirectories on demand This makes it possible to have a "sparse" git object subdirectory structure, something that has become much more attractive now that people use pack-files all the time. As a result of pack-files, a git object directory doesn't necessarily have any individual objects lying around, and in that case it's just wasting space to keep the empty first-level object directories around: on many filesystems the 256 empty directories will be aboue 1MB of diskspace. Even more importantly, after you re-pack a project that _used_ to be unpacked, you could be left with huge directories that no longer contain anything, but that waste space and take time to look through. With this change, "git prune-packed" can just do an rmdir() on the directories, and they'll get removed if empty, and re-created on demand. This patch also tries to fix up "write_sha1_from_fd()" to use the new common infrastructure for creating the object files, closing a hole where we might otherwise leave half-written objects in the object database. [jc: I unoptimized the part that really removes the fan-out directories to ease transition. init-db still wastes 1MB of diskspace to hold 256 empty fan-outs, and prune-packed rmdir()'s the grown but empty directories, but runs mkdir() immediately after that -- reducing the saving from 150KB to 146KB. These parts will be re-introduced when everybody has the on-demand capability.] Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
2005-10-08 22:54:01 +00:00
-d $GIT_DIR . "/objects/" && -d $GIT_DIR . "/refs") {
usage("Git repository not found.");
}
usage("") if scalar @ARGV != 2;
my ($src,$dst) = @ARGV;
unless (-f $src || -l $src || -d $src) {
usage("git rename: bad source '$src'");
}
if (-e $dst) {
usage("git rename: destinations '$dst' already exists");
}
my (@allfiles,@srcfiles,@dstfiles);
$/ = "\0";
open(F,"-|","git-ls-files","-z")
or die "Failed to open pipe from git-ls-files: " . $!;
@allfiles = map { chomp; $_; } <F>;
close(F);
my $safesrc = quotemeta($src);
@srcfiles = grep /^$safesrc/, @allfiles;
@dstfiles = @srcfiles;
s#^$safesrc(/|$)#$dst$1# for @dstfiles;
rename($src,$dst)
or die "rename failed: $!";
my $rc = system("git-update-index","--add","--",@dstfiles);
die "git-update-index failed to add new name with code $?\n" if $rc;
$rc = system("git-update-index","--remove","--",@srcfiles);
die "git-update-index failed to remove old name with code $?\n" if $rc;
sub usage($) {
my $s = shift;
print $s, "\n" if (length $s != 0);
print <<EOT;
$0 <source> <dest>
source must exist and be either a file, symlink or directory.
dest must NOT exist.
Renames source to dest, and updates the git cache to reflect the change.
Use "git commit" to make record the change permanently.
EOT
exit(1);
}