git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] Add support for directories to git-rename-script.

From
RARyan Anderson <ryan@michonline.com>
Date
Jul 25, 2005, 05:26 UTC
Message-ID
<20050725052646.GB6098@mythryan2.michonline.com>
Oh, and in the process, rewrite it in Perl.
Signed-off-by: Ryan Anderson <ryan@michonline.com>
---
 git-rename-script |   68 +++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 63 insertions(+), 5 deletions(-)
f46717f9116bba7efb6a10ed99cd2fcea00fe5da
diff --git a/git-rename-script b/git-rename-script
--- a/git-rename-script
+++ b/git-rename-script
@@ -1,7 +1,65 @@
-#!/bin/sh
+#!/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.
 
-. git-sh-setup-script || die "Not a git archive"
 
-[ -f "$1" ] || [ -h "$1" ] || die "git rename: bad source"
-[ -e "$2" ] && die "git rename: destination already exists"
-mv -- "$1" "$2" && git-update-cache --add --remove -- "$1" "$2"
+use warnings;
+use strict;
+
+sub usage($);
+
+# Sanity checks:
+unless ( -d ".git" && -d ".git/objects" && 
+	-d ".git/objects/00" && -d ".git/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);
+
+open(F,"-|","git-ls-files")
+	or die "Failed to open pipe from git-ls-files: " . $!;
+
+@allfiles = <F>;
+close(F);
+chomp for @allfiles;
+
+
+@srcfiles = grep /^$src/, @allfiles;
+@dstfiles = @srcfiles;
+s#^$src(/|$)#$dst$1# for @dstfiles;
+
+rename($src,$dst)
+	or die "rename failed: $!";
+
+system("git-update-cache","--remove","--",@srcfiles);
+system("git-update-cache","--add","--",@dstfiles);
+
+
+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);
+}
-- 
Ryan Anderson
  sometimes Pug Majere
Next: Ryan Anderson
Message 1 of 5 in “Add support for directories to git-rename-script.”
  1. Add support for directories to git-rename-script.Ryan Anderson, Jul 25, 2005
  2. Add documentation for git-rename-scriptRyan Anderson, Jul 25, 2005
  3. Junio C HamanoJul 25, 2005
  4. Make git-rename-script behave much better when faced with input contain Perl regular expression metacharacters.Ryan Anderson, Jul 26, 2005
  5. Make git-rename-script behave better with input containing Perl regexp metachars.Ryan Anderson, Jul 26, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.