From 859607dfe04260d55f5901974fdd660aebc427fa Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Fri, 2 Feb 2007 00:06:08 -0500 Subject: Teach 'git remote' how to cleanup stale tracking branches. Since it can be annoying to manually cleanup 40 tracking branches which were removed by the remote system, 'git remote prune ' can now be used to delete any tracking branches under which are no longer available on the remote system. Signed-off-by: Shawn O. Pearce Signed-off-by: Junio C Hamano --- git-remote.perl | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'git-remote.perl') diff --git a/git-remote.perl b/git-remote.perl index 969d33bc5e..f16ff21b8b 100755 --- a/git-remote.perl +++ b/git-remote.perl @@ -200,7 +200,7 @@ sub show_mapping { print " @$new\n"; } if (@$stale) { - print " Stale tracking branches in remotes/$name (you'd better remove them)\n"; + print " Stale tracking branches in remotes/$name (use 'git remote prune')\n"; print " @$stale\n"; } if (@$tracked) { @@ -209,6 +209,23 @@ sub show_mapping { } } +sub prune_remote { + my ($name, $ls_remote) = @_; + if (!exists $remote->{$name}) { + print STDERR "No such remote $name\n"; + return; + } + my $info = $remote->{$name}; + update_ls_remote($ls_remote, $info); + + my ($new, $stale, $tracked) = list_mapping($name, $info); + my $prefix = "refs/remotes/$name"; + foreach my $to_prune (@$stale) { + my @v = $git->command(qw(rev-parse --verify), "$prefix/$to_prune"); + $git->command(qw(update-ref -d), "$prefix/$to_prune", $v[0]); + } +} + sub show_remote { my ($name, $ls_remote) = @_; if (!exists $remote->{$name}) { @@ -270,6 +287,25 @@ elsif ($ARGV[0] eq 'show') { show_remote($ARGV[$i], $ls_remote); } } +elsif ($ARGV[0] eq 'prune') { + my $ls_remote = 1; + my $i; + for ($i = 1; $i < @ARGV; $i++) { + if ($ARGV[$i] eq '-n') { + $ls_remote = 0; + } + else { + last; + } + } + if ($i >= @ARGV) { + print STDERR "Usage: git remote prune \n"; + exit(1); + } + for (; $i < @ARGV; $i++) { + prune_remote($ARGV[$i], $ls_remote); + } +} elsif ($ARGV[0] eq 'add') { if (@ARGV != 3) { print STDERR "Usage: git remote add \n"; @@ -281,5 +317,6 @@ else { print STDERR "Usage: git remote\n"; print STDERR " git remote add \n"; print STDERR " git remote show \n"; + print STDERR " git remote prune \n"; exit(1); } -- cgit v1.2.1