@matheusbsbs / matheus-bagliano / commits / 1114b26e8f
Fork 0

Add git-mv

It supersedes git-rename by adding functionality to move multiple files, directories or symlinks into another directory. It also provides according documentation. The implementation renames multiple files, using the arguments from the command line to produce an array of sources and destinations. In a first pass, all requested renames are checked for errors, and overwriting of existing files is only allowed with '-f'. The actual renaming is done in a second pass. This ensures that any error condition is checked before anything is changed. Signed-off-by: Josef Weidendorfer <Josef.Weidendorfer@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Josef Weidendorfer committed Oct 23, 2005 at 18:15 UTC 1114b26e8f2d06912d855c631e51a4ee8a06c4e2
3 files changed +237 -1
Documentation/git-mv.txt new
+51
@@ -0,0 +1,51 @@
1 +git-mv(1)
2 +=========
3 +
4 +NAME
5 +----
6 +git-mv - Script used to move or rename a file, directory or symlink.
7 +
8 +
9 +SYNOPSIS
10 +--------
11 +'git-mv' [-f] [-n] <source> <destination>
12 +'git-mv' [-f] [-k] [-n] <source> ... <destination directory>
13 +
14 +DESCRIPTION
15 +-----------
16 +This script is used to move or rename a file, directory or symlink.
17 +In the first form, it renames <source>, which must exist and be either
18 +a file, symlink or directory, to <destination>, which must not exist.
19 +In the second form, the last argument has to be an existing
20 +directory; the given sources will be moved into this directory.
21 +
22 +The index is updated after successful completion, but the change must still be
23 +committed.
24 +
25 +OPTIONS
26 +-------
27 +-f::
28 + Force renaming or moving even targets exist
29 +-k::
30 + Skip move or rename actions which would lead to an error
31 + condition. An error happens when a source is neither existing nor
32 + controlled by GIT, or when it would overwrite an existing
33 + file unless '-f' is given.
34 +-n::
35 + Do nothing; only show what would happen
36 +
37 +
38 +Author
39 +------
40 +Written by Linus Torvalds <torvalds@osdl.org>
41 +Rewritten by Ryan Anderson <ryan@michonline.com>
42 +Move functionality added by Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
43 +
44 +Documentation
45 +--------------
46 +Documentation by David Greaves, Junio C Hamano and the git-list <git@vger.kernel.org>.
47 +
48 +GIT
49 +---
50 +Part of the gitlink:git[7] suite
51 +
Makefile
+1 -1
@@ -94,7 +94,7 @@ SCRIPT_SH = \
94 SCRIPT_PERL = \
95 git-archimport.perl git-cvsimport.perl git-relink.perl \
96 git-rename.perl git-shortlog.perl git-fmt-merge-msg.perl \
97 - git-findtags.perl git-svnimport.perl
97 + git-findtags.perl git-svnimport.perl git-mv.perl
98
99 SCRIPT_PYTHON = \
100 git-merge-recursive.py
git-mv.perl new
+185
@@ -0,0 +1,185 @@
1 +#!/usr/bin/perl
2 +#
3 +# Copyright 2005, Ryan Anderson <ryan@michonline.com>
4 +# Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
5 +#
6 +# This file is licensed under the GPL v2, or a later version
7 +# at the discretion of Linus Torvalds.
8 +
9 +
10 +use warnings;
11 +use strict;
12 +use Getopt::Std;
13 +
14 +sub usage() {
15 + print <<EOT;
16 +$0 [-f] [-n] <source> <dest>
17 +$0 [-f] [-k] [-n] <source> ... <dest directory>
18 +
19 +In the first form, source must exist and be either a file,
20 +symlink or directory, dest must not exist. It renames source to dest.
21 +In the second form, the last argument has to be an existing
22 +directory; the given sources will be moved into this directory.
23 +
24 +Updates the git cache to reflect the change.
25 +Use "git commit" to make the change permanently.
26 +
27 +Options:
28 + -f Force renaming/moving, even if target exists
29 + -k Continue on error by skipping
30 + not-existing or not revision-controlled source
31 + -n Do nothing; show what would happen
32 +EOT
33 + exit(1);
34 +}
35 +
36 +# Sanity checks:
37 +my $GIT_DIR = $ENV{'GIT_DIR'} || ".git";
38 +
39 +unless ( -d $GIT_DIR && -d $GIT_DIR . "/objects" &&
40 + -d $GIT_DIR . "/objects/" && -d $GIT_DIR . "/refs") {
41 + print "Git repository not found.";
42 + usage();
43 +}
44 +
45 +
46 +our ($opt_n, $opt_f, $opt_h, $opt_k, $opt_v);
47 +getopts("hnfkv") || usage;
48 +usage() if $opt_h;
49 +@ARGV >= 1 or usage;
50 +
51 +my (@srcArgs, @dstArgs, @srcs, @dsts);
52 +my ($src, $dst, $base, $dstDir);
53 +
54 +my $argCount = scalar @ARGV;
55 +if (-d $ARGV[$argCount-1]) {
56 + $dstDir = $ARGV[$argCount-1];
57 + @srcArgs = @ARGV[0..$argCount-2];
58 +
59 + foreach $src (@srcArgs) {
60 + $base = $src;
61 + $base =~ s/^.*\///;
62 + $dst = "$dstDir/". $base;
63 + push @dstArgs, $dst;
64 + }
65 +}
66 +else {
67 + if ($argCount != 2) {
68 + print "Error: moving to directory '"
69 + . $ARGV[$argCount-1]
70 + . "' not possible; not exisiting\n";
71 + usage;
72 + }
73 + @srcArgs = ($ARGV[0]);
74 + @dstArgs = ($ARGV[1]);
75 + $dstDir = "";
76 +}
77 +
78 +my (@allfiles,@srcfiles,@dstfiles);
79 +my $safesrc;
80 +my %overwritten;
81 +
82 +$/ = "\0";
83 +open(F,"-|","git-ls-files","-z")
84 + or die "Failed to open pipe from git-ls-files: " . $!;
85 +
86 +@allfiles = map { chomp; $_; } <F>;
87 +close(F);
88 +
89 +
90 +my ($i, $bad);
91 +while(scalar @srcArgs > 0) {
92 + $src = shift @srcArgs;
93 + $dst = shift @dstArgs;
94 + $bad = "";
95 +
96 + if ($opt_v) {
97 + print "Checking rename of '$src' to '$dst'\n";
98 + }
99 +
100 + unless (-f $src || -l $src || -d $src) {
101 + $bad = "bad source '$src'";
102 + }
103 +
104 + $overwritten{$dst} = 0;
105 + if (($bad eq "") && -e $dst) {
106 + $bad = "destination '$dst' already exists";
107 + if (-f $dst && $opt_f) {
108 + print "Warning: $bad; will overwrite!\n";
109 + $bad = "";
110 + $overwritten{$dst} = 1;
111 + }
112 + }
113 +
114 + if (($bad eq "") && ($src eq $dstDir)) {
115 + $bad = "can not move directory '$src' into itself";
116 + }
117 +
118 + if ($bad eq "") {
119 + $safesrc = quotemeta($src);
120 + @srcfiles = grep /^$safesrc(\/|$)/, @allfiles;
121 + if (scalar @srcfiles == 0) {
122 + $bad = "'$src' not under version control";
123 + }
124 + }
125 +
126 + if ($bad ne "") {
127 + if ($opt_k) {
128 + print "Warning: $bad; skipping\n";
129 + next;
130 + }
131 + print "Error: $bad\n";
132 + usage();
133 + }
134 + push @srcs, $src;
135 + push @dsts, $dst;
136 +}
137 +
138 +# Final pass: rename/move
139 +my (@deletedfiles,@addedfiles,@changedfiles);
140 +while(scalar @srcs > 0) {
141 + $src = shift @srcs;
142 + $dst = shift @dsts;
143 +
144 + if ($opt_n || $opt_v) { print "Renaming $src to $dst\n"; }
145 + if (!$opt_n) {
146 + rename($src,$dst)
147 + or die "rename failed: $!";
148 + }
149 +
150 + $safesrc = quotemeta($src);
151 + @srcfiles = grep /^$safesrc(\/|$)/, @allfiles;
152 + @dstfiles = @srcfiles;
153 + s/^$safesrc(\/|$)/$dst$1/ for @dstfiles;
154 +
155 + push @deletedfiles, @srcfiles;
156 + if (scalar @srcfiles == 1) {
157 + if ($overwritten{$dst} ==1) {
158 + push @changedfiles, $dst;
159 + } else {
160 + push @addedfiles, $dst;
161 + }
162 + }
163 + else {
164 + push @addedfiles, @dstfiles;
165 + }
166 +}
167 +
168 +if ($opt_n) {
169 + print "Changed : ". join(", ", @changedfiles) ."\n";
170 + print "Adding : ". join(", ", @addedfiles) ."\n";
171 + print "Deleting : ". join(", ", @deletedfiles) ."\n";
172 + exit(1);
173 +}
174 +
175 +my $rc;
176 +if (scalar @changedfiles >0) {
177 + $rc = system("git-update-index","--",@changedfiles);
178 + die "git-update-index failed to update changed files with code $?\n" if $rc;
179 +}
180 +if (scalar @addedfiles >0) {
181 + $rc = system("git-update-index","--add","--",@addedfiles);
182 + die "git-update-index failed to add new names with code $?\n" if $rc;
183 +}
184 +$rc = system("git-update-index","--remove","--",@deletedfiles);
185 +die "git-update-index failed to remove old names with code $?\n" if $rc;