2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
10 #include "git2/revwalk.h"
11 #include "git2/transport.h"
19 #include "repository.h"
22 static int maybe_want(git_remote
*remote
, git_remote_head
*head
, git_odb
*odb
, git_refspec
*tagspec
)
26 if (!git_reference_is_valid_name(head
->name
))
29 if (remote
->download_tags
== GIT_REMOTE_DOWNLOAD_TAGS_ALL
) {
31 * If tagopt is --tags, then we only use the default
32 * tags refspec and ignore the remote's
34 if (git_refspec_src_matches(tagspec
, head
->name
))
38 } else if (git_remote__matching_refspec(remote
, head
->name
))
44 /* If we have the object, mark it so we don't ask for it */
45 if (git_odb_exists(odb
, &head
->oid
))
48 remote
->need_pack
= 1;
50 return git_vector_insert(&remote
->refs
, head
);
53 static int filter_wants(git_remote
*remote
)
55 git_remote_head
**heads
;
56 git_refspec tagspec
, head
;
61 git_vector_clear(&remote
->refs
);
62 if ((error
= git_refspec__parse(&tagspec
, GIT_REFSPEC_TAGS
, true)) < 0)
66 * The fetch refspec can be NULL, and what this means is that the
67 * user didn't specify one. This is fine, as it means that we're
68 * not interested in any particular branch but just the remote's
69 * HEAD, which will be stored in FETCH_HEAD after the fetch.
71 if (remote
->active_refspecs
.length
== 0) {
72 if ((error
= git_refspec__parse(&head
, "HEAD", true)) < 0)
75 error
= git_refspec__dwim_one(&remote
->active_refspecs
, &head
, &remote
->refs
);
76 git_refspec__free(&head
);
82 if (git_repository_odb__weakptr(&odb
, remote
->repo
) < 0)
85 if (git_remote_ls((const git_remote_head
***)&heads
, &heads_len
, remote
) < 0)
88 for (i
= 0; i
< heads_len
; i
++) {
89 if ((error
= maybe_want(remote
, heads
[i
], odb
, &tagspec
)) < 0)
94 git_refspec__free(&tagspec
);
100 * In this first version, we push all our refs in and start sending
101 * them out. When we get an ACK we hide that commit and continue
102 * traversing until we're done
104 int git_fetch_negotiate(git_remote
*remote
)
106 git_transport
*t
= remote
->transport
;
108 if (filter_wants(remote
) < 0) {
109 giterr_set(GITERR_NET
, "Failed to filter the reference list for wants");
113 /* Don't try to negotiate when we don't want anything */
114 if (!remote
->need_pack
)
118 * Now we have everything set up so we can start tell the
119 * server what we want and what we have.
121 return t
->negotiate_fetch(t
,
123 (const git_remote_head
* const *)remote
->refs
.contents
,
124 remote
->refs
.length
);
127 int git_fetch_download_pack(git_remote
*remote
)
129 git_transport
*t
= remote
->transport
;
131 if (!remote
->need_pack
)
134 return t
->download_pack(t
, remote
->repo
, &remote
->stats
,
135 remote
->callbacks
.transfer_progress
, remote
->callbacks
.payload
);