Skip to content
This repository was archived by the owner on Jul 2, 2019. It is now read-only.

Commit 666b4c2

Browse files
committed
Merge branch 'tm/fetch-prune'
Fetching 'frotz' branch with "git fetch", while having 'frotz/nitfol' remote-tracking branch from an earlier fetch, would error out, primarily because the command has not been told to remove anything on our side. In such a case, "git fetch --prune" can be used to remove 'frotz/nitfol' to make room to fetch and store 'frotz' remote-tracking branch. * tm/fetch-prune: fetch --prune: Run prune before fetching fetch --prune: always print header url
2 parents 2da5cbd + 10a6cc8 commit 666b4c2

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

builtin/fetch.c

+32-10
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static struct transport *gtransport;
4444
static struct transport *gsecondary;
4545
static const char *submodule_prefix = "";
4646
static const char *recurse_submodules_default;
47+
static int shown_url = 0;
4748

4849
static int option_parse_recurse_submodules(const struct option *opt,
4950
const char *arg, int unset)
@@ -535,7 +536,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
535536
{
536537
FILE *fp;
537538
struct commit *commit;
538-
int url_len, i, shown_url = 0, rc = 0;
539+
int url_len, i, rc = 0;
539540
struct strbuf note = STRBUF_INIT;
540541
const char *what, *kind;
541542
struct ref *rm;
@@ -708,24 +709,44 @@ static int fetch_refs(struct transport *transport, struct ref *ref_map)
708709
return ret;
709710
}
710711

711-
static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map)
712+
static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,
713+
const char *raw_url)
712714
{
713-
int result = 0;
715+
int url_len, i, result = 0;
714716
struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
717+
char *url;
715718
const char *dangling_msg = dry_run
716719
? _(" (%s will become dangling)")
717720
: _(" (%s has become dangling)");
718721

722+
if (raw_url)
723+
url = transport_anonymize_url(raw_url);
724+
else
725+
url = xstrdup("foreign");
726+
727+
url_len = strlen(url);
728+
for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
729+
;
730+
731+
url_len = i + 1;
732+
if (4 < i && !strncmp(".git", url + i - 3, 4))
733+
url_len = i - 3;
734+
719735
for (ref = stale_refs; ref; ref = ref->next) {
720736
if (!dry_run)
721737
result |= delete_ref(ref->name, NULL, 0);
738+
if (verbosity >= 0 && !shown_url) {
739+
fprintf(stderr, _("From %.*s\n"), url_len, url);
740+
shown_url = 1;
741+
}
722742
if (verbosity >= 0) {
723743
fprintf(stderr, " x %-*s %-*s -> %s\n",
724744
TRANSPORT_SUMMARY(_("[deleted]")),
725745
REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name));
726746
warn_dangling_symref(stderr, dangling_msg, ref->name);
727747
}
728748
}
749+
free(url);
729750
free_refs(stale_refs);
730751
return result;
731752
}
@@ -842,25 +863,26 @@ static int do_fetch(struct transport *transport,
842863

843864
if (tags == TAGS_DEFAULT && autotags)
844865
transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
845-
if (fetch_refs(transport, ref_map)) {
846-
free_refs(ref_map);
847-
retcode = 1;
848-
goto cleanup;
849-
}
850866
if (prune) {
851867
/*
852868
* We only prune based on refspecs specified
853869
* explicitly (via command line or configuration); we
854870
* don't care whether --tags was specified.
855871
*/
856872
if (ref_count) {
857-
prune_refs(refs, ref_count, ref_map);
873+
prune_refs(refs, ref_count, ref_map, transport->url);
858874
} else {
859875
prune_refs(transport->remote->fetch,
860876
transport->remote->fetch_refspec_nr,
861-
ref_map);
877+
ref_map,
878+
transport->url);
862879
}
863880
}
881+
if (fetch_refs(transport, ref_map)) {
882+
free_refs(ref_map);
883+
retcode = 1;
884+
goto cleanup;
885+
}
864886
free_refs(ref_map);
865887

866888
/* if neither --no-tags nor --tags was specified, do automated tag

t/t5510-fetch.sh

+26
Original file line numberDiff line numberDiff line change
@@ -614,4 +614,30 @@ test_expect_success 'all boundary commits are excluded' '
614614
test_bundle_object_count .git/objects/pack/pack-${pack##pack }.pack 3
615615
'
616616

617+
test_expect_success 'fetch --prune prints the remotes url' '
618+
git branch goodbye &&
619+
git clone . only-prunes &&
620+
git branch -D goodbye &&
621+
(
622+
cd only-prunes &&
623+
git fetch --prune origin 2>&1 | head -n1 >../actual
624+
) &&
625+
echo "From ${D}/." >expect &&
626+
test_cmp expect actual
627+
'
628+
629+
test_expect_success 'branchname D/F conflict resolved by --prune' '
630+
git branch dir/file &&
631+
git clone . prune-df-conflict &&
632+
git branch -D dir/file &&
633+
git branch dir &&
634+
(
635+
cd prune-df-conflict &&
636+
git fetch --prune &&
637+
git rev-parse origin/dir >../actual
638+
) &&
639+
git rev-parse dir >expect &&
640+
test_cmp expect actual
641+
'
642+
617643
test_done

0 commit comments

Comments
 (0)