]> git.proxmox.com Git - libgit2.git/blob - src/fetch.c
Merge pull request #1920 from libgit2/cmn/ref-with-log
[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 static int maybe_want(git_remote *remote, git_remote_head *head, git_odb *odb, git_refspec *tagspec)
23 {
24 int match = 0;
25
26 if (!git_reference_is_valid_name(head->name))
27 return 0;
28
29 if (remote->download_tags == GIT_REMOTE_DOWNLOAD_TAGS_ALL) {
30 /*
31 * If tagopt is --tags, then we only use the default
32 * tags refspec and ignore the remote's
33 */
34 if (git_refspec_src_matches(tagspec, head->name))
35 match = 1;
36 else
37 return 0;
38 } else if (git_remote__matching_refspec(remote, head->name))
39 match = 1;
40
41 if (!match)
42 return 0;
43
44 /* If we have the object, mark it so we don't ask for it */
45 if (git_odb_exists(odb, &head->oid))
46 head->local = 1;
47 else
48 remote->need_pack = 1;
49
50 return git_vector_insert(&remote->refs, head);
51 }
52
53 static int filter_wants(git_remote *remote)
54 {
55 git_remote_head **heads;
56 git_refspec tagspec, head;
57 int error = 0;
58 git_odb *odb;
59 size_t i, heads_len;
60
61 git_vector_clear(&remote->refs);
62 if ((error = 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 if (remote->active_refspecs.length == 0) {
72 if ((error = git_refspec__parse(&head, "HEAD", true)) < 0)
73 goto cleanup;
74
75 error = git_refspec__dwim_one(&remote->active_refspecs, &head, &remote->refs);
76 git_refspec__free(&head);
77
78 if (error < 0)
79 goto cleanup;
80 }
81
82 if (git_repository_odb__weakptr(&odb, remote->repo) < 0)
83 goto cleanup;
84
85 if (git_remote_ls((const git_remote_head ***)&heads, &heads_len, remote) < 0)
86 goto cleanup;
87
88 for (i = 0; i < heads_len; i++) {
89 if ((error = maybe_want(remote, heads[i], odb, &tagspec)) < 0)
90 break;
91 }
92
93 cleanup:
94 git_refspec__free(&tagspec);
95
96 return error;
97 }
98
99 /*
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
103 */
104 int git_fetch_negotiate(git_remote *remote)
105 {
106 git_transport *t = remote->transport;
107
108 if (filter_wants(remote) < 0) {
109 giterr_set(GITERR_NET, "Failed to filter the reference list for wants");
110 return -1;
111 }
112
113 /* Don't try to negotiate when we don't want anything */
114 if (!remote->need_pack)
115 return 0;
116
117 /*
118 * Now we have everything set up so we can start tell the
119 * server what we want and what we have.
120 */
121 return t->negotiate_fetch(t,
122 remote->repo,
123 (const git_remote_head * const *)remote->refs.contents,
124 remote->refs.length);
125 }
126
127 int git_fetch_download_pack(git_remote *remote)
128 {
129 git_transport *t = remote->transport;
130
131 if (!remote->need_pack)
132 return 0;
133
134 return t->download_pack(t, remote->repo, &remote->stats,
135 remote->callbacks.transfer_progress, remote->callbacks.payload);
136 }