26
26
TAGS_SET = 2
27
27
};
28
28
29
- static int all , append , force , keep , multiple , update_head_ok , verbosity ;
29
+ static int all , append , force , keep , multiple , prune , update_head_ok , verbosity ;
30
30
static int tags = TAGS_DEFAULT ;
31
31
static const char * depth ;
32
32
static const char * upload_pack ;
@@ -49,6 +49,8 @@ static struct option builtin_fetch_options[] = {
49
49
"fetch all tags and associated objects" , TAGS_SET ),
50
50
OPT_SET_INT ('n' , NULL , & tags ,
51
51
"do not fetch all tags (--no-tags)" , TAGS_UNSET ),
52
+ OPT_BOOLEAN ('p' , "prune" , & prune ,
53
+ "prune tracking branches no longer on remote" ),
52
54
OPT_BOOLEAN ('k' , "keep" , & keep , "keep downloaded pack" ),
53
55
OPT_BOOLEAN ('u' , "update-head-ok" , & update_head_ok ,
54
56
"allow updating of HEAD ref" ),
@@ -492,6 +494,28 @@ static int fetch_refs(struct transport *transport, struct ref *ref_map)
492
494
return ret ;
493
495
}
494
496
497
+ static int prune_refs (struct transport * transport , struct ref * ref_map )
498
+ {
499
+ int result = 0 ;
500
+ struct ref * ref , * stale_refs = get_stale_heads (transport -> remote , ref_map );
501
+ const char * dangling_msg = dry_run
502
+ ? " (%s will become dangling)\n"
503
+ : " (%s has become dangling)\n" ;
504
+
505
+ for (ref = stale_refs ; ref ; ref = ref -> next ) {
506
+ if (!dry_run )
507
+ result |= delete_ref (ref -> name , NULL , 0 );
508
+ if (verbosity >= 0 ) {
509
+ fprintf (stderr , " x %-*s %-*s -> %s\n" ,
510
+ SUMMARY_WIDTH , "[deleted]" ,
511
+ REFCOL_WIDTH , "(none)" , prettify_refname (ref -> name ));
512
+ warn_dangling_symref (stderr , dangling_msg , ref -> name );
513
+ }
514
+ }
515
+ free_refs (stale_refs );
516
+ return result ;
517
+ }
518
+
495
519
static int add_existing (const char * refname , const unsigned char * sha1 ,
496
520
int flag , void * cbdata )
497
521
{
@@ -616,6 +640,8 @@ static int do_fetch(struct transport *transport,
616
640
free_refs (ref_map );
617
641
return 1 ;
618
642
}
643
+ if (prune )
644
+ prune_refs (transport , ref_map );
619
645
free_refs (ref_map );
620
646
621
647
/* if neither --no-tags nor --tags was specified, do automated tag
@@ -699,9 +725,11 @@ static int add_remote_or_group(const char *name, struct string_list *list)
699
725
static int fetch_multiple (struct string_list * list )
700
726
{
701
727
int i , result = 0 ;
702
- const char * argv [] = { "fetch" , NULL , NULL , NULL , NULL };
728
+ const char * argv [] = { "fetch" , NULL , NULL , NULL , NULL , NULL };
703
729
int argc = 1 ;
704
730
731
+ if (prune )
732
+ argv [argc ++ ] = "--prune" ;
705
733
if (verbosity >= 2 )
706
734
argv [argc ++ ] = "-v" ;
707
735
if (verbosity >= 1 )
0 commit comments