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