]> git.proxmox.com Git - libgit2.git/blob - src/fetch.c
Merge pull request #1562 from libgit2/cmn/refs-namespace-lookup
[libgit2.git] / src / fetch.c
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 "git2/oid.h"
9 #include "git2/refs.h"
10 #include "git2/revwalk.h"
11 #include "git2/transport.h"
12
13 #include "common.h"
14 #include "remote.h"
15 #include "refspec.h"
16 #include "pack.h"
17 #include "fetch.h"
18 #include "netops.h"
19 #include "repository.h"
20 #include "refs.h"
21
22 struct filter_payload {
23 git_remote *remote;
24 const git_refspec *spec, *tagspec;
25 git_odb *odb;
26 int found_head;
27 };
28
29 static int filter_ref__cb(git_remote_head *head, void *payload)
30 {
31 struct filter_payload *p = payload;
32 int match = 0;
33
34 if (!git_reference_is_valid_name(head->name))
35 return 0;
36
37 if (!p->found_head && strcmp(head->name, GIT_HEAD_FILE) == 0)
38 p->found_head = 1;
39 else if (p->remote->download_tags == GIT_REMOTE_DOWNLOAD_TAGS_ALL) {
40 /*
41 * If tagopt is --tags, then we only use the default
42 * tags refspec and ignore the remote's
43 */
44 if (git_refspec_src_matches(p->tagspec, head->name))
45 match = 1;
46 else
47 return 0;
48 } else if (git_remote__matching_refspec(p->remote, head->name))
49 match = 1;
50
51 if (!match)
52 return 0;
53
54 /* If we have the object, mark it so we don't ask for it */
55 if (git_odb_exists(p->odb, &head->oid))
56 head->local = 1;
57 else
58 p->remote->need_pack = 1;
59
60 return git_vector_insert(&p->remote->refs, head);
61 }
62
63 static int filter_wants(git_remote *remote)
64 {
65 struct filter_payload p;
66 git_refspec tagspec;
67 int error = -1;
68
69 git_vector_clear(&remote->refs);
70 if (git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true) < 0)
71 return error;
72
73 /*
74 * The fetch refspec can be NULL, and what this means is that the
75 * user didn't specify one. This is fine, as it means that we're
76 * not interested in any particular branch but just the remote's
77 * HEAD, which will be stored in FETCH_HEAD after the fetch.
78 */
79 p.tagspec = &tagspec;
80 p.found_head = 0;
81 p.remote = remote;
82
83 if (git_repository_odb__weakptr(&p.odb, remote->repo) < 0)
84 goto cleanup;
85
86 error = git_remote_ls(remote, filter_ref__cb, &p);
87
88 cleanup:
89 git_refspec__free(&tagspec);
90
91 return error;
92 }
93
94 /*
95 * In this first version, we push all our refs in and start sending
96 * them out. When we get an ACK we hide that commit and continue
97 * traversing until we're done
98 */
99 int git_fetch_negotiate(git_remote *remote)
100 {
101 git_transport *t = remote->transport;
102
103 if (filter_wants(remote) < 0) {
104 giterr_set(GITERR_NET, "Failed to filter the reference list for wants");
105 return -1;
106 }
107
108 /* Don't try to negotiate when we don't want anything */
109 if (remote->refs.length == 0 || !remote->need_pack)
110 return 0;
111
112 /*
113 * Now we have everything set up so we can start tell the
114 * server what we want and what we have.
115 */
116 return t->negotiate_fetch(t,
117 remote->repo,
118 (const git_remote_head * const *)remote->refs.contents,
119 remote->refs.length);
120 }
121
122 int git_fetch_download_pack(git_remote *remote)
123 {
124 git_transport *t = remote->transport;
125
126 if(!remote->need_pack)
127 return 0;
128
129 return t->download_pack(t, remote->repo, &remote->stats,
130 remote->callbacks.transfer_progress, remote->callbacks.payload);
131 }