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