| 1 |
|
| /* |
| 2 |
|
| * Copyright (C) the libgit2 contributors. All rights reserved. |
| 3 |
|
| * |
| 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. |
| 6 |
|
| */ |
| 7 |
|
| |
| 8 |
|
| #include "fetch.h" |
| 9 |
|
| |
| 10 |
|
| #include "git2/oid.h" |
| 11 |
|
| #include "git2/refs.h" |
| 12 |
|
| #include "git2/revwalk.h" |
| 13 |
|
| #include "git2/transport.h" |
| 14 |
|
| #include "git2/sys/remote.h" |
| 15 |
|
| |
| 16 |
|
| #include "remote.h" |
| 17 |
|
| #include "refspec.h" |
| 18 |
|
| #include "pack.h" |
| 19 |
|
| #include "netops.h" |
| 20 |
|
| #include "repository.h" |
| 21 |
|
| #include "refs.h" |
| 22 |
|
| |
| 23 |
0 |
| static int maybe_want(git_remote *remote, git_remote_head *head, git_refspec *tagspec, git_remote_autotag_option_t tagopt) |
| 24 |
|
| { |
| 25 |
0 |
| int match = 0, valid; |
| 26 |
|
| |
| 27 |
0 |
▶ | if (git_reference_name_is_valid(&valid, head->name) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_reference_name_is_valid(&valid, head->name) < 0) |
0 |
0 |
0 |
|
| 28 |
0 |
| return -1; |
| 29 |
|
| |
| 30 |
0 |
▶ | if (!valid) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!valid) |
0 |
0 |
0 |
|
| 31 |
0 |
| return 0; |
| 32 |
|
| |
| 33 |
0 |
▶ | if (tagopt == GIT_REMOTE_DOWNLOAD_TAGS_ALL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tagopt == GIT_REMOTE_DOWNLOAD_TAGS_ALL) { |
0 |
0 |
0 |
|
| 34 |
|
| /* |
| 35 |
|
| * If tagopt is --tags, always request tags |
| 36 |
|
| * in addition to the remote's refspecs |
| 37 |
|
| */ |
| 38 |
0 |
▶ | if (git_refspec_src_matches(tagspec, head->name)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_refspec_src_matches(tagspec, head->name)) |
0 |
0 |
0 |
|
| 39 |
0 |
| match = 1; |
| 40 |
|
| } |
| 41 |
|
| |
| 42 |
0 |
▶ | if (!match && git_remote__matching_refspec(remote, head->name)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!match && git_remote__matching_refspec(remote, head->name)) |
0 |
0 |
0 |
| if (!match && git_remote__matching_refspec(remote, head->name)) |
0 |
0 |
0 |
|
| 43 |
0 |
| match = 1; |
| 44 |
|
| |
| 45 |
0 |
▶ | if (!match) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!match) |
0 |
0 |
0 |
|
| 46 |
0 |
| return 0; |
| 47 |
|
| |
| 48 |
0 |
| return git_vector_insert(&remote->refs, head); |
| 49 |
|
| } |
| 50 |
|
| |
| 51 |
0 |
| static int mark_local(git_remote *remote) |
| 52 |
|
| { |
| 53 |
|
| git_remote_head *head; |
| 54 |
|
| git_odb *odb; |
| 55 |
|
| size_t i; |
| 56 |
|
| |
| 57 |
0 |
▶ | if (git_repository_odb__weakptr(&odb, remote->repo) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_repository_odb__weakptr(&odb, remote->repo) < 0) |
0 |
0 |
0 |
|
| 58 |
0 |
| return -1; |
| 59 |
|
| |
| 60 |
0 |
▶ | git_vector_foreach(&remote->refs, i, head) { |
Branch: 1 branch
| Branch | True | False | Total |
| git_vector_foreach(&remote->refs, i, head) { |
0 |
0 |
0 |
|
| 61 |
|
| /* If we have the object, mark it so we don't ask for it */ |
| 62 |
0 |
▶ | if (git_odb_exists(odb, &head->oid)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_odb_exists(odb, &head->oid)) |
0 |
0 |
0 |
|
| 63 |
0 |
| head->local = 1; |
| 64 |
|
| else |
| 65 |
0 |
| remote->need_pack = 1; |
| 66 |
|
| } |
| 67 |
|
| |
| 68 |
0 |
| return 0; |
| 69 |
|
| } |
| 70 |
|
| |
| 71 |
0 |
| static int maybe_want_oid(git_remote *remote, git_refspec *spec) |
| 72 |
|
| { |
| 73 |
|
| git_remote_head *oid_head; |
| 74 |
|
| |
| 75 |
0 |
| oid_head = git__calloc(1, sizeof(git_remote_head)); |
| 76 |
0 |
▶ | GIT_ERROR_CHECK_ALLOC(oid_head); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(oid_head); |
0 |
0 |
0 |
|
| 77 |
|
| |
| 78 |
0 |
| git_oid_fromstr(&oid_head->oid, spec->src); |
| 79 |
|
| |
| 80 |
0 |
▶ | if (spec->dst) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (spec->dst) { |
0 |
0 |
0 |
|
| 81 |
0 |
| oid_head->name = git__strdup(spec->dst); |
| 82 |
0 |
▶ | GIT_ERROR_CHECK_ALLOC(oid_head->name); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(oid_head->name); |
0 |
0 |
0 |
|
| 83 |
|
| } |
| 84 |
|
| |
| 85 |
0 |
| if (git_vector_insert(&remote->local_heads, oid_head) < 0 || |
| 86 |
0 |
| git_vector_insert(&remote->refs, oid_head) < 0) |
| 87 |
0 |
| return -1; |
| 88 |
|
| |
| 89 |
0 |
| return 0; |
| 90 |
|
| } |
| 91 |
|
| |
| 92 |
0 |
| static int filter_wants(git_remote *remote, const git_fetch_options *opts) |
| 93 |
|
| { |
| 94 |
|
| git_remote_head **heads; |
| 95 |
|
| git_refspec tagspec, head, *spec; |
| 96 |
0 |
| int error = 0; |
| 97 |
|
| git_odb *odb; |
| 98 |
|
| size_t i, heads_len; |
| 99 |
|
| unsigned int remote_caps; |
| 100 |
0 |
| unsigned int oid_mask = GIT_REMOTE_CAPABILITY_TIP_OID | |
| 101 |
|
| GIT_REMOTE_CAPABILITY_REACHABLE_OID; |
| 102 |
0 |
| git_remote_autotag_option_t tagopt = remote->download_tags; |
| 103 |
|
| |
| 104 |
0 |
▶ | if (opts && opts->download_tags != GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED) |
Branch: 2 branches
| Branch | True | False | Total |
| if (opts && opts->download_tags != GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED) |
0 |
0 |
0 |
| if (opts && opts->download_tags != GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED) |
0 |
0 |
0 |
|
| 105 |
0 |
| tagopt = opts->download_tags; |
| 106 |
|
| |
| 107 |
0 |
| git_vector_clear(&remote->refs); |
| 108 |
0 |
▶ | if ((error = git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true)) < 0) |
0 |
0 |
0 |
|
| 109 |
0 |
| return error; |
| 110 |
|
| |
| 111 |
|
| /* |
| 112 |
|
| * The fetch refspec can be NULL, and what this means is that the |
| 113 |
|
| * user didn't specify one. This is fine, as it means that we're |
| 114 |
|
| * not interested in any particular branch but just the remote's |
| 115 |
|
| * HEAD, which will be stored in FETCH_HEAD after the fetch. |
| 116 |
|
| */ |
| 117 |
0 |
▶ | if (remote->active_refspecs.length == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (remote->active_refspecs.length == 0) { |
0 |
0 |
0 |
|
| 118 |
0 |
▶ | if ((error = git_refspec__parse(&head, "HEAD", true)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_refspec__parse(&head, "HEAD", true)) < 0) |
0 |
0 |
0 |
|
| 119 |
0 |
| goto cleanup; |
| 120 |
|
| |
| 121 |
0 |
| error = git_refspec__dwim_one(&remote->active_refspecs, &head, &remote->refs); |
| 122 |
0 |
| git_refspec__dispose(&head); |
| 123 |
|
| |
| 124 |
0 |
▶ | if (error < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (error < 0) |
0 |
0 |
0 |
|
| 125 |
0 |
| goto cleanup; |
| 126 |
|
| } |
| 127 |
|
| |
| 128 |
0 |
▶ | if ((error = git_repository_odb__weakptr(&odb, remote->repo)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_repository_odb__weakptr(&odb, remote->repo)) < 0) |
0 |
0 |
0 |
|
| 129 |
0 |
| goto cleanup; |
| 130 |
|
| |
| 131 |
0 |
| if ((error = git_remote_ls((const git_remote_head ***)&heads, &heads_len, remote)) < 0 || |
| 132 |
0 |
| (error = git_remote_capabilities(&remote_caps, remote)) < 0) |
| 133 |
0 |
| goto cleanup; |
| 134 |
|
| |
| 135 |
|
| /* Handle remote heads */ |
| 136 |
0 |
▶ | for (i = 0; i < heads_len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < heads_len; i++) { |
0 |
0 |
0 |
|
| 137 |
0 |
▶ | if ((error = maybe_want(remote, heads[i], &tagspec, tagopt)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = maybe_want(remote, heads[i], &tagspec, tagopt)) < 0) |
0 |
0 |
0 |
|
| 138 |
0 |
| goto cleanup; |
| 139 |
|
| } |
| 140 |
|
| |
| 141 |
|
| /* Handle explicitly specified OID specs */ |
| 142 |
0 |
▶ | git_vector_foreach(&remote->active_refspecs, i, spec) { |
Branch: 1 branch
| Branch | True | False | Total |
| git_vector_foreach(&remote->active_refspecs, i, spec) { |
0 |
0 |
0 |
|
| 143 |
0 |
▶ | if (!git_oid__is_hexstr(spec->src)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!git_oid__is_hexstr(spec->src)) |
0 |
0 |
0 |
|
| 144 |
0 |
| continue; |
| 145 |
|
| |
| 146 |
0 |
▶ | if (!(remote_caps & oid_mask)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(remote_caps & oid_mask)) { |
0 |
0 |
0 |
|
| 147 |
0 |
| git_error_set(GIT_ERROR_INVALID, "cannot fetch a specific object from the remote repository"); |
| 148 |
0 |
| error = -1; |
| 149 |
0 |
| goto cleanup; |
| 150 |
|
| } |
| 151 |
|
| |
| 152 |
0 |
▶ | if ((error = maybe_want_oid(remote, spec)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = maybe_want_oid(remote, spec)) < 0) |
0 |
0 |
0 |
|
| 153 |
0 |
| goto cleanup; |
| 154 |
|
| } |
| 155 |
|
| |
| 156 |
0 |
| error = mark_local(remote); |
| 157 |
|
| |
| 158 |
0 |
| cleanup: |
| 159 |
0 |
| git_refspec__dispose(&tagspec); |
| 160 |
|
| |
| 161 |
0 |
| return error; |
| 162 |
|
| } |
| 163 |
|
| |
| 164 |
|
| /* |
| 165 |
|
| * In this first version, we push all our refs in and start sending |
| 166 |
|
| * them out. When we get an ACK we hide that commit and continue |
| 167 |
|
| * traversing until we're done |
| 168 |
|
| */ |
| 169 |
0 |
| int git_fetch_negotiate(git_remote *remote, const git_fetch_options *opts) |
| 170 |
|
| { |
| 171 |
0 |
| git_transport *t = remote->transport; |
| 172 |
|
| |
| 173 |
0 |
| remote->need_pack = 0; |
| 174 |
|
| |
| 175 |
0 |
▶ | if (filter_wants(remote, opts) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (filter_wants(remote, opts) < 0) |
0 |
0 |
0 |
|
| 176 |
0 |
| return -1; |
| 177 |
|
| |
| 178 |
|
| /* Don't try to negotiate when we don't want anything */ |
| 179 |
0 |
▶ | if (!remote->need_pack) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!remote->need_pack) |
0 |
0 |
0 |
|
| 180 |
0 |
| return 0; |
| 181 |
|
| |
| 182 |
|
| /* |
| 183 |
|
| * Now we have everything set up so we can start tell the |
| 184 |
|
| * server what we want and what we have. |
| 185 |
|
| */ |
| 186 |
0 |
| return t->negotiate_fetch(t, |
| 187 |
|
| remote->repo, |
| 188 |
0 |
| (const git_remote_head * const *)remote->refs.contents, |
| 189 |
|
| remote->refs.length); |
| 190 |
|
| } |
| 191 |
|
| |
| 192 |
0 |
| int git_fetch_download_pack(git_remote *remote) |
| 193 |
|
| { |
| 194 |
0 |
| git_transport *t = remote->transport; |
| 195 |
|
| |
| 196 |
0 |
▶ | if (!remote->need_pack) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!remote->need_pack) |
0 |
0 |
0 |
|
| 197 |
0 |
| return 0; |
| 198 |
|
| |
| 199 |
0 |
| return t->download_pack(t, remote->repo, &remote->stats); |
| 200 |
|
| } |
| 201 |
|
| |
| 202 |
0 |
| int git_fetch_options_init(git_fetch_options *opts, unsigned int version) |
| 203 |
|
| { |
| 204 |
0 |
▶ | GIT_INIT_STRUCTURE_FROM_TEMPLATE( |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_INIT_STRUCTURE_FROM_TEMPLATE( |
0 |
0 |
0 |
|
| 205 |
|
| opts, version, git_fetch_options, GIT_FETCH_OPTIONS_INIT); |
| 206 |
0 |
| return 0; |
| 207 |
|
| } |
| 208 |
|
| |
| 209 |
|
| #ifndef GIT_DEPRECATE_HARD |
| 210 |
0 |
| int git_fetch_init_options(git_fetch_options *opts, unsigned int version) |
| 211 |
|
| { |
| 212 |
0 |
| return git_fetch_options_init(opts, version); |
| 213 |
|
| } |
| 214 |
|
| #endif |