]> git.proxmox.com Git - libgit2.git/blob - src/fetch.c
Merge pull request #1493 from carlosmn/remotes
[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
20 struct filter_payload {
21 git_remote *remote;
22 const git_refspec *spec, *tagspec;
23 git_odb *odb;
24 int found_head;
25 };
26
27 static int filter_ref__cb(git_remote_head *head, void *payload)
28 {
29 struct filter_payload *p = payload;
30 int match = 0;
31
32 if (!git_reference_is_valid_name(head->name))
33 return 0;
34
35 if (!p->found_head && strcmp(head->name, GIT_HEAD_FILE) == 0)
36 p->found_head = 1;
37 else if (git_remote__matching_refspec(p->remote, head->name))
38 match = 1;
39 else if (p->remote->download_tags == GIT_REMOTE_DOWNLOAD_TAGS_ALL &&
40 git_refspec_src_matches(p->tagspec, head->name))
41 match = 1;
42
43 if (!match)
44 return 0;
45
46 /* If we have the object, mark it so we don't ask for it */
47 if (git_odb_exists(p->odb, &head->oid))
48 head->local = 1;
49 else
50 p->remote->need_pack = 1;
51
52 return git_vector_insert(&p->remote->refs, head);
53 }
54
55 static int filter_wants(git_remote *remote)
56 {
57 struct filter_payload p;
58 git_refspec tagspec;
59 int error = -1;
60
61 git_vector_clear(&remote->refs);
62 if (git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true) < 0)
63 return error;
64
65 /*
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.
70 */
71 p.tagspec = &tagspec;
72 p.found_head = 0;
73 p.remote = remote;
74
75 if (git_repository_odb__weakptr(&p.odb, remote->repo) < 0)
76 goto cleanup;
77
78 error = git_remote_ls(remote, filter_ref__cb, &p);
79
80 cleanup:
81 git_refspec__free(&tagspec);
82
83 return error;
84 }
85
86 /*
87 * In this first version, we push all our refs in and start sending
88 * them out. When we get an ACK we hide that commit and continue
89 * traversing until we're done
90 */
91 int git_fetch_negotiate(git_remote *remote)
92 {
93 git_transport *t = remote->transport;
94
95 if (filter_wants(remote) < 0) {
96 giterr_set(GITERR_NET, "Failed to filter the reference list for wants");
97 return -1;
98 }
99
100 /* Don't try to negotiate when we don't want anything */
101 if (remote->refs.length == 0 || !remote->need_pack)
102 return 0;
103
104 /*
105 * Now we have everything set up so we can start tell the
106 * server what we want and what we have.
107 */
108 return t->negotiate_fetch(t,
109 remote->repo,
110 (const git_remote_head * const *)remote->refs.contents,
111 remote->refs.length);
112 }
113
114 int git_fetch_download_pack(
115 git_remote *remote,
116 git_transfer_progress_callback progress_cb,
117 void *progress_payload)
118 {
119 git_transport *t = remote->transport;
120
121 if(!remote->need_pack)
122 return 0;
123
124 return t->download_pack(t, remote->repo, &remote->stats, progress_cb, progress_payload);
125 }