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 <n>' can now be used to delete any tracking branches under <n> which are no longer available on the remote system. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Shawn O. Pearce committed
Feb 2, 2007 at 00:06 UTC
859607dfe04260d55f5901974fdd660aebc427fa
2 files changed
+43
-1
Documentation/git-remote.txt
+5
@@ -12,6 +12,7 @@ SYNOPSIS
12
'git-remote'
13
'git-remote' add <name> <url>
14
'git-remote' show <name>
15
+'git-remote' prune <name>
16
17
DESCRIPTION
18
-----------
@@ -26,6 +27,10 @@ update remote-tracking branches <name>/<branch>.
27
28
In the third form, gives some information about the remote <name>.
29
30
+In the fourth form, deletes all stale tracking branches under <name>.
31
+These stale branches have already been removed from the remote repository
32
+referenced by <name>, but are still locally available in "remotes/<name>".
33
+
34
The remote configuration is achieved using the `remote.origin.url` and
35
`remote.origin.fetch` configuration variables. (See
36
gitlink:git-config[1]).
git-remote.perl
+38
-1
@@ -200,7 +200,7 @@ sub show_mapping {
200
print " @$new\n";
201
}
202
if (@$stale) {
203
- print " Stale tracking branches in remotes/$name (you'd better remove them)\n";
203
+ print " Stale tracking branches in remotes/$name (use 'git remote prune')\n";
204
print " @$stale\n";
205
}
206
if (@$tracked) {
@@ -209,6 +209,23 @@ sub show_mapping {
209
}
210
}
211
212
+sub prune_remote {
213
+ my ($name, $ls_remote) = @_;
214
+ if (!exists $remote->{$name}) {
215
+ print STDERR "No such remote $name\n";
216
+ return;
217
+ }
218
+ my $info = $remote->{$name};
219
+ update_ls_remote($ls_remote, $info);
220
+
221
+ my ($new, $stale, $tracked) = list_mapping($name, $info);
222
+ my $prefix = "refs/remotes/$name";
223
+ foreach my $to_prune (@$stale) {
224
+ my @v = $git->command(qw(rev-parse --verify), "$prefix/$to_prune");
225
+ $git->command(qw(update-ref -d), "$prefix/$to_prune", $v[0]);
226
+ }
227
+}
228
+
229
sub show_remote {
230
my ($name, $ls_remote) = @_;
231
if (!exists $remote->{$name}) {
@@ -270,6 +287,25 @@ elsif ($ARGV[0] eq 'show') {
287
show_remote($ARGV[$i], $ls_remote);
288
}
289
}
290
+elsif ($ARGV[0] eq 'prune') {
291
+ my $ls_remote = 1;
292
+ my $i;
293
+ for ($i = 1; $i < @ARGV; $i++) {
294
+ if ($ARGV[$i] eq '-n') {
295
+ $ls_remote = 0;
296
+ }
297
+ else {
298
+ last;
299
+ }
300
+ }
301
+ if ($i >= @ARGV) {
302
+ print STDERR "Usage: git remote prune <remote>\n";
303
+ exit(1);
304
+ }
305
+ for (; $i < @ARGV; $i++) {
306
+ prune_remote($ARGV[$i], $ls_remote);
307
+ }
308
+}
309
elsif ($ARGV[0] eq 'add') {
310
if (@ARGV != 3) {
311
print STDERR "Usage: git remote add <name> <url>\n";
@@ -281,5 +317,6 @@ else {
317
print STDERR "Usage: git remote\n";
318
print STDERR " git remote add <name> <url>\n";
319
print STDERR " git remote show <name>\n";
320
+ print STDERR " git remote prune <name>\n";
321
exit(1);
322
}