builtin-fetch: add --prune option
Teach fetch to cull stale remote tracking branches after fetching via --prune. Signed-off-by: Jay Soffian <jaysoffian@gmail.com> Signed-off-by: Björn Gustavsson <bgustavsson@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jay Soffian committed
Nov 10, 2009 at 09:15 UTC
f360d844de4b752b4cba2343b95592ae4d55d09a
2 files changed
+34
-2
Documentation/fetch-options.txt
+4
@@ -28,6 +28,10 @@ ifndef::git-pull[]
28
--multiple::
29
Allow several <repository> and <group> arguments to be
30
specified. No <refspec>s may be specified.
31
+
32
+--prune::
33
+ After fetching, remove any remote tracking branches which
34
+ no longer exist on the remote.
35
endif::git-pull[]
36
37
ifdef::git-pull[]
builtin-fetch.c
+30
-2
@@ -26,7 +26,7 @@ enum {
26
TAGS_SET = 2
27
};
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
static int tags = TAGS_DEFAULT;
31
static const char *depth;
32
static const char *upload_pack;
@@ -49,6 +49,8 @@ static struct option builtin_fetch_options[] = {
49
"fetch all tags and associated objects", TAGS_SET),
50
OPT_SET_INT('n', NULL, &tags,
51
"do not fetch all tags (--no-tags)", TAGS_UNSET),
52
+ OPT_BOOLEAN('p', "prune", &prune,
53
+ "prune tracking branches no longer on remote"),
54
OPT_BOOLEAN('k', "keep", &keep, "keep downloaded pack"),
55
OPT_BOOLEAN('u', "update-head-ok", &update_head_ok,
56
"allow updating of HEAD ref"),
@@ -492,6 +494,28 @@ static int fetch_refs(struct transport *transport, struct ref *ref_map)
494
return ret;
495
}
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
+
519
static int add_existing(const char *refname, const unsigned char *sha1,
520
int flag, void *cbdata)
521
{
@@ -616,6 +640,8 @@ static int do_fetch(struct transport *transport,
640
free_refs(ref_map);
641
return 1;
642
}
643
+ if (prune)
644
+ prune_refs(transport, ref_map);
645
free_refs(ref_map);
646
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)
725
static int fetch_multiple(struct string_list *list)
726
{
727
int i, result = 0;
702
- const char *argv[] = { "fetch", NULL, NULL, NULL, NULL };
728
+ const char *argv[] = { "fetch", NULL, NULL, NULL, NULL, NULL };
729
int argc = 1;
730
731
+ if (prune)
732
+ argv[argc++] = "--prune";
733
if (verbosity >= 2)
734
argv[argc++] = "-v";
735
if (verbosity >= 1)