Git fork
1#ifndef REMOTE_H
2#define REMOTE_H
3
4#include "hash.h"
5#include "hashmap.h"
6#include "refspec.h"
7#include "string-list.h"
8#include "strvec.h"
9
10struct option;
11struct transport_ls_refs_options;
12struct repository;
13
14/**
15 * The API gives access to the configuration related to remotes. It handles
16 * all three configuration mechanisms historically and currently used by Git,
17 * and presents the information in a uniform fashion. Note that the code also
18 * handles plain URLs without any configuration, giving them just the default
19 * information.
20 */
21
22enum {
23 REMOTE_UNCONFIGURED = 0,
24 REMOTE_CONFIG,
25#ifndef WITH_BREAKING_CHANGES
26 REMOTE_REMOTES,
27 REMOTE_BRANCHES
28#endif /* WITH_BREAKING_CHANGES */
29};
30
31struct rewrite {
32 const char *base;
33 size_t baselen;
34 struct counted_string *instead_of;
35 int instead_of_nr;
36 int instead_of_alloc;
37};
38
39struct rewrites {
40 struct rewrite **rewrite;
41 int rewrite_alloc;
42 int rewrite_nr;
43};
44
45struct remote_state {
46 struct remote **remotes;
47 int remotes_alloc;
48 int remotes_nr;
49 struct hashmap remotes_hash;
50
51 struct hashmap branches_hash;
52
53 struct branch *current_branch;
54 char *pushremote_name;
55
56 struct rewrites rewrites;
57 struct rewrites rewrites_push;
58
59 int initialized;
60};
61
62void remote_state_clear(struct remote_state *remote_state);
63struct remote_state *remote_state_new(void);
64
65 enum follow_remote_head_settings {
66 FOLLOW_REMOTE_NEVER = -1,
67 FOLLOW_REMOTE_CREATE = 0,
68 FOLLOW_REMOTE_WARN = 1,
69 FOLLOW_REMOTE_ALWAYS = 2,
70 };
71
72struct remote {
73 struct hashmap_entry ent;
74
75 /* The user's nickname for the remote */
76 const char *name;
77
78 int origin, configured_in_repo;
79
80 char *foreign_vcs;
81
82 /* An array of all of the url_nr URLs configured for the remote */
83 struct strvec url;
84 /* An array of all of the pushurl_nr push URLs configured for the remote */
85 struct strvec pushurl;
86
87 struct refspec push;
88
89 struct refspec fetch;
90
91 /*
92 * The setting for whether to fetch tags (as a separate rule from the
93 * configured refspecs);
94 * -1 to never fetch tags
95 * 0 to auto-follow tags on heuristic (default)
96 * 1 to always auto-follow tags
97 * 2 to always fetch tags
98 */
99 int fetch_tags;
100
101 int skip_default_update;
102 int mirror;
103 int prune;
104 int prune_tags;
105
106 /**
107 * The configured helper programs to run on the remote side, for
108 * Git-native protocols.
109 */
110 const char *receivepack;
111 const char *uploadpack;
112
113 /* The proxy to use for curl (http, https, ftp, etc.) URLs. */
114 char *http_proxy;
115
116 /* The method used for authenticating against `http_proxy`. */
117 char *http_proxy_authmethod;
118
119 struct string_list server_options;
120
121 enum follow_remote_head_settings follow_remote_head;
122 const char *no_warn_branch;
123};
124
125/**
126 * struct remotes can be found by name with remote_get().
127 * remote_get(NULL) will return the default remote, given the current branch
128 * and configuration.
129 */
130struct remote *remote_get(const char *name);
131struct remote *remote_get_early(const char *name);
132
133struct remote *pushremote_get(const char *name);
134int remote_is_configured(struct remote *remote, int in_repo);
135
136typedef int each_remote_fn(struct remote *remote, void *priv);
137
138/* iterate through struct remotes */
139int for_each_remote(each_remote_fn fn, void *priv);
140
141int remote_has_url(struct remote *remote, const char *url);
142struct strvec *push_url_of_remote(struct remote *remote);
143
144struct ref_push_report {
145 char *ref_name;
146 struct object_id *old_oid;
147 struct object_id *new_oid;
148 unsigned int forced_update:1;
149 struct ref_push_report *next;
150};
151
152void ref_push_report_free(struct ref_push_report *);
153
154struct ref {
155 struct ref *next;
156 struct object_id old_oid;
157 struct object_id new_oid;
158 struct object_id old_oid_expect; /* used by expect-old */
159 char *symref;
160 char *tracking_ref;
161 unsigned int
162 force:1,
163 forced_update:1,
164 expect_old_sha1:1,
165 exact_oid:1,
166 deletion:1,
167 /* Need to check if local reflog reaches the remote tip. */
168 check_reachable:1,
169 /*
170 * Store the result of the check enabled by "check_reachable";
171 * implies the local reflog does not reach the remote tip.
172 */
173 unreachable:1;
174
175 enum {
176 REF_NOT_MATCHED = 0, /* initial value */
177 REF_MATCHED,
178 REF_UNADVERTISED_NOT_ALLOWED
179 } match_status;
180
181 /*
182 * Order is important here, as we write to FETCH_HEAD
183 * in numeric order. And the default NOT_FOR_MERGE
184 * should be 0, so that xcalloc'd structures get it
185 * by default.
186 */
187 enum fetch_head_status {
188 FETCH_HEAD_MERGE = -1,
189 FETCH_HEAD_NOT_FOR_MERGE = 0,
190 FETCH_HEAD_IGNORE = 1
191 } fetch_head_status;
192
193 enum {
194 REF_STATUS_NONE = 0,
195 REF_STATUS_OK,
196 REF_STATUS_REJECT_NONFASTFORWARD,
197 REF_STATUS_REJECT_ALREADY_EXISTS,
198 REF_STATUS_REJECT_NODELETE,
199 REF_STATUS_REJECT_FETCH_FIRST,
200 REF_STATUS_REJECT_NEEDS_FORCE,
201 REF_STATUS_REJECT_STALE,
202 REF_STATUS_REJECT_SHALLOW,
203 REF_STATUS_REJECT_REMOTE_UPDATED,
204 REF_STATUS_UPTODATE,
205 REF_STATUS_REMOTE_REJECT,
206 REF_STATUS_EXPECTING_REPORT,
207 REF_STATUS_ATOMIC_PUSH_FAILED
208 } status;
209 char *remote_status;
210 struct ref_push_report *report;
211 struct ref *peer_ref; /* when renaming */
212 char name[FLEX_ARRAY]; /* more */
213};
214
215#define REF_NORMAL (1u << 0)
216#define REF_BRANCHES (1u << 1)
217#define REF_TAGS (1u << 2)
218
219struct ref *find_ref_by_name(const struct ref *list, const char *name);
220
221struct ref *alloc_ref(const char *name);
222struct ref *copy_ref(const struct ref *ref);
223struct ref *copy_ref_list(const struct ref *ref);
224int count_refspec_match(const char *, struct ref *refs, struct ref **matched_ref);
225/*
226 * Put a ref in the tail and prepare tail for adding another one.
227 * *tail is the pointer to the tail of the list of refs.
228 */
229void tail_link_ref(struct ref *ref, struct ref ***tail);
230
231int check_ref_type(const struct ref *ref, int flags);
232
233/*
234 * Free a single ref and its peer, or an entire list of refs and their peers,
235 * respectively.
236 */
237void free_one_ref(struct ref *ref);
238void free_refs(struct ref *ref);
239
240struct oid_array;
241struct packet_reader;
242struct strvec;
243struct string_list;
244struct ref **get_remote_heads(struct packet_reader *reader,
245 struct ref **list, unsigned int flags,
246 struct oid_array *extra_have,
247 struct oid_array *shallow_points);
248
249/* Used for protocol v2 in order to retrieve refs from a remote */
250struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
251 struct ref **list, int for_push,
252 struct transport_ls_refs_options *transport_options,
253 const struct string_list *server_options,
254 int stateless_rpc);
255
256/* Used for protocol v2 in order to retrieve refs from a remote */
257struct bundle_list;
258int get_remote_bundle_uri(int fd_out, struct packet_reader *reader,
259 struct bundle_list *bundles, int stateless_rpc);
260
261int resolve_remote_symref(struct ref *ref, struct ref *list);
262
263/*
264 * Remove and free all but the first of any entries in the input list
265 * that map the same remote reference to the same local reference. If
266 * there are two entries that map different remote references to the
267 * same local reference, emit an error message and die. Return a
268 * pointer to the head of the resulting list.
269 */
270struct ref *ref_remove_duplicates(struct ref *ref_map);
271
272int check_push_refs(struct ref *src, struct refspec *rs);
273int match_push_refs(struct ref *src, struct ref **dst,
274 struct refspec *rs, int flags);
275void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
276 int force_update);
277
278/*
279 * Given a list of the remote refs and the specification of things to
280 * fetch, makes a (separate) list of the refs to fetch and the local
281 * refs to store into. Note that negative refspecs are ignored here, and
282 * should be handled separately.
283 *
284 * *tail is the pointer to the tail pointer of the list of results
285 * beforehand, and will be set to the tail pointer of the list of
286 * results afterward.
287 *
288 * missing_ok is usually false, but when we are adding branch.$name.merge
289 * it is Ok if the branch is not at the remote anymore.
290 */
291int get_fetch_map(const struct ref *remote_refs, const struct refspec_item *refspec,
292 struct ref ***tail, int missing_ok);
293
294struct ref *get_remote_ref(const struct ref *remote_refs, const char *name);
295
296/*
297 * For the given remote, reads the refspec's src and sets the other fields.
298 */
299int remote_find_tracking(struct remote *remote, struct refspec_item *refspec);
300
301/**
302 * struct branch holds the configuration for a branch. It can be looked up with
303 * branch_get(name) for "refs/heads/{name}", or with branch_get(NULL) for HEAD.
304 */
305struct branch {
306 struct hashmap_entry ent;
307
308 /* The short name of the branch. */
309 const char *name;
310
311 /* The full path for the branch ref. */
312 const char *refname;
313
314 /* The name of the remote listed in the configuration. */
315 char *remote_name;
316
317 char *pushremote_name;
318
319 /* True if set_merge() has been called to finalize the merge array */
320 int set_merge;
321
322 /**
323 * An array of the struct refspecs used for the merge lines. That is,
324 * merge[i]->dst is a local tracking ref which should be merged into this
325 * branch by default.
326 */
327 struct refspec_item **merge;
328
329 /* The number of merge configurations */
330 int merge_nr;
331
332 int merge_alloc;
333
334 const char *push_tracking_ref;
335};
336
337struct branch *branch_get(const char *name);
338const char *remote_for_branch(struct branch *branch, int *explicit);
339const char *pushremote_for_branch(struct branch *branch, int *explicit);
340char *remote_ref_for_branch(struct branch *branch, int for_push);
341
342const char *repo_default_remote(struct repository *repo);
343const char *repo_remote_from_url(struct repository *repo, const char *url);
344
345/* returns true if the given branch has merge configuration given. */
346int branch_has_merge_config(struct branch *branch);
347
348int branch_merge_matches(struct branch *, int n, const char *);
349
350/**
351 * Return the fully-qualified refname of the tracking branch for `branch`.
352 * I.e., what "branch@{upstream}" would give you. Returns NULL if no
353 * upstream is defined.
354 *
355 * If `err` is not NULL and no upstream is defined, a more specific error
356 * message is recorded there (if the function does not return NULL, then
357 * `err` is not touched).
358 */
359const char *branch_get_upstream(struct branch *branch, struct strbuf *err);
360
361/**
362 * Return the tracking branch that corresponds to the ref we would push to
363 * given a bare `git push` while `branch` is checked out.
364 *
365 * The return value and `err` conventions match those of `branch_get_upstream`.
366 */
367const char *branch_get_push(struct branch *branch, struct strbuf *err);
368
369/* Flags to match_refs. */
370enum match_refs_flags {
371 MATCH_REFS_NONE = 0,
372 MATCH_REFS_ALL = (1 << 0),
373 MATCH_REFS_MIRROR = (1 << 1),
374 MATCH_REFS_PRUNE = (1 << 2),
375 MATCH_REFS_FOLLOW_TAGS = (1 << 3)
376};
377
378/* Flags for --ahead-behind option. */
379enum ahead_behind_flags {
380 AHEAD_BEHIND_UNSPECIFIED = -1,
381 AHEAD_BEHIND_QUICK = 0, /* just eq/neq reporting */
382 AHEAD_BEHIND_FULL = 1, /* traditional a/b reporting */
383};
384
385/* Reporting of tracking info */
386int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
387 const char **upstream_name, int for_push,
388 enum ahead_behind_flags abf);
389int format_tracking_info(struct branch *branch, struct strbuf *sb,
390 enum ahead_behind_flags abf,
391 int show_divergence_advice);
392
393struct ref *get_local_heads(void);
394
395/*
396 * Find refs from a list which are likely to be pointed to by the given HEAD
397 * ref. If REMOTE_GUESS_HEAD_ALL is set, return a list of all candidate refs;
398 * otherwise, return the most likely ref. If no match is found (or 'head' is
399 * NULL), returns NULL. All returns are newly allocated and should be freed.
400 */
401#define REMOTE_GUESS_HEAD_ALL (1 << 0)
402#define REMOTE_GUESS_HEAD_QUIET (1 << 1)
403struct ref *guess_remote_head(const struct ref *head,
404 const struct ref *refs,
405 unsigned flags);
406
407/* Return refs which no longer exist on remote */
408struct ref *get_stale_heads(struct refspec *rs, struct ref *fetch_map);
409
410/*
411 * Compare-and-swap
412 */
413struct push_cas_option {
414 unsigned use_tracking_for_rest:1;
415 unsigned use_force_if_includes:1;
416 struct push_cas {
417 struct object_id expect;
418 unsigned use_tracking:1;
419 char *refname;
420 } *entry;
421 int nr;
422 int alloc;
423};
424
425int parseopt_push_cas_option(const struct option *, const char *arg, int unset);
426void clear_cas_option(struct push_cas_option *);
427
428int is_empty_cas(const struct push_cas_option *);
429void apply_push_cas(struct push_cas_option *, struct remote *, struct ref *);
430
431/*
432 * The `url` argument is the URL that navigates to the submodule origin
433 * repo. When relative, this URL is relative to the superproject origin
434 * URL repo. The `up_path` argument, if specified, is the relative
435 * path that navigates from the submodule working tree to the superproject
436 * working tree. Returns the origin URL of the submodule.
437 *
438 * Return either an absolute URL or filesystem path (if the superproject
439 * origin URL is an absolute URL or filesystem path, respectively) or a
440 * relative file system path (if the superproject origin URL is a relative
441 * file system path).
442 *
443 * When the output is a relative file system path, the path is either
444 * relative to the submodule working tree, if up_path is specified, or to
445 * the superproject working tree otherwise.
446 *
447 * NEEDSWORK: This works incorrectly on the domain and protocol part.
448 * remote_url url outcome expectation
449 * http://a.com/b ../c http://a.com/c as is
450 * http://a.com/b/ ../c http://a.com/c same as previous line, but
451 * ignore trailing slash in url
452 * http://a.com/b ../../c http://c error out
453 * http://a.com/b ../../../c http:/c error out
454 * http://a.com/b ../../../../c http:c error out
455 * http://a.com/b ../../../../../c .:c error out
456 * http://a.com/b http://d.org/e http://d.org/e as is
457 * NEEDSWORK: Given how chop_last_dir() works, this function is broken
458 * when a local part has a colon in its path component, too.
459 */
460char *relative_url(const char *remote_url, const char *url,
461 const char *up_path);
462
463int valid_remote_name(const char *name);
464
465#endif