The sixteenth batch
[git/gitster.git] / remote.h
blob0ca399e1835bf1829054d8937d95e5625c38d881
1 #ifndef REMOTE_H
2 #define REMOTE_H
4 #include "hash.h"
5 #include "hashmap.h"
6 #include "refspec.h"
7 #include "string-list.h"
8 #include "strvec.h"
10 struct option;
11 struct transport_ls_refs_options;
12 struct repository;
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.
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 */
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;
39 struct rewrites {
40 struct rewrite **rewrite;
41 int rewrite_alloc;
42 int rewrite_nr;
45 struct remote_state {
46 struct remote **remotes;
47 int remotes_alloc;
48 int remotes_nr;
49 struct hashmap remotes_hash;
51 struct hashmap branches_hash;
53 struct branch *current_branch;
54 char *pushremote_name;
56 struct rewrites rewrites;
57 struct rewrites rewrites_push;
59 int initialized;
62 void remote_state_clear(struct remote_state *remote_state);
63 struct remote_state *remote_state_new(void);
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,
72 struct remote {
73 struct hashmap_entry ent;
75 /* The user's nickname for the remote */
76 const char *name;
78 int origin, configured_in_repo;
80 char *foreign_vcs;
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;
87 struct refspec push;
89 struct refspec fetch;
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
99 int fetch_tags;
101 int skip_default_update;
102 int mirror;
103 int prune;
104 int prune_tags;
107 * The configured helper programs to run on the remote side, for
108 * Git-native protocols.
110 const char *receivepack;
111 const char *uploadpack;
113 /* The proxy to use for curl (http, https, ftp, etc.) URLs. */
114 char *http_proxy;
116 /* The method used for authenticating against `http_proxy`. */
117 char *http_proxy_authmethod;
119 struct string_list server_options;
121 enum follow_remote_head_settings follow_remote_head;
122 const char *no_warn_branch;
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.
130 struct remote *remote_get(const char *name);
131 struct remote *remote_get_early(const char *name);
133 struct remote *pushremote_get(const char *name);
134 int remote_is_configured(struct remote *remote, int in_repo);
136 typedef int each_remote_fn(struct remote *remote, void *priv);
138 /* iterate through struct remotes */
139 int for_each_remote(each_remote_fn fn, void *priv);
141 int remote_has_url(struct remote *remote, const char *url);
142 struct strvec *push_url_of_remote(struct remote *remote);
144 struct 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;
152 void ref_push_report_free(struct ref_push_report *);
154 struct 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,
170 * Store the result of the check enabled by "check_reachable";
171 * implies the local reflog does not reach the remote tip.
173 unreachable:1;
175 enum {
176 REF_NOT_MATCHED = 0, /* initial value */
177 REF_MATCHED,
178 REF_UNADVERTISED_NOT_ALLOWED
179 } match_status;
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.
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;
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 */
215 #define REF_NORMAL (1u << 0)
216 #define REF_BRANCHES (1u << 1)
217 #define REF_TAGS (1u << 2)
219 struct ref *find_ref_by_name(const struct ref *list, const char *name);
221 struct ref *alloc_ref(const char *name);
222 struct ref *copy_ref(const struct ref *ref);
223 struct ref *copy_ref_list(const struct ref *ref);
224 int count_refspec_match(const char *, struct ref *refs, struct ref **matched_ref);
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.
229 void tail_link_ref(struct ref *ref, struct ref ***tail);
231 int check_ref_type(const struct ref *ref, int flags);
234 * Free a single ref and its peer, or an entire list of refs and their peers,
235 * respectively.
237 void free_one_ref(struct ref *ref);
238 void free_refs(struct ref *ref);
240 struct oid_array;
241 struct packet_reader;
242 struct strvec;
243 struct string_list;
244 struct 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);
249 /* Used for protocol v2 in order to retrieve refs from a remote */
250 struct 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);
256 /* Used for protocol v2 in order to retrieve refs from a remote */
257 struct bundle_list;
258 int get_remote_bundle_uri(int fd_out, struct packet_reader *reader,
259 struct bundle_list *bundles, int stateless_rpc);
261 int resolve_remote_symref(struct ref *ref, struct ref *list);
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.
270 struct ref *ref_remove_duplicates(struct ref *ref_map);
272 int check_push_refs(struct ref *src, struct refspec *rs);
273 int match_push_refs(struct ref *src, struct ref **dst,
274 struct refspec *rs, int flags);
275 void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
276 int force_update);
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.
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.
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.
291 int get_fetch_map(const struct ref *remote_refs, const struct refspec_item *refspec,
292 struct ref ***tail, int missing_ok);
294 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name);
297 * For the given remote, reads the refspec's src and sets the other fields.
299 int remote_find_tracking(struct remote *remote, struct refspec_item *refspec);
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.
305 struct branch {
306 struct hashmap_entry ent;
308 /* The short name of the branch. */
309 const char *name;
311 /* The full path for the branch ref. */
312 const char *refname;
314 /* The name of the remote listed in the configuration. */
315 char *remote_name;
317 char *pushremote_name;
319 /* True if set_merge() has been called to finalize the merge array */
320 int set_merge;
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.
327 struct refspec_item **merge;
329 /* The number of merge configurations */
330 int merge_nr;
332 int merge_alloc;
334 const char *push_tracking_ref;
337 struct branch *branch_get(const char *name);
338 const char *remote_for_branch(struct branch *branch, int *explicit);
339 const char *pushremote_for_branch(struct branch *branch, int *explicit);
340 char *remote_ref_for_branch(struct branch *branch, int for_push);
342 const char *repo_default_remote(struct repository *repo);
343 const char *repo_remote_from_url(struct repository *repo, const char *url);
345 /* returns true if the given branch has merge configuration given. */
346 int branch_has_merge_config(struct branch *branch);
348 int branch_merge_matches(struct branch *, int n, const char *);
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.
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).
359 const char *branch_get_upstream(struct branch *branch, struct strbuf *err);
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.
365 * The return value and `err` conventions match those of `branch_get_upstream`.
367 const char *branch_get_push(struct branch *branch, struct strbuf *err);
369 /* Flags to match_refs. */
370 enum 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)
378 /* Flags for --ahead-behind option. */
379 enum 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 */
385 /* Reporting of tracking info */
386 int 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);
389 int format_tracking_info(struct branch *branch, struct strbuf *sb,
390 enum ahead_behind_flags abf,
391 int show_divergence_advice);
393 struct ref *get_local_heads(void);
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.
401 #define REMOTE_GUESS_HEAD_ALL (1 << 0)
402 #define REMOTE_GUESS_HEAD_QUIET (1 << 1)
403 struct ref *guess_remote_head(const struct ref *head,
404 const struct ref *refs,
405 unsigned flags);
407 /* Return refs which no longer exist on remote */
408 struct ref *get_stale_heads(struct refspec *rs, struct ref *fetch_map);
411 * Compare-and-swap
413 struct 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;
425 int parseopt_push_cas_option(const struct option *, const char *arg, int unset);
426 void clear_cas_option(struct push_cas_option *);
428 int is_empty_cas(const struct push_cas_option *);
429 void apply_push_cas(struct push_cas_option *, struct remote *, struct ref *);
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.
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).
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.
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.
460 char *relative_url(const char *remote_url, const char *url,
461 const char *up_path);
463 int valid_remote_name(const char *name);
465 #endif