Skip to content

Commit f360d84

Browse files
jaysoffiangitster
authored andcommitted
builtin-fetch: add --prune option
Teach fetch to cull stale remote tracking branches after fetching via --prune. Signed-off-by: Jay Soffian <[email protected]> Signed-off-by: Björn Gustavsson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3cf6134 commit f360d84

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

Documentation/fetch-options.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ ifndef::git-pull[]
2828
--multiple::
2929
Allow several <repository> and <group> arguments to be
3030
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.
3135
endif::git-pull[]
3236

3337
ifdef::git-pull[]

builtin-fetch.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ enum {
2626
TAGS_SET = 2
2727
};
2828

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;
3030
static int tags = TAGS_DEFAULT;
3131
static const char *depth;
3232
static const char *upload_pack;
@@ -49,6 +49,8 @@ static struct option builtin_fetch_options[] = {
4949
"fetch all tags and associated objects", TAGS_SET),
5050
OPT_SET_INT('n', NULL, &tags,
5151
"do not fetch all tags (--no-tags)", TAGS_UNSET),
52+
OPT_BOOLEAN('p', "prune", &prune,
53+
"prune tracking branches no longer on remote"),
5254
OPT_BOOLEAN('k', "keep", &keep, "keep downloaded pack"),
5355
OPT_BOOLEAN('u', "update-head-ok", &update_head_ok,
5456
"allow updating of HEAD ref"),
@@ -492,6 +494,28 @@ static int fetch_refs(struct transport *transport, struct ref *ref_map)
492494
return ret;
493495
}
494496

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+
495519
static int add_existing(const char *refname, const unsigned char *sha1,
496520
int flag, void *cbdata)
497521
{
@@ -616,6 +640,8 @@ static int do_fetch(struct transport *transport,
616640
free_refs(ref_map);
617641
return 1;
618642
}
643+
if (prune)
644+
prune_refs(transport, ref_map);
619645
free_refs(ref_map);
620646

621647
/* 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)
699725
static int fetch_multiple(struct string_list *list)
700726
{
701727
int i, result = 0;
702-
const char *argv[] = { "fetch", NULL, NULL, NULL, NULL };
728+
const char *argv[] = { "fetch", NULL, NULL, NULL, NULL, NULL };
703729
int argc = 1;
704730

731+
if (prune)
732+
argv[argc++] = "--prune";
705733
if (verbosity >= 2)
706734
argv[argc++] = "-v";
707735
if (verbosity >= 1)

0 commit comments

Comments
 (0)