updated git and svn scripts
[xrzperl.git] / git-remote-editurl
blob3cf58ebf31b7eeb62bfcb9485c8cd9e5a9ed30fd
1 #!/usr/bin/perl -w
3 use Cwd qw/getcwd/;
4 sub run {
5 #print STDERR join(" ",@_),"\n";
6 return system(@_) == 0;
8 sub process {
9 my $dir = shift;
10 my $exp = shift;
11 if((!$dir) or (!-d $dir)) {
12 print STDERR "[ignored] path note exists $dir\n";
13 return undef;
15 if(! -d "$dir/.git") {
16 print STDERR "[ignored] $dir not a git working tree\n";
17 return undef;
19 print STDERR "processing $dir (s/$exp/g) \n";
20 my $cwd = getcwd();
21 chdir $dir or die("$!\n");
22 my %remotes;
23 open FI,"-|",qw/git remote/,"-v" or die("$!\n");
24 foreach(<FI>){
25 chomp;
26 if(m/^\s*([^\s]+)\s+([^\s]+)\s+\(fetch\)\s*$/) {
27 $remotes{$1} = $2;
30 close FI;
31 print STDERR "Get remotes [",scalar(keys %remotes), "]:\n";
32 print STDERR join("\n",map {"$_ = $remotes{$_}"} keys %remotes),"\n";
33 foreach my $host (keys %remotes) {
34 my $_ = $remotes{$host};
35 eval("s/$exp/g");
36 if($_ eq $remotes{$host}) {
37 print STDERR "[$host] doesn't change\n";
39 else {
40 print STDERR "[$host] => $_\n";
41 run('git','remote','rm',$host);
42 run('git','remote','add',$host,$_);
43 run('git','fetch',$host,'--verbose');
46 chdir $cwd;
48 if(@ARGV) {
49 my $exp = shift;
50 push @ARGV,"." unless(@ARGV);
51 foreach(@ARGV) {
52 &process($_,$exp);
55 else {
56 print STDERR "Usage:\n$0 <text/replacement> git_working_tree [git_working_tree...]\n"