remote: refactor update tips function Move the functionality to update an individual tip out of the loop; although the update tip function remains rather gnarly, at least the outer function is a bit less onerous.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
diff --git a/src/remote.c b/src/remote.c
index a0cb1ef..fd47036 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -1738,141 +1738,153 @@ static int update_ref(
return 0;
}
-static int update_tips_for_spec(
- git_remote *remote,
- const git_remote_callbacks *callbacks,
- int update_fetchhead,
- git_remote_autotag_option_t tagopt,
- git_refspec *spec,
- git_vector *refs,
- const char *log_message)
+static int update_one_tip(
+ git_vector *update_heads,
+ git_remote *remote,
+ git_refspec *spec,
+ git_remote_head *head,
+ git_refspec *tagspec,
+ git_remote_autotag_option_t tagopt,
+ const char *log_message,
+ const git_remote_callbacks *callbacks)
{
- int error = 0, autotag, valid;
- unsigned int i = 0;
+ git_odb *odb;
git_str refname = GIT_STR_INIT;
+ git_reference *ref = NULL;
+ bool autotag = false;
git_oid old;
- git_odb *odb;
- git_remote_head *head;
- git_reference *ref;
- git_refspec tagspec;
- git_vector update_heads;
+ int valid;
+ int error;
- GIT_ASSERT_ARG(remote);
+ if ((error = git_repository_odb__weakptr(&odb, remote->repo)) < 0)
+ goto done;
- if (git_repository_odb__weakptr(&odb, remote->repo) < 0)
- return -1;
+ /* Ignore malformed ref names (which also saves us from tag^{} */
+ if ((error = git_reference_name_is_valid(&valid, head->name)) < 0)
+ goto done;
- if (git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true) < 0)
- return -1;
+ if (!valid)
+ goto done;
- /* Make a copy of the transport's refs */
- if (git_vector_init(&update_heads, 16, NULL) < 0)
- return -1;
+ /* If we have a tag, see if the auto-follow rules say to update it */
+ if (git_refspec_src_matches(tagspec, head->name)) {
+ if (tagopt == GIT_REMOTE_DOWNLOAD_TAGS_AUTO)
+ autotag = true;
- for (; i < refs->length; ++i) {
- head = git_vector_get(refs, i);
- autotag = 0;
- git_str_clear(&refname);
+ if (tagopt != GIT_REMOTE_DOWNLOAD_TAGS_NONE) {
+ if (git_str_puts(&refname, head->name) < 0)
+ goto done;
+ }
+ }
- /* Ignore malformed ref names (which also saves us from tag^{} */
- if (git_reference_name_is_valid(&valid, head->name) < 0)
- goto on_error;
+ /* If we didn't want to auto-follow the tag, check if the refspec matches */
+ if (!autotag && git_refspec_src_matches(spec, head->name)) {
+ if (spec->dst) {
+ if ((error = git_refspec__transform(&refname, spec, head->name)) < 0)
+ goto done;
+ } else {
+ /*
+ * no rhs means store it in FETCH_HEAD, even if we don't
+ * update anything else.
+ */
+ error = git_vector_insert(update_heads, head);
+ goto done;
+ }
+ }
- if (!valid)
- continue;
+ /* If we still don't have a refname, we don't want it */
+ if (git_str_len(&refname) == 0)
+ goto done;
- /* If we have a tag, see if the auto-follow rules say to update it */
- if (git_refspec_src_matches(&tagspec, head->name)) {
- if (tagopt != GIT_REMOTE_DOWNLOAD_TAGS_NONE) {
+ /* In autotag mode, only create tags for objects already in db */
+ if (autotag && !git_odb_exists(odb, &head->oid))
+ goto done;
- if (tagopt == GIT_REMOTE_DOWNLOAD_TAGS_AUTO)
- autotag = 1;
+ if (!autotag && (error = git_vector_insert(update_heads, head)) < 0)
+ goto done;
- git_str_clear(&refname);
- if (git_str_puts(&refname, head->name) < 0)
- goto on_error;
- }
- }
+ error = git_reference_name_to_id(&old, remote->repo, refname.ptr);
- /* If we didn't want to auto-follow the tag, check if the refspec matches */
- if (!autotag && git_refspec_src_matches(spec, head->name)) {
- if (spec->dst) {
- if (git_refspec__transform(&refname, spec, head->name) < 0)
- goto on_error;
- } else {
- /*
- * no rhs mans store it in FETCH_HEAD, even if we don't
- update anything else.
- */
- if ((error = git_vector_insert(&update_heads, head)) < 0)
- goto on_error;
+ if (error < 0 && error != GIT_ENOTFOUND)
+ goto done;
- continue;
- }
- }
+ if (!spec->force &&
+ !git_graph_descendant_of(remote->repo, &head->oid, &old)) {
+ error = 0;
+ goto done;
+ }
- /* If we still don't have a refname, we don't want it */
- if (git_str_len(&refname) == 0) {
- continue;
- }
+ if (error == GIT_ENOTFOUND) {
+ memset(&old, 0, GIT_OID_RAWSZ);
+ error = 0;
- /* In autotag mode, only create tags for objects already in db */
- if (autotag && !git_odb_exists(odb, &head->oid))
- continue;
+ if (autotag && (error = git_vector_insert(update_heads, head)) < 0)
+ goto done;
+ }
- if (!autotag && git_vector_insert(&update_heads, head) < 0)
- goto on_error;
+ if (!git_oid__cmp(&old, &head->oid))
+ goto done;
- error = git_reference_name_to_id(&old, remote->repo, refname.ptr);
- if (error < 0 && error != GIT_ENOTFOUND)
- goto on_error;
+ /* In autotag mode, don't overwrite any locally-existing tags */
+ error = git_reference_create(&ref, remote->repo, refname.ptr, &head->oid, !autotag,
+ log_message);
- if (!(error || error == GIT_ENOTFOUND)
- && !spec->force
- && !git_graph_descendant_of(remote->repo, &head->oid, &old))
- continue;
+ if (error < 0) {
+ if (error == GIT_EEXISTS)
+ error = 0;
- if (error == GIT_ENOTFOUND) {
- memset(&old, 0, GIT_OID_RAWSZ);
+ goto done;
+ }
- if (autotag && git_vector_insert(&update_heads, head) < 0)
- goto on_error;
- }
+ if (callbacks && callbacks->update_tips != NULL &&
+ callbacks->update_tips(refname.ptr, &old, &head->oid, callbacks->payload) < 0)
+ git_error_set_after_callback_function(error, "git_remote_fetch");
- if (!git_oid__cmp(&old, &head->oid))
- continue;
+done:
+ git_reference_free(ref);
+ return error;
+}
- /* In autotag mode, don't overwrite any locally-existing tags */
- error = git_reference_create(&ref, remote->repo, refname.ptr, &head->oid, !autotag,
- log_message);
+static int update_tips_for_spec(
+ git_remote *remote,
+ const git_remote_callbacks *callbacks,
+ int update_fetchhead,
+ git_remote_autotag_option_t tagopt,
+ git_refspec *spec,
+ git_vector *refs,
+ const char *log_message)
+{
+ git_refspec tagspec;
+ git_remote_head *head;
+ git_vector update_heads;
+ int error = 0;
+ size_t i;
- if (error == GIT_EEXISTS)
- continue;
+ GIT_ASSERT_ARG(remote);
- if (error < 0)
- goto on_error;
+ if (git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true) < 0)
+ return -1;
- git_reference_free(ref);
+ /* Make a copy of the transport's refs */
+ if (git_vector_init(&update_heads, 16, NULL) < 0)
+ return -1;
- if (callbacks && callbacks->update_tips != NULL) {
- if (callbacks->update_tips(refname.ptr, &old, &head->oid, callbacks->payload) < 0)
- goto on_error;
- }
+ git_vector_foreach(refs, i, head) {
+ if (update_one_tip(&update_heads, remote, spec, head, &tagspec, tagopt, log_message, callbacks) < 0)
+ goto on_error;
}
if (update_fetchhead &&
(error = git_remote_write_fetchhead(remote, spec, &update_heads)) < 0)
goto on_error;
- git_vector_free(&update_heads);
git_refspec__dispose(&tagspec);
- git_str_dispose(&refname);
+ git_vector_free(&update_heads);
return 0;
on_error:
- git_vector_free(&update_heads);
git_refspec__dispose(&tagspec);
- git_str_dispose(&refname);
+ git_vector_free(&update_heads);
return -1;
}
@@ -1938,8 +1950,11 @@ static int next_head(const git_remote *remote, git_vector *refs,
return GIT_ITEROVER;
}
-static int opportunistic_updates(const git_remote *remote, const git_remote_callbacks *callbacks,
- git_vector *refs, const char *msg)
+static int opportunistic_updates(
+ const git_remote *remote,
+ const git_remote_callbacks *callbacks,
+ git_vector *refs,
+ const char *msg)
{
size_t i, j, k;
git_refspec *spec;
@@ -1949,6 +1964,7 @@ static int opportunistic_updates(const git_remote *remote, const git_remote_call
i = j = k = 0;
+ /* Handle refspecs matching remote heads */
while ((error = next_head(remote, refs, &spec, &head, &i, &j, &k)) == 0) {
/*
* If we got here, there is a refspec which was used
@@ -1964,8 +1980,10 @@ static int opportunistic_updates(const git_remote *remote, const git_remote_call
goto cleanup;
}
- if (error == GIT_ITEROVER)
- error = 0;
+ if (error != GIT_ITEROVER)
+ goto cleanup;
+
+ error = 0;
cleanup:
git_str_dispose(&refname);