{"thread":{"id":"2200","subject":"[PATCH] This commit implements git-mv","startedAt":"2005-10-23T16:15:34Z","lastAt":"2005-10-23T16:15:34Z","messageCount":1,"participants":["Josef Weidendorfer"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"10484","messageId":"200510231815.35551.Josef.Weidendorfer@gmx.de","threadId":"2200","inReplyTo":null,"subject":"[PATCH] This commit implements git-mv","fromName":"Josef Weidendorfer","fromEmail":"josef.weidendorfer@gmx.de","sentAt":"2005-10-23T16:15:34Z","receivedAt":"2005-10-23T16:15:34Z","isPatch":true,"sender":{"key":"josef.weidendorfer@gmx.de","avatar":null},"body":"It superceeds git-rename by adding functionality to move\nmultiple files, directories or symlinks into another directory.\nIt also provides according documentation.\n\nThe implementation renames multiple files, using the arguments\nfrom the command line to produce an array of sources and destinations.\nIn a first pass, all requested renames are checked for errors, and\noverwriting of existing files is only allowed with '-f'.\nThe actual renaming is done in a second pass.\nThis ensures that any error condition is checked before anything is\nchanged.\n\nSigned-off-by: Josef Weidendorfer <Josef.Weidendorfer@gmx.de>\n\n---\nThe recent request on the list for \"mv\" in GIT reminded me about\nan addition to git-rename I made a week ago. I renamed it to\n\"git-mv\" and added some documentation.\n\nIf this works, we can remove git-rename sometimes in the future.\n\nI should complement this command with tests. Also, a nice addition\nwould be to support an interactive mode like 'mv', by asking if\nfiles should be overwritten.\n\nBy the way, it also checks for a request to move a directory into\nitself, which of course is an error.\n\nOption \"-k\" is good for this:\nE.g. a \"git-mv -k * dir\" moves all revision controlled files and\ndirectories (but not \"dir\"!) into \"dir\". \"-k\" makes sure that\nthe errors (trying to move \"dir\" into itself, or move files\nwithout revision control around) will not terminate the command\nbut silently ignored and skipped.\n\"-k\" was taken from \"make\": Continue even on an error.\n\nJosef\n\n Documentation/git-mv.txt |   51 +++++++++++++\n Makefile                 |    2 \n git-mv.perl              |  185 ++++++++++++++++++++++++++++++++++++++++++++++\n 3 files changed, 237 insertions(+), 1 deletions(-)\n create mode 100644 Documentation/git-mv.txt\n create mode 100755 git-mv.perl\n\napplies-to: b9f56dae60decf015079f5feef4544e7177b143a\n0baacba7907046796e5a15eb2b191c1e2cb48793\ndiff --git a/Documentation/git-mv.txt b/Documentation/git-mv.txt\nnew file mode 100644\nindex 0000000..f2d5882\n--- /dev/null\n+++ b/Documentation/git-mv.txt\n@@ -0,0 +1,51 @@\n+git-mv(1)\n+=========\n+\n+NAME\n+----\n+git-mv - Script used to move or rename a file, directory or symlink.\n+\n+\n+SYNOPSIS\n+--------\n+'git-mv' [-f] [-n] <source> <destination>\n+'git-mv' [-f] [-k] [-n] <source> ... <destination directory>\n+\n+DESCRIPTION\n+-----------\n+This script is used to move or rename a file, directory or symlink.\n+In the first form, it renames <source>, which must exist and be either\n+a file, symlink or directory, to <destination>, which must not exist.\n+In the second form, the last argument has to be an existing\n+directory; the given sources will be moved into this directory.\n+\n+The index is updated after successful completion, but the change must still be\n+committed.\n+\n+OPTIONS\n+-------\n+-f::\n+\tForce renaming or moving even targets exist\n+-k::\n+        Skip move or rename actions which would lead to an error\n+\tcondition. An error happens when a source is neither existing nor\n+        controlled by GIT, or when it would overwrite an existing\n+        file unless '-f' is given.\n+-n::\n+\tDo nothing; only show what would happen\n+\n+\n+Author\n+------\n+Written by Linus Torvalds <torvalds@osdl.org>\n+Rewritten by Ryan Anderson <ryan@michonline.com>\n+Move functionality added by Josef Weidendorfer <Josef.Weidendorfer@gmx.de>\n+\n+Documentation\n+--------------\n+Documentation by David Greaves, Junio C Hamano and the git-list <git@vger.kernel.org>.\n+\n+GIT\n+---\n+Part of the gitlink:git[7] suite\n+\ndiff --git a/Makefile b/Makefile\nindex 5ee72bc..b43c170 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -94,7 +94,7 @@ SCRIPT_SH = \\\n SCRIPT_PERL = \\\n \tgit-archimport.perl git-cvsimport.perl git-relink.perl \\\n \tgit-rename.perl git-shortlog.perl git-fmt-merge-msg.perl \\\n-\tgit-findtags.perl git-svnimport.perl\n+\tgit-findtags.perl git-svnimport.perl git-mv.perl\n \n SCRIPT_PYTHON = \\\n \tgit-merge-recursive.py\ndiff --git a/git-mv.perl b/git-mv.perl\nnew file mode 100755\nindex 0000000..28bced9\n--- /dev/null\n+++ b/git-mv.perl\n@@ -0,0 +1,185 @@\n+#!/usr/bin/perl\n+#\n+# Copyright 2005, Ryan Anderson <ryan@michonline.com>\n+#                 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>\n+#\n+# This file is licensed under the GPL v2, or a later version\n+# at the discretion of Linus Torvalds.\n+\n+\n+use warnings;\n+use strict;\n+use Getopt::Std;\n+\n+sub usage() {\n+\tprint <<EOT;\n+$0 [-f] [-n] <source> <dest>\n+$0 [-f] [-k] [-n] <source> ... <dest directory>\n+\n+In the first form, source must exist and be either a file,\n+symlink or directory, dest must not exist. It renames source to dest.\n+In the second form, the last argument has to be an existing\n+directory; the given sources will be moved into this directory.\n+\n+Updates the git cache to reflect the change.\n+Use \"git commit\" to make the change permanently.\n+\n+Options:\n+  -f   Force renaming/moving, even if target exists\n+  -k   Continue on error by skipping\n+       not-existing or not revision-controlled source\n+  -n   Do nothing; show what would happen\n+EOT\n+\texit(1);\n+}\n+\n+# Sanity checks:\n+my $GIT_DIR = $ENV{'GIT_DIR'} || \".git\";\n+\n+unless ( -d $GIT_DIR && -d $GIT_DIR . \"/objects\" && \n+\t-d $GIT_DIR . \"/objects/\" && -d $GIT_DIR . \"/refs\") {\n+    print \"Git repository not found.\";\n+    usage();\n+}\n+\n+\n+our ($opt_n, $opt_f, $opt_h, $opt_k, $opt_v);\n+getopts(\"hnfkv\") || usage;\n+usage() if $opt_h;\n+@ARGV >= 1 or usage;\n+\n+my (@srcArgs, @dstArgs, @srcs, @dsts);\n+my ($src, $dst, $base, $dstDir);\n+\n+my $argCount = scalar @ARGV;\n+if (-d $ARGV[$argCount-1]) {\n+\t$dstDir = $ARGV[$argCount-1];\n+\t@srcArgs = @ARGV[0..$argCount-2];\n+\t\n+\tforeach $src (@srcArgs) {\n+\t\t$base = $src;\n+\t\t$base =~ s/^.*\\///;\n+\t\t$dst = \"$dstDir/\". $base;\n+\t\tpush @dstArgs, $dst;\n+\t}\n+}\n+else {\n+    if ($argCount != 2) {\n+\tprint \"Error: moving to directory '\"\n+\t    . $ARGV[$argCount-1]\n+\t    . \"' not possible; not exisiting\\n\";\n+\tusage;\n+    }\n+    @srcArgs = ($ARGV[0]);\n+    @dstArgs = ($ARGV[1]);\n+    $dstDir = \"\";\n+}\n+\n+my (@allfiles,@srcfiles,@dstfiles);\n+my $safesrc;\n+my %overwritten;\n+\n+$/ = \"\\0\";\n+open(F,\"-|\",\"git-ls-files\",\"-z\")\n+        or die \"Failed to open pipe from git-ls-files: \" . $!;\n+\n+@allfiles = map { chomp; $_; } <F>;\n+close(F);\n+\n+\n+my ($i, $bad);\n+while(scalar @srcArgs > 0) {\n+    $src = shift @srcArgs;\n+    $dst = shift @dstArgs;\n+    $bad = \"\";\n+\n+    if ($opt_v) {\n+\tprint \"Checking rename of '$src' to '$dst'\\n\";\n+    }\n+\n+    unless (-f $src || -l $src || -d $src) {\n+\t$bad = \"bad source '$src'\";\n+    }\n+\n+    $overwritten{$dst} = 0;\n+    if (($bad eq \"\") && -e $dst) {\n+\t$bad = \"destination '$dst' already exists\";\n+\tif (-f $dst && $opt_f) {\n+\t    print \"Warning: $bad; will overwrite!\\n\";\n+\t    $bad = \"\";\n+\t    $overwritten{$dst} = 1;\n+\t}\n+    }\n+    \n+    if (($bad eq \"\") && ($src eq $dstDir)) {\n+\t$bad = \"can not move directory '$src' into itself\";\n+    }\n+\n+    if ($bad eq \"\") {\n+\t$safesrc = quotemeta($src);\n+\t@srcfiles = grep /^$safesrc(\\/|$)/, @allfiles;\n+        if (scalar @srcfiles == 0) {\n+\t    $bad = \"'$src' not under version control\";\n+\t}\n+    }\n+\n+    if ($bad ne \"\") {\n+\tif ($opt_k) {\n+\t    print \"Warning: $bad; skipping\\n\";\n+\t    next;\n+\t}\n+\tprint \"Error: $bad\\n\";\n+\tusage();\n+    }\n+    push @srcs, $src;\n+    push @dsts, $dst;\n+}\n+\n+# Final pass: rename/move\n+my (@deletedfiles,@addedfiles,@changedfiles);\n+while(scalar @srcs > 0) {\n+    $src = shift @srcs;\n+    $dst = shift @dsts;\n+\n+    if ($opt_n || $opt_v) { print \"Renaming $src to $dst\\n\"; }\n+    if (!$opt_n) {\n+\trename($src,$dst)\n+\t    or die \"rename failed: $!\";\n+    }\n+\n+    $safesrc = quotemeta($src);\n+    @srcfiles = grep /^$safesrc(\\/|$)/, @allfiles;\n+    @dstfiles = @srcfiles;\n+    s/^$safesrc(\\/|$)/$dst$1/ for @dstfiles;\n+\n+    push @deletedfiles, @srcfiles;\n+    if (scalar @srcfiles == 1) {\n+\tif ($overwritten{$dst} ==1) {\n+\t    push @changedfiles, $dst;\n+\t} else {\n+\t    push @addedfiles, $dst;\n+\t}\n+    }\n+    else {\n+\tpush @addedfiles, @dstfiles;\n+    }\n+}\n+\n+if ($opt_n) {\n+\tprint \"Changed  : \". join(\", \", @changedfiles) .\"\\n\";\n+\tprint \"Adding   : \". join(\", \", @addedfiles) .\"\\n\";\n+\tprint \"Deleting : \". join(\", \", @deletedfiles) .\"\\n\";\n+\texit(1);\n+}\n+\t\n+my $rc;\n+if (scalar @changedfiles >0) {\n+\t$rc = system(\"git-update-index\",\"--\",@changedfiles);\n+\tdie \"git-update-index failed to update changed files with code $?\\n\" if $rc;\n+}\n+if (scalar @addedfiles >0) {\n+\t$rc = system(\"git-update-index\",\"--add\",\"--\",@addedfiles);\n+\tdie \"git-update-index failed to add new names with code $?\\n\" if $rc;\n+}\n+$rc = system(\"git-update-index\",\"--remove\",\"--\",@deletedfiles);\n+die \"git-update-index failed to remove old names with code $?\\n\" if $rc;\n---\n0.99.8.GIT\n"}]}